i have esp32 and using arduino ide (2.0.18 board version) im trying to add a code that prevent esp from blocking. so if it stuck for some reasons to restart it.

i found following code but is not working

#include "esp_task_wdt.h"

#include <WiFi.h>
#include <HTTPClient.h>


// Define timeout in seconds (3minutes = 180seconds)
#define WDT_TIMEOUT 180

void setup() {
  esp_task_wdt_init(WDT_TIMEOUTtrue);  // True to reset on WDT timeout
  esp_task_wdt_add(NULL);  // Add the current thread to WDT monitoring
}

void loop() {
  esp_task_wdt_reset();  // Reset (feed) the watchdog to avoid a reset
  delay(200000);  // Simulating a task that takes 200 second
}

Which one? (how many cores?)
Does it use FreeRTOS?

The best way to answer this is to have you grab your favorite search engine and look for 'WDT RDP32 xxx,' where WDT stands for Watchdog Timer and xxx is the specific unit you have.

the code you are using is for ESP32 Arduino Core 2.x

for ESP32 Arduino Core 3.x have a look at Fixing error with enabling hardware WDT on ESP32 Arduino Core 3.x

is this ok?

#include "esp_task_wdt.h"

// Define the WDT timeout in milliseconds (3 seconds = 3000 ms)
#define WDT_TIMEOUT 3000  // Adjust this as needed

// Configuration for using WDT on a specific number of cores (usually 1 or 2)
#define CONFIG_FREERTOS_NUMBER_OF_CORES 1 

// Create a config structure for the WDT
esp_task_wdt_config_t twdt_config = {
    .timeout_ms = WDT_TIMEOUT // WDT timeout period
    .idle_core_mask = (1 << CONFIG_FREERTOS_NUMBER_OF_CORES) - 1 // Bitmask of cores to monitor
    .trigger_panic = true // Trigger a panic and restart if WDT is not reset in time
};

void setup() {
  Serial.begin(115200);
  Serial.println("Configuring WDT...");

  // Deinitialize the WDT if it was already enabled by default
  esp_task_wdt_deinit(); 

  // Initialize the WDT with the new configuration
  esp_task_wdt_init(&twdt_config); 

  // Add the current task (main loop task) to the WDT's monitoring
  esp_task_wdt_add(NULL); 
}

// Global variables to keep track of time and iterations
int i = 0;
int last = millis();

void loop() {
  // Simulate periodic task that resets the WDT every 2 seconds
  if (millis() - last >= 2000 && i < 5) {
    Serial.println("Resetting WDT...");
    
    // Reset (feed) the watchdog to prevent it from timing out
    esp_task_wdt_reset(); 
    
    last = millis();  // Update the last reset time
    i++;  // Increment the counter
    
    // After 5 resetsstop feeding the WDT
    if (i == 5) {
      Serial.println("Stopping WDT reset. CPU should reboot in 3 seconds.");
    }
  }
}

code looks OK - does it work?

I find it a good idea on reset to figure out the causee.g. power onWDTbrownoutetc and then take appropriate action

in this code the WD timesout out after 5seconds then prints the reason for the reset

// Blink a LED - enter pin number via serial monitor
//
// outputs 200mSec square wave to specified pin
//
// start blinking LED_BUILTIN then asks for pin number

int led=15;//LED_BUILTIN;

#include <esp_task_wdt.h>
#define WDT_TIMEOUT 5000  // WDT timeout

//if 1 core doesn't worktry 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);
  delay(5000);
  int watchDogTimerReset = 0brownOutReset = 0;
  Serial.println("\n\nBlink - testing WDT with ESP32 Arduino Core  3.x");
  Serial.println("Configuring WDT...");
  esp_task_wdt_deinit(); //wdt is enabled by defaultso 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
  // get reason for powerup reset and save to NVM
  int reason = print_reset_reason();
  switch (reason) {
    case ESP_RST_POWERON:
      Serial.printf("POR reset\n");
      break;
    case ESP_RST_BROWNOUT:
      brownOutReset = 1;
      Serial.printf("Brown out reset\n");
      break;
    case ESP_RST_DEEPSLEEP:
      Serial.printf("Exit from Sleep detected\r\n");
      break;
    case ESP_RST_WDT:
    case ESP_RST_TASK_WDT:
      Serial.printf("Watchdog timer reset\n");
      watchDogTimerReset = 1;
      break;
  }

  Serial.println();
    Serial.print("Led to blink ");
    delay(1000);
    Serial.println(led);
  Serial.println("enter pin number to blink ? ");
  // initialize digital pin LED_BUILTIN as an output.
  pinMode(ledOUTPUT);
}

