|
1 | 1 | # SPDX-FileCopyrightText: 2021 Eva Herrada for Adafruit Industries |
2 | 2 | # SPDX-License-Identifier: MIT |
3 | 3 |
|
| 4 | +from os import getenv |
4 | 5 | import time |
5 | 6 | import ssl |
6 | 7 | import displayio |
|
15 | 16 | from adafruit_io.adafruit_io import IO_MQTT |
16 | 17 | from adafruit_dash_display import Hub |
17 | 18 |
|
| 19 | +# Get WiFi details and Adafruit IO keys, ensure these are setup in settings.toml |
| 20 | +# (visit io.adafruit.com if you need to create an account, or if you need your Adafruit IO key.) |
| 21 | +ssid = getenv("CIRCUITPY_WIFI_SSID") |
| 22 | +password = getenv("CIRCUITPY_WIFI_PASSWORD") |
| 23 | +aio_username = getenv("ADAFRUIT_AIO_USERNAME") |
| 24 | +aio_key = getenv("ADAFRUIT_AIO_KEY") |
| 25 | + |
| 26 | +if None in [ssid, password, aio_username, aio_key]: |
| 27 | + raise RuntimeError( |
| 28 | + "WiFi and Adafruit IO settings are kept in settings.toml, " |
| 29 | + "please add them there. The settings file must contain " |
| 30 | + "'CIRCUITPY_WIFI_SSID', 'CIRCUITPY_WIFI_PASSWORD', " |
| 31 | + "'ADAFRUIT_AIO_USERNAME' and 'ADAFRUIT_AIO_KEY' at a minimum." |
| 32 | + ) |
| 33 | + |
18 | 34 | # Set up navigation buttons |
19 | 35 | up = DigitalInOut(board.BUTTON_UP) |
20 | 36 | up.direction = Direction.INPUT |
|
31 | 47 | back = touchio.TouchIn(board.CAP7) |
32 | 48 | submit = touchio.TouchIn(board.CAP8) |
33 | 49 |
|
34 | | -# Check for secrets.py. Note: for this project, your secrets.py needs an adafruit io api key as |
35 | | -# well as the wifi information |
36 | | -try: |
37 | | - from secrets import secrets |
38 | | -except ImportError: |
39 | | - print("WiFi secrets are kept in secrets.py, please add them there!") |
40 | | - raise |
41 | | - |
42 | 50 | # Make the rgb group for setting rgb hex values for NeoPixels |
43 | 51 | rgb_group = displayio.Group() |
44 | 52 | R_label = Label( |
@@ -174,24 +182,18 @@ def pub_lamp(lamp): |
174 | 182 |
|
175 | 183 | display = board.DISPLAY |
176 | 184 |
|
177 | | -# Set your Adafruit IO Username and Key in secrets.py |
178 | | -# (visit io.adafruit.com if you need to create an account, |
179 | | -# or if you need your Adafruit IO key.) |
180 | | -aio_username = secrets["aio_username"] |
181 | | -aio_key = secrets["aio_key"] |
182 | | - |
183 | | -print("Connecting to %s" % secrets["ssid"]) |
184 | | -wifi.radio.connect(secrets["ssid"], secrets["password"]) |
185 | | -print("Connected to %s!" % secrets["ssid"]) |
| 185 | +print(f"Connecting to {ssid}") |
| 186 | +wifi.radio.connect(ssid, password) |
| 187 | +print(f"Connected to {ssid}!") |
186 | 188 |
|
187 | 189 | # Create a socket pool |
188 | 190 | pool = socketpool.SocketPool(wifi.radio) |
189 | 191 |
|
190 | 192 | # Initialize a new MQTT Client object |
191 | 193 | mqtt_client = MQTT.MQTT( |
192 | 194 | broker="io.adafruit.com", |
193 | | - username=secrets["aio_username"], |
194 | | - password=secrets["aio_key"], |
| 195 | + username=aio_username, |
| 196 | + password=aio_key, |
195 | 197 | socket_pool=pool, |
196 | 198 | ssl_context=ssl.create_default_context(), |
197 | 199 | ) |
|
0 commit comments