Hi,
I’m getting strange behavior on my Timer widget. I am running Blynk cloud and using it on iOS (all lastest versions).
I am using Timer widget to turn servo via NodeMCU with code below. Device does more things than just turning servo but it should not be the issue. When I turn servo with slider or button it works great but when I set up timer widget it just turn it to value 39 and it does not matter how I configure it and it does the same on Timer end or start.
Is it a bug or am I doing someting wrong?
#define BLYNK_PRINT Serial
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <Servo.h>
#include <OneWire.h>
#include <DallasTemperature.h>
#include <SimpleTimer.h>
SimpleTimer timer;
// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "12345678987654321234";
// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "SSID";
char pass[] = "pass";
Servo servo;
BLYNK_CONNECTED(){
Blynk.syncAll();
}
BLYNK_WRITE(V3) // Slider set to Vx and to range 0-180
{
servo.attach(2); // Attach Servo
// Get slider value
servo.write(param.asInt()); // Move servo to value
delay(800); // Set minimum delay for servo to reach desired position, keep as low as possible.
servo.detach(); // Detach Servo - NOTE servo pin designation not required for detach
} // END Blynk Function
/* DS18B20 Temperature Sensor */
#define ONE_WIRE_BUS 9 // pin temperature set
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature DS18B20(&oneWire);
float temp;
void setup()
{
// Debug console
Serial.begin(9600);
Blynk.begin(auth, ssid, pass);
// You can also specify server:
//Blynk.begin(auth, ssid, pass, "blynk-cloud.com", 80);
//Blynk.begin(auth, ssid, pass, IPAddress(192,168,1,100), 8080);
DS18B20.begin();
timer.setInterval(1000L, getSendData);
pinMode(2, OUTPUT);
}
void loop()
{
Blynk.run();
timer.run();
digitalWrite(2, HIGH); //disables pesky blue diod
}
/***************************************************
* Send Sensor data to Blynk
**************************************************/
void getSendData()
{
DS18B20.requestTemperatures();
temp = DS18B20.getTempCByIndex(0);
Serial.println(temp);
Blynk.virtualWrite(10, temp); //virtual pin V10
int pos = servo.read();
Blynk.virtualWrite(4, pos);
}
Probably a timing issue. Your timer is set to run every second (1000 ms):
timer.setInterval(1000L, getSendData);
This puppy requires about 800 ms to finish:
DS18B20.requestTemperatures();
With everything else going on, that might be too long before the timer want to start over again and it will normally generate strange behavior
I would use two timers, one for the temp and one for whatever int pos = servo.read(); does.
To avoid unnecessary delays when fetching the temperature, you could
use DS18B20.setWaitForConversion(0); to speed things up. It requires some coding, I’ve written about it many times so just search the forum or look at my old project: Cheap(est) RF 433 Mhz bridge for home automation with both Tx and Rx
It is for feeding SuperChart with position value because it can’t read constantly (only on change and it doesn’t draw nice graph) from V3 pin which i use to set postion. But I set another timer for that and Timer widget is still malfunctioning even when i remove this part completely.
Actually I do because this LED is hardwired to someting inside NodeMCU and every time servo turns it leaves LED on. But I’ve moved this under Servo servo; and works just fine.
I want to use Timer Widget to turn servo at position calling BLYNK_WRITE(vPin) at desired time given the setting of the widget. But now when I use it it ignores values in the app (see pic below) and just set value to 40 or 39. Documentation says that I can send any value not just HIGH or LOW. Control with button or slider in app works great.
NO, you shouldn’t need to. What was happening is becasue of something else you had done. GPIO2 is hardwired to the built in LED and a Pull Up resistor, thus the LED is active LOW.
You are trying to use that same pin for the servo as well, thus your issues… probably with both LED and servo.
Move the servo to another pin! Start with the Green = Safe pins
I’ve been replying to @Gunner about the LED issue which might be relevant to Timer Widget issue but as it turns out it isn’t. Then I’ve simplyfied code just to turn servo to eliminate other interferences but it generates still the same issue.
Yeah, that comment is old and doesn’t make sense anymore and is confusing. Sorry about that.
This line brings value from blynk app to the nodeMCU on V3. On my App I now control this with buttons (pre set values) and slider (value I want to - especially for testing) without problem. And I want to control it also with Timer widget but that fails.
To underestand this whole set-up. It is simple “IoT servo” that is glued onto knob on my home thermostat (easy but works). So that is the reason why I want so many different ways to set servo position.