A watchdog timer is an internal or external timer that monitors a microcontroller’s program to ensure the application remains operative without failure. It serves as a safety feature in critical applications by monitoring the microcontroller’s output signal.
The watchdog can operate in two modes:
- Timeout mode – the timer establishes the microcontroller is not working properly if it inputs multiple signals (for example, if a double pulse is detected within a set period).
- Window mode – the timer establishes the microcontroller is not working properly if it fails to receive a signal or receives too many in a set period.
If either of these events occurs, the watchdog timer resets the microcontroller.
The WDT class
MicroPython provides class WDT to configure and activate the 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 starts, it cannot be reconfigured or stopped. It’s enabled as soon as an object of the WDT class is instantiated. Additionally, the script/program must feed the watchdog timer periodically to avoid expiring or resetting the controller automatically.
The WDT class is imported in a MicroPython script using this statement:
from machine import WDT
The constructor method for 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 multiple watchdog timers in 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 enabled immediately after an object of the WDT class is instantiated. The WDT class provides only one method.
WDT.feed(): When this method is called, it feeds the watchdog timer periodically to prevent it from resetting the controller. This method must be called within or at the end of a script, so the watchdog timer is only fed when the main execution code is confirmed. If the script does not contain an infinite loop, it can be called at the end of the script. If the main execution script contains an infinite loop, it must be placed as the last statement of the loop. The method does not take any arguments.
Watchdog timer in ESP8266
When using ESP8266, the timeout cannot be specified for the watchdog timer and is automatically determined by the underlying system. This board only has one watchdog timer, so there’s no need to pass its “id.”
Here is a valid example of instantiating and configuring the watchdog timer in ESP8266:
from machine import WDT
wdt = WDT()
wdt.feed()
A MicroPython script that uses the watchdog timer without an infinite loop (in ESP8266) should look like this:
from machine import WDT
wdt = WDT()
….#Main execution code
wdt.feed()
A MicroPython script that uses the watchdog timer with an infinite loop (in ESP8266) should look like this:
from machine import WDT
wdt = WDT()
….# Non-repeating MicroPython code
while True :
# Code of infinite loop
wdt.feed()
Watchdog timer in ESP32
In ESP32, one second is the minimum timeout that can be specified. ESP32 only has one watchdog timer, so it does not require specifying the “id.”
This is a valid example of instantiating and configuring the watchdog timer in ESP32.
from machine import WDT
wdt = WDT(timeout = 5000)
wdt.feed()
A MicroPython script that uses the watchdog timer without an infinite loop (in ESP32) should look like this:
from machine import WDT
wdt = WDT(timeout = 5000)
….#Main execution code
wdt.feed()
A MicroPython script that uses the watchdog timer with an infinite loop (in ESP32) should look like this:
from machine import WDT
wdt = WDT(timeout = 5000)
….# Non-repeating MicroPython code
while True :
# Code of infinite loop
wdt.feed()
Conclusion
The watchdog timer is an excellent safety feature that ensures microcontrollers execute the correct application without failure. The watchdog detects any malfunctioning and resets the controller if it stops working or is in a non-recoverable state.
MicroPython supports the watchdog timer’s functionality via the WDT class, but only for ESP32, ESP8266, WiPy, and pyboard. The watchdog timer must be carefully placed and fed within a MicroPython script so that it first ensures confirmation of the execution of the MicroPython’s main code.
Filed Under: Python, Tutorials
Questions related to this article?
👉Ask and discuss on EDAboard.com and Electro-Tech-Online.com forums.
Tell Us What You Think!!
You must be logged in to post a comment.