I have two time inputs.
At startup the first time input syncs correctly.
But the second one gets time start ‘0:0:40’, althoigh widget shows 23:00:00-21:01:00.
When edit the widget, just press ok, then the time gets synced correctly.
19:10:28.589 -> BLYNK_WRITE_DEFAULT:3
19:10:28.589 -> Start: 19:0:0
19:10:28.627 -> Stop: 19:1:0
19:10:28.627 -> Time zone: Europe/Sofia
19:10:28.656 -> Time zone offset: 7200
19:10:28.689 -> Days selected: 1 2 3 4 5 6 7
19:10:28.722 -> now:19:10:27
19:10:28.722 -> BLYNK_WRITE_DEFAULT:4
19:10:28.756 -> Start: 0:0:40
19:10:28.756 -> Time zone:
19:10:28.790 -> Time zone offset: 0
19:10:28.790 -> Days selected: 1 2 3 4 5 6 7
19:10:28.823 -> now:19:10:28
19:10:28.857 -> BLYNK_WRITE_DEFAULT:5
19:10:28.857 -> Time zone: Europe/Sofia
19:10:28.891 -> Time zone offset: 7200
19:10:28.924 ->
19:10:28.924 -> now:19:10:28
BLYNK_WRITE_DEFAULT()
{
Serial.println(String("BLYNK_WRITE_DEFAULT:") + request.pin);
if (request.pin != 3 && request.pin != 4 && request.pin != 5)
{
return;
}
TimeInputParam t(param);
// Process start time
if (t.hasStartTime())
{
Serial.println(String("Start: ") +
t.getStartHour() + ":" +
t.getStartMinute() + ":" +
t.getStartSecond());
}
else if (t.isStartSunrise())
{
Serial.println("Start at sunrise");
}
else if (t.isStartSunset())
{
Serial.println("Start at sunset");
}
if (t.hasStopTime())
{
Serial.println(String("Stop: ") +
t.getStopHour() + ":" +
t.getStopMinute() + ":" +
t.getStopSecond());
}
else if (t.isStopSunrise())
{
Serial.println("Stop at sunrise");
}
else if (t.isStopSunset())
{
Serial.println("Stop at sunset");
}
// Process timezone
// Timezone is already added to start/stop time
Serial.println(String("Time zone: ") + t.getTZ());
// Get timezone offset (in seconds)
Serial.println(String("Time zone offset: ") + t.getTZ_Offset());
// Process weekdays (1. Mon, 2. Tue, 3. Wed, ...)
if (t.hasStartTime() || t.hasStopTime())
{
Serial.print(String("Days selected: "));
for (int i = 1; i <= 7; i++) {
if (t.isWeekdaySelected(i)) {
Serial.print(String(i) + " ");
}
}
}
Serial.println();
timeTrigger(t, request.pin);
}
void timeTrigger(TimeInputParam t, int pin)
{
if(year() == 1970)
return;
Serial.println(String("now:") + hour() + ":" + minute() + ":" + second());
}
void activetoday(){ // check if schedule should run today
if(year() != 1970){
Blynk.syncVirtual(V3); // sync timeinput widget
Blynk.syncVirtual(V4); // sync timeinput widget
Blynk.syncVirtual(V5); // sync timeinput widget
}
}
void setup()
{
// Debug console
Serial.begin(9600);
setupWifi();
Blynk.begin(auth, ssid, pass);
rtc.begin();
timer.setInterval(10000L, activetoday); // check every 10 SECONDS if schedule should run today
}
void loop()
{
Blynk.run();
timer.run();
}
Why are you doing a Blynk.syncVirtual on the three widgets every 10 seconds?
Have you tried separate BLYNK_WRITE(vPin)
callback functions for each of the three widgets?
Pete.
It’s an alarm setup, checking every 10 seconds if alarm should be triggered.
I will try to rewrite the code with separate BLYNK_WRITE(vPin)
If you structure your code correctly, you don’t need to do this, other than as part of a BLYNK_CONNECTED
process.
Normally, you would hold the various start and stop times, along with active days etc. as variables. These variables are updated whenever the values in the app change, because the corresponding BLYNK_WRITE(vPin)
function is called (or when Blynk connects/reconnects). All you then need to do is check if any of the changeover times have been met using your 10 second timer.
Pete.
If I want to save memory and not hold set of variables for the three time inptus, wouldn’t this be the approach?
Or is there something against syncing every 10 seconds?
As far as I’m concerned, repeatedly calling Blynk.sync
commands is bad practice, and it also prevents the code executing correctly if there is an interruption to the WiFi/Internet connection.
Memory shouldn’t be an issue with an ESP8266/NodeMCU, even if you’re using OTA - which isn’t included in your existing sketch.
Pete.
I rewrote the code using BLYNK_WRITE and reinserted the second time input widget.
Still getting the same results, just for V4
09:40:41.885 -> handleTimeInput:3
09:40:41.885 -> Start: 19:0:0
09:40:41.925 -> Stop: 19:1:0
09:40:41.925 -> Time zone: Europe/Sofia
09:40:41.972 -> Time zone offset: 7200
09:40:41.972 -> Days selected: 1 2 3 4 5 6 7
09:40:42.019 -> now:9:40:40
09:40:42.019 -> handleTimeInput:4
09:40:42.061 -> Start: 0:0:40
09:40:42.061 -> Time zone:
09:40:42.061 -> Time zone offset: 0
09:40:42.108 -> Days selected: 1 2 3 4 5 6 7
09:40:42.108 -> now:9:40:41
09:40:42.108 -> handleTimeInput:5
09:40:42.156 -> Start: 23:0:0
09:40:42.156 -> Stop: 23:1:0
09:40:42.204 -> Time zone: Europe/Sofia
09:40:42.204 -> Time zone offset: 7200
09:40:42.204 -> Days selected: 1 2 3 4 5 6 7
09:40:42.245 -> now:9:40:41
BLYNK_WRITE(V3)
{
TimeInputParam t(param);
handleTimeInput(V3, t);
}
BLYNK_WRITE(V4)
{
TimeInputParam t(param);
handleTimeInput(V4, t);
}
BLYNK_WRITE(V5)
{
TimeInputParam t(param);
handleTimeInput(V5, t);
}
//BLYNK_WRITE_DEFAULT()
void handleTimeInput(int pin, TimeInputParam t)
{
Serial.println(String("handleTimeInput:") + pin);
if (pin != 3 && pin != 4 && pin != 5)
{
return;
}
// Process start time
if (t.hasStartTime())
{
Serial.println(String("Start: ") +
t.getStartHour() + ":" +
t.getStartMinute() + ":" +
t.getStartSecond());
}
else if (t.isStartSunrise())
{
Serial.println("Start at sunrise");
}
else if (t.isStartSunset())
{
Serial.println("Start at sunset");
}
if (t.hasStopTime())
{
Serial.println(String("Stop: ") +
t.getStopHour() + ":" +
t.getStopMinute() + ":" +
t.getStopSecond());
}
else if (t.isStopSunrise())
{
Serial.println("Stop at sunrise");
}
else if (t.isStopSunset())
{
Serial.println("Stop at sunset");
}
// Process timezone
// Timezone is already added to start/stop time
Serial.println(String("Time zone: ") + t.getTZ());
// Get timezone offset (in seconds)
Serial.println(String("Time zone offset: ") + t.getTZ_Offset());
// Process weekdays (1. Mon, 2. Tue, 3. Wed, ...)
if (t.hasStartTime() || t.hasStopTime())
{
Serial.print(String("Days selected: "));
for (int i = 1; i <= 7; i++) {
if (t.isWeekdaySelected(i)) {
Serial.print(String(i) + " ");
}
}
}
Serial.println();
timeTrigger(t, pin);
}
void timeTrigger(TimeInputParam t, int pin)
{
if(year() == 1970)
return;
Serial.println(String("now:") + hour() + ":" + minute() + ":" + second());
}
void activetoday(){ // check if schedule should run today
if(year() != 1970){
Blynk.syncVirtual(V3); // sync timeinput widget
Blynk.syncVirtual(V4); // sync timeinput widget
Blynk.syncVirtual(V5); // sync timeinput widget
}
}
void setup()
{
// Debug console
Serial.begin(9600);
setupWifi();
Blynk.begin(auth, ssid, pass);
rtc.begin();
timer.setInterval(10000L, activetoday); // check every 10 SECONDS if schedule should run today
}
void loop()
{
Blynk.run();
timer.run();
}
If I change V4 to V7, then it works as expected.
Then change again from V7 to V4, still wrong behaviour.
Is there some kind of cache on the blynk cloud I could clear?