Skip to content

Commit bedfa65

Browse files
authored
Merge pull request #2995 from justmobilize/secrets-cleanup-f-i
Secrets Cleanup: F and I
2 parents 7406a2b + 906e10e commit bedfa65

File tree

24 files changed

+302
-233
lines changed

24 files changed

+302
-233
lines changed

Feather_ESP32-S2_TFT_Azure/code.py

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# SPDX-FileCopyrightText: 2022 Liz Clark for Adafruit Industries
22
# SPDX-License-Identifier: MIT
33

4+
from os import getenv
45
import time
56
import json
67
import supervisor
@@ -17,20 +18,24 @@
1718
from adafruit_display_text import bitmap_label, wrap_text_to_lines
1819
from adafruit_bitmap_font import bitmap_font
1920
from adafruit_azureiot import IoTCentralDevice
20-
import adafruit_bme680
2121
import adafruit_max1704x
22+
import adafruit_bme680
2223
#from adafruit_lc709203f import LC709203F, PackSize
2324

25+
# Get WiFi details, ensure these are setup in settings.toml
26+
ssid = getenv("CIRCUITPY_WIFI_SSID")
27+
password = getenv("CIRCUITPY_WIFI_PASSWORD")
2428

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
29+
if None in [ssid, password]:
30+
raise RuntimeError(
31+
"WiFi settings are kept in settings.toml, "
32+
"please add them there. The settings file must contain "
33+
"'CIRCUITPY_WIFI_SSID', 'CIRCUITPY_WIFI_PASSWORD', "
34+
"at a minimum."
35+
)
3136

3237
print("Connecting to WiFi...")
33-
wifi.radio.connect(secrets["ssid"], secrets["password"])
38+
wifi.radio.connect(ssid, password)
3439

3540
print("Connected to WiFi!")
3641

@@ -75,7 +80,7 @@
7580
#
7681
# Next create a device using the device template, and select Connect to get the
7782
# device connection details.
78-
# Add the connection details to your secrets.py file, using the following values:
83+
# Add the connection details to your settings.toml file, using the following values:
7984
#
8085
# 'id_scope' - the devices ID scope
8186
# 'device_id' - the devices device id
@@ -100,7 +105,7 @@
100105
esp = None
101106
pool = socketpool.SocketPool(wifi.radio)
102107
device = IoTCentralDevice(
103-
pool, ssl_context, secrets["id_scope"], secrets["device_id"], secrets["device_primary_key"]
108+
pool, ssl_context, getenv("id_scope"), getenv("device_id"), getenv("device_primary_key")
104109
)
105110

106111
print("Connecting to Azure IoT Central...")

Feather_Freezer_Alarm/code.py

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#
33
# SPDX-License-Identifier: MIT
44

5+
from os import getenv
56
import time
67
import ssl
78
import microcontroller
@@ -38,23 +39,26 @@
3839
pir.direction = digitalio.Direction.INPUT
3940
motion = Debouncer(pir)
4041

41-
try:
42-
from secrets import secrets
43-
except ImportError:
44-
print("WiFi and Adafruit IO credentials are kept in secrets.py - please add them there!")
45-
raise
42+
# Get WiFi details and Adafruit IO keys, ensure these are setup in settings.toml
43+
# (visit io.adafruit.com if you need to create an account, or if you need your Adafruit IO key.)
44+
ssid = getenv("CIRCUITPY_WIFI_SSID")
45+
password = getenv("CIRCUITPY_WIFI_PASSWORD")
46+
aio_username = getenv("ADAFRUIT_AIO_USERNAME")
47+
aio_key = getenv("ADAFRUIT_AIO_KEY")
4648

47-
# Add your Adafruit IO Username and Key to secrets.py
48-
# (visit io.adafruit.com if you need to create an account,
49-
# or if you need to obtain your Adafruit IO key.)
50-
aio_username = secrets["aio_username"]
51-
aio_key = secrets["aio_key"]
49+
if None in [ssid, password, aio_username, aio_key]:
50+
raise RuntimeError(
51+
"WiFi and Adafruit IO settings are kept in settings.toml, "
52+
"please add them there. The settings file must contain "
53+
"'CIRCUITPY_WIFI_SSID', 'CIRCUITPY_WIFI_PASSWORD', "
54+
"'ADAFRUIT_AIO_USERNAME' and 'ADAFRUIT_AIO_KEY' at a minimum."
55+
)
5256

