Timer widget activates at wrong time

I am using a timer widget to control my sunscreens, 1 project, 2 arduino+esp, 3 sunscreens. It’s self-build and the arduino just runs a motor for a number of seconds with no extra sensors.
All works fine, the screens go up and down at the programmed times but one of the three screens activates a second time every day.

  • arduino uno with esp8266
  • android 2.27.17
  • blynk server
  • blynk library 0.6.1
  • central European summer time

the set times are 05:00 start and 11:50 stop. The ‘ghost’ start event is around 23:12. There is no ghost stop event, I am using a terminal widged to monitor.

The V3 timer has the error, the V4 timer runs fine
slaapneer() and slaapop() (slaap = sleeping room, neer = down, op = up) void’s can be activated by the timer and by a button. The ti_s (timer sleep) =1 just means the terminal prints a 1 instead off a 0 in the conformation line that is part off the slaapneer() and slaapop() voids.

Since the error occurred I updated the code and rebooted the arduino, and the ghost event also happens when I have the project in edit mode.

edit> Is it possible that the blynk server sends an old start time that I no-longer use?

BLYNK_WRITE(V3) {TimeInputParam t(param);
  unsigned int nowseconds = ((hour() * 3600) + (minute() * 60) + second());
  unsigned int startseconds = (t.getStartHour() * 3600) + (t.getStartMinute() * 60);  
  unsigned int stopseconds = (t.getStopHour() * 3600) + (t.getStopMinute() * 60);
      if(nowseconds >= startseconds - 61 && nowseconds <= startseconds + 61 ){ti_s = 1; slaapneer();}    // 62s on 120s timer ensures 1 trigger command is sent
      if(nowseconds >= stopseconds - 61 && nowseconds <= stopseconds + 61 ){ti_s = 1; slaapop();}  
}
BLYNK_WRITE(V4) {TimeInputParam t(param);
  unsigned int nowseconds = ((hour() * 3600) + (minute() * 60) + second());
  unsigned int startseconds = (t.getStartHour() * 3600) + (t.getStartMinute() * 60);  
  unsigned int stopseconds = (t.getStopHour() * 3600) + (t.getStopMinute() * 60);
      if(nowseconds >= startseconds - 61 && nowseconds <= startseconds + 61 ){ti_a = 1; achterneer();}    
      if(nowseconds >= stopseconds - 61 && nowseconds <= stopseconds + 61 ){ti_a = 1; achterop();}  
}

void loop() {
 if (Blynk.connected()){Blynk.run();}
 else {Blynk.begin(auth, wifi, ssid, pass);
      snprintf_P(s, sizeof(s), PSTR("%i-%i %i:%i herstart"), day(), month(), hour(), minute());
      Blynk.virtualWrite(V0, s);
      Blynk.run();}
 timer.run();
}

edit> screenshot of my app

Is it possible that your app has a Timer widget (as opposed to a Time input widget) that is also connected to pin V3 and is set-up with just a start time and no end time?

Pete.

@PeteKnight added a screenshot of my app. No timer widged as far as I know

In that case I think you need to post your full code.

Pete.

full code

//#define BLYNK_PRINT Serial 
#include <ESP8266_Lib.h>
#include <BlynkSimpleShieldEsp8266.h>         
//#define EspSerial Serial1
//#define ESP8266_BAUD 115200
ESP8266 wifi(&Serial);
#include <WidgetRTC.h>
#include <BlynkConfig.h>
#ifndef BLYNK_HEARTBEAT
#define BLYNK_HEARTBEAT 60
#endif
WidgetRTC rtc;
BlynkTimer timer;

const char auth[] = "xxxxxx";
const char ssid[] = "xxxxx";
const char pass[] = "xxxxx";                

int pinsn = 8;
int pinso = 9;
int pinan = 11;
int pinao = 10;
int st_a = 0; //0 is hoog 1 is laag
int st_s = 0;  
int ti_s = 0;   // timer is 1, knop 0
int ti_a = 0;   

long DAN = 50200;
long DAO = 39500;

long DSN = 1100;
long DSO = 920;
long DSNP = 2000;
long DSOP = 2000;

