[SOLVED] Timer widget fails to trigger every so often with ESP32

Hello!
My timer functions fail to trigger every now and then. I am guessing that this happens because my ESP32 goes offline for a few seconds at the exact time that the timer would trigger. I have 6 active timers on my project, and I would say one fails to either turn on or off at least a couple times a week.

Is there a way to ensure that the blynk server updates even if my ESP happens to be offline for exact moment my timer triggers? I think I remember @Pavel saying that blynk actually uses MQTT, is there a way to set the QoS to “at least once” to make sure that the message is received?

I’m open to any other solutions as well.

Another idea I had was to have the timer event just record some int like

int timerTrigger1 = hour() * 100 + minute(); //no idea if this actually works

and then have a simpleTimer function to check if the time is within the “on” range or the “off” range. This way if it misses the trigger one day it will at least have the correct trigger time stored from previous days. However, at that point it seems like there’s not much reason to actually have the timer widget other than the fact it makes it slightly easier to change the timer on the fly.

I changed your title to something a bit more informative.

Can you provide more details on the timers and issues…

Are the timers controlling digital or Virtual pns?

What, if any, code is working in conjunction with the timers?

Wave you confirmed that your ESP is loosing connection every so often… if so, how and could it be because of a timed action?

Supply info on phone type; What versions of App & Library; Are you using Local Server or Cloud; Show your code, etc.

Are the timers controlling digital or Virtual pns?

All the timers are controlling virtual pins.

What, if any, code is working in conjunction with the timers?

    BLYNK_WRITE(V47)
    {
      int pinValue = param.asInt(); // assigning incoming value from pin V1 to a variable
      if (pinValue == 1)
      {
      client.publish("Commands","Dawn Lights On"); 
      Blynk.virtualWrite(V43, 1);
      dawnLED.on();
      }
      if (pinValue == 0)
      {
      client.publish("Commands","Dawn Lights Off");
      Blynk.virtualWrite(V43, 0);
      dawnLED.off();
      }
    }

Have you confirmed that your ESP is loosing connection every so often… if so, how and could it be because of a timed action?

Yeah, my ESP has a MQTT message that it publishes on reconnect, every so often it loses connection… I’m guessing due to less than ideal wifi reception (this ESP happens to be in my garage, which is 50ft from my router through a concrete wall).

Supply info on phone type; What versions of App & Library; Are you using Local Server or Cloud; Show your code, etc.

Iphone 6s, IOS, app version 2.9.7 (1), Blynk library version 0.4.8.

