|
2 | 2 | # |
3 | 3 | # SPDX-License-Identifier: MIT |
4 | 4 |
|
| 5 | +from os import getenv |
5 | 6 | import time |
6 | 7 | import board |
7 | 8 | import busio |
|
14 | 15 |
|
15 | 16 | import adafruit_minimqtt.adafruit_minimqtt as MQTT |
16 | 17 |
|
| 18 | +# Get WiFi details and Adafruit IO keys, ensure these are setup in settings.toml |
| 19 | +# (visit io.adafruit.com if you need to create an account, or if you need your Adafruit IO key.) |
| 20 | +ssid = getenv("CIRCUITPY_WIFI_SSID") |
| 21 | +password = getenv("CIRCUITPY_WIFI_PASSWORD") |
| 22 | +aio_username = getenv("ADAFRUIT_AIO_USERNAME") |
| 23 | +aio_key = getenv("ADAFRUIT_AIO_KEY") |
| 24 | + |
| 25 | +if None in [ssid, password, aio_username, aio_key]: |
| 26 | + raise RuntimeError( |
| 27 | + "WiFi and Adafruit IO settings are kept in settings.toml, " |
| 28 | + "please add them there. The settings file must contain " |
| 29 | + "'CIRCUITPY_WIFI_SSID', 'CIRCUITPY_WIFI_PASSWORD', " |
| 30 | + "'ADAFRUIT_AIO_USERNAME' and 'ADAFRUIT_AIO_KEY' at a minimum." |
| 31 | + ) |
| 32 | + |
17 | 33 | ### Sensor Calibration ### |
18 | 34 | # Appliance power LED's light level, in Lux |
19 | 35 | APPLIANCE_ON_LUX = 30.0 |
|
22 | 38 |
|
23 | 39 | ### WiFi ### |
24 | 40 |
|
25 | | -# Get wifi details and more from a secrets.py file |
26 | | -try: |
27 | | - from secrets import secrets |
28 | | -except ImportError: |
29 | | - print("WiFi secrets are kept in secrets.py, please add them there!") |
30 | | - raise |
31 | | - |
32 | 41 | # If you are using a board with pre-defined ESP32 Pins: |
33 | 42 | esp32_cs = DigitalInOut(board.ESP_CS) |
34 | 43 | esp32_ready = DigitalInOut(board.ESP_BUSY) |
|
42 | 51 | spi = busio.SPI(board.SCK, board.MOSI, board.MISO) |
43 | 52 | esp = adafruit_esp32spi.ESP_SPIcontrol(spi, esp32_cs, esp32_ready, esp32_reset) |
44 | 53 | """Use below for Most Boards""" |
45 | | -status_light = neopixel.NeoPixel( |
| 54 | +status_pixel = neopixel.NeoPixel( |
46 | 55 | board.NEOPIXEL, 1, brightness=0.2 |
47 | 56 | ) # Uncomment for Most Boards |
48 | 57 | """Uncomment below for ItsyBitsy M4""" |
49 | | -# status_light = dotstar.DotStar(board.APA102_SCK, board.APA102_MOSI, 1, brightness=0.2) |
| 58 | +# status_pixel = dotstar.DotStar(board.APA102_SCK, board.APA102_MOSI, 1, brightness=0.2) |
50 | 59 | # Uncomment below for an externally defined RGB LED |
51 | 60 | # import adafruit_rgbled |
52 | 61 | # from adafruit_esp32spi import PWMOut |
53 | 62 | # RED_LED = PWMOut.PWMOut(esp, 26) |
54 | 63 | # GREEN_LED = PWMOut.PWMOut(esp, 27) |
55 | 64 | # BLUE_LED = PWMOut.PWMOut(esp, 25) |
56 | | -# status_light = adafruit_rgbled.RGBLED(RED_LED, BLUE_LED, GREEN_LED) |
57 | | -wifi = adafruit_esp32spi_wifimanager.ESPSPI_WiFiManager(esp, secrets, status_light) |
| 65 | +# status_pixel = adafruit_rgbled.RGBLED(RED_LED, BLUE_LED, GREEN_LED) |
| 66 | +wifi = adafruit_esp32spi_wifimanager.WiFiManager(esp, ssid, password, status_pixel=status_pixel) |
58 | 67 |
|
59 | 68 | # Set up a pin for controlling the relay |
60 | 69 | power_pin = DigitalInOut(board.D3) |
|
67 | 76 |
|
68 | 77 | ### Feeds ### |
69 | 78 | # Set up a feed named Relay for subscribing to the relay feed on Adafruit IO |
70 | | -feed_relay = secrets["aio_username"] + "/feeds/relay" |
| 79 | +feed_relay = f"{aio_username}/feeds/relay" |
71 | 80 |
|
72 | 81 | # Set up a feed named status for subscribing to the status feed on Adafruit IO |
73 | | -feed_status = secrets["aio_username"] + "/feeds/status" |
| 82 | +feed_status = f"{aio_username}/feeds/status" |
74 | 83 |
|
75 | 84 | ### Code ### |
76 | 85 |
|
@@ -122,8 +131,8 @@ def on_relay_msg(client, topic, value): |
122 | 131 | # Set up a MiniMQTT Client |
123 | 132 | client = MQTT.MQTT( |
124 | 133 | broker="io.adafruit.com", |
125 | | - username=secrets["aio_username"], |
126 | | - password=secrets["aio_key"], |
| 134 | + username=aio_username, |
| 135 | + password=aio_key, |
127 | 136 | socket_pool=pool, |
128 | 137 | ssl_context=ssl_context, |
129 | 138 | ) |
|
0 commit comments