int ANA = LOW;
int ANP = HIGH;
int AOA = LOW;
int AOP = HIGH;

int SNA = LOW;
int SNP = HIGH;
int SOA = LOW;
int SOP = HIGH;

char s[48];

void setup() {
  pinMode(pinsn, OUTPUT);
  digitalWrite(pinsn, HIGH);
  pinMode(pinso, OUTPUT);
  digitalWrite(pinso, HIGH);
  pinMode(pinan, OUTPUT);
  digitalWrite(pinan, HIGH);
  pinMode(pinao, OUTPUT);
  digitalWrite(pinao, HIGH);

  Serial.begin(115200);
  Blynk.begin(auth, wifi, ssid, pass);
   timer.setInterval(120000L, activetoday);   // check every 60s if ON / OFF trigger time has been reached
    }

BLYNK_CONNECTED() {rtc.begin();}

void checkled(){
  if (st_s==0){Blynk.virtualWrite(V5, 0);}
  else if (st_s == 1){Blynk.virtualWrite(V5, 255);}
  if (st_a==0){Blynk.virtualWrite(V6, 0);}
  else if (st_a == 1){Blynk.virtualWrite(V6, 255);}
}

void activetoday(){         // check if schedule #1 should run today
  checkled();
   if(year() != 1970){
    Blynk.syncVirtual(V3);  // sync scheduler #1  
    Blynk.syncVirtual(V4);
  } 
}
void slaapneer(){
  Blynk.virtualWrite(V7, 255);
 if (st_s == 1){                     // is al laag
   snprintf_P(s, sizeof(s), PSTR("%i-%i %i:%i Nee slaap is al laag (%i)"), day(), month(), hour(), minute(), ti_s);
   Blynk.virtualWrite(V0, s);
  }
 else {
  st_s = 1;
  digitalWrite(pinsn, SNA); //1
  delay(DSN);
  digitalWrite(pinsn, SNP);
  delay(DSNP);
  digitalWrite(pinsn, SNA); //2
  delay(DSN);
  digitalWrite(pinsn, SNP);
  delay(DSNP);
  digitalWrite(pinsn, SNA); //3
  delay(DSN);
  digitalWrite(pinsn, SNP);
  delay(DSNP);
  digitalWrite(pinsn, SNA); //4
  delay(DSN);
  digitalWrite(pinsn, SNP);
  delay(DSNP);
  digitalWrite(pinsn, SNA); //5
  delay(DSN);
  digitalWrite(pinsn, SNP);
  delay(DSNP);
  digitalWrite(pinsn, SNA); //6
  delay(DSN);
  digitalWrite(pinsn, SNP);
  delay(DSNP);
  digitalWrite(pinsn, SNA); //7
  delay(DSN);
  digitalWrite(pinsn, SNP);
  delay(DSNP);
  digitalWrite(pinsn, SNA); //8
  delay(DSN);
  digitalWrite(pinsn, SNP);
  delay(DSNP);
  digitalWrite(pinsn, SNA); //9
  delay(DSN);
  digitalWrite(pinsn, SNP);
  delay(DSNP);
  digitalWrite(pinsn, SNA); //10
  delay(DSN);
  digitalWrite(pinsn, SNP);
  snprintf_P(s, sizeof(s), PSTR("%i-%i %i:%i slaap laag (%i)"), day(), month(), hour(), minute(), ti_s);
  Blynk.virtualWrite(V0, s);
  }
    Blynk.virtualWrite(V1, 1);
    Blynk.virtualWrite(V5, 255);
    Blynk.virtualWrite(V7, 0); 
    ti_s = 0;
}

