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$.

8 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 update together. My particular esp32 (ESP32_Core_Board_V2) with Arduino IDE 2.3.3 and ESP32 3.0.5 is almost working, the watchdog gets triggered but it doesn’t reboot. It seems like the “trigger_panic = true” configuration setting isn’t being respected. Here is the serial output and it does have a core dump check failure but am not sure if this is involved:

    12:04:21.318 -> entry 0x400805cc
    12:04:21.425 -> E (143) esp_corf�Eյ�}���͡� Core dump data check failed:
    12:04:21.425 -> Calculated checksum=’Configuring WDT…
    12:04:23.402 -> Resetting WDT…
    12:04:25.414 -> Resetting WDT…
    12:04:27.410 -> Resetting WDT…
    12:04:29.431 -> Resetting WDT…
    12:04:31.413 -> Resetting WDT…
    12:04:31.413 -> Stopping WDT reset. CPU should reboot in 3s
    12:04:34.440 -> E (12986) task_wdt: Task watchdog got triggered. The following tasks/users did not reset the watchdog in time:
    12:04:34.440 -> E (12986) task_wdt: – loopTask (CPU 1)
    12:04:34.440 -> E (12987) task_wdt: Tasks currently running:
    12:04:34.440 -> E (12987) task_wdt: CPU 0: IDLE0
    12:04:34.440 -> E (12987) task_wdt: CPU 1: loopTask
    12:04:34.440 -> E (12989) task_wdt: Aborting.
    12:04:34.440 -> E (13000) task_wdt: Print CPU 1 (current core) backtrace
    12:04:34.440 ->
    12:04:34.440 -> Backtrace: 0x4000bfed:0x3ffb2190 0x4008a4b8:0x3ffb21a0 0x40088895:0x3ffb21c0 0x400d293a:0x3ffb2200 0x400d1775:0x3ffb2230 0x400d1819:0x3ffb2250 0x400d316d:0x3ffb2270 0x4008a23d:0x3ffb2290
    12:04:34.472 ->
    12:04:34.472 -> ELF file SHA256: 2f67cda36645b749
    12:04:34.472 ->

    I did some digging into https://docs.espressif.com/projects/esp-idf/en/latest/esp32/api-reference/system/wdts.html?highlight=wdt#task-watchdog-timer-twdt and noticed that we can define a function to receive the timeout event to extend its behavior, and if I add a restart there, the reboot works successfully. Do you have any idea how I can get the normal trigger_panic behavior to cause a restart?

    void esp_task_wdt_isr_user_handler() {
    ESP.restart();
    }

    Here’s the serial output when adding the function above showing the successful SW_CPU_RESET reason during the reboot.

    12:57:34.522 -> entry 0x400805cc
    12:57:34.589 -> E (88) esp_core�fVW}���͡� Core dump data check failed:
    12:57:34.589 -> Calculated checksum=’2Configuring WDT…
    12:57:36.576 -> Resetting WDT…
    12:57:38.548 -> Resetting WDT…
    12:57:40.560 -> Resetting WDT…
    12:57:42.558 -> Resetting WDT…
    12:57:44.587 -> Resetting WDT…
    12:57:44.587 -> Stopping WDT reset. CPU should reboot in 3s
    12:57:47.592 -> E (12986) task_wdt: Task watchdog got triggered. The following tasks/users did not reset the watchdog in time:
    12:57:47.593 -> E (12986) task_wdt: – loopTask (CPU 1)
    12:57:47.593 -> E (12987) task_wdt: Tasks currently running:
    12:57:47.593 -> E (12987) task_wdt: CPU 0: IDLE0
    12:57:47.593 -> E (12987) task_wdt: CPU 1: loopTask
    12:57:47.636 -> ets Jun 8 2016 00:22:57
    12:57:47.636 ->
    12:57:47.636 -> rst:0xc (SW_CPU_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT)
    12:57:47.636 -> configsip: 0, SPIWP:0xee
    12:57:47.636 -> clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00
    12:57:47.636 -> mode:DIO, clock div:1
    12:57:47.636 -> load:0x3fff0030,len:4832
    12:57:47.636 -> load:0x40078000,len:16460
    12:57:47.636 -> load:0x40080400,len:4
    12:57:47.636 -> load:0x40080404,len:3504
    12:57:47.636 -> entry 0x400805cc
    12:57:47.677 -> E (88) esp_core�fVW}���͡� Core dump data check failed:
    12:57:47.677 -> Calculated checksum=’2Configuring WDT…
    12:57:49.654 -> Resetting WDT…
    12:57:51.668 -> Resetting WDT…
    12:57:53.681 -> Resetting WDT…
    12:57:55.676 -> Resetting WDT…
    12:57:57.674 -> Resetting WDT…

    Here’s the code I’m using with the esp_task_wdt_isr_user_handler commented out:

    #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");
    }
    }
    }

  4. 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