5357
# WiFi
5458
try:
55-
print("Connecting to %s" % secrets["ssid"])
56-
wifi.radio.connect(secrets["ssid"], secrets["password"])
57-
print("Connected to %s!" % secrets["ssid"])
59+
print(f"Connecting to {ssid}")
60+
wifi.radio.connect(ssid, password)
61+
print(f"Connected to {ssid}!")
5862
# Wi-Fi connectivity fails with error messages, not specific errors, so this except is broad.
5963
except Exception as e: # pylint: disable=broad-except
6064
print("Failed to connect to WiFi. Error:", e, "\nBoard will restart in 5 seconds.")
@@ -67,8 +71,8 @@
6771
# Initialize a new MQTT Client object
6872
mqtt_client = MQTT.MQTT(
6973
broker="io.adafruit.com",
70-
username=secrets["aio_username"],
71-
password=secrets["aio_key"],
74+
username=aio_username,
75+
password=aio_key,
7276
socket_pool=pool,
7377
ssl_context=ssl.create_default_context(),
7478
)

FunHouse_IOT_Hub/battery_peripheral/code.py

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
Either the Feather or the Airlift Featherwing should have stacking headers for the display.
1313
"""
1414

15+
from os import getenv
1516
import time
1617
import board
1718
from adafruit_lc709203f import LC709203F
@@ -30,18 +31,25 @@
3031
from adafruit_display_text import label
3132
import adafruit_displayio_ssd1306
3233

34+
# Get WiFi details and Adafruit IO keys, ensure these are setup in settings.toml
35+
# (visit io.adafruit.com if you need to create an account, or if you need your Adafruit IO key.)
36+
ssid = getenv("CIRCUITPY_WIFI_SSID")
37+
password = getenv("CIRCUITPY_WIFI_PASSWORD")
38+
aio_username = getenv("ADAFRUIT_AIO_USERNAME")
39+
aio_key = getenv("ADAFRUIT_AIO_KEY")
40+
41+
if None in [ssid, password, aio_username, aio_key]:
42+
raise RuntimeError(
43+
"WiFi and Adafruit IO settings are kept in settings.toml, "
44+
"please add them there. The settings file must contain "
45+
"'CIRCUITPY_WIFI_SSID', 'CIRCUITPY_WIFI_PASSWORD', "
46+
"'ADAFRUIT_AIO_USERNAME' and 'ADAFRUIT_AIO_KEY' at a minimum."
47+
)
48+
3349
displayio.release_displays()
3450

3551
### WiFi ###
3652

37-
# Get wifi details and more from a secrets.py file
38-
try:
39-
from secrets import secrets
40-
except ImportError:
41-
print("WiFi secrets are kept in secrets.py, please add them there!")
42-
raise
43-
44-
4553
# If you are using a board with pre-defined ESP32 Pins:
4654
esp32_cs = DigitalInOut(board.D13)
4755
esp32_ready = DigitalInOut(board.D11)
@@ -50,10 +58,10 @@
5058
spi = busio.SPI(board.SCK, board.MOSI, board.MISO)
5159
esp = adafruit_esp32spi.ESP_SPIcontrol(spi, esp32_cs, esp32_ready, esp32_reset)
5260
"""Use below for Most Boards"""
53-
status_light = neopixel.NeoPixel(
61+
status_pixel = neopixel.NeoPixel(
5462
board.NEOPIXEL, 1, brightness=0.2
5563
) # Uncomment for Most Boards
56-
wifi = adafruit_esp32spi_wifimanager.ESPSPI_WiFiManager(esp, secrets, status_light)
64+
wifi = adafruit_esp32spi_wifimanager.WiFiManager(esp, ssid, password, status_pixel=status_pixel)
5765

5866
# Define callback functions which will be called when certain events happen.
5967
# pylint: disable=unused-argument
@@ -81,8 +89,8 @@ def message(client, feed_id, payload):
8189
# Initialize a new MQTT Client object
8290
mqtt_client = MQTT.MQTT(
8391
broker="io.adafruit.com",
84-
username=secrets["aio_username"],
85-
password=secrets["aio_key"],
92+
username=aio_username,
93+
password=aio_key,
8694
socket_pool=pool,
8795
ssl_context=ssl_context,
8896
)

FunHouse_IOT_Hub/door_peripheral/code.py

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
either the Feather or the AirLift Featherwing has stacking headers.
1515
"""
1616

