Skip to content

Commit ca3e8b4

Browse files
authored
Merge pull request #20 from justmobilize/remove-secrets-usage
Remove secrets usage
2 parents da966da + 9c29b78 commit ca3e8b4

10 files changed

+157
-282
lines changed

README.rst

Lines changed: 15 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -43,12 +43,11 @@ Usage Example
4343
.. code :: python3
4444
4545
import time
46-
import ssl
4746
import board
4847
from digitalio import DigitalInOut, Direction, Pull
4948
import touchio
50-
import socketpool
5149
import wifi
50+
import adafruit_connection_manager
5251
import adafruit_minimqtt.adafruit_minimqtt as MQTT
5352
from adafruit_io.adafruit_io import IO_MQTT
5453
from adafruit_dash_display import Hub
@@ -68,34 +67,30 @@ Usage Example
6867
back = touchio.TouchIn(board.CAP7)
6968
submit = touchio.TouchIn(board.CAP8)
7069
71-
try:
72-
from secrets import secrets
73-
except ImportError:
74-
print("WiFi secrets are kept in secrets.py, please add them there!")
75-
raise
70+
# Get WiFi details and Adafruit IO keys, ensure these are setup in settings.toml
71+
# (visit io.adafruit.com if you need to create an account, or if you need your Adafruit IO key.)
72+
ssid = getenv("CIRCUITPY_WIFI_SSID")
73+
password = getenv("CIRCUITPY_WIFI_PASSWORD")
74+
aio_username = getenv("ADAFRUIT_AIO_USERNAME")
75+
aio_key = getenv("ADAFRUIT_AIO_KEY")
7676
7777
display = board.DISPLAY
7878
79-
# Set your Adafruit IO Username and Key in secrets.py
80-
# (visit io.adafruit.com if you need to create an account,
81-
# or if you need your Adafruit IO key.)
82-
aio_username = secrets["aio_username"]
83-
aio_key = secrets["aio_key"]
84-
85-
print("Connecting to %s" % secrets["ssid"])
86-
wifi.radio.connect(secrets["ssid"], secrets["password"])
87-
print("Connected to %s!" % secrets["ssid"])
79+
print(f"Connecting to {ssid}")
80+
wifi.radio.connect(ssid, password)
81+
print(f"Connected to {ssid}!")
8882
8983
# Create a socket pool
90-
pool = socketpool.SocketPool(wifi.radio)
84+
pool = adafruit_connection_manager.get_radio_socketpool(wifi.radio)
85+
ssl_context = adafruit_connection_manager.get_radio_ssl_context(wifi.radio)
9186
9287
# Initialize a new MQTT Client object
9388
mqtt_client = MQTT.MQTT(
9489
broker="io.adafruit.com",
95-
username=secrets["aio_username"],
96-
password=secrets["aio_key"],
90+
username=aio_username,
91+
password=aio_key,
9792
socket_pool=pool,
98-
ssl_context=ssl.create_default_context(),
93+
ssl_context=ssl_context,
9994
)
10095
10196
# Initialize an Adafruit IO MQTT Client

examples/dash_display_advancedtest.py

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

44
import time
5-
import ssl
65
from os import getenv
76
import displayio
87
import board
98
from digitalio import DigitalInOut, Direction, Pull
109
from adafruit_display_text.label import Label
1110
import terminalio
1211
import touchio
13-
import socketpool
1412
import wifi
13+
import adafruit_connection_manager
1514
import adafruit_minimqtt.adafruit_minimqtt as MQTT
1615
from adafruit_io.adafruit_io import IO_MQTT
1716
from adafruit_dash_display import Hub
@@ -31,24 +30,12 @@
3130
back = touchio.TouchIn(board.CAP7)
3231
submit = touchio.TouchIn(board.CAP8)
3332

34-
# Get wifi details and more from a settings.toml file
35-
# tokens used by this Demo: CIRCUITPY_WIFI_SSID, CIRCUITPY_WIFI_PASSWORD
36-
# CIRCUITPY_AIO_USERNAME, CIRCUITPY_AIO_KEY
37-
secrets = {}
38-
for token in ["SSID", "PASSWORD"]:
39-
if getenv("CIRCUITPY_WIFI_" + token):
40-
secrets[token.lower()] = getenv("CIRCUITPY_WIFI_" + token)
41-
for token in ["AIO_USERNAME", "AIO_KEY"]:
42-
if getenv("CIRCUITPY_" + token):
43-
secrets[token.lower()] = getenv("CIRCUITPY_" + token)
44-
45-
if not secrets:
46-
try:
47-
# Fallback on secrets.py until depreciation is over and option is removed
48-
from secrets import secrets
49-
except ImportError:
50-
print("WiFi secrets are kept in settings.toml, please add them there!")
51-
raise
33+
# Get WiFi details and Adafruit IO keys, ensure these are setup in settings.toml
34+
# (visit io.adafruit.com if you need to create an account, or if you need your Adafruit IO key.)
35+
ssid = getenv("CIRCUITPY_WIFI_SSID")
36+
password = getenv("CIRCUITPY_WIFI_PASSWORD")
37+
aio_username = getenv("ADAFRUIT_AIO_USERNAME")
38+
aio_key = getenv("ADAFRUIT_AIO_KEY")
5239

