Automatic reset if Blynk not connected

@NickMurray in the good old days when I was using those Arduino things I had sketches like the one below to cover your requirement :smile:
Change Mega bits to Uno bits and ESP bit to Ethernet etc.

/* Code extract to reset Arduino (Nano's require optiboot to fix WDT bug)
 * You need to add all the regular Blynk libraries for your specific system (sketch is for Mega with ESP8266 shield)
 * Sketch will not compile for Uno until you modify for that hardware i.e. just one serial port etc
 * Sketch shows Watchdog Timer option for general Arduino lock ups and resetFunc for disconnects with Blynk.
 */
//#define BLYNK_DEBUG         // Comment this out to disable prints and save space
#define BLYNK_PRINT Serial    // Comment this out to disable prints and save space
#include <ESP8266_HardSer.h>
#include <BlynkSimpleShieldEsp8266_HardSer.h>

#define EspSerial Serial1   // Set ESP8266 Serial object

char auth[] = "YourAuthToken";

ESP8266 wifi(EspSerial);
#include <avr/wdt.h> // watchdog timer, maybe set at 8 seconds in the sketch
#include <SimpleTimer.h>
SimpleTimer timer;

int disconnects; // number of times disconnected from server
byte pinV11 = 0;  // flag to ensure LED on Blynk V11 is only lit once for all disconnects.

void(* resetFunc) (void) = 0; //declare reset function @ address 0 THIS IS VERY USEFUL

void checkBlynk(){  // called every 3 seconds by SimpleTimer

  bool isconnected = Blynk.connected();
  if (isconnected == false){
    disconnects++;     
  }
  if(disconnects > 0){
     if (pinV11 == 0){  // this stops the sketch sending 255 to V11 over and over again
       Blynk.virtualWrite(V11, 255);
       pinV11 = 1;
       resetFunc();
     }
  }
}

void setup(){
    
  Serial.begin(9600); // Set console baud rate
  delay(10);
  EspSerial.begin(115200); // Set ESP8266 baud rate
  delay(10);

  Blynk.begin(auth, wifi, "ssid", "pass");
  wdt_enable(WDTO_8S);  // set WDT to 8 seconds
  timer.setInterval(3000L, checkBlynk); // check if connected to Blynk server every 3 seconds
}

void loop()
{
  Blynk.run(); // Initiates Blynk
  timer.run(); // Initiates SimpleTimer
  wdt_reset(); // if we get here turn off 8 second WDT  
}
1 Like