Blynk server connection (randomly offline)

Hi,
Blynk randomly get offline and about once a day won’t reconnect to online.
When I see such a problem, I restart power with sonoff and everything starts working normally.
But I would like to solve this problem.
After searching the forums, I wrote such a code
What am I doing wrong?

//blynk auth information
#define BLYNK_TEMPLATE_ID "xxxxxx"
#define BLYNK_DEVICE_NAME "xxxxxxx"
#define BLYNK_AUTH_TOKEN "xxxxxxxx"

// Send Blynk user messages to the hardware serial port…
#define BLYNK_PRINT Serial 

//blynk library
#include <ESP8266_Lib.h>
#include <BlynkSimpleShieldEsp8266.h>

//blynk auth
char auth[] = BLYNK_AUTH_TOKEN;

// wifi password, login
char ssid[] = "xxxxx";
char pass[] = "xxxxxx";

char server[] = "blynk.cloud";
unsigned int port = 8080;

// Define your ESP8266 baud rate:
#define ESP8266_BAUD 115200

// Tell the Blynk library to use the  Hardware Serial port 1 for WiFi..
ESP8266 wifi(&Serial3);

BlynkTimer timer;

  //Offline status
  int OfflineStatus =1;
  bool FirstTimeIsEmpty=true;
  int TryToConnectAgainCount=0;

void setup() 

{


      Serial.begin(115200);

   // Initialise the Hardware serial1 port…
      Serial3.begin(ESP8266_BAUD);
      delay(10);

     wifi.setDHCP(1, 1, 1); //Enable dhcp in station mode and save in flash of esp8266
     Blynk.config(wifi, auth, server, port);
    Blynk.connect();
}

void TryConnectServer() 
  {
  if(Blynk.connected())
  {
   return;
   }
      wifi.setDHCP(1, 1, 1); //Enable dhcp in station mode and save in flash of esp8266
      Blynk.config(wifi, auth, server, port);

  while (Blynk.connectWiFi(ssid, pass)==false) 
  {
     TryToConnectAgainCount++;
     //delay(50);
     Serial.println("tekrar yoxlanis");
     if(TryToConnectAgainCount==3) 
     {
     TryToConnectAgainCount=0;
     break;
     }
  } 
      

       Blynk.connect();
       


   }

void loop() 
{


  Blynk.run();
  timer.run();
  

 if(Blynk.connected()&& OfflineStatus==0) 
    {
OfflineStatus=1;

    FirstTimeIsEmpty=true;
           
     }  else if (!Blynk.connected() ) i
           {
                 
            
            if(FirstTimeIsEmpty==true)
               {
                 FirstTimeDisconnection=millis();
                 FirstTimeIsEmpty=false;
                 }
            if(FirstTimeIsEmpty==false)
               {
                 if(millis()-FirstTimeDisconnection >=interval)
                    {
                      OfflineStatus=0;
                      
                           
                       TryConnectServer();
                                        
                     }
                 }

           }
}

First of all, you should read this
https://docs.blynk.io/en/legacy-platform/legacy-articles/keep-your-void-loop-clean

Hi,
Thanks for reply.
You suggest to use this function with blynk timer?

Ok, I will check connection with blynk timer. Could something else be causing disconnection or will it be fixed if I fix this loop issue?

 if(Blynk.connected()&& OfflineStatus==0) 
    {
OfflineStatus=1;

    FirstTimeIsEmpty=true;
           
     }  else if (!Blynk.connected() ) i
           {
                 
            
            if(FirstTimeIsEmpty==true)
               {
                 FirstTimeDisconnection=millis();
                 FirstTimeIsEmpty=false;
                 }
            if(FirstTimeIsEmpty==false)
               {
                 if(millis()-FirstTimeDisconnection >=interval)
                    {
                      OfflineStatus=0;
                      
                           
                       TryConnectServer();
                                        
                     }
                 }

           }

It is “vshymanskyy” examples :

Have any another examples how handle connection?

/*************************************************************
  Download latest Blynk library here:
    https://github.com/blynkkk/blynk-library/releases/latest

  Blynk is a platform with iOS and Android apps to control
  Arduino, Raspberry Pi and the likes over the Internet.
  You can easily build graphic interfaces for all your
  projects by simply dragging and dropping widgets.

    Downloads, docs, tutorials: http://www.blynk.cc
    Sketch generator:           http://examples.blynk.cc
    Blynk community:            http://community.blynk.cc
    Follow us:                  http://www.fb.com/blynkapp
                                http://twitter.com/blynk_app

  Blynk library is licensed under MIT license
  This example code is in public domain.

 *************************************************************

  This example shows how to keep WiFi connection on ESP8266.

 *************************************************************/

/* Comment this out to disable prints and save space */
#define BLYNK_PRINT Serial

/* Fill-in your Template ID (only if using Blynk.Cloud) */
//#define BLYNK_TEMPLATE_ID   "YourTemplateID"


#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>

// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "YourAuthToken";

// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "YourNetworkName";
char pass[] = "YourPassword";


int lastConnectionAttempt = millis();
int connectionDelay = 5000; // try to reconnect every 5 seconds

void setup()
{
  // Debug console
  Serial.begin(9600);

  Blynk.begin(auth, ssid, pass);
}

