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
#define CONFIG_FREERTOS_NUMBER_OF_CORES 2

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_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).

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

1 thought on “Fixing error with enabling hardware WDT on ESP32 using Arduino IDE”

Leave a Reply