Full code (far from optimized, I’m a novice at best):

    #include <WiFi.h>
    #include <WiFiClient.h>
    #include <BlynkSimpleEsp32.h>
    #include <PubSubClient.h>

    #include <WiFiUdp.h>
    #include <ArduinoOTA.h>
    #include <TimeLib.h>
    #include <WidgetRTC.h>


    //Blynk Auth
    char auth[] = "--";
    char ssid1[] = "--";
    char pass[] = "--";

    //PubSub Stuff
    const char* ssid = "--";
    const char* password = "--";
    const char* mqtt_server = "m12.--.com";
    const int mqtt_port = --;
    const char *mqtt_user = "--";
    const char *mqtt_pass = "--";
    const char *mqtt_client_name = "Garage32"; // Client connections cant have the same connection name

    WiFiClient espClient;
    PubSubClient client(espClient);

    //Global Variables
    const int openerPin = 27;  
    const int sensorPin = 26; 
    String previousTemp = "0";
    int oldStatus = 0;
    int masterAlarm = 0;
    int scarlettAlarm = 0;
    int garageAlarm = 0;
    int gatesAlarm = 0;
    int frontAlarm = 0;
    int sliderAlarm = 0;
    String currentTime = "0";
    String currentDate = "0";
    bool boot = true;

    //Blynk Services
    BlynkTimer timer;
    WidgetLED frontLED(V35);
    WidgetLED sliderLED(V36);
    WidgetLED gatesLED(V37);
    WidgetLED gateLED(V29);
    WidgetLED garageLED(V32);
    WidgetLED scarlettLED(V33);
    WidgetLED masterLED(V34);
    WidgetLED openerLED(V10);
    WidgetLED timerLED(V38);
    WidgetLED bedtimeLED(V30);
    WidgetLED dawnLED(V48);
    WidgetLED daylightLED(V49);
    WidgetLED buffetLED(V50);
    WidgetTerminal terminal(V0);
    WidgetRTC rtc;

    //Functions
    void setup_wifi() {

      delay(10);
      // We start by connecting to a WiFi network
      Serial.println();
      Serial.print("Connecting to ");
      Serial.println(ssid);

      WiFi.begin(ssid, password);

      while (WiFi.status() != WL_CONNECTED) {
        delay(500);
        Serial.print(".");
      }

      Serial.println("");
      Serial.println("WiFi connected");
      Serial.println("IP address: ");
      Serial.println(WiFi.localIP());
    }

    void reconnect() 
    {
      // Loop until we're reconnected
      while (!client.connected()) {
        Serial.print("Attempting MQTT connection...");
        // Attempt to connect
        if (client.connect(mqtt_client_name, mqtt_user, mqtt_pass)) {
          Serial.println("connected");
          // Once connected, publish an announcement...
          if(boot == false)
            {
              client.publish("Device_Status", "Garage32 Reconnected"); 
            }
            if(boot == true)
            {
              client.publish("Device_Status", "Garage32 Rebooted");
              boot = false;
              
            }
          // ... and resubscribe
          client.subscribe("Current_Temperature");
          client.subscribe("Pool_Mode");
          client.subscribe("Door_Status");
          client.subscribe("Device_Status");  
          client.subscribe("Aquarium_R");
          client.subscribe("Aquarium_L"); 
          Blynk.run();
          rtc.begin();
        } else {
          Serial.print("failed, rc=");
          Serial.print(client.state());
          Serial.println(" try again in 5 seconds");
          // Wait 5 seconds before retrying
          delay(5000);
        }
      }
    }

    void clockUpdate()
    {
      if(minute() < 10 && second() < 10)
      {
        currentTime = String(hour()) + ":0" + minute() + ":0" + second();
      }
      else if(minute() < 10)
      {
        currentTime = String(hour()) + ":0" + minute() + ":" + second();
      }
      else if(second() < 10)
      {
        currentTime = String(hour()) + ":" + minute() + ":0" + second();
      }
      else
      {
        currentTime = String(hour()) + ":" + minute() + ":" + second();
      }
      currentDate = String(month()) + "/" + day();
    }


    void callback(char* topic, byte* payload, unsigned int length) 
    {
      Serial.print("Message arrived [");
      String newTopic = topic;
      Serial.print(topic);
      Serial.print("] ");
      payload[length] = '\0';
      String newPayload = String((char *)payload);
      Serial.println(newPayload);
      Serial.println();
      if (newTopic == "Current_Temperature") 
      {
        Blynk.virtualWrite(V24, newPayload);
      }
      if (newTopic == "Aquarium_R") 
      {
        Blynk.virtualWrite(V39, newPayload);
      }
      if (newTopic == "Aquarium_L") 
      {
        Blynk.virtualWrite(V40, newPayload);
      }
      if (newTopic == "Pool_Mode")
      {
        if (newPayload == "SPA")
        {
          Blynk.virtualWrite(V22, 1);
          Blynk.setProperty(V24, "label", "Current Mode: SPA");
        }
        if (newPayload == "POOL")
        {
          Blynk.virtualWrite(V22, 0);
          Blynk.setProperty(V24, "label", "Current Mode: POOL");      
        }
      }
      if (newTopic == "Device_Status")
      {
        if (newPayload == "Garage 32 OK!")
        {
          Blynk.virtualWrite(51, currentTime);
        }
        if (newPayload == "Pool MCU OK!")
        {
          Blynk.virtualWrite(52, currentTime);
        }
        if (newPayload == "PatioMCU OK!")
        {
          Blynk.virtualWrite(53, currentTime);
        }
        if (newPayload == "Aquarium MCU OK!")
        {
          Blynk.virtualWrite(54, currentTime);
        }
      }
      if (newTopic == "Door_Status")
      {
        terminal.print(currentDate);
        terminal.print(" ");
        terminal.print(currentTime);
        terminal.print(" ");
        terminal.println(newPayload); 
        terminal.flush();
        if(newPayload == "Garage Open")
        {
          Blynk.virtualWrite(12, "Garage Door Open");
          if(garageAlarm == 1)
          {
            Blynk.notify("The Garage Door Has Opened");
            client.publish("Alarms","Garage Alarm");
          }
        }
        if(newPayload == "Garage Closed")
        {
          Blynk.virtualWrite(12, "Garage Door Closed");
          if(garageAlarm == 1)
          {
            Blynk.notify("The Garage Door Has Closed");
            client.publish("Alarms","Garage Alarm");
          }
        }
        if(newPayload == "Doorbell")
        {
          Blynk.notify("Doorbell!");
          client.publish("Alarms","Doorbell");
        }
        if(newPayload == "Scarlett Open")
        {
          Blynk.virtualWrite(13, "Scarlett Door Open");
          if(scarlettAlarm == 1)
          {
            Blynk.notify("Scarlett's Door Has Opened!");
            client.publish("Alarms","Scarlett Alarm");
          }
        }
        if(newPayload == "Scarlett Closed")
        {
          Blynk.virtualWrite(13, "Scarlett Door Closed");
          if(scarlettAlarm == 1)
          {
            Blynk.notify("Scarlett's Door Has Closed!");
          }
        }
        if(newPayload == "Master Open")
        {
          Blynk.virtualWrite(14, "Master Door Open");
          if(masterAlarm == 1)
          {
            Blynk.notify("Master Door Has Opened!");
            client.publish("Alarms","Master Alarm");      
          }
        }
        if(newPayload == "Master Closed")
        {
          Blynk.virtualWrite(14, "Master Door Closed");
          if(masterAlarm == 1)
          {
            Blynk.notify("Master Door Has Closed!");    
          }
        }
        if(newPayload == "Exercise Open")
        {
          Blynk.virtualWrite(26, "Exercise Door Open");
        }
        if(newPayload == "Exercise Closed")
        {
          Blynk.virtualWrite(26, "Exercise Door Closed");
        }
        if(newPayload == "Zeier Gate Open")
        {
          Blynk.virtualWrite(15, "Zeier Gate Open");
          Blynk.notify("Zeier Gate Open!");
          if(gatesAlarm == 1)
          {
            client.publish("Alarms","Gate Alarm");     
          }
        }
        if(newPayload == "Zeier Gate Closed")
        {
          Blynk.virtualWrite(15, "Zeier Gate Closed");
          Blynk.notify("Zeier Gate Has Closed!");
        }
        if(newPayload == "Equipment Gate Open")
        {
          Blynk.virtualWrite(23, "Equipment Gate Open");
          Blynk.notify("Equipment Gate Open!"); 
          if(gatesAlarm == 1)
          {
            client.publish("Alarms","Gate Alarm");     
          }
        }
        if(newPayload == "Equipment Gate Closed")
        {
          Blynk.virtualWrite(23, "Equipment Gate Closed");
          Blynk.notify("Equipment Gate Has Closed!");   
        }     
        if(newPayload == "Front Door Open")
        {
          Blynk.virtualWrite(16, "Front Door Open");
          if(frontAlarm == 1)
          {
            Blynk.notify("Front Door Open!");   
            client.publish("Alarms","Front Door Alarm");   
          }
        }
        if(newPayload == "Front Door Closed")
        {
          Blynk.virtualWrite(16, "Front Door Closed");
          if(frontAlarm == 1)
          {
            Blynk.notify("Front Door Closed!");     
          }
        }
        if(newPayload == "Sliding Door Open")
        {
          Blynk.virtualWrite(17, "Sliding Door Open");
          if(sliderAlarm == 1)
          {
            Blynk.notify("Sliding Door Open!");   
            client.publish("Alarms","Slider Alarm");   
          }
        }
        if(newPayload == "Sliding Door Closed")
        {
          Blynk.virtualWrite(17, "Sliding Door Closed");
          if(sliderAlarm == 1)
          {
            Blynk.notify("Sliding Door Has Closed!");    
          }
        }  
      }
    }

    //Timer Functions


    void getGarageDoorState()
    {
      int newStatus = digitalRead(sensorPin);
      if(newStatus != oldStatus && newStatus == 1)
      {
        client.publish("Door_Status","Garage Open");
        oldStatus = newStatus;
      }
      if(newStatus != oldStatus && newStatus == 0)
      {
        client.publish("Door_Status","Garage Closed");
        oldStatus = newStatus;
      }
    }


    void checkIn()
    {
      client.publish("Device_Status","Garage 32 OK!");
    }

    void alarmStatus()
    {
      if(masterAlarm == 0)
      {
        masterLED.off();
        Blynk.virtualWrite(V4, 0);
      }
      if(masterAlarm == 1)
      {
        masterLED.on();
        Blynk.virtualWrite(V4, 1);
      }
      if(garageAlarm == 0)
      {
        garageLED.off();
        Blynk.virtualWrite(V2, 0);
      }
      if(garageAlarm == 1)
      {
        garageLED.on();
        Blynk.virtualWrite(V2, 1);
      }
      if(gatesAlarm == 0)
      {
        gateLED.off();
        gatesLED.off();
        Blynk.virtualWrite(V9, 0);
      }
      if(gatesAlarm == 1)
      {
        gateLED.on();
        gatesLED.on();
        Blynk.virtualWrite(V9, 1);
      }
      if(frontAlarm == 0)
      {
        frontLED.off();
        Blynk.virtualWrite(V8, 0);
      }
      if(frontAlarm == 1)
      {
        frontLED.on();
        Blynk.virtualWrite(V8, 1);
      }
      if(sliderAlarm == 0)
      {
        sliderLED.off();
        Blynk.virtualWrite(V10, 0);
      }
      if(sliderAlarm == 1)
      {
        sliderLED.on();
        Blynk.virtualWrite(V10, 1);
      }
      if(scarlettAlarm == 0)
      {
        scarlettLED.off();
        Blynk.virtualWrite(V3, 0);
      }
      if(scarlettAlarm == 1)
      {
        scarlettLED.on();
        Blynk.virtualWrite(V3, 1);
      }
    }


    //Blynk Writes
    BLYNK_WRITE(V20)
    {
      int pinValue = param.asInt(); // assigning incoming value from pin V1 to a variable
      if (pinValue == 1)
      {
        if (! client.publish("Commands","Pool_Light")) 
        {
        Blynk.virtualWrite(V25, "Command Failed");
        delay(1000);
        Blynk.virtualWrite(V25, " ");
        }
        else 
        {
        Blynk.virtualWrite(V25, "Pool Light Sent");
        delay(1000);
        Blynk.virtualWrite(V25, " ");
        }
      }
    }

    BLYNK_WRITE(V21)
    {
      int pinValue = param.asInt(); // assigning incoming value from pin V1 to a variable
      if (pinValue == 1)
      {
        if (! client.publish("Commands","Spa_Light")) 
        {
        Blynk.virtualWrite(V25, "Command Failed");
        delay(1000);
        Blynk.virtualWrite(V25, " ");
        }
        else 
        {
        Blynk.virtualWrite(V25, "Spa Light Sent");
        delay(1000);
        Blynk.virtualWrite(V25, " ");
        }
      }
    }

    BLYNK_WRITE(V22)
    {
      int pinValue = param.asInt(); // assigning incoming value from pin V1 to a variable
      if (pinValue == 1)
      {
        if (! client.publish("Commands","SPA")) 
        {
        Blynk.virtualWrite(V25, "Command Failed");
        delay(1000);
        Blynk.virtualWrite(V25, " ");
        }
        else 
        {
        Blynk.virtualWrite(V25, "Mode: SPA Sent");
        delay(1000);
        Blynk.virtualWrite(V25, " ");
        }
      }
      if (pinValue == 0)
      {
        if (! client.publish("Commands","POOL")) 
        {
        Blynk.virtualWrite(V25, "Command Failed");
        delay(1000);
        Blynk.virtualWrite(V25, " ");
        }
        else 
        {
        Blynk.virtualWrite(V25, "Mode: POOL Sent");
        delay(1000);
        Blynk.virtualWrite(V25, " ");
        }
      }
    }

    BLYNK_WRITE(V2)
    {
      int pinValue = param.asInt(); // assigning incoming value from pin V1 to a variable
      if (pinValue == 1 && garageAlarm == 0)
      {
        garageAlarm = 1;
        garageLED.on();
      }
      if (pinValue == 0 && garageAlarm == 1)
      {
        garageAlarm = 0;
        garageLED.off();
      }
    }

    BLYNK_WRITE(V8)
    {
      int pinValue = param.asInt(); // assigning incoming value from pin V1 to a variable
      if (pinValue == 1 && frontAlarm == 0)
      {
        frontAlarm = 1;
        frontLED.on();
      }
      if (pinValue == 0 && frontAlarm == 1)
      {
        frontAlarm = 0;
        frontLED.off();
      }
    }

    BLYNK_WRITE(V9)
    {
      int pinValue = param.asInt(); // assigning incoming value from pin V1 to a variable
      if (pinValue == 1 && gatesAlarm == 0)
      {
        gatesAlarm = 1;
        gatesLED.on();
        gateLED.on();
      }
      if (pinValue == 0 && gatesAlarm == 1)
      {
        gatesAlarm = 0;
        gatesLED.off();
        gateLED.off();
      }
    }

    BLYNK_WRITE(V3)
    {
      int pinValue = param.asInt(); // assigning incoming value from pin V1 to a variable
      if (pinValue == 1 && scarlettAlarm == 0)
      {
        scarlettAlarm = 1;
        scarlettLED.on();
      }
      if (pinValue == 0 && scarlettAlarm == 1)
      {
        scarlettAlarm = 0;
        scarlettLED.off();
      }
    }

    BLYNK_WRITE(V10)
    {
      int pinValue = param.asInt(); // assigning incoming value from pin V1 to a variable
      if (pinValue == 1 && sliderAlarm == 0)
      {
        sliderAlarm = 1;
        sliderLED.on();
      }
      if (pinValue == 0 && sliderAlarm == 1)
      {
        sliderAlarm = 0;
        sliderLED.off();
      }
    }

    BLYNK_WRITE(V4)
    {
      int pinValue = param.asInt(); // assigning incoming value from pin V1 to a variable
      if (pinValue == 1 && masterAlarm == 0)
      {
        masterAlarm = 1;
        masterLED.on();
      }
      if (pinValue == 0 && masterAlarm == 1)
      {
        masterAlarm = 0;
        masterLED.off();
      }
    }

    BLYNK_WRITE(V7)
    {
      int pinValue = param.asInt(); // assigning incoming value from pin V1 to a variable
      if (pinValue == 1)
      {
        digitalWrite(openerPin, HIGH);
        openerLED.on();
        delay(1500);
        digitalWrite(openerPin, LOW);
        openerLED.off();
      }
    }


    BLYNK_WRITE(V5)
    {
      int pinValue = param.asInt(); // assigning incoming value from pin V1 to a variable
      if (pinValue == 1)
      {
        client.publish("Commands","Patio Light On");
      }
      if (pinValue == 0)
      {
        client.publish("Commands","Patio Light Off");
      }
    }

    BLYNK_WRITE(V6)
    {
      int pinValue = param.asInt(); // assigning incoming value from pin V1 to a variable
      if (pinValue == 1)
      {
        client.publish("Commands","Patio Fan On");
      }
      if (pinValue == 0)
      {
        client.publish("Commands","Patio Fan Off");
      }
    }

    BLYNK_WRITE(V1)
    {
      int pinValue = param.asInt(); // assigning incoming value from pin V1 to a variable
      if (pinValue == 1)
      {
        client.publish("Alarms","Clear Alarms");
      }
    }

    BLYNK_WRITE(V18)
    {
      int pinValue = param.asInt(); // assigning incoming value from pin V1 to a variable
      if (pinValue == 1)
      {
        masterAlarm = 0;
        scarlettAlarm = 0;
        garageAlarm = 0;
        gatesAlarm = 0;
        frontAlarm = 0;
        sliderAlarm = 0;
      }
    }

    BLYNK_WRITE(V28)
    {
      int pinValue = param.asInt(); // assigning incoming value from pin V1 to a variable
      if (pinValue == 1)
      {
        gatesAlarm = 1;
        frontAlarm = 1;
        sliderAlarm = 1;
        timerLED.on();
      }
      if (pinValue == 0)
      {
        gatesAlarm = 0;
        frontAlarm = 0;
        sliderAlarm = 0;
        timerLED.off();
      }
    }

    BLYNK_WRITE(V19)
    {
      int pinValue = param.asInt(); // assigning incoming value from pin V1 to a variable
      if (pinValue == 1)
      {
        scarlettAlarm = 1;
        bedtimeLED.on();
      }
      if (pinValue == 0)
      {
        scarlettAlarm = 0;
        bedtimeLED.off();
      }
    }

    BLYNK_WRITE(V42)
    {
      int pinValue = param.asInt(); // assigning incoming value from pin V1 to a variable
      if (pinValue == 1)
      {
      client.publish("Commands","Daylight LED On"); 
      }
      if (pinValue == 0)
      {
      client.publish("Commands","Daylight LED Off");
      }
    }

    BLYNK_WRITE(V43)
    {
      int pinValue = param.asInt(); // assigning incoming value from pin V1 to a variable
      if (pinValue == 1)
      {
      client.publish("Commands","Dawn LED On"); 
      }
      if (pinValue == 0)
      {
      client.publish("Commands","Dawn LED Off");
      }
    }

    BLYNK_WRITE(V44)
    {
      int pinValue = param.asInt(); // assigning incoming value from pin V1 to a variable
      if (pinValue == 1)
      {
      client.publish("Commands","Filter On"); 
      }
      if (pinValue == 0)
      {
      client.publish("Commands","Filter Off");
      }
    }

    BLYNK_WRITE(V47)
    {
      int pinValue = param.asInt(); // assigning incoming value from pin V1 to a variable
      if (pinValue == 1)
      {
      client.publish("Commands","Dawn LED On"); 
      Blynk.virtualWrite(V43, 1);
      dawnLED.on();
      }
      if (pinValue == 0)
      {
      client.publish("Commands","Dawn LED Off");
      Blynk.virtualWrite(V43, 0);
      dawnLED.off();
      }
    }

    BLYNK_WRITE(V46)
    {
      int pinValue = param.asInt(); // assigning incoming value from pin V1 to a variable
      if (pinValue == 1)
      {
      client.publish("Commands","Daylight LED On"); 
      Blynk.virtualWrite(V42, 1);
      daylightLED.on();
      }
      if (pinValue == 0)
      {
      client.publish("Commands","Daylight LED Off");
      Blynk.virtualWrite(V42, 0);
      daylightLED.off();
      }
    }

    BLYNK_WRITE(V31)
    {
      int pinValue = param.asInt(); // assigning incoming value from pin V1 to a variable
      if (pinValue == 1)
      {
      client.publish("Commands","Buffet Light On"); 
      }
      if (pinValue == 0)
      {
      client.publish("Commands","Buffet Light Off");
      }
    }

    BLYNK_WRITE(V41)
    {
      int pinValue = param.asInt(); // assigning incoming value from pin V1 to a variable
      if (pinValue == 1)
      {
      client.publish("Commands","Buffet Light On"); 
      Blynk.virtualWrite(V31, 1);
      buffetLED.on();
      }
      if (pinValue == 0)
      {
      client.publish("Commands","Buffet Light Off");
      Blynk.virtualWrite(V31, 0);
      buffetLED.off();
      }
    }

    BLYNK_WRITE(V11)
    {
      int pinValue = param.asInt(); // assigning incoming value from pin V1 to a variable
      if (pinValue == 1)
      {
        Blynk.syncAll();
        terminal.println("Updated");
        terminal.flush();
      }
    }

    BLYNK_APP_CONNECTED()
    {
      Blynk.syncAll();
      Blynk.syncVirtual(V0);
      client.publish("Device_Status", "App Connected");
    }

    void setup() 
    {
      Serial.begin(115200);
      setup_wifi();
      client.setServer(mqtt_server, 16552);
      client.setCallback(callback);
      ArduinoOTA.setHostname("garage32");
      ArduinoOTA.begin(); 

      Blynk.config(auth);
      
      pinMode(sensorPin, INPUT);
      pinMode(openerPin, OUTPUT);

      setSyncInterval(60);
      // timer setup in millis
      timer.setInterval(1000, getGarageDoorState);
      timer.setInterval(1000, alarmStatus);
      timer.setInterval(60000, checkIn);
      timer.setInterval(5000, clockUpdate);  
    }

    void loop() 
    {
      if (!client.connected()) 
      {
        reconnect();
      }
      client.loop();
      ArduinoOTA.handle();
      Blynk.run();
      timer.run(); 
      ArduinoOTA.handle();
      
    }

