[SYS] Add global task watchdog timer (#1724)

This commit is contained in:
Florian authored and GitHub committed 2023-08-03 06:48:25 -05:00
1 parent e673788da5
commit 375ce91ece
4 files changed
+47 -8

No files matched your search

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