Example sketches for the new Arduino IDE for ESP8266

Moderator: igrr

User avatar
By UrsEppenberger
#69047 I write code for my ESP8266 based Sonoff-TH and Sonoff-POW devices. Basically they do their measurements and send the results via MQTT every 10 seconds.

I have to cope with unreliable WiFi. It seems similar to the one who started this topic. Therefore I post it here.

I wrote some recovery code at the beginning of the main loop.
The device works, recovers WiFi/MQTT from time to time, or restarts. But after a day or more, the WiFi hangs in a state, where the tests think everything is fine, but it isn't. The rest of the code continues to work, but all the WiFi related stuff doesn't. As a consequence the WiFi does not restart and the communication with the device is broken.

Please have a look at the error checking code. What can I do better to detect failures and do a recovery?
Plan B is to restart the device every hour:
if (millis() > 3600000) { ESP.restart(); }
But I'd rather have better code.

Code: Select allvoid loop() {
  if (WiFi.status() != WL_CONNECTED) {                //       WiFi not connected, try to fix it:

    WiFi.mode(WIFI_STA);                              // start setup WiFi
    WiFi.config(ip_sensor, ip_gateway, ip_subnet);    //       set fix WiFi config
    delay(10);
    WiFi.begin(WiFi_SSID, WiFi_PW);
    for ( int i = 0; i < 300; i++) {                  //       try to connect to WiFi for max 30s
      if (WiFi.status() == WL_CONNECTED) {break;}
      delay(100);
    }
    WiFiRestart = true;
  }
   
  if (WiFi.status() != WL_CONNECTED) {                //       if WiFi still failed, then
    delay(5000);                                      //         wait 5 sec and reboot
    ESP.restart();
  }
 
  if (!mqttClient.connected() || WiFiRestart) {       // start MQTT connection if not connected
    mqttClient.set_server(MQTT_Broker, 8883);         //       config MQTT Server
    mqttClient.connect(MQTT::Connect(Hostname).set_auth(MQTT_User, MQTT_PW));
    mqttClient.loop();
    delay(1000);
    if (!mqttClient.connected()) {                    //       if MQTT still not connected, then
      delay(5000);                                    //         wait 5 sec and reboot
      ESP.restart();
    } else {                                          //       MQTT connection succeeded, therefore
      mqttClient.set_callback(command_callback);      //         setup callback routine for commands from broker
      mqttClient.subscribe(cmd_topic);                //         subscribe to command topic
      mqttClient.publish(input_topic, Version);       //         publish SW version as a (re-)connect info
      mqttClient.loop();
      delay(100);                                     //         and get the messages out of the door before continuing
      WiFiRestart = false;
    }
  }                                                   // at this point WiFi and MQTT are up and running

//------
// more code here for measurements and MQTT communication
// deleted since I think it is not relevant for the discussion here
//------

  ArduinoOTA.handle();                                // start section with regular household functions
  mqttClient.loop();
  yield();                                            // end
}