BLYNK
HOME       📲 GETTING STARTED       📗 DOCS       ❓HELP CENTER       👉 SKETCH BUILDER

Esp8622 Blynk reconnection error

Hi guys! I wrote recently about a blink reconnection error when the internet connection lost and resume.

Now I maked simple sketch to localize problem.
We have esp8266 (Wemos D1 mini), only board is connected to usb.
Code:


#define BLYNK_PRINT Serial
#include <BlynkSimpleEsp8266.h>
#include <WiFiClient.h>

BlynkTimer mainTimer;

char ssid[] = "xxx";
char password[] = "xxx";

void setup() 
{
  Serial.begin(115200);

  WiFi.mode(WIFI_AP_STA);
  IPAddress staticIPAP(192, 168, 4, 30);
  IPAddress staticIP(192, 168, 0, 30);
  IPAddress subnet(255, 255, 255, 0);
  IPAddress gateway(192, 168, 0, 1);

  WiFi.softAP("UberDevice", "xxx");
  WiFi.softAPConfig(staticIPAP, gateway, subnet);

  Serial.print("Soft-AP IP address: ");
  Serial.println(WiFi.softAPIP());
  Serial.printf("SoftAP MAC address: %s\n", WiFi.softAPmacAddress().c_str());

  WiFi.config(staticIP, gateway, subnet);
  WiFi.begin(ssid, password);
  Serial.printf("Connecting to: %s\n", ssid);

  // Wait for connection
  int iWaitWifi = 0;
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");

    if (++iWaitWifi > 50) {
      //Serial.println("Error connection to Wifi %s", ssid);
      break;
    }
  }
  Serial.println("");

  if (WiFi.status() == WL_CONNECTED) {

    Serial.print("Connected to ");
    Serial.println(WiFi.SSID());
    Serial.print("IP address: ");
    Serial.println(WiFi.localIP());
  }
  else {
    Serial.println("Not connected to Wifi!");
   
  }

  char auth[] = "xxx";
  Blynk.config(auth, "139.59.206.133", 8080);

  if (Blynk.connect())
    Serial.println("Blynk ESP8288 connected.");
  else 
    Serial.println("Blynk NOT connected!!");

   mainTimer.setInterval(3000, ProcessTimer);
}

void ProcessTimer()
{
  if (WiFi.status() != WL_CONNECTED)
    Serial.println("Wifi NOT connected!");
  CheckConnections();
}

int  iBlynkRF = 0;
void CheckConnections()
{  
   if (!Blynk.connected()) {

    //try reconnect every 10-th timer iteration
    if(iBlynkRF++ < 10)
      return;
    iBlynkRF = 0;
    
    Serial.println("Blynk NOT connected. Trying to reconnect...");
    if (Blynk.connect())
      Serial.println("Blynk reconnected.");
    else
      Serial.println("Blynk reconnection error!");
  }
  else{
    Serial.println("Blynk connected.");
    iBlynkRF = 0;
  }
}

void loop() 
{
  if(Blynk.connected())
    Blynk.run();
  mainTimer.run();
}

Output:
Blynk connected.
Blynk connected.
Blynk connected.
Blynk connected.
Blynk connected.
Blynk connected. ****************************Here I disable Internet on router
Wifi NOT connected!
Wifi NOT connected!
Blynk NOT connected. Trying to reconnect…
[202487] Connecting to 139.59.206.133:8080
[207488] Connecting to 139.59.206.133:8080
Blynk reconnection error!
Blynk NOT connected. Trying to reconnect…
[238487] Connecting to 139.59.206.133:8080
[243488] Connecting to 139.59.206.133:8080
Blynk reconnection error! ****************************Here enable it again
Blynk NOT connected. Trying to reconnect…
[274487] Connecting to 139.59.206.133:8080
[279488] Connecting to 139.59.206.133:8080
Blynk reconnection error!
Blynk NOT connected. Trying to reconnect…
[310487] Connecting to 139.59.206.133:8080
[315488] Connecting to 139.59.206.133:8080
Blynk reconnection error!
Blynk NOT connected. Trying to reconnect…
[346487] Connecting to 139.59.206.133:8080
[351488] Connecting to 139.59.206.133:8080
Blynk reconnection error!

