Help before I spend time rewriting

Stability with Arduino Mega and ESP (ESP-01 in my case) is fully doable :wink:

This is what happens when I am busy with other projects and don’t keep reloading new additions to my testbench… it actually gets to stay running on it’s own :smiley:

I use this for connection

char auth[] = "xxxxxxxxxx";  // Local Server
//char auth[] = "xxxxxxxxxx";  // Cloud Server
char ssid[] = "xxxxxxxxxx";
char pass[] = "xxxxxxxxxx";
char server[] = "xxx.xxx.xxx.xxx";
// char server[] = "blynk-cloud.com";
int port = 8080;

And in void setup()

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

And my reconnection routine is a bit complex with notifications… but this is the jist of it

void loop() {
  timer.run();
  CNTR++;  // Loop/second counter for App display
  digitalWrite(HTB, !digitalRead(HTB));  // heartbeat LED

  if (Blynk.connected()) {  // If connected run as normal
    Blynk.run();
  } else if (ReCnctFlag == 0) {  // If NOT connected and not already tring to reconnect, set timer to try to reconnect in 60 seconds
    ReCnctFlag = 1;  // Set reconnection Flag
    Serial.println("Starting reconnection timer in 30 seconds...");
    timer.setTimeout(30000L, []() {  // Lambda Reconnection Timer Function
      ReCnctFlag = 0;  // Reset reconnection Flag
      ReCnctCount++;  // Increment reconnection Counter
      Serial.print("Attempting reconnection #");
      Serial.println(ReCnctCount);
      wifi.setDHCP(1, 1, 1); //Enable dhcp in station mode and save in flash of esp8266
      Blynk.config(wifi, auth, server, port);
      Blynk.connect();  // Try to reconnect to the server
      if (Blynk.connectWiFi(ssid, pass)) {
        Blynk.connect();
      }
    });  // END Timer Function
  }
}
1 Like