Skip to content

Commit 3b82052

Browse files
authored
Merge pull request #2122 from kattni/wifi-example-update
Update WiFi example.
2 parents a3ba5af + f856f39 commit 3b82052

File tree

1 file changed

+35
-20
lines changed
  • CircuitPython_Templates/adafruit_io_cpu_temp_neopixel_color

1 file changed

+35
-20
lines changed

CircuitPython_Templates/adafruit_io_cpu_temp_neopixel_color/code.py

Lines changed: 35 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import time
55
import ssl
66
from random import randint
7+
import microcontroller
78
import socketpool
89
import wifi
910
import board
@@ -19,29 +20,33 @@
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.)
2324
aio_username = secrets["aio_username"]
2425
aio_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, "\nBoard will hard reset in 30 seconds.")
35+
time.sleep(30)
36+
microcontroller.reset()
3037

3138
# Initialise NeoPixel
3239
pixel = 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
3743
def 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):
6671
io.on_connect = connected
6772
io.on_message = message
6873

69-
# Connect the client to the MQTT broker.
70-
print("Connecting to Adafruit IO...")
71-
io.connect()
72-
7374
timestamp = 0
7475
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+
"\nBoard will hard reset in 30 seconds.")
97+
time.sleep(30)
98+
microcontroller.reset()

0 commit comments

Comments
 (0)