PS. After I enable connection on the router again all other devices were successfully connected.
PPS. Please note that WiFi in AP and Sta mode, but this issue is appear also in Sta mode.

It seems to me that you’re making no attempt to re-connect to your Wi-Fi after an internet outage.

Pete.

In this test sketch no need for that, because WiFi connection not lost.

WiFi.status() != WL_CONNECTED 

only 2 times, while router applied setting (disable/enable internet connection). After

WiFi.status() is WL_CONNECTED and I see board in router monitor.

In main sketch I track WiFi connection status before check Blynk connection:

//reconnect WiFi
  if (WiFi.status() != WL_CONNECTED) {

    Serial.printf("Wifi NOT connected. Trying reconnect to: %s\n", ssid);
    
    if(iWiFiRF++ && (iWiFiRF < 10))
      return;
    iWiFiRF = 1;
    
    WiFi.begin(ssid, password);
        
    return;
  }
   else{
      if(iWiFiRF)
        Serial.printf("WiFi reconnected! SSID:%s\n", WiFi.SSID().c_str());
      iWiFiRF = 0;
   }

That’s not what your serial output says…

Pete.

It happens only 2 times (2 timer iteration) after I change router settings (disable/enable internet connection).
The WiFi connection is not lost during this 2 timer iteration but interrupted. And after resumed without reconnection.
PS. In the near future I will check this issue on different routers and types of Internet connections.

Update, Blynk cloud ping added

#define BLYNK_PRINT Serial
#include <BlynkSimpleEsp8266.h>
#include <WiFiClient.h>
#include <ESP8266Ping.h>

BlynkTimer mainTimer;

char ssid[] = "Xiaomi6";
//char ssid[] = "Bel";
char password[] = "wwwwwwwwwwe";

void setup() 
{
  Serial.begin(115200);

  WiFi.mode(WIFI_AP_STA);
  IPAddress staticIPAP(192, 168, 4, 30);
  IPAddress staticIP(192, 168, 0, 30);
  IPAddress subnet(255, 255, 255, 0);
  IPAddress gateway(192, 168, 0, 1);

  WiFi.softAP("UberDevice", "wwwwwwwwwwe");
  WiFi.softAPConfig(staticIPAP, gateway, subnet);

  Serial.print("Soft-AP IP address: ");
  Serial.println(WiFi.softAPIP());
  Serial.printf("SoftAP MAC address: %s\n", WiFi.softAPmacAddress().c_str());

  WiFi.config(staticIP, gateway, subnet);
  WiFi.begin(ssid, password);
  Serial.printf("Connecting to: %s\n", ssid);

  // Wait for connection
  int iWaitWifi = 0;
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");

    if (++iWaitWifi > 50) {
      //Serial.println("Error connection to Wifi %s", ssid);
      break;
    }
  }
  Serial.println("");

  if (WiFi.status() == WL_CONNECTED) {

    Serial.print("Connected to ");
    Serial.println(WiFi.SSID());
    Serial.print("IP address: ");
    Serial.println(WiFi.localIP());
  }
  else {
    Serial.println("Not connected to Wifi!");
   
  }

  char auth[] = "xxx";
  Blynk.config(auth, "139.59.206.133", 8080);
  //Blynk.config(auth);

  if (Blynk.connect())
    Serial.println("Blynk ESP8288 connected.");
  else 
    Serial.println("Blynk NOT connected!!");

   mainTimer.setInterval(3000, ProcessTimer);
}

void ProcessTimer()
{
  if (WiFi.status() != WL_CONNECTED)
    Serial.println("Wifi NOT connected!");

  Serial.print("Blynk ping: ");
  Serial.println(Ping.ping(IPAddress(139, 59, 206, 133)) ? "OK" : "ERROR!");
    
  CheckConnections();
}

int  iBlynkRF = 0;
void CheckConnections()
{  
   if (!Blynk.connected()) {

    //try reconnect every 10-th timer iteration
    if(iBlynkRF++ < 10)
      return;
    iBlynkRF = 0;
    
    Serial.println("Blynk NOT connected. Trying to reconnect...");
    if (Blynk.connect())
      Serial.println("Blynk reconnected.");
    else
      Serial.println("Blynk reconnection error!");
  }
  else{
    Serial.println("Blynk connected.");
    iBlynkRF = 0;
  }
}

