11# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
22# SPDX-License-Identifier: MIT
33
4- import os
54import time
5+ from os import getenv
66
77import adafruit_connection_manager
88import board
1313
1414import adafruit_minimqtt .adafruit_minimqtt as MQTT
1515
16- # Add settings.toml to your filesystem CIRCUITPY_WIFI_SSID and CIRCUITPY_WIFI_PASSWORD keys
17- # with your WiFi credentials. Add your Adafruit IO username and key as well.
18- # DO NOT share that file or commit it into Git or other source control.
19-
20- aio_username = os . getenv ("aio_username " )
21- aio_key = os . getenv ("aio_key " )
16+ # Get WiFi details and Adafruit IO keys, ensure these are setup in settings.toml
17+ # (visit io.adafruit.com if you need to create an account, or if you need your Adafruit IO key.)
18+ ssid = getenv ( "CIRCUITPY_WIFI_SSID" )
19+ password = getenv ( "CIRCUITPY_WIFI_PASSWORD" )
20+ aio_username = getenv ("ADAFRUIT_AIO_USERNAME " )
21+ aio_key = getenv ("ADAFRUIT_AIO_KEY " )
2222
2323# If you are using a board with pre-defined ESP32 Pins:
2424esp32_cs = DigitalInOut (board .ESP_CS )
3333spi = busio .SPI (board .SCK , board .MOSI , board .MISO )
3434esp = adafruit_esp32spi .ESP_SPIcontrol (spi , esp32_cs , esp32_ready , esp32_reset )
3535"""Use below for Most Boards"""
36- status_light = neopixel .NeoPixel (board .NEOPIXEL , 1 , brightness = 0.2 ) # Uncomment for Most Boards
36+ status_pixel = neopixel .NeoPixel (board .NEOPIXEL , 1 , brightness = 0.2 ) # Uncomment for Most Boards
3737"""Uncomment below for ItsyBitsy M4"""
38- # status_light = dotstar.DotStar(board.APA102_SCK, board.APA102_MOSI, 1, brightness=0.2)
38+ # status_pixel = dotstar.DotStar(board.APA102_SCK, board.APA102_MOSI, 1, brightness=0.2)
3939# Uncomment below for an externally defined RGB LED
4040# import adafruit_rgbled
4141# from adafruit_esp32spi import PWMOut
4242# RED_LED = PWMOut.PWMOut(esp, 26)
4343# GREEN_LED = PWMOut.PWMOut(esp, 27)
4444# BLUE_LED = PWMOut.PWMOut(esp, 25)
45- # status_light = adafruit_rgbled.RGBLED(RED_LED, BLUE_LED, GREEN_LED)
45+ # status_pixel = adafruit_rgbled.RGBLED(RED_LED, BLUE_LED, GREEN_LED)
4646
4747### Feeds ###
4848
4949# Setup a feed named 'photocell' for publishing to a feed
50- photocell_feed = aio_username + " /feeds/photocell"
50+ photocell_feed = f" { aio_username } /feeds/photocell"
5151
5252# Setup a feed named 'onoff' for subscribing to changes
53- onoff_feed = aio_username + " /feeds/onoff"
53+ onoff_feed = f" { aio_username } /feeds/onoff"
5454
5555### Code ###
5656
5959def connected (client , userdata , flags , rc ):
6060 # This function will be called when the client is connected
6161 # successfully to the broker.
62- print ("Connected to Adafruit IO! Listening for topic changes on %s" % onoff_feed )
62+ print (f "Connected to Adafruit IO! Listening for topic changes on { onoff_feed } " )
6363 # Subscribe to all changes on the onoff_feed.
6464 client .subscribe (onoff_feed )
6565
@@ -77,7 +77,7 @@ def message(client, topic, message):
7777
7878# Connect to WiFi
7979print ("Connecting to WiFi..." )
80- esp .connect_AP (os . getenv ( "CIRCUITPY_WIFI_SSID" ), os . getenv ( "CIRCUITPY_WIFI_PASSWORD" ) )
80+ esp .connect_AP (ssid , password )
8181print ("Connected!" )
8282
8383pool = adafruit_connection_manager .get_radio_socketpool (esp )
@@ -107,7 +107,7 @@ def message(client, topic, message):
107107 mqtt_client .loop ()
108108
109109 # Send a new message
110- print ("Sending photocell value: %d ..." % photocell_val )
110+ print (f "Sending photocell value: { photocell_val } ..." )
111111 mqtt_client .publish (photocell_feed , photocell_val )
112112 print ("Sent!" )
113113 photocell_val += 1
0 commit comments