I see you have a lot of 1 second delays in many of your BLYNK_WRITE() loops… that WILL cause many issues with disconnections and reliability, etc. (even on an ESP32, which I believe has less picky WiFi timing issues then the 8266… but Blynk still has it’s own timing requirements).

Avoid using delays for more than a few ms in critical needs (like Ultrasonic sensors, etc)… use BlynkTimer routines instead. A bit more coding and fancy placement of functions, but it is NON-Blocking.

http://docs.blynk.cc/#blynk-firmware-blynktimer

@taitrt here is what you need - http://docs.blynk.cc/#blynk-main-operations-state-syncing

@Dmitriy While you are bringing that method up… is there a way to have Blynk.syncAll() do so with small (adjustable?) delays in between each vPin? I have tried that command as is, but since I have so much to sync, it instantly floods the system.

Nope.

How is that? HArdware drops connection?

Essentially… massive Buffer Overflow errors and system never recovers until physical reset.

Since I switched to ESP from USB, I have had random disconnections (possibly WiFi congestion at home :wink: ). I have managed to code recovery routines that work quite nicely… but I can only manually synchronise a few key vPins at a time, since syncAll just makes it worse.

1 Like

I see… I’ll check what we can do here.

2 Likes

Thank you.

1 Like

@taitrt Here is a blocking-free timer example for one of your BLYNK_WRITE() routines

