BLYNK
HOME       📲 GETTING STARTED       📗 DOCS       ❓HELP CENTER       👉 SKETCH BUILDER

Prevent Crashes and Resets - Heartbeat and Watchdog Timers

hardware
esp8266

#1

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


Sonoff S20 esp8266 reboots periodicly
Sending Multiple Values using Blynk Timer - Updated Code
Project going off-line
#2

Very nice write up!! Thanks :smile:

I believe it will break the server connection

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

image

Can also be done with a define command…

#define BLYNK_HEARTBEAT 60

#3

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


#4

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