void loop() 
{
  if(Blynk.connected())
    Blynk.run();
  mainTimer.run();
}

Output:
Soft-AP IP address: 192.168.4.30
SoftAP MAC address: 4E:11:AE:0C:3E:4D
Connecting to: Xiaomi6
.
Connected to Xiaomi6
IP address: 192.168.0.30
[583]
___ __ __
/ _ )/ /_ _____ / /__
/ _ / / // / _ / '/
/
//_, /////_
/
__/ v0.6.1 on ESP8266

[589] Connecting to 139.59.206.133:8080
[727] Ready (ping: 57ms).
Blynk ESP8288 connected.
Blynk ping: OK
Blynk connected.
Blynk ping: OK
Blynk connected.
Blynk ping: OK
Blynk connected.
Blynk ping: OK
Blynk connected.
Blynk ping: OK
Blynk connected.
Blynk ping: OK
Blynk connected.
Blynk ping: OK
Blynk connected.
Blynk ping: ERROR! // Here Internet connection disabled
Wifi NOT connected!
Blynk ping: ERROR!
Blynk ping: ERROR!
Blynk ping: ERROR!
Blynk ping: ERROR!
Blynk ping: ERROR!
Blynk ping: ERROR!
Blynk ping: ERROR!
Blynk ping: ERROR!
Blynk ping: ERROR!
Blynk ping: ERROR!
Blynk NOT connected. Trying to reconnect…
[62794] Connecting to 139.59.206.133:8080
[67795] Connecting to 139.59.206.133:8080
Blynk reconnection error!
Blynk ping: ERROR!
Wifi NOT connected!
Blynk ping: OK // And here enabled again
Blynk ping: OK
Blynk ping: OK
Blynk ping: OK
Blynk ping: OK
Blynk ping: OK
Blynk ping: OK
Blynk ping: OK
Blynk ping: OK
Blynk ping: OK
Blynk NOT connected. Trying to reconnect…
[118382] Connecting to 139.59.206.133:8080
[118536] Invalid auth token
Blynk reconnection error!
Blynk ping: OK
Blynk ping: OK
Blynk ping: OK
Blynk ping: OK
Blynk ping: OK
Blynk ping: OK
Blynk ping: OK
Blynk ping: OK
Blynk ping: OK
Blynk ping: OK
Blynk ping: OK
Blynk NOT connected. Trying to reconnect…
[172670] Connecting to 139.59.206.133:8080
[172798] Invalid auth token
Blynk reconnection error!
Blynk ping: OK
Blynk ping: OK
Blynk ping: OK
Blynk ping: OK

So, we have stable ping to Blynk cloud server, but Blynk.connect() does not connect to it.

Adding Blynk.config(auth, "139.59.206.133", 8080); before Blynk.connect(); in reconnect function fix this issue for me. I think it’s worth to adding this to the documentation.

During tests I wrote a small example, it shows how to reconnect WiFi and Blynk in case of connection(s) loss. I’ll leave it here, it might be useful for someone. Tested on Wemos D1 mini.

#define BLYNK_PRINT Serial
#include <BlynkSimpleEsp8266.h>
#include <WiFiClient.h>

BlynkTimer mainTimer;

char ssid[] = "xxx";
char password[] = "xxx";

//WiFi reconnection timeout, iWiFiReconnection * mainTimer interval sec.
const int iWiFiReconnection = 10;
//try to reconnect to Blynk, if connection lost, every iBlynkReconnection timer iteration
const int iBlynkReconnection = 8;

bool Connect2Blynk()
{
  char auth[] = "xxx";
  //Blynk.config must be called every time before Blynk.connect()
  Blynk.config(auth, "139.59.206.133", 8080);
  return Blynk.connect();
}