5340
rgb_group = displayio.Group()
5441
R_label = Label(
@@ -185,26 +172,21 @@ def pub_lamp(lamp):
185172

186173
display = board.DISPLAY
187174

188-
# Set your Adafruit IO Username and Key in settings.toml
189-
# (visit io.adafruit.com if you need to create an account,
190-
# or if you need your Adafruit IO key.)
191-
aio_username = secrets["aio_username"]
192-
aio_key = secrets["aio_key"]
175+
print(f"Connecting to {ssid}")
176+
wifi.radio.connect(ssid, password)
177+
print(f"Connected to {ssid}!")
193178

194-
print("Connecting to %s" % secrets["ssid"])
195-
wifi.radio.connect(secrets["ssid"], secrets["password"])
196-
print("Connected to %s!" % secrets["ssid"])
197-
198-
# Create a socket pool
199-
pool = socketpool.SocketPool(wifi.radio)
179+
# Create a socket pool and ssl_context
180+
pool = adafruit_connection_manager.get_radio_socketpool(wifi.radio)
181+
ssl_context = adafruit_connection_manager.get_radio_ssl_context(wifi.radio)
200182

201183
# Initialize a new MQTT Client object
202184
mqtt_client = MQTT.MQTT(
203185
broker="io.adafruit.com",
204-
username=secrets["aio_username"],
205-
password=secrets["aio_key"],
186+
username=aio_username,
187+
password=aio_key,
206188
socket_pool=pool,
207-
ssl_context=ssl.create_default_context(),
189+
ssl_context=ssl_context,
208190
)
209191

210192
# Initialize an Adafruit IO MQTT Client

examples/dash_display_client_examples/battery_daughter.py

Lines changed: 15 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -21,26 +21,14 @@
2121

2222
displayio.release_displays()
2323

24-
### WiFi ###
24+
# Get WiFi details and Adafruit IO keys, ensure these are setup in settings.toml
25+
# (visit io.adafruit.com if you need to create an account, or if you need your Adafruit IO key.)
26+
ssid = getenv("CIRCUITPY_WIFI_SSID")
27+
password = getenv("CIRCUITPY_WIFI_PASSWORD")
28+
aio_username = getenv("ADAFRUIT_AIO_USERNAME")
29+
aio_key = getenv("ADAFRUIT_AIO_KEY")
2530

26-
# Get wifi details and more from a settings.toml file
27-
# tokens used by this Demo: CIRCUITPY_WIFI_SSID, CIRCUITPY_WIFI_PASSWORD
28-
# CIRCUITPY_AIO_USERNAME, CIRCUITPY_AIO_KEY
29-
secrets = {}
30-
for token in ["SSID", "PASSWORD"]:
31-
if getenv("CIRCUITPY_WIFI_" + token):
32-
secrets[token.lower()] = getenv("CIRCUITPY_WIFI_" + token)
33-
for token in ["AIO_USERNAME", "AIO_KEY"]:
34-
if getenv("CIRCUITPY_" + token):
35-
secrets[token.lower()] = getenv("CIRCUITPY_" + token)
36-
37-
if not secrets:
38-
try:
39-
# Fallback on secrets.py until depreciation is over and option is removed
40-
from secrets import secrets
41-
except ImportError:
42-
print("WiFi secrets are kept in settings.toml, please add them there!")
43-
raise
31+
### WiFi ###
4432

4533
# If you are using a board with pre-defined ESP32 Pins:
4634
esp32_cs = DigitalInOut(board.ESP_CS)
@@ -60,18 +48,20 @@
6048
esp = adafruit_esp32spi.ESP_SPIcontrol(spi, esp32_cs, esp32_ready, esp32_reset)
6149

6250
"""Use below for Most Boards"""
63-
status_light = neopixel.NeoPixel(board.NEOPIXEL, 1, brightness=0.2)
51+
status_pixel = neopixel.NeoPixel(board.NEOPIXEL, 1, brightness=0.2)
6452
"""Uncomment below for ItsyBitsy M4"""
65-
# status_light = dotstar.DotStar(board.APA102_SCK, board.APA102_MOSI, 1, brightness=0.2)
53+
# status_pixel = dotstar.DotStar(board.APA102_SCK, board.APA102_MOSI, 1, brightness=0.2)
6654
"""Uncomment below for an externally defined RGB LED (including Arduino Nano Connect)"""
6755
# import adafruit_rgbled
6856
# from adafruit_esp32spi import PWMOut
6957
# RED_LED = PWMOut.PWMOut(esp, 26)
7058
# GREEN_LED = PWMOut.PWMOut(esp, 27)
7159
# BLUE_LED = PWMOut.PWMOut(esp, 25)
72-
# status_light = adafruit_rgbled.RGBLED(RED_LED, BLUE_LED, GREEN_LED)
60+
# status_pixel = adafruit_rgbled.RGBLED(RED_LED, BLUE_LED, GREEN_LED)
7361

74-
wifi = adafruit_esp32spi_wifimanager.ESPSPI_WiFiManager(esp, secrets, status_light)
62+
wifi = adafruit_esp32spi_wifimanager.WiFiManager(
63+
esp, ssid, password, status_pixel=status_pixel
64+
)
7565

7666

7767
# Define callback functions which will be called when certain events happen.
@@ -100,8 +90,8 @@ def message(client, feed_id, payload):
10090
# Initialize a new MQTT Client object
10191
mqtt_client = MQTT.MQTT(
10292
broker="io.adafruit.com",
103-
username=secrets["aio_username"],
104-
password=secrets["aio_key"],
93+
username=aio_username,
94+
password=aio_key,
10595
socket_pool=pool,
10696
ssl_context=ssl_context,
10797
)

examples/dash_display_client_examples/door_daughter.py

Lines changed: 16 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -13,32 +13,20 @@
1313
from digitalio import DigitalInOut, Direction, Pull
1414
from adafruit_debouncer import Debouncer
1515

16-
### WiFi ###
17-
18-
# Get wifi details and more from a settings.toml file
19-
# tokens used by this Demo: CIRCUITPY_WIFI_SSID, CIRCUITPY_WIFI_PASSWORD
20-
# CIRCUITPY_AIO_USERNAME, CIRCUITPY_AIO_KEY
21-
secrets = {}
22-
for token in ["SSID", "PASSWORD"]:
23-
if getenv("CIRCUITPY_WIFI_" + token):
24-
secrets[token.lower()] = getenv("CIRCUITPY_WIFI_" + token)
25-
for token in ["AIO_USERNAME", "AIO_KEY"]:
26-
if getenv("CIRCUITPY_" + token):
27-
secrets[token.lower()] = getenv("CIRCUITPY_" + token)
28-
29-
if not secrets:
30-
try:
31-
# Fallback on secrets.py until depreciation is over and option is removed
32-
from secrets import secrets
33-
except ImportError:
34-
print("WiFi secrets are kept in settings.toml, please add them there!")
35-
raise
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")
3622

3723
switch_pin = DigitalInOut(board.D10)
3824
switch_pin.direction = Direction.INPUT
3925
switch_pin.pull = Pull.UP
4026
switch = Debouncer(switch_pin)
4127

28+
### WiFi ###
29+
4230
# If you are using a board with pre-defined ESP32 Pins:
4331
esp32_cs = DigitalInOut(board.ESP_CS)
4432
esp32_ready = DigitalInOut(board.ESP_BUSY)
@@ -57,18 +45,20 @@
5745
esp = adafruit_esp32spi.ESP_SPIcontrol(spi, esp32_cs, esp32_ready, esp32_reset)
5846

5947
"""Use below for Most Boards"""
60-
status_light = neopixel.NeoPixel(board.NEOPIXEL, 1, brightness=0.2)
48+
status_pixel = neopixel.NeoPixel(board.NEOPIXEL, 1, brightness=0.2)
6149
"""Uncomment below for ItsyBitsy M4"""
62-
# status_light = dotstar.DotStar(board.APA102_SCK, board.APA102_MOSI, 1, brightness=0.2)
50+
# status_pixel = dotstar.DotStar(board.APA102_SCK, board.APA102_MOSI, 1, brightness=0.2)
6351
"""Uncomment below for an externally defined RGB LED (including Arduino Nano Connect)"""
6452
# import adafruit_rgbled
6553
# from adafruit_esp32spi import PWMOut
6654
# RED_LED = PWMOut.PWMOut(esp, 26)
6755
# GREEN_LED = PWMOut.PWMOut(esp, 27)
6856
# BLUE_LED = PWMOut.PWMOut(esp, 25)
69-
# status_light = adafruit_rgbled.RGBLED(RED_LED, BLUE_LED, GREEN_LED)
57+
# status_pixel = adafruit_rgbled.RGBLED(RED_LED, BLUE_LED, GREEN_LED)
7058

71-
wifi = adafruit_esp32spi_wifimanager.ESPSPI_WiFiManager(esp, secrets, status_light)
59+
wifi = adafruit_esp32spi_wifimanager.WiFiManager(
60+
esp, ssid, password, status_pixel=status_pixel
61+
)
7262

7363
# Connect to WiFi
7464
print("Connecting to WiFi...")
@@ -81,8 +71,8 @@
8171
# Initialize a new MQTT Client object
8272
mqtt_client = MQTT.MQTT(
8373
broker="io.adafruit.com",
84-
username=secrets["aio_username"],
85-
password=secrets["aio_key"],
74+
username=aio_username,
75+
password=aio_key,
8676
socket_pool=pool,
8777
ssl_context=ssl_context,
8878
)

examples/dash_display_client_examples/neopixel_daughter.py

Lines changed: 16 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -12,29 +12,17 @@
1212
from adafruit_io.adafruit_io import IO_MQTT
1313
from digitalio import DigitalInOut
1414

15-
### WiFi ###
16-
17-
# Get wifi details and more from a settings.toml file
18-
# tokens used by this Demo: CIRCUITPY_WIFI_SSID, CIRCUITPY_WIFI_PASSWORD
19-
# CIRCUITPY_AIO_USERNAME, CIRCUITPY_AIO_KEY
20-
secrets = {}
21-
for token in ["SSID", "PASSWORD"]:
22-
if getenv("CIRCUITPY_WIFI_" + token):
23-
secrets[token.lower()] = getenv("CIRCUITPY_WIFI_" + token)
24-
for token in ["AIO_USERNAME", "AIO_KEY"]:
25-
if getenv("CIRCUITPY_" + token):
26-
secrets[token.lower()] = getenv("CIRCUITPY_" + token)
27-
28-
if not secrets:
29-
try:
30-
# Fallback on secrets.py until depreciation is over and option is removed
31-
from secrets import secrets
32-
except ImportError:
33-
print("WiFi secrets are kept in settings.toml, please add them there!")
34-
raise
15+
# Get WiFi details and Adafruit IO keys, ensure these are setup in settings.toml
16+
# (visit io.adafruit.com if you need to create an account, or if you need your Adafruit IO key.)
17+
ssid = getenv("CIRCUITPY_WIFI_SSID")
18+
password = getenv("CIRCUITPY_WIFI_PASSWORD")
19+
aio_username = getenv("ADAFRUIT_AIO_USERNAME")
20+
aio_key = getenv("ADAFRUIT_AIO_KEY")
3521

3622
pixels = neopixel.NeoPixel(board.D5, 300)
3723

24+
### WiFi ###
25+
3826
# If you are using a board with pre-defined ESP32 Pins:
3927
esp32_cs = DigitalInOut(board.ESP_CS)
4028
esp32_ready = DigitalInOut(board.ESP_BUSY)
@@ -53,18 +41,20 @@
5341
esp = adafruit_esp32spi.ESP_SPIcontrol(spi, esp32_cs, esp32_ready, esp32_reset)
5442

5543
"""Use below for Most Boards"""
56-
status_light = neopixel.NeoPixel(board.NEOPIXEL, 1, brightness=0.2)
44+
status_pixel = neopixel.NeoPixel(board.NEOPIXEL, 1, brightness=0.2)
5745
"""Uncomment below for ItsyBitsy M4"""
58-
# status_light = dotstar.DotStar(board.APA102_SCK, board.APA102_MOSI, 1, brightness=0.2)
46+
# status_pixel = dotstar.DotStar(board.APA102_SCK, board.APA102_MOSI, 1, brightness=0.2)
5947
"""Uncomment below for an externally defined RGB LED (including Arduino Nano Connect)"""
6048
# import adafruit_rgbled
6149
# from adafruit_esp32spi import PWMOut
6250
# RED_LED = PWMOut.PWMOut(esp, 26)
6351
# GREEN_LED = PWMOut.PWMOut(esp, 27)
6452
# BLUE_LED = PWMOut.PWMOut(esp, 25)
65-
# status_light = adafruit_rgbled.RGBLED(RED_LED, BLUE_LED, GREEN_LED)
53+
# status_pixel = adafruit_rgbled.RGBLED(RED_LED, BLUE_LED, GREEN_LED)
6654

67-
wifi = adafruit_esp32spi_wifimanager.ESPSPI_WiFiManager(esp, secrets, status_light)
55+
wifi = adafruit_esp32spi_wifimanager.WiFiManager(
56+
esp, ssid, password, status_pixel=status_pixel
57+
)
6858

6959

7060
# Define callback functions which will be called when certain events happen.
@@ -98,8 +88,8 @@ def on_neopixel(client, topic, message):
9888
# Initialize a new MQTT Client object
9989
mqtt_client = MQTT.MQTT(
10090
broker="io.adafruit.com",
101-
username=secrets["aio_username"],
102-
password=secrets["aio_key"],
91+
username=aio_username,
92+
password=aio_key,
10393
socket_pool=pool,
10494
ssl_context=ssl_context,
10595
)

0 commit comments

Comments
 (0)