17+
from os import getenv
1718
import board
1819
import busio
1920
import adafruit_connection_manager
@@ -25,14 +26,22 @@
2526
from digitalio import DigitalInOut, Direction, Pull
2627
from adafruit_debouncer import Debouncer
2728

28-
### WiFi ###
29+
# Get WiFi details and Adafruit IO keys, ensure these are setup in settings.toml
30+
# (visit io.adafruit.com if you need to create an account, or if you need your Adafruit IO key.)
31+
ssid = getenv("CIRCUITPY_WIFI_SSID")
32+
password = getenv("CIRCUITPY_WIFI_PASSWORD")
33+
aio_username = getenv("ADAFRUIT_AIO_USERNAME")
34+
aio_key = getenv("ADAFRUIT_AIO_KEY")
35+
36+
if None in [ssid, password, aio_username, aio_key]:
37+
raise RuntimeError(
38+
"WiFi and Adafruit IO settings are kept in settings.toml, "
39+
"please add them there. The settings file must contain "
40+
"'CIRCUITPY_WIFI_SSID', 'CIRCUITPY_WIFI_PASSWORD', "
41+
"'ADAFRUIT_AIO_USERNAME' and 'ADAFRUIT_AIO_KEY' at a minimum."
42+
)
2943

30-
# Get wifi details and more from a secrets.py file
31-
try:
32-
from secrets import secrets
33-
except ImportError:
34-
print("WiFi secrets are kept in secrets.py, please add them there!")
35-
raise
44+
### WiFi ###
3645

