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,28 @@ 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 ()
72
-
73
74
timestamp = 0
74
75
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 ()
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