Check connection status in loop and reconect

Hi. I cant figure out how to check connection status and reconnect again, if it was lost.
My problem is: everything work perfectly, until wifi router is rebooting. I’m not sure why is it happening, but to recreate the problem i just switch off my router for a few seconds. Every device in house reconnects and my wemos d1 reconnects as well, at least trying.

This is what i’ve on serial:

[79350] Connecting to blynk-cloud.com:8442
DHT11 temperature: 23.00
temp >=20
DHT11 humidity: 36.00
[86350] Connecting to blynk-cloud.com:8442
DHT11 temperature: 23.00
temp >=20
DHT11 humidity: 36.00
[93424] Connecting to blynk-cloud.com:8442
DHT11 temperature: 23.00
temp >=20
DHT11 humidity: 37.00
[100599] Connecting to blynk-cloud.com:8442

Connecting to blynk-cloud.com:8442 appears every ~7 seconds and forever.
For now the only way to fix that is just reboot wemos and everything start working again.

So what im trying to do is to check in loop something like:
–if(connected){work} else if(no_connected){full reconnection to blynk};

Thanks in advance

Hi, I had the same problem: my 3G router stops working about twice a week and I was obliged to press the push button of the router in order to reconnect to internet. I solved in this way:

  • I connected a normally open contact of a relay in parallel to the push button of the router.
  • The relay is driven by a digital output of arduino through a mosfet.
  • I put the following lines of code in the timer routine:
    if ( ! Blynk.connected() )
    {
    … relay on;
    delay(1000);
    … relay off;
    }

So, when the connection is lost the arduino perform a restart of the router. It is not very smart procedure but it works !

1 Like

That’s a bit rough way, but yea, sure it works :slight_smile:

but im wondering if there’s a way to reconnect without rebooting

Yes study Connection Management in the docs.

I’ve read it and tryed few variations of code, but didn’t manage to do that:(

Show us your best attempt (code) and the response you get from the hardware and we might be able to fix your sketch.

void handleBynkConnection(){
  bool result = Blynk.connect(timeout);
  if(result){
    Brynk.run();  
  } else{
      Blynk.disconnect();
  }
}

void loop(){
handleBynkConnection()
}
1 Like

@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

ok, i see, that quite tricky implementation :slight_smile:
so as i understand, blynk doesnt have api for these purposes, so you need to code it yourself (kind of), yes?

today will try this example (in documentaion i didnt noticed that there are Blynk.connect() and Blynk.connectED()) and will tell the results of investiongation:

Hi Kongol, I realized this draft router restart system because, im my case, the issue is due to the SIM Card of Vodafone provider that, depending on the type of SIM card, closes the connecrion on Internet every a certain period of time.
When this happens, the “way” from the hardware up to the server and the smartphone is completely interrupted and, in my opinion, there is no software system to restart the router except that pressing its power switch (manually or by a relay). But I am not sure. What do you think about?

@Gianca, im pretty sure that you can restart router programmatically, because usually you can do that through web interface. but as i understood, in your case the problem is that you router can’t connect to the internet, not you hardware, where you run blynk, right?

Guide to reboot router with Telnet http://www.howtogeek.com/206620/how-to-automatically-reboot-your-router-the-geeky-way/

My router has a timed reboot facility and the firmware doesn’t provide Telnet access.
It does however offer ssh access and a simple ‘reboot’ command will restart the device.

if( !Blynk.connected() ){
    Blynk.run();
}

and putting this code intro a timer still gives me only ```Connecting to blynk-cloud.com:8442


Is there really a way to check if connection was lost, then reconnect to blynk server properly, like it does when WeMos was restarted?
Maybe @Dmitriy  or @vhymanskyy can help?

Blynk.run has, I think, somewhat different functions than connect. It runs the main program. Blynk.config and Blynk.begin are, I think, the things you need to consider for connecting, not Blynk.run

yea, but what exactly? Blynk.run() or Blynk.connect() doesnt help in REconnection. im sure there is some simple solution, but cant figure out which

http://docs.blynk.cc/#blynk-firmware-connection-management does this help maybe? I never bother with it because Blynk will reconnect automatically so I guess I let it decide for itself most of the time :slight_smile:

well no, this is what i’ve tryed:

void handleConnection(){
  Serial.print("Blynk is now ");
  Serial.println(Blynk.connected());  
  if( !Blynk.connected() ){
    Blynk.run();
  } 
};

and running this function with SimpleTimer every 5 seconds in loop.

And this is what i’ve got:

  • first boot - everything is perfect
  • turn wifi router off for a while and this is what i’ve got every 7± seconds:
Blynk is now 0
[1150041] Connecting to blynk-cloud.com:8442

Have you considered the following:

if(Blynk.connected() )
{
  Blynk.disconnect();
}
else
{
  Blynk.connect();
}

Instead of using Blynk.run? I’m not sure if it’ll do the trick though :slight_smile:

i will try right now, but can you still explain the logic? if connected - disconnect, else connect.
Isnt that will run a loop of connect/disconnect?