Watchdog - reset esp32 if stuck more than 120 seconds

The watchdog timer defaults to 5 seconds. It applies to all tasks. The documented range for this config value is up to 60 seconds, but 120 appears to work, at least for now.

There's no need to call esp_task_wdt_reset from loop. That is already done in the wrapper in main.cpp that calls setup and loop

as long as you call enableLoopWDT from setup, which both calls esp_task_wdt_add for you and sets that loopTaskWDTEnabled variable to true.

void setup() {
  enableLoopWDT();
}

void loop() {
  for (;;) {}  // don't return
}

This resets every 5 seconds because loop does not return.

entry 0x400805f0
[ 5 seconds later ]
E (10139) task_wdt: Task watchdog got triggered. The following tasks did not reset the watchdog in time:
E (10139) task_wdt:  - loopTask (CPU 1)
E (10139) task_wdt: Tasks currently running:
E (10139) task_wdt: CPU 0: IDLE
E (10139) task_wdt: CPU 1: loopTask
E (10139) task_wdt: Aborting.

Remove the infinite loop and it's fine.

Conversely, you need to call both _add and _reset for your custom tasks, like your task1.

void task1(void *parameter) {
  esp_task_wdt_add(nullptr);
  for (;;) {
    esp_task_wdt_reset();
    // code here
  }
}

If you don't _add, the task is not added to the watch list. And if it is watched, you need to _reset periodically.

You should also consider the priority of your new task. If it is lower than the main loop task, and the loop never yields/delays, then the new task will never start. If the priority is equal, ESP32 will time-slice between them. So when creating the task, you might do

#include <esp_task_wdt.h>

void setup() {
  auto priority = uxTaskPriorityGet(nullptr);
  xTaskCreateUniversal(
    [](void *pvParameters) {
      esp_task_wdt_add(nullptr);
      for (;;) {
        esp_task_wdt_reset();
        // call other function that does actual work here
      }
    },
    "Actual_Work",
    CONFIG_ARDUINO_LOOP_STACK_SIZE, nullptr,
    priority, nullptr, ARDUINO_RUNNING_CORE);
}

void loop () {
}

As for your reported error, arduino-esp32 v3.0.0 is based on ESP-IDF v5.1.4. The latest v2.x is based on ESP-IDF v4.4.7. One of the breaking changes for v5 is the signature for esp_task_wdt_init, which in v4 used to take a uint32_t (which is a lot of seconds) and a bool

/**
  * @brief  Initialize the Task Watchdog Timer (TWDT)
  *
  * This function configures and initializes the TWDT. If the TWDT is already
  * initialized when this function is called, this function will update the
  * TWDT's timeout period and panic configurations instead.
  */
esp_err_t esp_task_wdt_init(uint32_t timeout, bool panic);

but is now -- as the error message said -- a single struct. Furthermore, there is a separate function to reconfigure, with the same argument

esp_err_t esp_task_wdt_reconfigure(const esp_task_wdt_config_t *config);

So you might try (I don't have 3.0.0 installed)

esp_task_wdt_config_t config = {
  .timeout_ms = 120 * 1000,
  .trigger_panic = true,
}
esp_task_wdt_reconfigure(&config);
1 Like