2018/05/27

Watchdog for ESP8266 and ESP32

This post is a quick summary of using watchdog to help deal with unexpected hang / crash.

Arduino Sketch

Note, the code "watchdogCount = 0;" is commented out so the watchdog is triggered every 5 seconds.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <Ticker.h>

Ticker secondTick;

int watchdogCount = 0;

void ISRwatchdog() {
    watchdogCount++;
   if (watchdogCount == 5) {
      Serial.println();
      Serial.println("The watchdog bites!!!");
      ESP.reset(); 
   }
}

void setup() {
    Serial.begin(115200);
    secondTick.attach(1, ISRwatchdog);
}

void loop() {
    Serial.printf("Watchdog counter= %d\n", watchdogCount);
    //watchdogCount = 0;
    delay(1000);
}

The Result (watchdong not reset before it's triggered)


The Result (watchdog reset before it's triggered)


Reference:

HOW TO CREATE A WATCHDOG TO STABILIZE YOUR ESP8266
http://onlineshouter.com/how-to-create-a-watchdog-to-stabilise-your-esp8266/

ESP8266: Watchdog functions
https://techtutorialsx.com/2017/01/21/esp8266-watchdog-functions/

No comments:

Post a Comment