1
1
# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
2
2
# SPDX-License-Identifier: MIT
3
3
4
- import os
5
4
import time
5
+ from os import getenv
6
6
7
7
import adafruit_connection_manager
8
8
import board
13
13
14
14
import adafruit_minimqtt .adafruit_minimqtt as MQTT
15
15
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 " )
22
22
23
23
# If you are using a board with pre-defined ESP32 Pins:
24
24
esp32_cs = DigitalInOut (board .ESP_CS )
33
33
spi = busio .SPI (board .SCK , board .MOSI , board .MISO )
34
34
esp = adafruit_esp32spi .ESP_SPIcontrol (spi , esp32_cs , esp32_ready , esp32_reset )
35
35
"""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
37
37
"""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)
39
39
# Uncomment below for an externally defined RGB LED
40
40
# import adafruit_rgbled
41
41
# from adafruit_esp32spi import PWMOut
42
42
# RED_LED = PWMOut.PWMOut(esp, 26)
43
43
# GREEN_LED = PWMOut.PWMOut(esp, 27)
44
44
# 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)
46
46
47
47
### Feeds ###
48
48
49
49
# 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"
51
51
52
52
# Setup a feed named 'onoff' for subscribing to changes
53
- onoff_feed = aio_username + " /feeds/onoff"
53
+ onoff_feed = f" { aio_username } /feeds/onoff"
54
54
55
55
### Code ###
56
56
59
59
def connected (client , userdata , flags , rc ):
60
60
# This function will be called when the client is connected
61
61
# 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 } " )
63
63
# Subscribe to all changes on the onoff_feed.
64
64
client .subscribe (onoff_feed )
65
65
@@ -77,7 +77,7 @@ def message(client, topic, message):
77
77
78
78
# Connect to WiFi
79
79
print ("Connecting to WiFi..." )
80
- esp .connect_AP (os . getenv ( "CIRCUITPY_WIFI_SSID" ), os . getenv ( "CIRCUITPY_WIFI_PASSWORD" ) )
80
+ esp .connect_AP (ssid , password )
81
81
print ("Connected!" )
82
82
83
83
pool = adafruit_connection_manager .get_radio_socketpool (esp )
@@ -107,7 +107,7 @@ def message(client, topic, message):
107
107
mqtt_client .loop ()
108
108
109
109
# Send a new message
110
- print ("Sending photocell value: %d ..." % photocell_val )
110
+ print (f "Sending photocell value: { photocell_val } ..." )
111
111
mqtt_client .publish (photocell_feed , photocell_val )
112
112
print ("Sent!" )
113
113
photocell_val += 1
0 commit comments