A watchdog timer is an internal or external timer that monitors a microcontroller's program to ensure that the application remains operational without failure. It serves as a safety feature in critical applications, monitoring the microcontroller output signal.
The watchdog can operate in two modes:
- Timeout Mode – The timer establishes that the microcontroller is not functioning properly if it receives multiple signals (for example, if a double pulse is detected within a set period).
- Window mode – the timer establishes that the microcontroller is not working properly if it does not receive a signal or receives too many in a given period.
If any of these events occur, the watchdog timer resets the microcontroller.
The WDT class
MicroPython provides WDT class to configure and enable watchdog timer in ESP8266, ESP32, WiPy and pyboard. These are the only platforms for which WDT is available.
A WDT object in a MicroPython script is used to restart the controller when an application crashes or is in a non-responsive or non-recoverable state.
Once the watchdog timer has started, it cannot be reset or stopped. It is enabled as soon as a WDT class object is instantiated. Additionally, the script/program must feed the watchdog timer periodically to prevent expiration or automatic reset of the controller.
The WDT class is imported into a MicroPython script using this statement:
WDT machine import
The constructor method for the WDT class has this prototype:
class machine.WDT(id=0, timeout=5000)
The constructor method can take two keyword parameters, id and timeout. The “id” is that of the watchdog timer and should only be passed if there are several watchdog timers on a microcontroller.
The timeout parameter specifies the feed timeout and is required depending on the MicroPython port. The timeout period is port-specific and specified in milliseconds.
The watchdog timer is activated immediately after the instantiation of a WDT class object. The WDT class provides only one method.
WDT.feed: When this method is called, it feeds the watchdog timer periodically to prevent it from restarting the controller. This method must be called within or at the end of a script, so that the watchdog timer is only fed when the main execution code is committed. If the script does not contain an infinite loop, it can be called at the end of the script. If the main running script contains an infinite loop, it must be placed as the last statement in the loop. The method does not accept any arguments.
Watchdog timer in ESP8266
When using ESP8266, timeout cannot be specified for the watchdog timer and is determined automatically by the underlying system. This board only has a watchdog timer, so there is no need to pass your “id”.
Here is a valid example of instantiating and configuring the watchdog timer in ESP8266:
WDT machine import
wdt = WDT
wdt.feed
A MicroPython script that uses the watchdog timer without an infinite loop (on the ESP8266) should look like this:
WDT machine import
wdt = WDT
….#Main execution code
wdt.feed
A MicroPython script that uses the watchdog timer with an infinite loop (on the ESP8266) should look like this:
WDT machine import
wdt = WDT
….# Non-repetitive MicroPython code
while True:
# Infinite loop code
wdt.feed
Watchdog timer on ESP32
In ESP32, one second is the minimum timeout that can be specified. The ESP32 only has a watchdog timer, so it does not require the “id” to be specified.
This is a valid example of instantiating and configuring the watchdog timer in ESP32.
WDT machine import
wdt = WDT(timeout = 5000)
wdt.feed
A MicroPython script that uses the watchdog timer without an infinite loop (on ESP32) should look like this:
WDT machine import
wdt = WDT(timeout = 5000)
….#Main execution code
wdt.feed
A MicroPython script that uses the watchdog timer with an infinite loop (on ESP32) should look like this:
WDT machine import
wdt = WDT(timeout = 5000)
….# Non-repetitive MicroPython code
while True:
# Infinite loop code
wdt.feed
Conclusion
The watchdog timer is an excellent safety feature that ensures that microcontrollers execute the correct application without failure. The watchdog detects any malfunction and resets the controller if it stops working or is in a non-recoverable state.
MicroPython supports watchdog timer functionality through the WDT class, but only for ESP32, ESP8266, WiPy, and pyboard. The watchdog timer must be carefully placed and fed into a MicroPython script so that it first ensures the execution of the main MicroPython code is confirmed.