ESP-8266 and Solving WDT Resets

So without adding much more complexity than WiFi to an ESP8266 sketch, you might get some WDT errors; if you have a serial link you can see the restart message, if not you might just notice a hang followed by a restart. Or not; if the hang is short enough but at the wrong (right?) time, you’ll just get a restart.

Long story short: Writing software for the ESP8266 isn’t like writing a blink sketch for an Arduino. But it’s also not much different than writing a WiFi sketch for an Arduino, either. I think the primary difference is the libraries and Espressif SDK underpinning the ESP are a bit more complex, and it includes a hardware / software watchdog. If the watchdog timer expires, the system assumes that something hung, and restarts to recover.

There are some voodoo-type references to peppering your code with delay() and yield() statements to solve this. What I’ve done is wrap them in a simple function that manages  when to execute them.

There is one global (counter) and  you can call WDTPulse() whenever you might have a long running routine that won’t quickly exit. Inside there is also a call to ESP.wdtFeed(), this requires no special #includes or other declarations as it’s declared extern . It really does seem to feed the watchdog.

long counter;
// ******************************** wdt pulse ********************************
void WDTPulse() {
if ( (counter % 100000) == 0 ) {
ESP.wdtFeed();
yield();
delay(1);
}
counter++;
}
// Example of use; within the possibly-never-ending WiFi setup:
while (stat != WL_CONNECTED) {
WiFi.begin(ssid, pass);
WiFi.waitForConnectResult();
stat = WiFi.status();
WDTPulse();
}

Or the as yet not totally tested version, which might be better….

// ******************************** wdt pulse ********************************
void WDTPulse() {
if ( (counter % 100000) == 0 ) {
ESP.wdtFeed();
delay(1);
}
yield();
counter++;
}

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.