void slaapop(){
  Blynk.virtualWrite(V7, 255);
 if (st_s == 0){                    // is al hoog
   snprintf_P(s, sizeof(s), PSTR("%i-%i %i:%i Nee slaap is al hoog (%i)"), day(), month(), hour(), minute(), ti_s);
   Blynk.virtualWrite(V0, s);
  }
 else {
  st_s = 0;
  digitalWrite(pinso, SOA); //1
  delay(DSO);
  digitalWrite(pinso, SOP);
  delay(DSOP);
  digitalWrite(pinso, SOA); //2
  delay(DSO);
  digitalWrite(pinso, SOP);
  delay(DSOP);
  digitalWrite(pinso, SOA); //3
  delay(DSO);
  digitalWrite(pinso, SOP);
  delay(DSOP);
  digitalWrite(pinso, SOA); //4
  delay(DSO);
  digitalWrite(pinso, SOP);
  delay(DSOP);
  digitalWrite(pinso, SOA); //5
  delay(DSO);
  digitalWrite(pinso, SOP);
  delay(DSOP);
  digitalWrite(pinso, SOA); //6
  delay(DSO);
  digitalWrite(pinso, SOP);
  delay(DSOP);
  digitalWrite(pinso, SOA); //7
  delay(DSO);
  digitalWrite(pinso, SOP);
  delay(DSOP);
  digitalWrite(pinso, SOA); //8
  delay(DSO);
  digitalWrite(pinso, SOP);
  delay(DSOP);
  digitalWrite(pinso, SOA); //9
  delay(DSO);
  digitalWrite(pinso, SOP);
  delay(DSOP);
  digitalWrite(pinso, SOA); //10
  delay(DSO);
  digitalWrite(pinso, SOP);
  snprintf_P(s, sizeof(s), PSTR("%i-%i %i:%i slaap hoog (%i)"), day(), month(), hour(), minute(), ti_s);
  Blynk.virtualWrite(V0, s);
 }
  Blynk.virtualWrite(V1, 0);
  Blynk.virtualWrite(V5, 0);
  Blynk.virtualWrite(V7, 0); 
  ti_s = 0;
}

void achterneer(){
  Blynk.virtualWrite(V8, 255);
 if (st_a == 1){        // is al laag
  snprintf_P(s, sizeof(s), PSTR("%i-%i %i:%i Nee achter is al laag (%i)"), day(), month(), hour(), minute(), ti_a);
  Blynk.virtualWrite(V0, s);
}
 else {
  st_a = 1;
  digitalWrite(pinan, ANA);
  delay(DAN);
  digitalWrite(pinan, ANP);
  snprintf_P(s, sizeof(s), PSTR("%i-%i %i:%i achter laag (%i)"), day(), month(), hour(), minute(), ti_a);
  Blynk.virtualWrite(V0, s);
 }
  Blynk.virtualWrite(V2, 1);
  Blynk.virtualWrite(V6, 255);
  Blynk.virtualWrite(V8, 0);
  ti_a = 0;
}

void achterop(){
  Blynk.virtualWrite(V8, 255);
 if (st_a == 0){          // is al hoog
  snprintf_P(s, sizeof(s), PSTR("%i-%i %i:%i Nee achter is al hoog (%i)"), day(), month(), hour(), minute(), ti_a);
  Blynk.virtualWrite(V0, s);
 }
 else {
  st_a = 0;
  digitalWrite(pinao, AOA);
  delay(DAO);
  digitalWrite(pinao, AOP);
  snprintf_P(s, sizeof(s), PSTR("%i-%i %i:%i achter hoog (%i)"), day(), month(), hour(), minute(), ti_a);
  Blynk.virtualWrite(V0, s);
 }
  Blynk.virtualWrite(V2, 0);
  Blynk.virtualWrite(V6, 0);
  Blynk.virtualWrite(V8, 0);
  ti_a = 0;
}

