Arduino ESP32

Fixing error with enabling hardware WDT on ESP32 using Arduino IDE

Following my previous post regarding enabling WDT on ESP32, the new version of arduino-esp32 3.x comes with some breaking changes for esp_task_wdt_init. I will show you here how to fix the errors with enabling hardware WDT on ESP32 using Arduino IDE.

The previous code (using arduino-esp32 v2.x) now throws the following errors:

error: invalid conversion from 'int' to 'const esp_task_wdt_config_t*' [-fpermissive]
error: too many arguments to function 'esp_err_t esp_task_wdt_init(const esp_task_wdt_config_t*)'
invalid conversion from 'int' to 'const esp_task_wdt_config_t*' [-fpermissive]

Fixing error “invalid conversion from ‘int’ to ‘const esp_task_wdt_config_t*'”

The reason for this error is that the definition for esp_task_wdt_init function has changed. The new code should look like this:

#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 1 

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();
      last = millis();
      i++;
      if (i == 5) {
        Serial.println("Stopping WDT reset. CPU should reboot in 3s");
      }
  }
}

Also make sure to set CONFIG_FREERTOS_NUMBER_OF_CORES to the number of cores your ESP32 has (usually 1 or 2).

And the output:

entry 0x400805cc
Configuring WDT...
Resetting WDT...
Resetting WDT...
Resetting WDT...
Resetting WDT...
Resetting WDT...
Stopping WDT reset. CPU should reboot in 3s
E (12986) task_wdt: Task watchdog got triggered. The following tasks/users did not reset the watchdog in time:

 

ESP32 development boards can be bought from AliExpress for just 4$ using this link or the ESP32 module with OLED display for 6$.

6 thoughts on “Fixing error with enabling hardware WDT on ESP32 using Arduino IDE”

  1. I tried this code and it doesn’t work
    H/W Adafruit ESP32-S2 Feather

    I made only these changes
    #define WDT_TIMEOUT 15000
    #define CONFIG_FREERTOS_NUMBER_OF_CORES 1
    The Watch Dog continues to time out in 3 seconds

  2. Hi,
    having problems with wtd i found your site. I modified your code but the esp keeps reseting. Can you tell me what the problem is?

    #include “esp_task_wdt.h”
    //20 seconds WDT
    #define WDT_TIMEOUT 20000
    #define CONFIG_FREERTOS_NUMBER_OF_CORES 2
    esp_task_wdt_config_t twdt_config = {
    .timeout_ms = WDT_TIMEOUT,
    .idle_core_mask = (1 <= 2000 && i < 3) {
    Serial.println("Resetting WDT…");
    esp_task_wdt_reset();
    last = millis();
    i++;
    if (i == 3) {
    Serial.println("Stopping WDT reset. CPU should reboot in 20s");
    }
    }
    }

    Serial Monitor:
    Resetting WDT…
    Resetting WDT…
    Resetting WDT…
    Stopping WDT reset. CPU should reboot in 20s
    E (22067) task_wdt: Task watchdog got triggered. The following tasks/users did not reset the watchdog in time:
    E (22067) task_wdt: – loopTask (CPU 1)
    E (22067) task_wdt: Tasks currently running:
    E (22067) task_wdt: CPU 0: IDLE0
    E (22067) task_wdt: CPU 1: loopTask
    E (22067) task_wdt: Aborting.
    E (22067) task_wdt: Print CPU 1 backtrace

    Backtrace: 0x40084285:0x3ffb21f0 0x400842c3:0x3ffb2210 0x400d1ff7:0x3ffb2230 0x400d1657:0x3ffb2250 0x400d3128:0x3ffb2270 0x4008a46d:0x3ffb2290

    ELF file SHA256: c29d606fe7b22264

    Rebooting…

  3. Thanks for putting this together. I almost have it working but my particular issue is the restart never occurs, it’s like the trigger_panic setting isn’t being respected. Here’s the serial output and it just stops right here, not restarting:

    13:01:45.020 -> Resetting WDT…
    13:01:47.046 -> Resetting WDT…
    13:01:47.046 -> Stopping WDT reset. CPU should reboot in 3s
    13:01:50.074 -> E (12986) task_wdt: Task watchdog got triggered. The following tasks/users did not reset the watchdog in time:
    13:01:50.074 -> E (12986) task_wdt: – loopTask (CPU 1)
    13:01:50.074 -> E (12987) task_wdt: Tasks currently running:
    13:01:50.074 -> E (12987) task_wdt: CPU 0: IDLE0
    13:01:50.074 -> E (12987) task_wdt: CPU 1: loopTask
    13:01:50.074 -> E (12989) task_wdt: Aborting.
    13:01:50.074 -> E (13000) task_wdt: Print CPU 1 (current core) backtrace

    13:01:50.074 -> Backtrace: 0x4000bfed:0x3ffb2190 0x4008a4b8:0x3ffb21a0 0x400884cf:0x3ffb21c0 0x400d2961:0x3ffb2200 0x400d1775:0x3ffb2230 0x400d1819:0x3ffb2250 0x400d316d:0x3ffb2270 0x4008a23d:0x3ffb2290

    13:01:50.074 -> ELF file SHA256: 4c8948c7fbabc3f4
    13:01:50.074 ->

    If I define “esp_task_wdt_isr_user_handler” (mentioned here https://docs.espressif.com/projects/esp-idf/en/latest/esp32/api-reference/system/wdts.html?highlight=wdt#task-watchdog-timer-twdt) and call ESP.restart() within it, that causes a restart. Do you have any idea why this is occuring? I’ve tried using CONFIG_FREERTOS_NUMBER_OF_CORES 1 and 2 but neither helped.

    #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 1

    esp_task_wdt_config_t twdt_config = {
    .timeout_ms = WDT_TIMEOUT,
    .idle_core_mask = (1 <= 2000 && i < 5) {
    Serial.println("Resetting WDT…");
    esp_task_wdt_reset();
    last = millis();
    i++;
    if (i == 5) {
    Serial.println("Stopping WDT reset. CPU should reboot in 3s");
    }
    }
    }

Leave a Reply