//Blynk Writes
BLYNK_WRITE(V20)
{
  int pinValue = param.asInt(); // assigning incoming value from pin V1 to a variable
  if (pinValue == 1)
  {
    if (! client.publish("Commands", "Pool_Light"))
    {
      Blynk.virtualWrite(V25, "Command Failed");
      timer.setTimeout(1000L, ClearMsg);  // Run Clear Message routine in 1 second
    }
    else
    {
      Blynk.virtualWrite(V25, "Pool Light Sent");
      timer.setTimeout(1000L, ClearMsg);  // Run Clear Message routine in 1 second
    }
  }
}

void ClearMsg()
{
  Blynk.virtualWrite(V25, " ");
}

For some reason, that actually doesn’t correct the problem for me. My project actually has a Blynk.syncAll() button for testing purposes. Whenever I press it after one of my timers doesn’t trigger it syncs, but it doesn’t sync the correct state. For instance, lets say my timer is 6pm HIGH 10pm LOW, at 8pm if I use Blynk.syncAll() after missing a timer trigger it will send LOW again, instead of sending HIGH as it should. Does this indicate that the problem is actually between my phone and the blynk server, rather than my ESP?

Yeah, I know about blynktimer. The only reason those delays are there is because they actually shouldn’t ever be called. Those only happen if for some reason the publish to my MQTT server fails, which should basically never occur.

