4
4
import time
5
5
import ssl
6
6
from random import randint
7
+ import microcontroller
7
8
import socketpool
8
9
import wifi
9
10
import board
19
20
20
21
# Add your Adafruit IO Username and Key to secrets.py
21
22
# (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.)
23
24
aio_username = secrets ["aio_username" ]
24
25
aio_key = secrets ["aio_key" ]
25
26
26
27
# 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 ()
30
37
31
38
# Initialise NeoPixel
32
39
pixel = neopixel .NeoPixel (board .NEOPIXEL , 1 , brightness = 0.3 )
33
40
34
41
35
42
# Define callback functions which will be called when certain events happen.
36
- # pylint: disable=unused-argument
37
43
def connected (client ):
38
44
print ("Connected to Adafruit IO! Listening for NeoPixel changes..." )
39
45
# Subscribe to Adafruit IO feed called "neopixel"
40
46
client .subscribe ("neopixel" )
41
47
42
48
43
- # pylint: disable=unused-argument
44
- def message (client , feed_id , payload ):
49
+ def message (client , feed_id , payload ): # pylint: disable=unused-argument
45
50
print ("Feed {0} received new value: {1}" .format (feed_id , payload ))
46
51
if feed_id == "neopixel" :
47
52
pixel .fill (int (payload [1 :], 16 ))
@@ -66,18 +71,32 @@ def message(client, feed_id, payload):
66
71
io .on_connect = connected
67
72
io .on_message = message
68
73
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 ()
72
82
73
83
timestamp = 0
74
84
while 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