BLYNK_WRITE(V0){
  if (String("zet achter laag") == param.asStr()) {
    st_a = 1; 
    Blynk.virtualWrite(V2, 1);
    Blynk.virtualWrite(V6, 255);
    Blynk.virtualWrite(V8, 0);
    }
  else if   (String("zet achter hoog") == param.asStr()) {
    st_a = 0; 
    Blynk.virtualWrite(V2, 0);
    Blynk.virtualWrite(V6, 0);
    Blynk.virtualWrite(V8, 0);
    }
  else if (String("zet slaap laag") == param.asStr()) {
    st_s = 1; 
    Blynk.virtualWrite(V1, 1);
    Blynk.virtualWrite(V5, 255);
    Blynk.virtualWrite(V7, 0);
    }
  else if   (String("zet slaap hoog") == param.asStr()) {
    st_s = 0; 
    Blynk.virtualWrite(V1, 0);
    Blynk.virtualWrite(V5, 0);
    Blynk.virtualWrite(V7, 0);
   }  
  else if   (String("achter") == param.asStr()) {
    snprintf_P(s, sizeof(s), PSTR("%i-%i %i:%i slaap %i, achter %i (0 is hoog)"), day(), month(), hour(), minute(), st_s, st_a);
    Blynk.virtualWrite(V0, s);
    Blynk.virtualWrite(V7, 0);
    Blynk.virtualWrite(V8, 0);
  }
  else if (String("tijd") == param.asStr()) {
    snprintf_P(s, sizeof(s), PSTR("%i-%i %i:%i is huidige tijd"), day(), month(), hour(), minute());
    Blynk.virtualWrite(V0, s);
 }
}
BLYNK_WRITE(V1)
{
  int knopslaap = param.asInt();
  if (knopslaap == 0) {slaapop();}
  else if (knopslaap == 1) {slaapneer();}
 }
BLYNK_WRITE(V2)
{
  int knopachter = param.asInt();
  if (knopachter == 0) {achterop();}
  else if (knopachter == 1) {achterneer();}
}
BLYNK_WRITE(V3) {TimeInputParam t(param);
  unsigned int nowseconds = ((hour() * 3600) + (minute() * 60) + second());
  unsigned int startseconds = (t.getStartHour() * 3600) + (t.getStartMinute() * 60);  
  unsigned int stopseconds = (t.getStopHour() * 3600) + (t.getStopMinute() * 60);
      if(nowseconds >= startseconds - 61 && nowseconds <= startseconds + 61 ){ti_s = 1; slaapneer();}    // 62s on 60s timer ensures 1 trigger command is sent
      if(nowseconds >= stopseconds - 61 && nowseconds <= stopseconds + 61 ){ti_s = 1; slaapop();}  
}
BLYNK_WRITE(V4) {TimeInputParam t(param);
  unsigned int nowseconds = ((hour() * 3600) + (minute() * 60) + second());
  unsigned int startseconds = (t.getStartHour() * 3600) + (t.getStartMinute() * 60);  
  unsigned int stopseconds = (t.getStopHour() * 3600) + (t.getStopMinute() * 60);
      if(nowseconds >= startseconds - 61 && nowseconds <= startseconds + 61 ){ti_a = 1; achterneer();}    // 62s on 60s timer ensures 1 trigger command is sent
      if(nowseconds >= stopseconds - 61 && nowseconds <= stopseconds + 61 ){ti_a = 1; achterop();}  
}

void loop() {
 if (Blynk.connected()){Blynk.run();}
 else {Blynk.begin(auth, wifi, ssid, pass);
      snprintf_P(s, sizeof(s), PSTR("%i-%i %i:%i herstart"), day(), month(), hour(), minute());
      Blynk.virtualWrite(V0, s);
      Blynk.run();}
 timer.run();
}

Have you set the time zone in the RTC widget ?

please try this snippet code

BLYNK_WRITE(V1) {   // Scheduler #1 Time Input widget  
  TimeInputParam t(param);
  unsigned int nowseconds = ((hour() * 3600) + (minute() * 60) + second());
  unsigned int startseconds = (t.getStartHour() * 3600) + (t.getStartMinute() * 60);  
  unsigned int stopseconds = (t.getStopHour() * 3600) + (t.getStopMinute() * 60);
  int dayadjustment = -1;  
  if(weekday() == 1){
    dayadjustment = 6; // needed for Sunday Time library is day 1 and Blynk is day 7
  }
  if(t.isWeekdaySelected((weekday() + dayadjustment))){ //Time library starts week on Sunday, Blynk on Monday  
    //Schedule is ACTIVE today 
    if(nowseconds >= startseconds - 31 && nowseconds <= startseconds + 31 ){    // 62s on 60s timer ensures 1 trigger command is sent
      Serial.println("Schedule  started");
      
    }                  
    if(nowseconds >= stopseconds - 31 && nowseconds <= stopseconds + 31 ){   // 62s on 60s timer ensures 1 trigger command is sent
      Serial.println("Schedule  finished");
      
    }               
  }
}

