Prevent Crashes and Resets - Heartbeat and Watchdog Timers

I’ve seen several topics running into WDT issues so I thought it might help to write a short post concerning this, touching the basics.

ESP Watchdog Timers
All ESP’s are ‘fitted’ with 2 watchdog timers (WDT), a software WDT and a hardware WDT. If these ‘dogs’ are not regularly ‘fed’ then the ESP is ‘bitten’ and it resets. This feeding process goes automatically and you usually don’t have to do anything with it. As far as I know there are at least 3 functions during which the dog is fed: loop(), delay() and yield().

If however during a loop it takes too long to start the next loop (i’ll get to the times in a bit) then the dog bites and the ESP resets. The simplest way to test this is the following sketch:

void setup() {
   while (1) {};
}
 
void loop() {}

This will initiate an endless loop and after a while the WDT kicks in and the ESP is reset.

The same will happen if you e.g. do a readout of 10 sensors, taking one second per sensor, without taking care of the dog.

Feeding the dogs
So how to take care of the dog? Two ways;

  1. The proper way: feed it regularly by inserting this in strategic places:
ESP.wdtFeed(); 

This will reset the WDT timer and keep the dog happy.

  1. The ‘if you know what you’re doing way’: lock it up with
ESP.wdtDisable(); 

Note that you can take it out again with

ESP.wdtEnable(0); 

The ‘0’ is an arbitrary number that is required but not used). HOWEVER there’s still the hardware dog and you cannot disable that one!

Blynk heartbeat
Next to the 2 dogs, there’s the Blynk heartbeat. AFAIK this does NOT initiate any reset but this is simple ‘ping’ to the server notifying the server that its still alive. If the server does not receive a ping in time it assumes the ESP is offline (of which you are notified of in the Blynk app) and closes the connection to the server. Note that delay() and yield() do NOT ping the heartbeat! This is the reason why its regarded as bad practice to use them in a Blynk sketch. (use timer instead)
@Pavel: I could not find any function to ping the server yourself (similar to ESP.wdtFeed()), is there such a command?

Timers
So what times are we talking about? For an ESP8266 the:
Software WDT = 3.2 seconds (cannot be changed)
Hardware WDT = 8.2 seconds (cannot be changed)
Blynk Heartbeat = 10/15 seconds (can be changed). The device sends a heartbeat every 10 seconds, the server requires a ping every 15 seconds.

Changing the heartbeat can be done in the config:


or by adding

#define BLYNK_HEARTBEAT 60 //where '60' is an arbitrary number representing seconds.

To your sketch.

Arduino Watchdog timers
Some of you are using Arduino’s with an ESP shield. Keep in mind that Arduino’s are fitted with WDT but they are inactive by default! A basic sketch with an active Arduino WDT looks like this:

#include <avr/wdt.h>

void setup() {
	wdt_enable(WDTO_2S); //feed it within 2 seconds! There are shorter and longer variants: _1S, _4S and _8S
}
 
void loop() {
	wdt_reset(); //if you have long subroutines, make sure you add this line in those too!
}

More information on the topics:

Arduino:


Arduino more in depth: https://forum.arduino.cc/index.php?topic=63651.0

ESP:
https://www.sigmdel.ca/michel/program/esp8266/arduino/watchdogs_en.html

Blynk:
http://docs.blynk.cc/#blynk-main-operations-devices-online-status

10 Likes

Very nice write up!! Thanks :smile:

I believe it will break the server connection

image

Can also be done with a define command…

#define BLYNK_HEARTBEAT 60

Thnx. I’ve updated the OP.
@Gunner: is there a ‘topic’ type for these kinds of posts?

1 Like

Either this one or possibly the FAQ are fine I think.

just for the sake of knowledge :slight_smile: there is a way to completely disable the hardware wdt:

void hw_wdt_disable(){
  *((volatile uint32_t*) 0x60000900) &= ~(1); // Hardware WDT OFF
}

void hw_wdt_enable(){
  *((volatile uint32_t*) 0x60000900) |= 1; // Hardware WDT ON
}

https://www.esp8266.com/viewtopic.php?t=20206&p=83670#p83670

2 Likes

Hi this is indeed useful knowledge. I’ve absolutely no idea how to read those lines of code, but if they work: awesome!