To make 5 try connect

To make 5 connection attempts when the connection fails, restart the unit

Is this code correct?


void setup()
{

  Serial.begin(115200);

  Blynk.config(blynk_token);
  bool result = Blynk.connect();


  int cnt = 5;

  while (result != true) {
Blynk.connect();
    Serial.println (String("try connect :") + cnt);
    delay (1000);

    if (cnt-- == 0) {
      ESP.restart();
    }
  }
 
    Serial.println("BLYNK Connected");
    Serial.println(blynk_token);

}

I’ll give you a hint … you need to reinitialize “result”,

result = Blynk.connect();

within the while loop.

??? Can you explain more

void loop() {
  timer.run();
  if (Blynk.connected()) {  // If connected run as normal
    Blynk.run();
  } else {
     if(ReCnctCountFail++==5){
        Serial.print("Failed to connect");
        ESP.restart();
      }
    timer.setTimeout(60000L, []() {  // Lambda Reconnection Timer Function
      Serial.println("Connection lost, Starting reconnection in 60 seconds...");
      Serial.print("Attempting reconnection ");
       
      Blynk.connect();  // Try to reconnect to the server
    });  // END Timer Function
  }
1 Like

The code looks better :+1:
Thanks man

1 Like

Thanks to @Gunner :wink:

1 Like

Thank you to everyone in this community and thank you Blynk

1 Like