Check connection status in loop and reconect

@kongol.ml it is actually quite complex but this is the Serial Monitor output for the sketch below. Started with router powered up (but works with it powered off), after 33 seconds (3 iterations of the 11 second timer) power is removed from router. Router powered back up after a few seconds and then after about 10 iterations of the 11 second loop the ESP finally reconnects. Most routers take about a minute or so to do a full reset.

Started
..
IP address: 192.168.10.171
[1263] Blynk v0.3.8 on Arduino
[5001] Connecting to blynk-cloud.com:8442
[5215] Ready (ping: 31ms).
Still connected to Blynk server
Still connected to Blynk server
Still connected to Blynk server
[40442] Connecting to blynk-cloud.com:8442
Not connected to Blynk server
........
Check Router 
[51148] Blynk v0.3.8 on Arduino
[51148] Connecting to blynk-cloud.com:8442
[57171] Connecting to blynk-cloud.com:8442
Not connected to Blynk server
........
Check Router 
[68173] Blynk v0.3.8 on Arduino
[68173] Connecting to blynk-cloud.com:8442
[75170] Connecting to blynk-cloud.com:8442
Not connected to Blynk server
........
Check Router 

// repeated a further 7 or 8 times then:

Check Router 
[147263] Blynk v0.3.8 on Arduino
[147263] Connecting to blynk-cloud.com:8442
[147560] Ready (ping: 0ms).
[152561] Connecting to blynk-cloud.com:8442
[152683] Ready (ping: 1ms).
Still connected to Blynk server
Still connected to Blynk server
Still connected to Blynk server
Still connected to Blynk server
Still connected to Blynk server
Still connected to Blynk server

We actually use WiFiManager for most of our projects so we don’t have WiFi.begin section as we are already connected to WiFi when our sketch gets to this point.

The following 2 lines in the sketch (in MyWiFi() function) are important:

  Blynk.config(auth);
  Connected2Blynk = Blynk.connect(1000);  // 1000 is a timeout of 3333 milliseconds 

We found that you need Blynk.connect(timeoutinmilliseconds) to exit the Blynk connection loop and that Blynk.connect() can only be used with Blynk.config() rather than Blynk.begin(). It can probably be done but we couldn’t work out the required parameters to do it.

What does the sketch do:

  1. It tries to make a regular WiFi connection and continues after about 4 seconds whether it has connected to the router or not.

  2. Tries to connect to the Blynk server and continues after about 4 seconds whether it has connected to the server or not.

  3. Every 11 seconds it checks if we are still connected to the Blynk server. If we are not connected repeats steps 1 and 2 above.

I would suggest you get a new token and run the sketch over and over again to confirm it does what you want. Unplug the router, power back on etc. Then modify for your own use but ensure timer.setInterval() is called BEFORE making the connection (normally we recommend after) and watch the timings. We have:

WiFi Maximum Connection Time + Blynk Maximum Server Connection Time < 8 seconds (on a 11 second connection status timer).

// CheckConnection.ino by Costas 15/8/16 for kongol.ml

#define BLYNK_PRINT Serial    // Comment this out to disable prints and save space
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <SimpleTimer.h>  // Essential for all Blynk Projects

char auth[] = "xxxxxxxxxxxxxx";
char ssid[] = "xxxxxxxxxxxxxx";
char pass[] = "xxxxxxxxxxxxx";
bool Connected2Blynk = false;

SimpleTimer timer;

void setup() {
  Serial.begin(115200);
  delay(10);
  timer.setInterval(11000L, CheckConnection); // check if still connected every 11 seconds
  Serial.println("\nStarted");
  MyWiFi();
}

void MyWiFi(){
  int mytimeout = millis() / 1000;
  WiFi.begin(ssid, pass);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
    if((millis() / 1000) > mytimeout + 3){ // try for less than 4 seconds to connect to WiFi router
      break;
    }
  }

  if(WiFi.status() == WL_CONNECTED){  
    Serial.print("\nIP address: ");
    Serial.println(WiFi.localIP()); 
  }
  else{
    Serial.println("\nCheck Router ");    
  }
  Blynk.config(auth);
  Connected2Blynk = Blynk.connect(1000);  // 1000 is a timeout of 3333 milliseconds 
  mytimeout = millis() / 1000;
  while (Blynk.connect(1000) == false) { 
    if((millis() / 1000) > mytimeout + 3){ // try for less than 4 seconds
      break;
    }
  }  
}

void CheckConnection(){
  Connected2Blynk = Blynk.connected();
  if(!Connected2Blynk){
    Serial.println("Not connected to Blynk server");
    MyWiFi();  
  }
  else{
    Serial.println("Still connected to Blynk server");    
  }
}

void loop() {
  if(Connected2Blynk){
    Blynk.run();  // only process Blyk.run() function if we are connected to Blynk server
  }
  timer.run();
}
4 Likes