diff --git a/main/User_config.h b/main/User_config.h index 859c7df..20f48db 100644 --- a/main/User_config.h +++ b/main/User_config.h @@ -175,6 +175,10 @@ const byte mac[] = {0xDE, 0xED, 0xBA, 0xFE, 0x54, 0x95}; //W5100 ethernet shield # define MQTT_PORT "1883" #endif +#ifndef GeneralTimeOut +# define GeneralTimeOut 20 // time out if a task is stuck in seconds (should be more than TimeBetweenReadingRN8209/1000) and more than 3 seconds, the WDT will reset the ESP, used also for MQTT connection +#endif + #if defined(ESP8266) || defined(ESP32) // Uncomment to use a device running TheengsGateway to decode BLE data. (https://github.com/theengs/gateway) // Set the topic to the subscribe topic configured in the TheengGateway diff --git a/main/ZsensorRN8209.ino b/main/ZsensorRN8209.ino index 1a7356e..2dfda18 100644 --- a/main/ZsensorRN8209.ino +++ b/main/ZsensorRN8209.ino @@ -26,8 +26,6 @@ */ #ifdef ZsensorRN8209 -# include - # include "ArduinoJson.h" # include "driver/uart.h" # include "rn8209_flash.h" @@ -107,7 +105,6 @@ void setupRN8209() { cal.EC = RN8209_EC; set_user_param(cal); init_8209c_interface(); - esp_task_wdt_init(TimeOutWDTRN8209, true); xTaskCreate(rn8209_loop, "rn8209_loop", 5000, NULL, 10, &rn8209TaskHandle); esp_task_wdt_add(rn8209TaskHandle); Log.trace(F("ZsensorRN8209 setup done " CR)); diff --git a/main/config_RN8209.h b/main/config_RN8209.h index e244830..74083d4 100644 --- a/main/config_RN8209.h +++ b/main/config_RN8209.h @@ -47,9 +47,6 @@ extern void RN8209toMQTT(); # define TimeBetweenReadingRN8209 500 // time between 2 RN8209 readings in ms #endif -#ifndef TimeOutWDTRN8209 -# define TimeOutWDTRN8209 5 // time out if RN8209 task is stuck in seconds (should be more than TimeBetweenReadingRN8209/1000), the WDT will reset the ESP -#endif #ifndef TimeBetweenPublishingRN8209 # define TimeBetweenPublishingRN8209 60000 // time between 2 RN8209 publishing in ms #endif diff --git a/main/main.ino b/main/main.ino index e47f254..c8730c1 100644 --- a/main/main.ino +++ b/main/main.ino @@ -27,6 +27,15 @@ */ #include "User_config.h" +// States of the gateway +// Wm setup +// Connected to MQTT +// Disconnected from Wifi +// Disconnected from MQTT +// Deep sleep +// Local OTA in progress +// Remote OTA in progress + // Macros and structure to enable the duplicates removing on the following gateways #if defined(ZgatewayRF) || defined(ZgatewayIR) || defined(ZgatewaySRFB) || defined(ZgatewayWeatherStation) || defined(ZgatewayRTL_433) // array to store previous received RFs, IRs codes and their timestamps @@ -239,6 +248,7 @@ static String ota_server_cert = ""; # include # include # include +# include # include # include @@ -618,6 +628,9 @@ void delayWithOTA(long waitMillis) { ArduinoOTA.handle(); # if defined(ZwebUI) && defined(ESP32) WebUILoop(); +# endif +# ifdef ESP32 + esp_task_wdt_reset(); # endif delay(waitStep); } @@ -676,11 +689,16 @@ void connectMQTT() { #endif Log.warning(F("MQTT connection..." CR)); +#ifdef ESP32 + esp_task_wdt_add(NULL); + esp_task_wdt_reset(); +#endif char topic[mqtt_topic_max_size]; strcpy(topic, mqtt_topic); strcat(topic, gateway_name); strcat(topic, will_Topic); client.setBufferSize(mqtt_max_packet_size); + client.setSocketTimeout(GeneralTimeOut - 1); #if AWS_IOT if (client.connect(gateway_name, mqtt_user, mqtt_pass)) { // AWS doesn't support will topic for the moment #else @@ -920,7 +938,9 @@ void setup() { Log.error(F("Failed to set deep sleep EXT0 Wakeup." CR)); } # endif - +# ifdef ESP32 + esp_task_wdt_init(GeneralTimeOut, true); //enable panic so ESP32 restarts +# endif /* The 2 modules below are not connection dependent so start them before the connectivity functions Note that the ONOFF module need to start after the RN8209 so that the overCurrent function is launched after the setup of the sensor @@ -1221,6 +1241,9 @@ void setOTA() { }); ArduinoOTA.onProgress([](unsigned int progress, unsigned int total) { Log.trace(F("Progress: %u%%\r" CR), (progress / (total / 100))); +# ifdef ESP32 + esp_task_wdt_reset(); +# endif last_ota_activity_millis = millis(); }); ArduinoOTA.onError([](ota_error_t error) { @@ -1391,6 +1414,9 @@ void blockingWaitForReset() { if (level == ACTUATOR_ON) { ActuatorTrigger(); } +# endif +# ifdef ESP32 + esp_task_wdt_delete(NULL); # endif InfoIndicatorOFF(); SendReceiveIndicatorOFF(); @@ -1753,6 +1779,10 @@ void loop() { # endif #endif +#ifdef ESP32 + esp_task_wdt_reset(); +#endif + unsigned long now = millis(); // Switch off of the LED after TimeLedON @@ -1781,7 +1811,6 @@ void loop() { failure_number_ntwk = 0; // We have just re-connected if connected was previously false bool justReconnected = !connected; - #ifdef ZmqttDiscovery // Deactivate autodiscovery after DiscoveryAutoOffTimer if (SYSConfig.discovery && (now > lastDiscovery + DiscoveryAutoOffTimer)) @@ -1944,10 +1973,16 @@ void loop() { connectMQTT(); } } else { // disconnected from network +#ifdef ESP32 + esp_task_wdt_reset(); +#endif connected = false; Log.warning(F("Network disconnected:" CR)); ErrorIndicatorON(); delay(2000); // add a delay to avoid ESP32 crash and reset +#ifdef ESP32 + esp_task_wdt_reset(); +#endif ErrorIndicatorOFF(); delay(2000); #if defined(ESP8266) || defined(ESP32) && !defined(ESP32_ETHERNET) @@ -2068,6 +2103,7 @@ void eraseAndRestart() { delay(5000); ESP.reset(); # else + esp_task_wdt_delete(NULL); nvs_flash_erase(); ESP.restart(); # endif @@ -2374,6 +2410,7 @@ String latestVersion; bool checkForUpdates() { Log.notice(F("Update check, free heap: %d"), ESP.getFreeHeap()); HTTPClient http; + http.setTimeout(GeneralTimeOut - 1); // -1 to avoid WDT http.setFollowRedirects(HTTPC_STRICT_FOLLOW_REDIRECTS); http.begin(OTA_JSON_URL, OTAserver_cert); int httpCode = http.GET(); @@ -2458,8 +2495,12 @@ void MQTTHttpsFWUpdate(char* topicOri, JsonObject& HttpsFwUpdateData) { } # if defined(ZgatewayBT) stopProcessing(); +# endif +# ifdef ESP32 + esp_task_wdt_delete(NULL); // Stop task watchdog during update # endif Log.warning(F("Starting firmware update" CR)); + SendReceiveIndicatorON(); ErrorIndicatorON(); StaticJsonDocument jsonBuffer;