This works fine for me.
And i can see there are tons of delays in your code.

You will block Blynk.run with a delay()

Blynk.run() is a main Blynk routine responsible for keeping connection alive, sending data, receiving data, etc. When you use a delay() , you most likely are breaking a connection to Blynk Cloud or blocking some functions of Blynk library.

Basically, your sensorValue will never get to the Cloud.

1 Like

Yes I’ve set the time zone in the rtc widget.

My code has a lot of delay’s but that’s on purpose, the delay makes sure that nothing can interfere with the timing of the motor (e.a. connection loss) because if it runs to long things will break. Sometimes it has to reconnect because of the delays but that works fine.

I don’t really need new code, I need to now what is giving the extra start time. I think it is not in de code / arduino part but somewhere in the app or server/cloud side

edit> deleted the time input widged and created a new one on a new Vpin, hope this solves it…

If it doesn’t solve it then could you PM me your Auth code tomorrow please?
Click my name and you should see a button that says “Message”

Also, are you using relays to control the motors, or some sort of motor driver? I ask because these relays WILL cause you issues in the long term and we need to get rid of them. A timeout timer is one option, but it depends on your hardware.

Pete.

@PeteKnight I am a bit reluctant to send you my auth code, doesn’t that give you the possibility to control my arduino?
changing the Vpin didn’t solve the problem. But I remember now that the ghost event was ~12 minutes later when the starttime was set to 05:15, I will set it to 05:15 again to confirm.
What kink of issues are caused by relays?
I used a timeout timer before but as I remember a wifi/connection error sometimes resulted in not fully finishing the up or down sequence (I need the pauses to give the spring of the screen time to adjust)

You can change it as soon as @PeteKnight has solved your problem.

Yes it does.
You can regenerate a new Auth code later if you wish, but I only really wanted to see what widgets were set up in your app.
No problem if you don’t want to do that.

Pete.

Just place the code i gave you above along with the code with different virtual pin and see if the same problem occurs. It may hardly take few min to place the code.

Also try to Serial.print the current time and set time. See if there is any lag

int SThour;
int STmin;
int STsec;
int SPhour;
int SPmin;
int SPsec;

char currentTime[9];
char startTime[9];
char stopTime[9];

BLYNK_WRITE(YOUR_TIME_INPUT_WIDGET_PIN) {
  SThour = t.getStartHour();
  STmin = t.getStartMinute();
  STsec = t.getStartSecond();
  SPhour = t.getStopHour();
  SPmin = t.getStopMinute();
  SPsec = t.getStopSecond();
}

void setup () {
timer.setInterval(10000, Time_print);
}

void Time_print() {
      sprintf(currentTime, "%02d:%02d:%02d", hour(), minute(), second());
      Serial.print("Current Time: ");
      Serial.println(currentTime);

      sprintf(startTime, "%02d:%02d:%02d", SThour, STmin, STsec);
      Serial.print("Start Time: ");
      Serial.println(startTime);

      sprintf(startTime, "%02d:%02d:%02d", SPhour, SPmin, SPsec);
      Serial.print("Stop Time: ");
      Serial.println(startTime);
}

@Madhukesh I tried your time input snippet but the problem still happens.

Tonight my second sunscreen (same arduino) also got activated. The timer was set at 5:20 but it activated around 23:32, so the same 18h12 to late (or 5h48 to early…)

I also used the serial print code and there is no lag: current time is 2 seconds behind and the start/stop times are on the arduino just a few seconds after I set them in the app. And also the times are correct: 5:00 and 11:50 but still at 23:12 the timer activates…

@Rob2 Have you tried just the example sketch ? If not here is the link.

https://examples.blynk.cc/?board=Arduino%20Uno&shield=ESP8266%20WiFi%20Shield&example=Widgets%2FTimeInput%2FAdvancedTimeInput

For now lets just keep it simple (no additional code) and see if this solves the time lag.