Simple Watchdog Timer. A simple watchdog timer to reset the ESP8266 if it gets

Simple Watchdog Timer.

A simple watchdog timer to reset the ESP8266 if it gets stuck in a loop.

Capacitor = 4,700μF and R3 = 33K. Using Panasonic 1381R voltage monitor. (pin 3 to ground, pin 1 output, pin 2 voltage to be monitored)
With the values above reset time can be varied from 50 to 250 seconds, if the watchdog is not kicked.

Arduino IDE sketch for testing the watchdog.

//ESP8266 watchdog test
//D5 to watchdog circuit kick the watchdog terminal
//ESP8266 reset to watchdog reset terminal

//Set WDCOUNT to 600 for testing time to reset
//and vary KICKDELAY to modify time to reset
//display counts up in seconds
//When the required time to reset is determined
//set WDCOUNT earlier than this time

#define WDKICK D5
#define WDCOUNT 180
#define KICKDELAY 2000
int counter =0;

// the setup function runs once when you press reset or power the board
void setup() {
pinMode(WDKICK, OUTPUT);// initialize digital pin as an output.
digitalWrite(WDKICK, LOW);//discharge capacitor
delay(KICKDELAY);
pinMode(WDKICK, INPUT);//set to high impedance
pinMode(BUILTIN_LED, OUTPUT);
Serial.begin(115200);
Serial.println();
Serial.println();
Serial.println(“BOOTED*”);
Serial.println();
}

// the loop function runs over and over again forever
void loop() {
counter++;
Serial.println(counter);
digitalWrite(BUILTIN_LED, HIGH); // turn the LED on (HIGH is the voltage level)
delay(500); // wait for a second
//Serial.println(“off”);
digitalWrite(BUILTIN_LED, LOW); // turn the LED off by making the voltage LOW
delay(500); // wait for a second
if(counter >= WDCOUNT)//time to kick the dog
{
counter = 0;
pinMode(WDKICK, OUTPUT);
digitalWrite(WDKICK, LOW);//kick the dog
delay(KICKDELAY);
pinMode(WDKICK, INPUT);//set to high impedance
Serial.println(“Kicked the dog”);
}
}

Nice, bookmarking this for future use.

ESP8266 has an internal watchdog, I believe.

@Petr_Stehlik
True, but the internal watchdog is kicked if the ESP8266 is running in in a loop containing commands that would normally kick the internal watchdog. So the loop continues forever.

@Oliver_Hamilton
Its been running on my house monitoring system for some time now without any problems.
I have several ESP8266 around the house monitoring sensors. Each is polled by a master ESP6266 which collects all the sensor data. The master got in a loop from time to time, I think when a client connected but failed to send data for whatever reason. but with the it’s watchdog no longer a problem. Just reboots and off it goes again.

@Brian_Lambert I am afraid I don’t understand you. Both the internal and your external watchdogs work the same way: if you don’t touch them regularly they reset the CPU, so what’s the difference?

@Petr_Stehlik ah I didn’t know there was one built into it. Either way, it’s a useful circuit for use with other devices that don’t have a watchdog.

@Brian_Lambert I’m using a raspberry pi to receive all the sensor data. It runs a custom django web server so I can see the live and historic data. Alternatively you can just use https://nodered.org/ but I liked the challenge of creating it from scratch.

@Petr_Stehlik
This code will run forever.

while(1)
{
Serial.println(millis());
delay(1); //resets the internal watchdog
}
The internal watchdog never kicks in.
Remove the delay, and the watchdog resets every 3 seconds.
An external watchdog prevents loops that reset the internal watchdog running forever

@Oliver_Hamilton
I run a server on Google App Engine. Sensor data gets stuffed into a database on the server and I can access the database from and with wherever. Also stick commands up on the database, with say a mobile, and read them from the home clients for turning things on and off etc.

ESP8266 and ESP32 (as almost any microcontroller) have a watchdog built in:
ESP.wdtEnable(1000);
ESP.wdtFeed();
ESP.wdtDisable();

@Brian_Lambert you don’t call any watchdog functions in your code.

@Petr_Stehlik
The software watchdog is running without calling the watchdog function. If you remove the delay(1) in the code the watchdog kicks in after 3 seconds.

@Brian_Lambert that’s a different thing.

@Petr_Stehlik
I’m not sure that I understand what you mean.
Using
ESP.wdtEnable(10000);
while(1)
{
Serial.println(millis());
//delay(1);
ESP.wdtFeed();
}
using either delay(1) or ESP.wdtfeed() stops the watchdog resetting the ESP. If you remove both, the ESP resets every 3 seconds.

@Brian_Lambert if you set your watchdog to 10 seconds and it resets after 3 seconds it’s apparently due to a different thing.

The Panasonic MN1381-R is available here, quite cheap.

@Petr_Stehlik
You would think so, but the log does say wdt rst.

@Brian_Lambert if calling delay() indeed resets the internal ESP.wdt watchdog then it’s a bug in Arduino core. I’ll try it out when I get some spare time.
If your code can “get in a loop” that is not detected by ESP.wdt watchdog then it’s another bug. Hard to guess anything without inspecting your source code etc.
Anyway, glad you solved it with your nice external circuit.

@Petr_Stehlik
I’d be grateful to hear your findings. It would be nice not to need an external watchdog.

@Brian_Lambert BTW, there are two watchdogs in ESP8266 - a software and a hardware one. Those reboots after 3 seconds are most probably caused by the hardware watchdog. So when testing please set the software watchdog (ESP.wdtEnable) to less than 3 seconds.