44import time
55import ssl
66from random import randint
7+ import microcontroller
78import socketpool
89import wifi
910import board
1920
2021# Add your Adafruit IO Username and Key to secrets.py
2122# (visit io.adafruit.com if you need to create an account,
22- # or if you need your Adafruit IO key.)
23+ # or if you need to obtain your Adafruit IO key.)
2324aio_username = secrets ["aio_username" ]
2425aio_key = secrets ["aio_key" ]
2526
2627# WiFi
27- print ("Connecting to %s" % secrets ["ssid" ])
28- wifi .radio .connect (secrets ["ssid" ], secrets ["password" ])
29- print ("Connected to %s!" % secrets ["ssid" ])
28+ try :
29+ print ("Connecting to %s" % secrets ["ssid" ])
30+ wifi .radio .connect (secrets ["ssid" ], secrets ["password" ])
31+ print ("Connected to %s!" % secrets ["ssid" ])
32+ # Wi-Fi connectivity fails with error messages, not specific errors, so this except is broad.
33+ except Exception as e : # pylint: disable=broad-except
34+ print ("Failed to connect to WiFi. Error:" , e , "\n Board will hard reset in 30 seconds." )
35+ time .sleep (30 )
36+ microcontroller .reset ()
3037
3138# Initialise NeoPixel
3239pixel = neopixel .NeoPixel (board .NEOPIXEL , 1 , brightness = 0.3 )
3340
3441
3542# Define callback functions which will be called when certain events happen.
36- # pylint: disable=unused-argument
3743def connected (client ):
3844 print ("Connected to Adafruit IO! Listening for NeoPixel changes..." )
3945 # Subscribe to Adafruit IO feed called "neopixel"
4046 client .subscribe ("neopixel" )
4147
4248
43- # pylint: disable=unused-argument
44- def message (client , feed_id , payload ):
49+ def message (client , feed_id , payload ): # pylint: disable=unused-argument
4550 print ("Feed {0} received new value: {1}" .format (feed_id , payload ))
4651 if feed_id == "neopixel" :
4752 pixel .fill (int (payload [1 :], 16 ))
@@ -66,18 +71,32 @@ def message(client, feed_id, payload):
6671io .on_connect = connected
6772io .on_message = message
6873
69- # Connect the client to the MQTT broker.
70- print ("Connecting to Adafruit IO..." )
71- io .connect ()
74+ # Connect to Adafruit IO
75+ try :
76+ io .connect ()
77+ # connect() fails with an internal error type, so this except is broad.
78+ except Exception as e : # pylint: disable=broad-except
79+ print ("Failed to connect to Adafruit IO. Error:" , e , "\n Board will hard reset in 30 seconds." )
80+ time .sleep (30 )
81+ microcontroller .reset ()
7282
7383timestamp = 0
7484while True :
75- # Explicitly pump the message loop.
76- io .loop ()
77-
78- # Obtain the "random" value, print it and publish it to Adafruit IO every 10 seconds.
79- if (time .monotonic () - timestamp ) >= 10 :
80- random_number = "{}" .format (randint (0 , 255 ))
81- print ("Current 'random' number: {}" .format (random_number ))
82- io .publish ("random" , random_number )
83- timestamp = time .monotonic ()
85+ try :
86+ # Explicitly pump the message loop.
87+ io .loop ()
88+
89+ # Obtain the "random" value, print it and publish it to Adafruit IO every 10 seconds.
90+ if (time .monotonic () - timestamp ) >= 10 :
91+ random_number = "{}" .format (randint (0 , 255 ))
92+ print ("Current 'random' number: {}" .format (random_number ))
93+ io .publish ("random" , random_number )
94+ timestamp = time .monotonic ()
95+
96+ # Adafruit IO fails with internal error types and WiFi fails with specific messages.
97+ # This except is broad to handle any possible failure.
98+ except Exception as e : # pylint: disable=broad-except
99+ print ("Failed to get or send data, or connect. Error:" , e ,
100+ "\n Board will hard reset in 30 seconds." )
101+ time .sleep (30 )
102+ microcontroller .reset ()
0 commit comments