2727# Update to True if you want metadata sent to Adafruit IO. Defaults to False.
2828METADATA = False
2929
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 :
30+ # If the reason the board started up is due to a supervisor.reload() ...
31+ if supervisor .runtime .run_reason is supervisor .RunReason .SUPERVISOR_RELOAD :
3232 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...
33+ print (f"Reload number: { alarm .sleep_memory [3 ]} " ) # Print current supervisor reload number.
34+ if alarm .sleep_memory [3 ] > 5 : # If supervisor reload number exceeds 5...
3535 # Print the following...
36- print ("Reload not resolving the issue. \n Board will hard reset in 20 seconds. " )
36+ print ("Reload is not resolving the issue. \n Board will hard reset in 20 seconds. " )
3737 time .sleep (20 ) # ...wait 20 seconds...
3838 microcontroller .reset () # ...and hard reset the board. This will clear alarm.sleep_memory.
3939
4040# Initialise metadata.
4141if alarm .wake_alarm :
42- print ("Awake" , alarm .wake_alarm )
42+ print ("Awake! Alarm type: " , alarm .wake_alarm )
4343 # Increment wake count by 1.
4444 alarm .sleep_memory [0 ] += 1
4545else :
46- print ("No wake up alarm" )
46+ print ("Wakeup not caused by alarm. " )
4747 # Set wake count to 0.
4848 alarm .sleep_memory [0 ] = 0
4949 # Set error count to 0.
@@ -91,41 +91,50 @@ def send_io_data(feed, value):
9191 time .sleep (15 )
9292 supervisor .reload ()
9393
94+ # No data has been sent yet, so the send-count is 0.
95+ alarm .sleep_memory [1 ] = 0
96+
9497# Set your Adafruit IO Username and Key in secrets.py
9598# (visit io.adafruit.com if you need to create an account,
9699# or if you need your Adafruit IO key.)
97100aio_username = secrets ["aio_username" ]
98101aio_key = secrets ["aio_key" ]
99-
100102# Initialize an Adafruit IO HTTP API object
101103io = IO_HTTP (aio_username , aio_key , requests )
102104
103- # Print battery voltage to the serial console. Not necessary for Adafruit IO.
104- print (f"Current battery voltage: { voltage :.2f} " )
105-
106- # No data has been sent yet, so the send-count is 0.
107- alarm .sleep_memory [1 ] = 0
108-
109105# Turn on the LED to indicate data is being sent.
110106led .value = True
111107
112- # While the switch is open...
108+ # Print battery voltage to the serial console and send it to Adafruit IO.
109+ print (f"Current battery voltage: { voltage :.2f} V" )
110+ # Adafruit IO can run into issues if the network fails!
111+ # This ensures your code will continue to run.
112+ try :
113+ send_io_data (io .create_and_get_feed ("battery-voltage" ), f"{ voltage :.2f} V" )
114+ # Adafruit IO can fail with multiple errors depending on the situation, so this except is broad.
115+ except Exception as e : # pylint: disable=broad-except
116+ print ("Failed to send to Adafruit IO. Error:" , e , "\n Board will reload in 15 seconds." )
117+ alarm .sleep_memory [2 ] += 1 # Increment error count by one.
118+ time .sleep (15 )
119+ supervisor .reload ()
120+
121+ # While the door is open...
113122while not switch_pin .value :
114123 # Adafruit IO sending can run into issues if the network fails!
115124 # This ensures the code will continue to run.
116125 try :
117126 # Send data to Adafruit IO
118- print ("Sending new mail alert and battery voltage to Adafruit IO." )
127+ print ("Sending new mail alert to Adafruit IO." )
119128 send_io_data (io .create_and_get_feed ("new-mail" ), "New mail!" )
120- send_io_data (io .create_and_get_feed ("battery-voltage" ), f"{ voltage :.2f} V" )
121129 print ("Data sent!" )
130+ # If METADATA = True at the beginning of the code, send more data.
122131 if METADATA :
123132 print ("Sending metadata to AdafruitIO." )
124133 # The number of times the board has awakened in the current cycle.
125134 send_io_data (io .create_and_get_feed ("wake-count" ), alarm .sleep_memory [0 ])
126- # The number of times the mailbox/battery data has been sent.
135+ # The number of times the mailbox data has been sent.
127136 send_io_data (io .create_and_get_feed ("send-count" ), alarm .sleep_memory [1 ])
128- # The number of Adafruit IO errors that has occurred.
137+ # The number of WiFi or Adafruit IO errors that have occurred.
129138 send_io_data (io .create_and_get_feed ("error-count" ), alarm .sleep_memory [2 ])
130139 print ("Metadata sent!" )
131140 time .sleep (30 ) # Delay included to avoid data limit throttling on Adafruit IO.
@@ -149,10 +158,13 @@ def send_io_data(feed, value):
149158# Turn off LED for deep sleep.
150159led .value = False
151160
152- # Create an alarm on pin D27.
161+ # Create a timer alarm to be triggered every 12 hours (43200 seconds).
162+ time_alarm = alarm .time .TimeAlarm (monotonic_time = (time .monotonic () + 43200 ))
163+
164+ # Create a pin alarm on pin D27.
153165pin_alarm = alarm .pin .PinAlarm (pin = board .D27 , value = False , pull = True )
154166
155167print ("Entering deep sleep." )
156168
157169# Exit and set the alarm.
158- alarm .exit_and_deep_sleep_until_alarms (pin_alarm )
170+ alarm .exit_and_deep_sleep_until_alarms (pin_alarm , time_alarm )
0 commit comments