Possible incompatibility between ESP32 Watchdog library and WiFi/Network libraries

Not sure which Category to include this in so starting here.

Hi Folks

Hardware - Wemos D1 Mini ESP32
Arduino IDE 2.3.3

I'm having trouble getting the Watchdog timer provided by the esp_task_wdt.h library to execute a reboot on trigger if any or all of the the following libraries are included (whether referenced or not) in the code.

#include <WiFi.h>

#include <NTPClient.h>

#include <WiFiUdp.h>

#include <PubSubClient.h>

To put it into context I have several battery powered ESP32's with Capacitive Soil Moisture Sensors. Monitoring the sensor readings twice daily is quite sufficient so I'm putting the D1 Mini into Deep Sleep after each reading to preserve battery life.

I have found that sometimes on awakening the device files to either connect to the router or the MQTT server and just hangs until such time as the battery is depleted, Hence the attempt to use the watchdog timer to reboot.

In my own code which I won't include here for the time being I found the watchdog timer did trigger but failed to reboot.

I therefore found an example sketch which worked with the same hardware configuration, eliminating the possibility of the wrong GPIO's in use having any possible relevance and which repeatedly rebooted the processor.

After adding in my 4 libraries the sketch acted in precisely the same manner as my own code. Removal of each library individually had no effect on the result until all 4 had been removed.

This is the code for the test example

#include <WiFi.h>
#include <NTPClient.h>
#include <WiFiUdp.h>
#include <PubSubClient.h>
#include <esp_sleep.h>
#include <esp_task_wdt.h>

//3 seconds WDT
#define WDT_TIMEOUT 3000
//if 1 core doesn't work, try with 2
#define CONFIG_FREERTOS_NUMBER_OF_CORES 2
#define CONFIG_ESP_SYSTEM_PANIC CONFIG_ESP_SYSTEM_PANIC_SILENT_REBOOT
esp_task_wdt_config_t twdt_config = {
  .timeout_ms = WDT_TIMEOUT,
//  .idle_core_mask = (1 << CONFIG_FREERTOS_NUMBER_OF_CORES) - 1,  // Bitmask of all cores
   .trigger_panic = true,
};

void setup() {
  Serial.begin(115200);
  Serial.println("Configuring WDT...");
  esp_task_wdt_deinit();            //wdt is enabled by default, so we need to deinit it first
  esp_task_wdt_init(&twdt_config);  //enable panic so ESP32 restarts
  esp_task_wdt_add(NULL);           //add current thread to WDT watch

}

int i = 0;
int last = millis();

void loop() {
  // resetting WDT every 2s, 5 times only
  if (millis() - last >= 2000 && i < 5) {
      Serial.println("Resetting WDT...");
      esp_task_wdt_reset();
      delay(1000);
      last = millis();
      i++;
      if (i == 5) {
        Serial.println("Stopping WDT reset. CPU should reboot in 3s");
      }
  }
}

I would include the console output for the sketch with and without the the libraries included but My Arduino IDE does not appear to allow we to select more than a few lines at a time of the bottom pane. In that pane neither right click of the mouse nor Ctrl-A have any effect nor do normal text selection techniques work properly to allow text to be copied from it.

I hope someone my be able to help with the Watchdog issue.

which ESP32 core version are you using 2.x or 3.x ? (if I remember well, some APIs changed for the esp_task_wdt functions or related to which core it applied)

also the loop keep spinning in your case, I wonder if it does not impact the WDT

what if you add a

        while(true);

within the if

      if (i == 5) {
        Serial.println("Stopping WDT reset. CPU should reboot in 3s");
        while(true); // make sure we "die" here
      }

regarding the libraries, have you double checked if they instantiate any global variable for which the constructor would create a task or mess with the timers ?

Hi

Chip is ESP32-D0WD-V3 (revision v3.1)

Although the loop keeps spinning after I reaches 5, esp_task_wdt_reset(); doesn't get executed so the Timer is triggered. This happens whether or not the 4 libraries are included or not.

With the 4 libraries included, following Trigger of the timer, execution halts . Without the libraries the process reboots.

Adding the "while(true); " statement does make the process reboot even when the libraries are included. But this doesn't help with the application itself because I don't know where the code is hanging to trigger the timer. Adding this statement.

I really don't have any clue as to the ways the libraries work. I'm 76 years old and for much of my life have been an application programmer. All my work on IoT has been self taught.

may be ESP32 ESP-WROOM-32 and WDT() would help ?

The code in that does not compile. I think the libraries were modified for Version 3 of the ESP32 and the WDT_TIMEOUT which was in seconds is now milliseconds