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

To make 5 try connect

esp8266
#1

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);

}

0 Likes

#2

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

result = Blynk.connect();

within the while loop.

0 Likes

#3

??? Can you explain more

0 Likes

#4
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

#5

The code looks better :+1:
Thanks man

1 Like

#6

Thanks to @Gunner :wink:

1 Like

#7

Thank you to everyone in this community and thank you Blynk

1 Like