void setup() 
{
  Serial.begin(115200);

  WiFi.mode(WIFI_AP_STA);
  IPAddress staticIPAP(192, 168, 4, 30);
  IPAddress staticIP(192, 168, 0, 30);
  IPAddress subnet(255, 255, 255, 0);
  IPAddress gateway(192, 168, 0, 1);

  WiFi.softAP("xxx", "xxx");
  WiFi.softAPConfig(staticIPAP, gateway, subnet);

  Serial.print("Soft-AP IP address: ");
  Serial.println(WiFi.softAPIP());
  Serial.printf("SoftAP MAC address: %s\n", WiFi.softAPmacAddress().c_str());

  WiFi.config(staticIP, gateway, subnet);
  WiFi.begin(ssid, password);
  Serial.printf("Connecting to: %s\n", ssid);

  // Wait for connection
  int iWaitWifi = 0;
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");

    if (++iWaitWifi > 50)
      break;
  }
  Serial.println("");

  if (WiFi.status() == WL_CONNECTED) {

    Serial.print("Connected to ");
    Serial.println(WiFi.SSID());
    Serial.print("IP address: ");
    Serial.println(WiFi.localIP());
  }
  else {
    Serial.println("Not connected to Wifi!");
   
  }

  
  if (Connect2Blynk())
    Serial.println("Blynk ESP8288 connected.");
  else 
    Serial.println("Blynk NOT connected!!");

   mainTimer.setInterval(3000, ProcessTimer);
}

void ProcessTimer()
{
  CheckConnections();
  //do something here
}


//WiFi and Blynk Reconnection Flag
int iWiFiRF = 0,
    iBlynkRF = 0;
void CheckConnections()
{  
  //reconnect WiFi
  if (WiFi.status() != WL_CONNECTED) {

    Serial.printf("Wifi NOT connected. Trying reconnect to: %s\n", ssid);
    
    if(iWiFiRF++ && (iWiFiRF < iWiFiReconnection))
      return;
    iWiFiRF = 1;
    
    WiFi.begin(ssid, password);
        
    return;
  }
   else{
      if(iWiFiRF)
        Serial.printf("WiFi reconnected! SSID:%s\n", WiFi.SSID().c_str());
      iWiFiRF = 0;
   }

   //reconnect Blynk
   if (!Blynk.connected()) {
    
    if(++iBlynkRF < iBlynkReconnection)
      return;
    iBlynkRF = 0;
    
    Serial.println("Blynk NOT connected. Trying to reconnect...");
    if (Connect2Blynk())
      Serial.println("Blynk reconnected.");
    else
      Serial.println("Blynk reconnection error!");
  }
  else
    iBlynkRF = 0;
}

void loop() 
{
  if(Blynk.connected())
    Blynk.run();
  mainTimer.run();
}

This is the correct loop you need, else timer can’t run when blynk disconnect, and you need bracket for if condition.

void loop() 
{
mainTimer.run();
    if(Blynk.connected())
    {
    Blynk.run();
    }
}
1 Like

Thanks, but as far as I know Arduino language is a set of C/C++. So you code does the same thing as mine. Just another order of call.
As example.

if(bbb)
    function1();
function2();

bbb influenced only to function1();. fuction2(); executed regardless of value bbb.

PS. Maybe I’m wrong. I’ve been working relatively recently with Arduino language.

The brackets are needed.
And you talk about timer.run.
Yes you can write
Blynk.run;
Timer.run;

But if blynk disconnect, timer stop, I didn’t take a look at the full code, I don’t know il you need timer to run only if blynk run.
Maybe, my English is not enough good to explain.
Need help from @PeteKnight :joy:

my English is also far from ideal :slight_smile: )
If blynk disconnect, timer not stop.
Brackets are not required.
Just try code in Arduino IDE :wink:

Take a look here:
https://www.arduino.cc/reference/en/language/structure/control-structure/if/

It says, “The brackets may be omitted after an if statement. If this is done, the next line (defined by the semicolon) becomes the only conditional statement

This is something I did not know myself, but I will probably always still use them. Even if there is only one condition in the if statement.

Brackets may be omitted , I didn’t know that too !
Brackets can be omitted in some cases,
But as a general rule it is better to use them to avoid the next unwanted execution code.

Brackets, as a rule, not used in с/с++ programs if statement consist 1 stament. I’m a c/c++ programmer and newbie in arduino community, therefore I also didnt know that here on the contrary - the brackets are always used :slight_smile:

1 Like