From 1f3b5624cde6597ada45bd62ff77ca669faf8a33 Mon Sep 17 00:00:00 2001 From: iabdalkader Date: Mon, 23 Oct 2023 13:24:16 +0200 Subject: [PATCH] examples: Add WDT example. Update MicroPython example to show how WDT can be used to recover the system. --- examples/micropython.py | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/examples/micropython.py b/examples/micropython.py index 82927d9..da8bd3d 100644 --- a/examples/micropython.py +++ b/examples/micropython.py @@ -41,6 +41,12 @@ def user_task(client): client["clight"].bri = round(uniform(0, 100), 1) +def wdt_task(client): + global wdt + # Update the WDT to prevent it from resetting the system + wdt.feed() + + def wifi_connect(): if not WIFI_SSID or not WIFI_PASS: raise (Exception("Network is not configured. Set SSID and passwords in secrets.py")) @@ -109,5 +115,18 @@ def wifi_connect(): # to client.register(). client.register(Task("user_task", on_run=user_task, interval=1.0)) + # If a Watchdog timer is available, it can be used to recover the system by resetting it, if it ever + # hangs or crashes for any reason. NOTE: once the WDT is enabled it must be reset periodically to + # prevent it from resetting the system, which is done in another user task. + # NOTE: Change the following to True to enable the WDT. + if False: + try: + from machine import WDT + # Enable the WDT with a timeout of 5s (1s is the minimum) + wdt = WDT(timeout=7500) + client.register(Task("watchdog_task", on_run=wdt_task, interval=1.0)) + except (ImportError, AttributeError): + pass + # Start the Arduino IoT cloud client. client.start()