Skip to content

Commit fb01d61

Browse files
authored
Merge pull request #2268 from kattni/wifi-mailbox-update
Update WiFi mailbox notifier code.
2 parents 65905d7 + 464828c commit fb01d61

File tree

1 file changed

+18
-6
lines changed

1 file changed

+18
-6
lines changed

WiFi_Mailbox_Notifier/code.py

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import analogio
1313
import wifi
1414
import socketpool
15+
import supervisor
1516
import microcontroller
1617
import adafruit_requests
1718
from adafruit_io.adafruit_io import IO_HTTP
@@ -26,6 +27,16 @@
2627
# Update to True if you want metadata sent to Adafruit IO. Defaults to False.
2728
METADATA = False
2829

30+
# If the reason the board started up is not the standard CircuitPython reset-type start up...
31+
if supervisor.runtime.run_reason is not supervisor.RunReason.STARTUP:
32+
alarm.sleep_memory[3] += 1 # Increment reload number by 1.
33+
print(f"Reload number {alarm.sleep_memory[3]}") # Print current reload number.
34+
if alarm.sleep_memory[3] > 5: # If reload number exceeds 5...
35+
# Print the following...
36+
print("Reload not resolving the issue. \nBoard will hard reset in 20 seconds. ")
37+
time.sleep(20) # ...wait 20 seconds...
38+
microcontroller.reset() # ...and hard reset the board. This will clear alarm.sleep_memory.
39+
2940
# Initialise metadata.
3041
if alarm.wake_alarm:
3142
print("Awake", alarm.wake_alarm)
@@ -75,9 +86,10 @@ def send_io_data(feed, value):
7586
requests = adafruit_requests.Session(pool, ssl.create_default_context())
7687
# WiFi connectivity fails with error messages, not specific errors, so this except is broad.
7788
except Exception as e: # pylint: disable=broad-except
78-
print("Failed to connect to WiFi. Error:", e, "\nBoard will hard reset in 30 seconds.")
79-
time.sleep(30)
80-
microcontroller.reset()
89+
print("Failed to connect to WiFi. Error:", e, "\nBoard will reload in 15 seconds.")
90+
alarm.sleep_memory[2] += 1 # Increment error count by one.
91+
time.sleep(15)
92+
supervisor.reload()
8193

8294
# Set your Adafruit IO Username and Key in secrets.py
8395
# (visit io.adafruit.com if you need to create an account,
@@ -122,10 +134,10 @@ def send_io_data(feed, value):
122134

123135
# Adafruit IO can fail with multiple errors depending on the situation, so this except is broad.
124136
except Exception as e: # pylint: disable=broad-except
125-
print("Failed to send to Adafruit IO. Error:", e, "\nBoard will hard reset in 30 seconds.")
137+
print("Failed to send to Adafruit IO. Error:", e, "\nBoard will reload in 15 seconds.")
126138
alarm.sleep_memory[2] += 1 # Increment error count by one.
127-
time.sleep(30)
128-
microcontroller.reset()
139+
time.sleep(15)
140+
supervisor.reload()
129141

130142
# Deinitialise the alarm pin.
131143
switch_pin.deinit()

0 commit comments

Comments
 (0)