Skip to content

Commit c12ffd6

Browse files
authoredOct 23, 2023
Merge pull request #74 from arduino/update_examples_wdt
examples: Add WDT example.
2 parents cd59e69 + 1f3b562 commit c12ffd6

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed
 

‎examples/micropython.py

+19
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,12 @@ def user_task(client):
4141
client["clight"].bri = round(uniform(0, 100), 1)
4242

4343

44+
def wdt_task(client):
45+
global wdt
46+
# Update the WDT to prevent it from resetting the system
47+
wdt.feed()
48+
49+
4450
def wifi_connect():
4551
if not WIFI_SSID or not WIFI_PASS:
4652
raise (Exception("Network is not configured. Set SSID and passwords in secrets.py"))
@@ -109,5 +115,18 @@ def wifi_connect():
109115
# to client.register().
110116
client.register(Task("user_task", on_run=user_task, interval=1.0))
111117

118+
# If a Watchdog timer is available, it can be used to recover the system by resetting it, if it ever
119+
# hangs or crashes for any reason. NOTE: once the WDT is enabled it must be reset periodically to
120+
# prevent it from resetting the system, which is done in another user task.
121+
# NOTE: Change the following to True to enable the WDT.
122+
if False:
123+
try:
124+
from machine import WDT
125+
# Enable the WDT with a timeout of 5s (1s is the minimum)
126+
wdt = WDT(timeout=7500)
127+
client.register(Task("watchdog_task", on_run=wdt_task, interval=1.0))
128+
except (ImportError, AttributeError):
129+
pass
130+
112131
# Start the Arduino IoT cloud client.
113132
client.start()

0 commit comments

Comments
 (0)
Please sign in to comment.