// the loop function runs over and over again forever
void loop() {
  if(Serial.available()){
    int i;
    pinMode(ledINPUT);
    if(i=Serial.parseInt()) led=i;;
    Serial.print("Led to blink ");
    Serial.println(led);
    pinMode(ledOUTPUT);
  }
  digitalWrite(ledHIGH);   // turn the LED on (HIGH is the voltage level)
  delay(1000);                       // wait for a second
  digitalWrite(ledLOW);    // turn the LED off by making the voltage LOW
  delay(1000);                       // wait for a second
  Serial.print('*');
}

// print and return reason for system reset
// see https://docs.espressif.com/projects/esp-idf/en/latest/esp32/api-reference/system/misc_system_api.html#_CPPv418esp_reset_reason_t
int print_reset_reason(void) {
  int reason = esp_reset_reason();
  Serial.printf("CPU reset reason: %d "reason);
  switch (reason) {
    case ESP_RST_UNKNOWN: Serial.println("unknown_RESET"); break;
    case ESP_RST_POWERON: Serial.println("POWERON_RESET"); break;
    case ESP_RST_SW: Serial.println("SW_RESET"); break;
    case ESP_RST_PANIC: Serial.println("PANIC_RESET"); break;
    //case ESP_RST_INT_WD: Serial.println("INTERRUPT_WDT_RESET"); break;
    case ESP_RST_TASK_WDT: Serial.println("TASK_WDT_RESET"); break;
    case ESP_RST_WDT: Serial.println("OTHER_WDT_RESET"); break;
    case ESP_RST_DEEPSLEEP: Serial.println("EXIT_DEEP_SLEEP_RESET"); break;
    case ESP_RST_BROWNOUT: Serial.println("BROWNOUT_RESET"); break;
    case ESP_RST_SDIO:
      Serial.println("SDIO_RESET");
      break;
      //case ESP_RST_USB: Serial.println("USB_RESET"); break;
  }
  return reason;
}

serial monitor output

Blink - testing WDT with ESP32 Arduino Core  3.x
Configuring WDT...
CPU reset reason: 1 POWERON_RESET
POR reset

Led to blink 15
enter pin number to blink ? 
**E (10006) task_wdt: Task watchdog got triggered. The following tasks/users did not reset the watchdog in time:
E (10006) task_wdt:  - loopTask (CPU 1)
E (10007) task_wdt: Tasks currently running:
E (10007) task_wdt: CPU 0: IDLE0
E (10007) task_wdt: CPU 1: IDLE1
E (10009) task_wdt: Aborting.
E (10021) task_wdt: Print CPU 1 (current core) backtrace




Backtrace: 0x400866bb:0x3ffbcb90 0x400d9692:0x3ffbcbb0 0x40088da4:0x3ffbcbd0 0x4008a23d:0x3ffbcbf0

ELF file SHA256: 8df7780c82ff6a16

Rebooting...
ets Jul 29 2019 12:21:46

rst:0xc (SW_CPU_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT)
configsip: 0SPIWP:0xee
clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00
mode:DIOclock div:1
load:0x3fff0030,len:4832
load:0x40078000,len:16460
load:0x40080400,len:4
load:0x40080404,len:3504
entry 0x400805cc


Blink - testing WDT with ESP32 Arduino Core  3.x
Configuring WDT...
CPU reset reason: 6 TASK_WDT_RESET
Watchdog timer reset

Led to blink 15
enter pin number to blink ? 
*E (10006) task_wdt: Task watchdog got triggered. The following tasks/users did not reset the watchdog in time:
E (10006) task_wdt:  - loopTask (CPU 1)
E (10007) task_wdt: Tasks currently running:
E (10007) task_wdt: CPU 0: IDLE0
E (10007) task_wdt: CPU 1: IDLE1
E (10009) task_wdt: Aborting.
E (10021) task_wdt: Print CPU 1 (current core) backtrace




Backtrace: 0x400866bb:0x3ffbcb90 0x400d9692:0x3ffbcbb0 0x40088da4:0x3ffbcbd0 0x4008a23d:0x3ffbcbf0




ELF file SHA256: 8df7780c82ff6a16

Rebooting...
ets Jul 29 2019 12:21:46

rst:0xc (SW_CPU_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT)
configsip: 0SPIWP:0xee
clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00
mode:DIOclock div:1
load:0x3fff0030,len:4832
load:0x40078000,len:16460
load:0x40080400,len:4
load:0x40080404,len:3504
entry 0x400805cc


Blink - testing WDT with ESP32 Arduino Core  3.x
Configuring WDT...
CPU reset reason: 6 TASK_WDT_RESET
Watchdog timer reset