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,28 @@ 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 ()
72-
7374timestamp = 0
7475while 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 ()
76+ try :
77+ # If Adafruit IO is not connected...
78+ if not io .is_connected :
79+ # Connect the client to the MQTT broker.
80+ print ("Connecting to Adafruit IO..." )
81+ io .connect ()
82+
83+ # Explicitly pump the message loop.
84+ io .loop ()
85+ # Obtain the "random" value, print it and publish it to Adafruit IO every 10 seconds.
86+ if (time .monotonic () - timestamp ) >= 10 :
87+ random_number = "{}" .format (randint (0 , 255 ))
88+ print ("Current 'random' number: {}" .format (random_number ))
89+ io .publish ("random" , random_number )
90+ timestamp = time .monotonic ()
91+
92+ # Adafruit IO fails with internal error types and WiFi fails with specific messages.
93+ # This except is broad to handle any possible failure.
94+ except Exception as e : # pylint: disable=broad-except
95+ print ("Failed to get or send data, or connect. Error:" , e ,
96+ "\n Board will hard reset in 30 seconds." )
97+ time .sleep (30 )
98+ microcontroller .reset ()
0 commit comments