from secrets import secrets
except ImportError:
print("WiFi and Adafruit IO credentials are kept in secrets.py - please add
them there!")
raise
The code pulls your Adafruit IO username and key from secrets.py.
aio_username = secrets["aio_username"]
aio_key = secrets["aio_key"]
The WiFi attempts to connect, and prints the status to the serial console. If it connects
successfully, the code continues onto the NeoPixel set up.
try:
print("Connecting to %s" % secrets["ssid"])
wifi.radio.connect(secrets["ssid"], secrets["password"])
print("Connected to %s!" % secrets["ssid"])
If the WiFi connection is not successful, the error will be printed to the serial console,
and the board will hard reset after 30 seconds.
except Exception as e: # pylint: disable=broad-except
print("Failed to connect to WiFi. Error:", e, "\nBoard will hard reset in 30
seconds.")
time.sleep(30)
microcontroller.reset()
Once the WiFi successfully connects, the NeoPixel object is initiated.
pixel = neopixel.NeoPixel(board.NEOPIXEL, 1, brightness=0.3)
Following that are two callback methods. For more details, check out this guide(). The
connected method subscribes to the neopixel feed on Adafruit IO. The message
callback checks for updates to the neopixel feed, and turns the pixel the color from
the feed.
def connected(client):
print("Connected to Adafruit IO! Listening for NeoPixel changes...")
# Subscribe to Adafruit IO feed called "neopixel"
client.subscribe("neopixel")
# pylint: disable=unused-argument
Note that if a secrets.py file is not present on your CIRCUITPY drive, the code will
fail to run, and you will receive an error in the serial console. Add a secrets.py file
to your CIRCUITPY drive to resolve this error.
©Adafruit Industries Page 155 of 263