void loop()
{
  // check WiFi connection:
  if (WiFi.status() != WL_CONNECTED)
  {
    // (optional) "offline" part of code

    // check delay:
    if (millis() - lastConnectionAttempt >= connectionDelay)
    {
      lastConnectionAttempt = millis();

      // attempt to connect to Wifi network:
      if (pass && strlen(pass))
      {
        WiFi.begin((char*)ssid, (char*)pass);
      }
      else
      {
        WiFi.begin((char*)ssid);
      }
    }
  }
  else
  {
    Blynk.run();
  }
}

Your are flooding blynk server , your void loop must be clean

you have to use only 2 lines

void loop() 
{
  Blynk.run();
  timer.run();
}

Thanks for reply.
I will try this coding

void setup() 

 {

    
    // Initialise the debug serial port
       Serial.begin(115200);

    // Initialise the Hardware serial1 port…
       Serial3.begin(ESP8266_BAUD);
       delay(10);


      MyWiFi();
    timer.setInterval(11000L, CheckConnection);
}

void CheckConnection(){
  Connected2Blynk = Blynk.connected();
  if(Connected2Blynk) 
  {
    Blynk.syncAll();
    }
  

 
  if(Connected2Blynk && OfflineStatus==0) 
     {
     Blynk.syncAll();
     OfflineStatus=1;
     FirstTimeIsEmpty=true;        
      }  else if (!Blynk.connected() ) // 
            { 
             if(FirstTimeIsEmpty==true)
                {
                  FirstTimeDisconnection=millis();
                  FirstTimeIsEmpty=false;
                  }
             if(FirstTimeIsEmpty==false)
                {
                  if(millis()-FirstTimeDisconnection >=interval)
                     {
                       OfflineStatus=0;
                       TurnAllOFF() ; 
                            
                            MyWiFi();
                                         
                      }
                  }
            }




  
}


void MyWiFi(){
  int mytimeout = millis() / 1000;
         wifi.setDHCP(1, 1, 1); //Enable dhcp in station mode and save in flash of esp8266
       Blynk.config(wifi, auth, server, port);
  while (Blynk.connectWiFi(ssid, pass)==false) {
    delay(500);
    Serial.print(".");
    if((millis() / 1000) > mytimeout + 3){ // try for less than 4 seconds to connect to WiFi router
      break;
    }
  }

  
  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 loop() 
{
 
  
  if(!Blynk.connected() )  {    Blynk.run();        }
  timer.run();
   
 
}


You will sync every 11 seconds !

Yes,
Because sometimes the server does not respond when the button is pressed. And sometimes on automation system doesn’t respond and this sync helps the system be more durable.

I know also it can freeze the blynk system.

Am I on the wrong way?

Problem solved like this :

void setup() 

 {

    
    // Initialise the debug serial port
       Serial.begin(115200);

    // Initialise the Hardware serial1 port…
       Serial3.begin(ESP8266_BAUD);
       delay(10);


      MyWiFi();
    timer.setInterval(11000L, CheckConnection);
}

void CheckConnection(){
  Connected2Blynk = Blynk.connected();
  if(Connected2Blynk) 
  {
    Blynk.syncAll();
    }
  

 
  if(Connected2Blynk==true && OfflineStatus==true) 
     {
     Blynk.syncAll();
     OfflineStatus=false;
     FirstTimeIsEmpty=true;        
      }  else if (Blynk.connected()==false ) // 
            { 
             if(FirstTimeIsEmpty==true)
                {
                  FirstTimeDisconnection=millis();
                  FirstTimeIsEmpty=false;
                  }
             if(FirstTimeIsEmpty==false)
                {
                  if(millis()-FirstTimeDisconnection >=interval)
                     {
                       OfflineStatus=true;
                       TurnAllOFF() ; 
                            
                            MyWiFi();
                                         
                      }
                  }
            }




  
}


void MyWiFi()
{

       Blynk.config(wifi, auth, server, port);
      Blynk.connectWiFi(ssid, pass)
 
      Blynk.connect(); 

}

void loop() 
{
 
  
   Blynk.run();       
   timer.run();
   
 
}

Why are you still doing this???

Pete.

1 Like

Hi,
Explanation :
I realized that sometimes the virtual pin gets defaults after disconnecting from the server. For example, the blynk automation activates some virtual pin, but after disconnecting (it can be a few seconds) from the server, the virtual pin is deactivating immediately. After reconnection to the server automatically that pin does not sync and automation is losing its act. This sync rechecks that pin every 11 seconds.
Have any other suggestions?
If have, please share.

In addition, it is suspected that the pin will not turn on or turn off after pressing the virtual button. But this synchronization makes the system more robust, and durable.

Haven’t you ever heard of BLYNK_CONNECTED() ??

Pete.

Please check the upper code, it is synch only when blynk.connected () is true


  Connected2Blynk = Blynk.connected();
  if(Connected2Blynk) 
  {
    Blynk.syncAll();
    }

BLYNK_CONNECTED() and Blynk.connected() are very different and are used in very different ways.
You are doing the wrong thing with your code, you need to fix it.

I’d suggest that you read the documentation.

Pete.

2 Likes

Thanky,
BLYNK_CONNECTED() with synch command must be inside loop or with timer?

Pete.

1 Like

Ok, got it, thanks.