3746
switch_pin = DigitalInOut(board.D10)
3847
switch_pin.direction = Direction.INPUT
@@ -47,10 +56,10 @@
4756
spi = busio.SPI(board.SCK, board.MOSI, board.MISO)
4857
esp = adafruit_esp32spi.ESP_SPIcontrol(spi, esp32_cs, esp32_ready, esp32_reset)
4958
"""Use below for Most Boards"""
50-
status_light = neopixel.NeoPixel(
59+
status_pixel = neopixel.NeoPixel(
5160
board.NEOPIXEL, 1, brightness=0.2
5261
) # Uncomment for Most Boards
53-
wifi = adafruit_esp32spi_wifimanager.ESPSPI_WiFiManager(esp, secrets, status_light)
62+
wifi = adafruit_esp32spi_wifimanager.WiFiManager(esp, ssid, password, status_pixel=status_pixel)
5463

5564
# Connect to WiFi
5665
print("Connecting to WiFi...")
@@ -63,8 +72,8 @@
6372
# Initialize a new MQTT Client object
6473
mqtt_client = MQTT.MQTT(
6574
broker="io.adafruit.com",
66-
username=secrets["aio_username"],
67-
password=secrets["aio_key"],
75+
username=aio_username,
76+
password=aio_key,
6877
socket_pool=pool,
6978
ssl_context=ssl_context,
7079
)

FunHouse_IOT_Hub/iot_hub/code.py

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# SPDX-FileCopyrightText: 2021 Eva Herrada for Adafruit Industries
22
# SPDX-License-Identifier: MIT
33

4+
from os import getenv
45
import time
56
import ssl
67
import displayio
@@ -15,6 +16,21 @@
1516
from adafruit_io.adafruit_io import IO_MQTT
1617
from adafruit_dash_display import Hub
1718

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+
1834
# Set up navigation buttons
1935
up = DigitalInOut(board.BUTTON_UP)
2036
up.direction = Direction.INPUT
@@ -31,14 +47,6 @@
3147
back = touchio.TouchIn(board.CAP7)
3248
submit = touchio.TouchIn(board.CAP8)
3349

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-
4250
# Make the rgb group for setting rgb hex values for NeoPixels
4351
rgb_group = displayio.Group()
4452
R_label = Label(
@@ -174,24 +182,18 @@ def pub_lamp(lamp):
174182

175183
display = board.DISPLAY
176184

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}!")
186188

187189
# Create a socket pool
188190
pool = socketpool.SocketPool(wifi.radio)
189191

190192
# Initialize a new MQTT Client object
191193
mqtt_client = MQTT.MQTT(
192194
broker="io.adafruit.com",
193-
username=secrets["aio_username"],
194-
password=secrets["aio_key"],
195+
username=aio_username,
196+
password=aio_key,
195197
socket_pool=pool,
196198
ssl_context=ssl.create_default_context(),
197199
)

FunHouse_IOT_Hub/neopixel_remote/code.py

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
Colors used are taken from Adafruit_CircuitPython_LED_Animation library
88
"""
99

10+
from os import getenv
1011
import time
1112
import math
1213
import board
@@ -26,6 +27,21 @@
2627
# Import Adafruit IO HTTP Client
2728
from adafruit_io.adafruit_io import IO_HTTP, AdafruitIO_RequestError
2829

30+
# Get WiFi details and Adafruit IO keys, ensure these are setup in settings.toml
31+
# (visit io.adafruit.com if you need to create an account, or if you need your Adafruit IO key.)
32+
ssid = getenv("CIRCUITPY_WIFI_SSID")
33+
password = getenv("CIRCUITPY_WIFI_PASSWORD")
34+
aio_username = getenv("ADAFRUIT_AIO_USERNAME")
35+
aio_key = getenv("ADAFRUIT_AIO_KEY")
36+
37+
if None in [ssid, password, aio_username, aio_key]:
38+
raise RuntimeError(
39+
"WiFi and Adafruit IO settings are kept in settings.toml, "
40+
"please add them there. The settings file must contain "
41+
"'CIRCUITPY_WIFI_SSID', 'CIRCUITPY_WIFI_PASSWORD', "
42+
"'ADAFRUIT_AIO_USERNAME' and 'ADAFRUIT_AIO_KEY' at a minimum."
43+
)
44+
2945
ts = adafruit_touchscreen.Touchscreen(
3046
board.TOUCH_XL,
3147
board.TOUCH_XR,
@@ -92,30 +108,17 @@
92108
group.append(rect)
93109
print(len(group))
94110

95-
# Get wifi details and more from a secrets.py file
96-
try:
97-
from secrets import secrets
98-
except ImportError:
99-
print("WiFi secrets are kept in secrets.py, please add them there!")
100-
raise
101-
102111
# PyPortal ESP32 Setup
103112
esp32_cs = DigitalInOut(board.ESP_CS)
104113
esp32_ready = DigitalInOut(board.ESP_BUSY)
105114
esp32_reset = DigitalInOut(board.ESP_RESET)
106115
spi = busio.SPI(board.SCK, board.MOSI, board.MISO)
107116
esp = adafruit_esp32spi.ESP_SPIcontrol(spi, esp32_cs, esp32_ready, esp32_reset)
108-
status_light = neopixel.NeoPixel(board.NEOPIXEL, 1, brightness=0.2)
109-
wifi = adafruit_esp32spi_wifimanager.ESPSPI_WiFiManager(esp, secrets, status_light)
110-
111-
# Set your Adafruit IO Username and Key in secrets.py
112-
# (visit io.adafruit.com if you need to create an account,
113-
# or if you need your Adafruit IO key.)
114-
ADAFRUIT_IO_USER = secrets["aio_username"]
115-
ADAFRUIT_IO_KEY = secrets["aio_key"]
117+
status_pixel = neopixel.NeoPixel(board.NEOPIXEL, 1, brightness=0.2)
118+
wifi = adafruit_esp32spi_wifimanager.WiFiManager(esp, ssid, password, status_pixel=status_pixel)
116119

117120
# Create an instance of the Adafruit IO HTTP client
118-
io = IO_HTTP(ADAFRUIT_IO_USER, ADAFRUIT_IO_KEY, wifi)
121+
io = IO_HTTP(aio_username, aio_key, wifi)
119122

120123
try:
121124
# Get the 'temperature' feed from Adafruit IO

0 commit comments

Comments
 (0)