|
12 | 12 | import analogio |
13 | 13 | import wifi |
14 | 14 | import socketpool |
| 15 | +import supervisor |
15 | 16 | import microcontroller |
16 | 17 | import adafruit_requests |
17 | 18 | from adafruit_io.adafruit_io import IO_HTTP |
|
26 | 27 | # Update to True if you want metadata sent to Adafruit IO. Defaults to False. |
27 | 28 | METADATA = False |
28 | 29 |
|
| 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 | + |
29 | 40 | # Initialise metadata. |
30 | 41 | if alarm.wake_alarm: |
31 | 42 | print("Awake", alarm.wake_alarm) |
@@ -75,9 +86,10 @@ def send_io_data(feed, value): |
75 | 86 | requests = adafruit_requests.Session(pool, ssl.create_default_context()) |
76 | 87 | # WiFi connectivity fails with error messages, not specific errors, so this except is broad. |
77 | 88 | 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() |
81 | 93 |
|
82 | 94 | # Set your Adafruit IO Username and Key in secrets.py |
83 | 95 | # (visit io.adafruit.com if you need to create an account, |
@@ -122,10 +134,10 @@ def send_io_data(feed, value): |
122 | 134 |
|
123 | 135 | # Adafruit IO can fail with multiple errors depending on the situation, so this except is broad. |
124 | 136 | 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.") |
126 | 138 | alarm.sleep_memory[2] += 1 # Increment error count by one. |
127 | | - time.sleep(30) |
128 | | - microcontroller.reset() |
| 139 | + time.sleep(15) |
| 140 | + supervisor.reload() |
129 | 141 |
|
130 | 142 | # Deinitialise the alarm pin. |
131 | 143 | switch_pin.deinit() |
|
0 commit comments