Thanks for the non-blocking example though. I’ll probably just change it out because it’s good practice.

No. Timers are handled on the server. Seems like you did SYNC after 10PM. Is that correct?

No I did the sync at 8pm, meaning that it was in the time that the trigger should have sent HIGH, but it sent LOW instead.

I will make sure to document it better next time it happens and do more testing.

@Dmitriy It appears that Blynk.syncAll() does not sync timers at all. When I run the command all of my buttons that are configured as switches sync, but my timers do not.

Today the “Bedtime” timer failed to turn off:

BLYNK_WRITE(V19)
{
  int pinValue = param.asInt(); // assigning incoming value from pin V1 to a variable
  if (pinValue == 1)
  {
    scarlettAlarm = 1;
    bedtimeLED.on();
  }
  if (pinValue == 0)
  {
    scarlettAlarm = 0;
    bedtimeLED.off();
  }
} 

This timer should have turned off the bedtimeLED at 4am, but it did not and using syncAll() doesn’t result in any change in the LED even though the rest of the switches will update.

More testing:

I have a button set up like this:

BLYNK_WRITE(V11)
{
  int pinValue = param.asInt();
  if (pinValue == 1)
  {
    Blynk.syncAll();
    Blynk.syncVirtual(V19);
    terminal.println("Updated");
    terminal.flush();
  }
}

Pressing it does not update the timer vpin, is this expected functionality?

@Dmitriy is there a chance of getting timer vpins added to the sync functions in the future?

@taitrt timers already support syncs.

Mine do not respond to Blynk.syncvirtual or Blynk.syncall