Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit c413397

Browse files
authoredMar 6, 2025··
Merge pull request #245 from justmobilize/remove-secrets-usage
Remove secrets usage
2 parents 08253c4 + 29e12f6 commit c413397

24 files changed

+396
-400
lines changed
 

‎.pre-commit-config.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,11 @@ repos:
1111
- id: end-of-file-fixer
1212
- id: trailing-whitespace
1313
- repo: https://github.com/astral-sh/ruff-pre-commit
14-
rev: v0.3.4
14+
rev: v0.9.9
1515
hooks:
16-
- id: ruff-format
1716
- id: ruff
1817
args: ["--fix"]
18+
- id: ruff-format
1919
- repo: https://github.com/fsfe/reuse-tool
2020
rev: v3.0.1
2121
hooks:

‎adafruit_minimqtt/adafruit_minimqtt.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@
2929
3030
"""
3131

32+
# ruff: noqa: PLR6104,PLR6201,PLR6301 non-augmented-assignment,literal-membership,no-self-use
33+
3234
import errno
3335
import struct
3436
import time
@@ -111,7 +113,7 @@ def __init__(self) -> None:
111113
setattr(NullLogger, log_level, self.nothing)
112114

113115

114-
class MQTT:
116+
class MQTT: # noqa: PLR0904 # too-many-public-methods
115117
"""MQTT Client for CircuitPython.
116118
117119
:param str broker: MQTT Broker URL or IP Address.

‎examples/cellular/minimqtt_adafruitio_cellular.py

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
22
# SPDX-License-Identifier: MIT
33

4-
import os
54
import time
5+
from os import getenv
66

77
import adafruit_connection_manager
88
import adafruit_fona.adafruit_fona_network as network
@@ -14,12 +14,13 @@
1414

1515
import adafruit_minimqtt.adafruit_minimqtt as MQTT
1616

17-
# Add settings.toml to your filesystem CIRCUITPY_WIFI_SSID and CIRCUITPY_WIFI_PASSWORD keys
18-
# with your GPRS credentials. Add your Adafruit IO username and key as well.
19-
# DO NOT share that file or commit it into Git or other source control.
20-
21-
aio_username = os.getenv("aio_username")
22-
aio_key = os.getenv("aio_key")
17+
# Get FONA details and Adafruit IO keys, ensure these are setup in settings.toml
18+
# (visit io.adafruit.com if you need to create an account, or if you need your Adafruit IO key.)
19+
apn = getenv("apn")
20+
apn_username = getenv("apn_username")
21+
apn_password = getenv("apn_password")
22+
aio_username = getenv("ADAFRUIT_AIO_USERNAME")
23+
aio_key = getenv("ADAFRUIT_AIO_KEY")
2324

2425
### Cellular ###
2526

@@ -32,10 +33,10 @@
3233
### Feeds ###
3334

3435
# Setup a feed named 'photocell' for publishing to a feed
35-
photocell_feed = aio_username + "/feeds/photocell"
36+
photocell_feed = f"{aio_username}/feeds/photocell"
3637

3738
# Setup a feed named 'onoff' for subscribing to changes
38-
onoff_feed = aio_username + "/feeds/onoff"
39+
onoff_feed = f"{aio_username}/feeds/onoff"
3940

4041
### Code ###
4142

@@ -44,7 +45,7 @@
4445
def connected(client, userdata, flags, rc):
4546
# This function will be called when the client is connected
4647
# successfully to the broker.
47-
print("Connected to Adafruit IO! Listening for topic changes on %s" % onoff_feed)
48+
print(f"Connected to Adafruit IO! Listening for topic changes on {onoff_feed}")
4849
# Subscribe to all changes on the onoff_feed.
4950
client.subscribe(onoff_feed)
5051

@@ -61,9 +62,7 @@ def message(client, topic, message):
6162

6263

6364
# Initialize cellular data network
64-
network = network.CELLULAR(
65-
fona, (os.getenv("apn"), os.getenv("apn_username"), os.getenv("apn_password"))
66-
)
65+
network = network.CELLULAR(fona, (apn, apn_username, apn_password))
6766

6867
while not network.is_attached:
6968
print("Attaching to network...")
@@ -105,7 +104,7 @@ def message(client, topic, message):
105104
mqtt_client.loop()
106105

107106
# Send a new message
108-
print("Sending photocell value: %d..." % photocell_val)
107+
print(f"Sending photocell value: {photocell_val}...")
109108
mqtt_client.publish(photocell_feed, photocell_val)
110109
print("Sent!")
111110
photocell_val += 1

‎examples/cellular/minimqtt_simpletest_cellular.py

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
22
# SPDX-License-Identifier: MIT
33

4-
import os
54
import time
5+
from os import getenv
66

77
import adafruit_connection_manager
88
import adafruit_fona.adafruit_fona_network as network
@@ -14,12 +14,14 @@
1414

1515
import adafruit_minimqtt.adafruit_minimqtt as MQTT
1616

17-
# Add settings.toml to your filesystem CIRCUITPY_WIFI_SSID and CIRCUITPY_WIFI_PASSWORD keys
18-
# with your GPRS credentials. Add your Adafruit IO username and key as well.
19-
# DO NOT share that file or commit it into Git or other source control.
20-
21-
aio_username = os.getenv("aio_username")
22-
aio_key = os.getenv("aio_key")
17+
# Get FONA details and Adafruit IO keys, ensure these are setup in settings.toml
18+
# (visit io.adafruit.com if you need to create an account, or if you need your Adafruit IO key.)
19+
apn = getenv("apn")
20+
apn_username = getenv("apn_username")
21+
apn_password = getenv("apn_password")
22+
aio_username = getenv("ADAFRUIT_AIO_USERNAME")
23+
aio_key = getenv("ADAFRUIT_AIO_KEY")
24+
broker = getenv("broker", "io.adafruit.com")
2325

2426
# Create a serial connection for the FONA connection
2527
uart = busio.UART(board.TX, board.RX)
@@ -70,9 +72,7 @@ def publish(client, userdata, topic, pid):
7072

7173

7274
# Initialize cellular data network
73-
network = network.CELLULAR(
74-
fona, (os.getenv("apn"), os.getenv("apn_username"), os.getenv("apn_password"))
75-
)
75+
network = network.CELLULAR(fona, (apn, apn_username, apn_password))
7676

7777
while not network.is_attached:
7878
print("Attaching to network...")
@@ -89,9 +89,9 @@ def publish(client, userdata, topic, pid):
8989

9090
# Set up a MiniMQTT Client
9191
client = MQTT.MQTT(
92-
broker=os.getenv("broker"),
93-
username=os.getenv("username"),
94-
password=os.getenv("password"),
92+
broker=broker,
93+
username=aio_username,
94+
password=aio_key,
9595
is_ssl=False,
9696
socket_pool=pool,
9797
ssl_context=ssl_context,
@@ -104,17 +104,17 @@ def publish(client, userdata, topic, pid):
104104
client.on_unsubscribe = unsubscribe
105105
client.on_publish = publish
106106

107-
print("Attempting to connect to %s" % client.broker)
107+
print(f"Attempting to connect to {client.broker}")
108108
client.connect()
109109

110-
print("Subscribing to %s" % mqtt_topic)
110+
print(f"Subscribing to {mqtt_topic}")
111111
client.subscribe(mqtt_topic)
112112

113-
print("Publishing to %s" % mqtt_topic)
113+
print(f"Publishing to {mqtt_topic}")
114114
client.publish(mqtt_topic, "Hello Broker!")
115115

116-
print("Unsubscribing from %s" % mqtt_topic)
116+
print(f"Unsubscribing from {mqtt_topic}")
117117
client.unsubscribe(mqtt_topic)
118118

119-
print("Disconnecting from %s" % client.broker)
119+
print(f"Disconnecting from {client.broker}")
120120
client.disconnect()

‎examples/cpython/minimqtt_adafruitio_cpython.py

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,30 @@
11
# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
22
# SPDX-License-Identifier: MIT
33

4-
import os
54
import socket
65
import ssl
76
import time
7+
from os import getenv
88

99
import adafruit_minimqtt.adafruit_minimqtt as MQTT
1010

11-
### Secrets File Setup ###
11+
### Key Setup ###
1212

13-
# Add settings.toml to your filesystem. Add your Adafruit IO username and key as well.
14-
# DO NOT share that file or commit it into Git or other source control.
13+
# Add your Adafruit IO username and key to your env.
14+
# example:
15+
# export ADAFRUIT_AIO_USERNAME=your-aio-username
16+
# export ADAFRUIT_AIO_KEY=your-aio-key
1517

16-
aio_username = os.getenv("aio_username")
17-
aio_key = os.getenv("aio_key")
18+
aio_username = getenv("ADAFRUIT_AIO_USERNAME")
19+
aio_key = getenv("ADAFRUIT_AIO_KEY")
1820

1921
### Feeds ###
2022

2123
# Setup a feed named 'photocell' for publishing to a feed
22-
photocell_feed = aio_username + "/feeds/photocell"
24+
photocell_feed = f"{aio_username}/feeds/photocell"
2325

2426
# Setup a feed named 'onoff' for subscribing to changes
25-
onoff_feed = aio_username + "/feeds/onoff"
27+
onoff_feed = f"{aio_username}/feeds/onoff"
2628

2729
### Code ###
2830

@@ -31,7 +33,7 @@
3133
def connected(client, userdata, flags, rc):
3234
# This function will be called when the client is connected
3335
# successfully to the broker.
34-
print("Connected to Adafruit IO! Listening for topic changes on %s" % onoff_feed)
36+
print(f"Connected to Adafruit IO! Listening for topic changes on {onoff_feed}")
3537
# Subscribe to all changes on the onoff_feed.
3638
client.subscribe(onoff_feed)
3739

@@ -72,7 +74,7 @@ def message(client, topic, message):
7274
mqtt_client.loop()
7375

7476
# Send a new message
75-
print("Sending photocell value: %d..." % photocell_val)
77+
print(f"Sending photocell value: {photocell_val}...")
7678
mqtt_client.publish(photocell_feed, photocell_val)
7779
print("Sent!")
7880
photocell_val += 1

‎examples/cpython/minimqtt_simpletest_cpython.py

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,22 @@
11
# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
22
# SPDX-License-Identifier: MIT
33

4-
import os
54
import socket
65
import ssl
6+
from os import getenv
77

88
import adafruit_minimqtt.adafruit_minimqtt as MQTT
99

10-
# Add settings.toml to your filesystems. Add your Adafruit IO username and key as well.
11-
# DO NOT share that file or commit it into Git or other source control.
10+
# Add your Adafruit IO username and key to your env.
11+
# (visit io.adafruit.com if you need to create an account, or if you need your Adafruit IO key.)
12+
# example:
13+
# export ADAFRUIT_AIO_USERNAME=your-aio-username
14+
# export ADAFRUIT_AIO_KEY=your-aio-key
15+
# export broker=io.adafruit.com
1216

13-
aio_username = os.getenv("aio_username")
14-
aio_key = os.getenv("aio_key")
17+
aio_username = getenv("ADAFRUIT_AIO_USERNAME")
18+
aio_key = getenv("ADAFRUIT_AIO_KEY")
19+
broker = getenv("broker", "io.adafruit.com")
1520

1621
### Topic Setup ###
1722

@@ -21,7 +26,7 @@
2126

2227
# Adafruit IO-style Topic
2328
# Use this topic if you'd like to connect to io.adafruit.com
24-
# mqtt_topic = aio_username + "/feeds/temperature"
29+
# mqtt_topic = f"{aio_username}/feeds/temperature"
2530

2631

2732
### Code ###
@@ -61,7 +66,7 @@ def message(client, topic, message):
6166

6267
# Set up a MiniMQTT Client
6368
mqtt_client = MQTT.MQTT(
64-
broker=os.getenv("broker"),
69+
broker=broker,
6570
username=aio_username,
6671
password=aio_key,
6772
socket_pool=socket,
@@ -76,17 +81,17 @@ def message(client, topic, message):
7681
mqtt_client.on_publish = publish
7782
mqtt_client.on_message = message
7883

79-
print("Attempting to connect to %s" % mqtt_client.broker)
84+
print(f"Attempting to connect to {mqtt_client.broker}")
8085
mqtt_client.connect()
8186

82-
print("Subscribing to %s" % mqtt_topic)
87+
print(f"Subscribing to {mqtt_topic}")
8388
mqtt_client.subscribe(mqtt_topic)
8489

85-
print("Publishing to %s" % mqtt_topic)
90+
print(f"Publishing to {mqtt_topic}")
8691
mqtt_client.publish(mqtt_topic, "Hello Broker!")
8792

88-
print("Unsubscribing from %s" % mqtt_topic)
93+
print(f"Unsubscribing from {mqtt_topic}")
8994
mqtt_client.unsubscribe(mqtt_topic)
9095

91-
print("Disconnecting from %s" % mqtt_client.broker)
96+
print(f"Disconnecting from {mqtt_client.broker}")
9297
mqtt_client.disconnect()

‎examples/esp32spi/minimqtt_adafruitio_esp32spi.py

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
22
# SPDX-License-Identifier: MIT
33

4-
import os
54
import time
5+
from os import getenv
66

77
import adafruit_connection_manager
88
import board
@@ -13,12 +13,12 @@
1313

1414
import adafruit_minimqtt.adafruit_minimqtt as MQTT
1515

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")
2222

2323
# If you are using a board with pre-defined ESP32 Pins:
2424
esp32_cs = DigitalInOut(board.ESP_CS)
@@ -33,24 +33,24 @@
3333
spi = busio.SPI(board.SCK, board.MOSI, board.MISO)
3434
esp = adafruit_esp32spi.ESP_SPIcontrol(spi, esp32_cs, esp32_ready, esp32_reset)
3535
"""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
3737
"""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)
3939
# Uncomment below for an externally defined RGB LED
4040
# import adafruit_rgbled
4141
# from adafruit_esp32spi import PWMOut
4242
# RED_LED = PWMOut.PWMOut(esp, 26)
4343
# GREEN_LED = PWMOut.PWMOut(esp, 27)
4444
# 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)
4646

4747
### Feeds ###
4848

4949
# 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"
5151

5252
# Setup a feed named 'onoff' for subscribing to changes
53-
onoff_feed = aio_username + "/feeds/onoff"
53+
onoff_feed = f"{aio_username}/feeds/onoff"
5454

5555
### Code ###
5656

@@ -59,7 +59,7 @@
5959
def connected(client, userdata, flags, rc):
6060
# This function will be called when the client is connected
6161
# 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}")
6363
# Subscribe to all changes on the onoff_feed.
6464
client.subscribe(onoff_feed)
6565

@@ -77,7 +77,7 @@ def message(client, topic, message):
7777

7878
# Connect to WiFi
7979
print("Connecting to WiFi...")
80-
esp.connect_AP(os.getenv("CIRCUITPY_WIFI_SSID"), os.getenv("CIRCUITPY_WIFI_PASSWORD"))
80+
esp.connect_AP(ssid, password)
8181
print("Connected!")
8282

8383
pool = adafruit_connection_manager.get_radio_socketpool(esp)
@@ -107,7 +107,7 @@ def message(client, topic, message):
107107
mqtt_client.loop()
108108

109109
# Send a new message
110-
print("Sending photocell value: %d..." % photocell_val)
110+
print(f"Sending photocell value: {photocell_val}...")
111111
mqtt_client.publish(photocell_feed, photocell_val)
112112
print("Sent!")
113113
photocell_val += 1

‎examples/esp32spi/minimqtt_certificate_esp32spi.py

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

4+
from os import getenv
5+
46
import adafruit_connection_manager
57
import board
68
import busio
@@ -10,14 +12,14 @@
1012

1113
import adafruit_minimqtt.adafruit_minimqtt as MQTT
1214

13-
### WiFi ###
15+
# Get WiFi details and MQTT keys, ensure these are setup in settings.toml
16+
ssid = getenv("CIRCUITPY_WIFI_SSID")
17+
password = getenv("CIRCUITPY_WIFI_PASSWORD")
18+
broker = getenv("broker")
19+
username = getenv("username")
20+
paswword = getenv("paswword")
1421

15-
# Get wifi details and more from a secrets.py file
16-
try:
17-
from secrets import secrets
18-
except ImportError:
19-
print("WiFi secrets are kept in secrets.py, please add them there!")
20-
raise
22+
### WiFi ###
2123

2224
# If you are using a board with pre-defined ESP32 Pins:
2325
esp32_cs = DigitalInOut(board.ESP_CS)
@@ -32,17 +34,17 @@
3234
spi = busio.SPI(board.SCK, board.MOSI, board.MISO)
3335
esp = adafruit_esp32spi.ESP_SPIcontrol(spi, esp32_cs, esp32_ready, esp32_reset)
3436
"""Use below for Most Boards"""
35-
status_light = neopixel.NeoPixel(board.NEOPIXEL, 1, brightness=0.2) # Uncomment for Most Boards
37+
status_pixel = neopixel.NeoPixel(board.NEOPIXEL, 1, brightness=0.2) # Uncomment for Most Boards
3638
"""Uncomment below for ItsyBitsy M4"""
37-
# status_light = dotstar.DotStar(board.APA102_SCK, board.APA102_MOSI, 1, brightness=0.2)
39+
# status_pixel = dotstar.DotStar(board.APA102_SCK, board.APA102_MOSI, 1, brightness=0.2)
3840
# Uncomment below for an externally defined RGB LED
3941
# import adafruit_rgbled
4042
# from adafruit_esp32spi import PWMOut
4143
# RED_LED = PWMOut.PWMOut(esp, 26)
4244
# GREEN_LED = PWMOut.PWMOut(esp, 27)
4345
# BLUE_LED = PWMOut.PWMOut(esp, 25)
44-
# status_light = adafruit_rgbled.RGBLED(RED_LED, BLUE_LED, GREEN_LED)
45-
wifi = adafruit_esp32spi_wifimanager.ESPSPI_WiFiManager(esp, secrets, status_light)
46+
# status_pixel = adafruit_rgbled.RGBLED(RED_LED, BLUE_LED, GREEN_LED)
47+
wifi = adafruit_esp32spi_wifimanager.WiFiManager(esp, ssid, password, status_pixel=status_pixel)
4648

4749
### Topic Setup ###
4850

@@ -109,9 +111,9 @@ def publish(client, userdata, topic, pid):
109111

110112
# Set up a MiniMQTT Client
111113
client = MQTT.MQTT(
112-
broker=secrets["broker"],
113-
username=secrets["user"],
114-
password=secrets["pass"],
114+
broker=broker,
115+
username=username,
116+
password=password,
115117
socket_pool=pool,
116118
ssl_context=ssl_context,
117119
)
@@ -123,17 +125,17 @@ def publish(client, userdata, topic, pid):
123125
client.on_unsubscribe = unsubscribe
124126
client.on_publish = publish
125127

126-
print("Attempting to connect to %s" % client.broker)
128+
print(f"Attempting to connect to {client.broker}")
127129
client.connect()
128130

129-
print("Subscribing to %s" % mqtt_topic)
131+
print(f"Subscribing to {mqtt_topic}")
130132
client.subscribe(mqtt_topic)
131133

132-
print("Publishing to %s" % mqtt_topic)
134+
print(f"Publishing to {mqtt_topic}")
133135
client.publish(mqtt_topic, "Hello Broker!")
134136

135-
print("Unsubscribing from %s" % mqtt_topic)
137+
print(f"Unsubscribing from {mqtt_topic}")
136138
client.unsubscribe(mqtt_topic)
137139

138-
print("Disconnecting from %s" % client.broker)
140+
print(f"Disconnecting from {client.broker}")
139141
client.disconnect()

‎examples/esp32spi/minimqtt_pub_sub_blocking_esp32spi.py

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
22
# SPDX-License-Identifier: MIT
33

4-
import os
54
import time
5+
from os import getenv
66

77
import adafruit_connection_manager
88
import board
@@ -13,12 +13,12 @@
1313

1414
import adafruit_minimqtt.adafruit_minimqtt as MQTT
1515

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")
2222

2323
# If you are using a board with pre-defined ESP32 Pins:
2424
esp32_cs = DigitalInOut(board.ESP_CS)
@@ -33,21 +33,21 @@
3333
spi = busio.SPI(board.SCK, board.MOSI, board.MISO)
3434
esp = adafruit_esp32spi.ESP_SPIcontrol(spi, esp32_cs, esp32_ready, esp32_reset)
3535
"""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
3737
"""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)
3939
# Uncomment below for an externally defined RGB LED
4040
# import adafruit_rgbled
4141
# from adafruit_esp32spi import PWMOut
4242
# RED_LED = PWMOut.PWMOut(esp, 26)
4343
# GREEN_LED = PWMOut.PWMOut(esp, 27)
4444
# 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)
4646

4747
### Adafruit IO Setup ###
4848

4949
# Setup a feed named `testfeed` for publishing.
50-
default_topic = aio_username + "/feeds/testfeed"
50+
default_topic = f"{aio_username}/feeds/testfeed"
5151

5252
### Code ###
5353

@@ -56,7 +56,7 @@
5656
def connected(client, userdata, flags, rc):
5757
# This function will be called when the client is connected
5858
# successfully to the broker.
59-
print("Connected to MQTT broker! Listening for topic changes on %s" % default_topic)
59+
print(f"Connected to MQTT broker! Listening for topic changes on {default_topic}")
6060
# Subscribe to all changes on the default_topic feed.
6161
client.subscribe(default_topic)
6262

@@ -77,7 +77,7 @@ def message(client, topic, message):
7777

7878
# Connect to WiFi
7979
print("Connecting to WiFi...")
80-
esp.connect_AP(os.getenv("CIRCUITPY_WIFI_SSID"), os.getenv("CIRCUITPY_WIFI_PASSWORD"))
80+
esp.connect_AP(ssid, password)
8181
print("Connected!")
8282

8383
pool = adafruit_connection_manager.get_radio_socketpool(esp)
@@ -111,7 +111,7 @@ def message(client, topic, message):
111111
print("Failed to get data, retrying\n", e)
112112
esp.reset()
113113
time.sleep(1)
114-
esp.connect_AP(os.getenv("CIRCUITPY_WIFI_SSID"), os.getenv("CIRCUITPY_WIFI_PASSWORD"))
114+
esp.connect_AP(ssid, password)
115115
mqtt_client.reconnect()
116116
continue
117117
time.sleep(1)

‎examples/esp32spi/minimqtt_pub_sub_blocking_topic_callbacks_esp32spi.py

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
22
# SPDX-License-Identifier: MIT
33

4-
import os
54
import time
5+
from os import getenv
66

77
import adafruit_connection_manager
88
import board
@@ -13,14 +13,16 @@
1313

1414
import adafruit_minimqtt.adafruit_minimqtt as MQTT
1515

16-
### WiFi ###
16+
# Get WiFi details and broker 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+
broker = getenv("broker", "io.adafruit.com")
23+
broker_port = int(getenv("broker_port", "8883")) # Port 1883 insecure, 8883 secure
1724

18-
# Get wifi details and more from a secrets.py file
19-
try:
20-
from secrets import secrets
21-
except ImportError:
22-
print("WiFi secrets are kept in secrets.py, please add them there!")
23-
raise
25+
### WiFi ###
2426

2527
# If you are using a board with pre-defined ESP32 Pins:
2628
esp32_cs = DigitalInOut(board.ESP_CS)
@@ -35,17 +37,17 @@
3537
spi = busio.SPI(board.SCK, board.MOSI, board.MISO)
3638
esp = adafruit_esp32spi.ESP_SPIcontrol(spi, esp32_cs, esp32_ready, esp32_reset)
3739
"""Use below for Most Boards"""
38-
status_light = neopixel.NeoPixel(board.NEOPIXEL, 1, brightness=0.2) # Uncomment for Most Boards
40+
status_pixel = neopixel.NeoPixel(board.NEOPIXEL, 1, brightness=0.2) # Uncomment for Most Boards
3941
"""Uncomment below for ItsyBitsy M4"""
40-
# status_light = dotstar.DotStar(board.APA102_SCK, board.APA102_MOSI, 1, brightness=0.2)
42+
# status_pixel = dotstar.DotStar(board.APA102_SCK, board.APA102_MOSI, 1, brightness=0.2)
4143
# Uncomment below for an externally defined RGB LED
4244
# import adafruit_rgbled
4345
# from adafruit_esp32spi import PWMOut
4446
# RED_LED = PWMOut.PWMOut(esp, 26)
4547
# GREEN_LED = PWMOut.PWMOut(esp, 27)
4648
# BLUE_LED = PWMOut.PWMOut(esp, 25)
47-
# status_light = adafruit_rgbled.RGBLED(RED_LED, BLUE_LED, GREEN_LED)
48-
wifi = adafruit_esp32spi_wifimanager.ESPSPI_WiFiManager(esp, secrets, status_light)
49+
# status_pixel = adafruit_rgbled.RGBLED(RED_LED, BLUE_LED, GREEN_LED)
50+
wifi = adafruit_esp32spi_wifimanager.WiFiManager(esp, ssid, password, status_pixel=status_pixel)
4951

5052
### Code ###
5153

@@ -76,7 +78,7 @@ def on_battery_msg(client, topic, message):
7678
# Method called when device/batteryLife has a new value
7779
print(f"Battery level: {message}v")
7880

79-
# client.remove_topic_callback(secrets["aio_username"] + "/feeds/device.batterylevel")
81+
# client.remove_topic_callback(f"{aio_username}/feeds/device.batterylevel")
8082

8183

8284
def on_message(client, topic, message):
@@ -94,8 +96,10 @@ def on_message(client, topic, message):
9496

9597
# Set up a MiniMQTT Client
9698
client = MQTT.MQTT(
97-
broker=os.getenv("broker"),
98-
port=os.getenv("broker_port"),
99+
broker=broker,
100+
port=broker_port,
101+
username=aio_username,
102+
password=aio_key,
99103
socket_pool=pool,
100104
ssl_context=ssl_context,
101105
)
@@ -106,14 +110,14 @@ def on_message(client, topic, message):
106110
client.on_subscribe = subscribe
107111
client.on_unsubscribe = unsubscribe
108112
client.on_message = on_message
109-
client.add_topic_callback(secrets["aio_username"] + "/feeds/device.batterylevel", on_battery_msg)
113+
client.add_topic_callback(f"{aio_username}/feeds/device.batterylevel", on_battery_msg)
110114

111115
# Connect the client to the MQTT broker.
112116
print("Connecting to MQTT broker...")
113117
client.connect()
114118

115119
# Subscribe to all notifications on the device group
116-
client.subscribe(secrets["aio_username"] + "/groups/device", 1)
120+
client.subscribe(f"{aio_username}/groups/device", 1)
117121

118122
# Start a blocking message loop...
119123
# NOTE: NO code below this loop will execute

‎examples/esp32spi/minimqtt_pub_sub_nonblocking_esp32spi.py

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
22
# SPDX-License-Identifier: MIT
33

4-
import os
54
import time
5+
from os import getenv
66

77
import adafruit_connection_manager
88
import board
@@ -13,12 +13,12 @@
1313

1414
import adafruit_minimqtt.adafruit_minimqtt as MQTT
1515

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")
2222

2323
# If you are using a board with pre-defined ESP32 Pins:
2424
esp32_cs = DigitalInOut(board.ESP_CS)
@@ -33,21 +33,21 @@
3333
spi = busio.SPI(board.SCK, board.MOSI, board.MISO)
3434
esp = adafruit_esp32spi.ESP_SPIcontrol(spi, esp32_cs, esp32_ready, esp32_reset)
3535
"""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
3737
"""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)
3939
# Uncomment below for an externally defined RGB LED
4040
# import adafruit_rgbled
4141
# from adafruit_esp32spi import PWMOut
4242
# RED_LED = PWMOut.PWMOut(esp, 26)
4343
# GREEN_LED = PWMOut.PWMOut(esp, 27)
4444
# 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)
4646

4747
### Adafruit IO Setup ###
4848

4949
# Setup a feed named `testfeed` for publishing.
50-
default_topic = aio_username + "/feeds/testfeed"
50+
default_topic = f"{aio_username}/feeds/testfeed"
5151

5252

5353
### Code ###
@@ -76,7 +76,7 @@ def message(client, topic, message):
7676

7777
# Connect to WiFi
7878
print("Connecting to WiFi...")
79-
esp.connect_AP(os.getenv("CIRCUITPY_WIFI_SSID"), os.getenv("CIRCUITPY_WIFI_PASSWORD"))
79+
esp.connect_AP(ssid, password)
8080
print("Connected!")
8181

8282
pool = adafruit_connection_manager.get_radio_socketpool(esp)

‎examples/esp32spi/minimqtt_pub_sub_pyportal_esp32spi.py

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
22
# SPDX-License-Identifier: MIT
33

4-
import os
54
import time
5+
from os import getenv
66

77
import adafruit_connection_manager
88
import adafruit_pyportal
@@ -11,12 +11,11 @@
1111

1212
pyportal = adafruit_pyportal.PyPortal()
1313

14-
# Add settings.toml to your filesystem CIRCUITPY_WIFI_SSID and CIRCUITPY_WIFI_PASSWORD keys
15-
# with your WiFi credentials. Add your Adafruit IO username and key as well.
16-
# DO NOT share that file or commit it into Git or other source control.
17-
18-
aio_username = os.getenv("aio_username")
19-
aio_key = os.getenv("aio_key")
14+
# Get Adafruit IO keys, ensure these are setup in settings.toml
15+
# (visit io.adafruit.com if you need to create an account, or if you need your Adafruit IO key.)
16+
aio_username = getenv("ADAFRUIT_AIO_USERNAME")
17+
aio_key = getenv("ADAFRUIT_AIO_KEY")
18+
broker = getenv("broker", "io.adafruit.com")
2019

2120
# ------------- MQTT Topic Setup ------------- #
2221
mqtt_topic = "test/topic"
@@ -27,7 +26,7 @@
2726
def connected(client, userdata, flags, rc):
2827
# This function will be called when the client is connected
2928
# successfully to the broker.
30-
print("Subscribing to %s" % (mqtt_topic))
29+
print(f"Subscribing to {mqtt_topic}")
3130
client.subscribe(mqtt_topic)
3231

3332

@@ -55,9 +54,9 @@ def message(client, topic, message):
5554

5655
# Set up a MiniMQTT Client
5756
mqtt_client = MQTT.MQTT(
58-
broker=os.getenv("broker"),
59-
username=os.getenv("username"),
60-
password=os.getenv("password"),
57+
broker=broker,
58+
username=aio_username,
59+
password=aio_key,
6160
is_ssl=False,
6261
socket_pool=pool,
6362
ssl_context=ssl_context,
@@ -77,7 +76,7 @@ def message(client, topic, message):
7776
mqtt_client.loop()
7877

7978
# Send a new message
80-
print("Sending photocell value: %d" % photocell_val)
79+
print(f"Sending photocell value: {photocell_val}")
8180
mqtt_client.publish(mqtt_topic, photocell_val)
8281
photocell_val += 1
8382
time.sleep(1)

‎examples/esp32spi/minimqtt_simpletest_esp32spi.py

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

4-
import os
4+
from os import getenv
55

66
import adafruit_connection_manager
77
import board
@@ -11,12 +11,13 @@
1111

1212
import adafruit_minimqtt.adafruit_minimqtt as MQTT
1313

14-
# Add settings.toml to your filesystem CIRCUITPY_WIFI_SSID and CIRCUITPY_WIFI_PASSWORD keys
15-
# with your WiFi credentials. Add your Adafruit IO username and key as well.
16-
# DO NOT share that file or commit it into Git or other source control.
17-
18-
aio_username = os.getenv("aio_username")
19-
aio_key = os.getenv("aio_key")
14+
# Get WiFi details and Adafruit IO keys, ensure these are setup in settings.toml
15+
# (visit io.adafruit.com if you need to create an account, or if you need your Adafruit IO key.)
16+
ssid = getenv("CIRCUITPY_WIFI_SSID")
17+
password = getenv("CIRCUITPY_WIFI_PASSWORD")
18+
aio_username = getenv("ADAFRUIT_AIO_USERNAME")
19+
aio_key = getenv("ADAFRUIT_AIO_KEY")
20+
broker = getenv("broker", "io.adafruit.com")
2021

2122
# If you are using a board with pre-defined ESP32 Pins:
2223
esp32_cs = DigitalInOut(board.ESP_CS)
@@ -34,11 +35,11 @@
3435
print("Connecting to AP...")
3536
while not esp.is_connected:
3637
try:
37-
esp.connect_AP(os.getenv("ssid"), os.getenv("password"))
38+
esp.connect_AP(ssid, password)
3839
except RuntimeError as e:
3940
print("could not connect to AP, retrying: ", e)
4041
continue
41-
print("Connected to", str(esp.ssid, "utf-8"), "\tRSSI:", esp.rssi)
42+
print("Connected to", esp.ap_info.ssid, "\tRSSI:", esp.ap_info.rssi)
4243

4344
### Topic Setup ###
4445

@@ -48,7 +49,7 @@
4849

4950
# Adafruit IO-style Topic
5051
# Use this topic if you'd like to connect to io.adafruit.com
51-
# mqtt_topic = aio_username + '/feeds/temperature'
52+
# mqtt_topic = f"{aio_username}/feeds/temperature"
5253

5354
### Code ###
5455

@@ -91,10 +92,9 @@ def message(client, topic, message):
9192

9293
# Set up a MiniMQTT Client
9394
mqtt_client = MQTT.MQTT(
94-
broker=os.getenv("broker"),
95-
port=os.getenv("port"),
96-
username=os.getenv("username"),
97-
password=os.getenv("password"),
95+
broker=broker,
96+
username=aio_username,
97+
password=aio_key,
9898
socket_pool=pool,
9999
ssl_context=ssl_context,
100100
)
@@ -107,17 +107,17 @@ def message(client, topic, message):
107107
mqtt_client.on_publish = publish
108108
mqtt_client.on_message = message
109109

110-
print("Attempting to connect to %s" % mqtt_client.broker)
110+
print(f"Attempting to connect to {mqtt_client.broker}")
111111
mqtt_client.connect()
112112

113-
print("Subscribing to %s" % mqtt_topic)
113+
print(f"Subscribing to {mqtt_topic}")
114114
mqtt_client.subscribe(mqtt_topic)
115115

116-
print("Publishing to %s" % mqtt_topic)
116+
print(f"Publishing to {mqtt_topic}")
117117
mqtt_client.publish(mqtt_topic, "Hello Broker!")
118118

119-
print("Unsubscribing from %s" % mqtt_topic)
119+
print(f"Unsubscribing from {mqtt_topic}")
120120
mqtt_client.unsubscribe(mqtt_topic)
121121

122-
print("Disconnecting from %s" % mqtt_client.broker)
122+
print(f"Disconnecting from {mqtt_client.broker}")
123123
mqtt_client.disconnect()

‎examples/ethernet/minimqtt_adafruitio_eth.py

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
22
# SPDX-License-Identifier: MIT
33

4-
import os
54
import time
5+
from os import getenv
66

77
import adafruit_connection_manager
88
import board
@@ -12,11 +12,10 @@
1212

1313
import adafruit_minimqtt.adafruit_minimqtt as MQTT
1414

15-
# Add settings.toml to your filesystem. Add your Adafruit IO username and key as well.
16-
# DO NOT share that file or commit it into Git or other source control.
17-
18-
aio_username = os.getenv("aio_username")
19-
aio_key = os.getenv("aio_key")
15+
# Get 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+
aio_username = getenv("ADAFRUIT_AIO_USERNAME")
18+
aio_key = getenv("ADAFRUIT_AIO_KEY")
2019

2120
cs = DigitalInOut(board.D10)
2221
spi_bus = busio.SPI(board.SCK, MOSI=board.MOSI, MISO=board.MISO)
@@ -27,10 +26,10 @@
2726
### Feeds ###
2827

2928
# Setup a feed named 'photocell' for publishing to a feed
30-
photocell_feed = aio_username + "/feeds/photocell"
29+
photocell_feed = f"{aio_username}/feeds/photocell"
3130

3231
# Setup a feed named 'onoff' for subscribing to changes
33-
onoff_feed = aio_username + "/feeds/onoff"
32+
onoff_feed = f"{aio_username}/feeds/onoff"
3433

3534
### Code ###
3635

@@ -39,7 +38,7 @@
3938
def connected(client, userdata, flags, rc):
4039
# This function will be called when the client is connected
4140
# successfully to the broker.
42-
print("Connected to Adafruit IO! Listening for topic changes on %s" % onoff_feed)
41+
print(f"Connected to Adafruit IO! Listening for topic changes on {onoff_feed}")
4342
# Subscribe to all changes on the onoff_feed.
4443
client.subscribe(onoff_feed)
4544

@@ -84,7 +83,7 @@ def message(client, topic, message):
8483
mqtt_client.loop()
8584

8685
# Send a new message
87-
print("Sending photocell value: %d..." % photocell_val)
86+
print(f"Sending photocell value: {photocell_val}...")
8887
mqtt_client.publish(photocell_feed, photocell_val)
8988
print("Sent!")
9089
photocell_val += 1

‎examples/ethernet/minimqtt_simpletest_eth.py

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

4-
import os
4+
from os import getenv
55

66
import adafruit_connection_manager
77
import board
@@ -11,11 +11,11 @@
1111

1212
import adafruit_minimqtt.adafruit_minimqtt as MQTT
1313

14-
# Add settings.toml to your filesystem. Add your Adafruit IO username and key as well.
15-
# DO NOT share that file or commit it into Git or other source control.
16-
17-
aio_username = os.getenv("aio_username")
18-
aio_key = os.getenv("aio_key")
14+
# Get Adafruit IO keys, ensure these are setup in settings.toml
15+
# (visit io.adafruit.com if you need to create an account, or if you need your Adafruit IO key.)
16+
aio_username = getenv("ADAFRUIT_AIO_USERNAME")
17+
aio_key = getenv("ADAFRUIT_AIO_KEY")
18+
broker = getenv("broker", "io.adafruit.com")
1919

2020
cs = DigitalInOut(board.D10)
2121
spi_bus = busio.SPI(board.SCK, MOSI=board.MOSI, MISO=board.MISO)
@@ -70,9 +70,9 @@ def publish(client, userdata, topic, pid):
7070
# Set up a MiniMQTT Client
7171
# NOTE: We'll need to connect insecurely for ethernet configurations.
7272
client = MQTT.MQTT(
73-
broker=os.getenv("broker"),
74-
username=os.getenv("username"),
75-
password=os.getenv("password"),
73+
broker=broker,
74+
username=aio_username,
75+
password=aio_key,
7676
is_ssl=False,
7777
socket_pool=pool,
7878
ssl_context=ssl_context,
@@ -85,17 +85,17 @@ def publish(client, userdata, topic, pid):
8585
client.on_unsubscribe = unsubscribe
8686
client.on_publish = publish
8787

88-
print("Attempting to connect to %s" % client.broker)
88+
print(f"Attempting to connect to {client.broker}")
8989
client.connect()
9090

91-
print("Subscribing to %s" % mqtt_topic)
91+
print(f"Subscribing to {mqtt_topic}")
9292
client.subscribe(mqtt_topic)
9393

94-
print("Publishing to %s" % mqtt_topic)
94+
print(f"Publishing to {mqtt_topic}")
9595
client.publish(mqtt_topic, "Hello Broker!")
9696

97-
print("Unsubscribing from %s" % mqtt_topic)
97+
print(f"Unsubscribing from {mqtt_topic}")
9898
client.unsubscribe(mqtt_topic)
9999

100-
print("Disconnecting from %s" % client.broker)
100+
print(f"Disconnecting from {client.broker}")
101101
client.disconnect()

‎examples/minimqtt_simpletest.py

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

4-
import os
4+
from os import getenv
55

66
import adafruit_connection_manager
77
import board
@@ -11,12 +11,12 @@
1111

1212
import adafruit_minimqtt.adafruit_minimqtt as MQTT
1313

14-
# Add settings.toml to your filesystem CIRCUITPY_WIFI_SSID and CIRCUITPY_WIFI_PASSWORD keys
15-
# with your WiFi credentials. Add your Adafruit IO username and key as well.
16-
# DO NOT share that file or commit it into Git or other source control.
17-
18-
aio_username = os.getenv("aio_username")
19-
aio_key = os.getenv("aio_key")
14+
# Get WiFi details and Adafruit IO keys, ensure these are setup in settings.toml
15+
# (visit io.adafruit.com if you need to create an account, or if you need your Adafruit IO key.)
16+
ssid = getenv("CIRCUITPY_WIFI_SSID")
17+
password = getenv("CIRCUITPY_WIFI_PASSWORD")
18+
aio_username = getenv("ADAFRUIT_AIO_USERNAME")
19+
aio_key = getenv("ADAFRUIT_AIO_KEY")
2020

2121
# If you are using a board with pre-defined ESP32 Pins:
2222
esp32_cs = DigitalInOut(board.ESP_CS)
@@ -34,11 +34,11 @@
3434
print("Connecting to AP...")
3535
while not esp.is_connected:
3636
try:
37-
esp.connect_AP(os.getenv("CIRCUITPY_WIFI_SSID"), os.getenv("CIRCUITPY_WIFI_PASSWORD"))
37+
esp.connect_AP(ssid, password)
3838
except RuntimeError as e:
3939
print("could not connect to AP, retrying: ", e)
4040
continue
41-
print("Connected to", str(esp.ssid, "utf-8"), "\tRSSI:", esp.rssi)
41+
print("Connected to", esp.ap_info.ssid, "\tRSSI:", esp.ap_info.rssi)
4242

4343
### Topic Setup ###
4444

@@ -48,7 +48,7 @@
4848

4949
# Adafruit IO-style Topic
5050
# Use this topic if you'd like to connect to io.adafruit.com
51-
mqtt_topic = aio_username + "/feeds/temperature"
51+
mqtt_topic = f"{aio_username}/feeds/temperature"
5252

5353

5454
### Code ###
@@ -107,17 +107,17 @@ def message(client, topic, message):
107107
mqtt_client.on_publish = publish
108108
mqtt_client.on_message = message
109109

110-
print("Attempting to connect to %s" % mqtt_client.broker)
110+
print(f"Attempting to connect to {mqtt_client.broker}")
111111
mqtt_client.connect()
112112

113-
print("Subscribing to %s" % mqtt_topic)
113+
print(f"Subscribing to {mqtt_topic}")
114114
mqtt_client.subscribe(mqtt_topic)
115115

116-
print("Publishing to %s" % mqtt_topic)
116+
print(f"Publishing to {mqtt_topic}")
117117
mqtt_client.publish(mqtt_topic, "Hello Broker!")
118118

119-
print("Unsubscribing from %s" % mqtt_topic)
119+
print(f"Unsubscribing from {mqtt_topic}")
120120
mqtt_client.unsubscribe(mqtt_topic)
121121

122-
print("Disconnecting from %s" % mqtt_client.broker)
122+
print(f"Disconnecting from {mqtt_client.broker}")
123123
mqtt_client.disconnect()

‎examples/native_networking/minimqtt_adafruitio_native_networking.py

Lines changed: 18 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,25 @@
11
# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
22
# SPDX-License-Identifier: MIT
33

4-
import os
5-
import ssl
64
import time
5+
from os import getenv
76

8-
import socketpool
7+
import adafruit_connection_manager
98
import wifi
109

1110
import adafruit_minimqtt.adafruit_minimqtt as MQTT
1211

13-
# Add settings.toml to your filesystem CIRCUITPY_WIFI_SSID and CIRCUITPY_WIFI_PASSWORD keys
14-
# with your WiFi credentials. DO NOT share that file or commit it into Git or other
15-
# source control.
16-
17-
# Set your Adafruit IO Username, Key and Port in settings.toml
18-
# (visit io.adafruit.com if you need to create an account,
19-
# or if you need your Adafruit IO key.)
20-
aio_username = os.getenv("aio_username")
21-
aio_key = os.getenv("aio_key")
22-
23-
print(f"Connecting to {os.getenv('CIRCUITPY_WIFI_SSID')}")
24-
wifi.radio.connect(os.getenv("CIRCUITPY_WIFI_SSID"), os.getenv("CIRCUITPY_WIFI_PASSWORD"))
25-
print(f"Connected to {os.getenv('CIRCUITPY_WIFI_SSID')}!")
12+
# Get WiFi details and Adafruit IO keys, ensure these are setup in settings.toml
13+
# (visit io.adafruit.com if you need to create an account, or if you need your Adafruit IO key.)
14+
ssid = getenv("CIRCUITPY_WIFI_SSID")
15+
password = getenv("CIRCUITPY_WIFI_PASSWORD")
16+
aio_username = getenv("ADAFRUIT_AIO_USERNAME")
17+
aio_key = getenv("ADAFRUIT_AIO_KEY")
18+
broker = getenv("broker", "io.adafruit.com")
19+
20+
print(f"Connecting to {ssid}")
21+
wifi.radio.connect(ssid, password)
22+
print(f"Connected to {ssid}!")
2623
### Feeds ###
2724

2825
# Setup a feed named 'photocell' for publishing to a feed
@@ -54,22 +51,21 @@ def message(client, topic, message):
5451
print(f"New message on topic {topic}: {message}")
5552

5653

57-
# Create a socket pool
58-
pool = socketpool.SocketPool(wifi.radio)
59-
ssl_context = ssl.create_default_context()
54+
# Create a socket pool and ssl_context
55+
pool = adafruit_connection_manager.get_radio_socketpool(wifi.radio)
56+
ssl_context = adafruit_connection_manager.get_radio_ssl_context(wifi.radio)
6057

6158
# If you need to use certificate/key pair authentication (e.g. X.509), you can load them in the
6259
# ssl context by uncommenting the lines below and adding the following keys to your settings.toml:
6360
# "device_cert_path" - Path to the Device Certificate
6461
# "device_key_path" - Path to the RSA Private Key
6562
# ssl_context.load_cert_chain(
66-
# certfile=os.getenv("device_cert_path"), keyfile=os.getenv("device_key_path")
63+
# certfile=getenv("device_cert_path"), keyfile=getenv("device_key_path")
6764
# )
6865

6966
# Set up a MiniMQTT Client
7067
mqtt_client = MQTT.MQTT(
71-
broker="io.adafruit.com",
72-
port=1883,
68+
broker=broker,
7369
username=aio_username,
7470
password=aio_key,
7571
socket_pool=pool,

‎examples/native_networking/minimqtt_pub_sub_blocking_native_networking.py

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
22
# SPDX-License-Identifier: MIT
33

4-
import os
5-
import ssl
64
import time
5+
from os import getenv
76

8-
import socketpool
7+
import adafruit_connection_manager
98
import wifi
109

1110
import adafruit_minimqtt.adafruit_minimqtt as MQTT
@@ -14,20 +13,21 @@
1413
# with your WiFi credentials. DO NOT share that file or commit it into Git or other
1514
# source control.
1615

17-
# Set your Adafruit IO Username, Key and Port in settings.toml
18-
# (visit io.adafruit.com if you need to create an account,
19-
# or if you need your Adafruit IO key.)
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")
2222

23-
print("Connecting to %s" % os.getenv("CIRCUITPY_WIFI_SSID"))
24-
wifi.radio.connect(os.getenv("CIRCUITPY_WIFI_SSID"), os.getenv("CIRCUITPY_WIFI_PASSWORD"))
25-
print("Connected to %s!" % os.getenv("CIRCUITPY_WIFI_SSID"))
23+
print(f"Connecting to {ssid}")
24+
wifi.radio.connect(ssid, password)
25+
print(f"Connected to {ssid}!")
2626

2727
### Adafruit IO Setup ###
2828

2929
# Setup a feed named `testfeed` for publishing.
30-
default_topic = aio_username + "/feeds/testfeed"
30+
default_topic = f"{aio_username}/feeds/testfeed"
3131

3232

3333
### Code ###
@@ -54,22 +54,21 @@ def message(client, topic, message):
5454
print(f"New message on topic {topic}: {message}")
5555

5656

57-
# Create a socket pool
58-
pool = socketpool.SocketPool(wifi.radio)
59-
ssl_context = ssl.create_default_context()
57+
# Create a socket pool and ssl_context
58+
pool = adafruit_connection_manager.get_radio_socketpool(wifi.radio)
59+
ssl_context = adafruit_connection_manager.get_radio_ssl_context(wifi.radio)
6060

6161
# If you need to use certificate/key pair authentication (e.g. X.509), you can load them in the
6262
# ssl context by uncommenting the lines below and adding the following keys to your settings.toml:
6363
# "device_cert_path" - Path to the Device Certificate
6464
# "device_key_path" - Path to the RSA Private Key
6565
# ssl_context.load_cert_chain(
66-
# certfile=os.getenv("device_cert_path"), keyfile=os.getenv("device_key_path")
66+
# certfile=getenv("device_cert_path"), keyfile=getenv("device_key_path")
6767
# )
6868

6969
# Set up a MiniMQTT Client
7070
mqtt_client = MQTT.MQTT(
7171
broker="io.adafruit.com",
72-
port=1883,
7372
username=aio_username,
7473
password=aio_key,
7574
socket_pool=pool,

‎examples/native_networking/minimqtt_pub_sub_blocking_topic_callbacks_native_networking.py

Lines changed: 18 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,24 @@
11
# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
22
# SPDX-License-Identifier: MIT
33

4-
import os
5-
import ssl
64
import time
5+
from os import getenv
76

8-
import socketpool
7+
import adafruit_connection_manager
98
import wifi
109

1110
import adafruit_minimqtt.adafruit_minimqtt as MQTT
1211

13-
# Add settings.toml to your filesystem CIRCUITPY_WIFI_SSID and CIRCUITPY_WIFI_PASSWORD keys
14-
# with your WiFi credentials. DO NOT share that file or commit it into Git or other
15-
# source control.
12+
# Get WiFi details and Adafruit IO keys, ensure these are setup in settings.toml
13+
# (visit io.adafruit.com if you need to create an account, or if you need your Adafruit IO key.)
14+
ssid = getenv("CIRCUITPY_WIFI_SSID")
15+
password = getenv("CIRCUITPY_WIFI_PASSWORD")
16+
aio_username = getenv("ADAFRUIT_AIO_USERNAME")
17+
aio_key = getenv("ADAFRUIT_AIO_KEY")
1618

17-
# Set your Adafruit IO Username, Key and Port in settings.toml
18-
# (visit io.adafruit.com if you need to create an account,
19-
# or if you need your Adafruit IO key.)
20-
aio_username = os.getenv("aio_username")
21-
aio_key = os.getenv("aio_key")
22-
23-
print("Connecting to %s" % os.getenv("CIRCUITPY_WIFI_SSID"))
24-
wifi.radio.connect(os.getenv("CIRCUITPY_WIFI_SSID"), os.getenv("CIRCUITPY_WIFI_PASSWORD"))
25-
print("Connected to %s!" % os.getenv("CIRCUITPY_WIFI_SSID"))
19+
print(f"Connecting to {ssid}")
20+
wifi.radio.connect(ssid, password)
21+
print(f"Connected to {ssid}!")
2622

2723
### Code ###
2824

@@ -53,30 +49,29 @@ def on_battery_msg(client, topic, message):
5349
# Method called when device/batteryLife has a new value
5450
print(f"Battery level: {message}v")
5551

56-
# client.remove_topic_callback(aio_username + "/feeds/device.batterylevel")
52+
# client.remove_topic_callback(f"{aio_username}/feeds/device.batterylevel")
5753

5854

5955
def on_message(client, topic, message):
6056
# Method callled when a client's subscribed feed has a new value.
6157
print(f"New message on topic {topic}: {message}")
6258

6359

64-
# Create a socket pool
65-
pool = socketpool.SocketPool(wifi.radio)
66-
ssl_context = ssl.create_default_context()
60+
# Create a socket pool and ssl_context
61+
pool = adafruit_connection_manager.get_radio_socketpool(wifi.radio)
62+
ssl_context = adafruit_connection_manager.get_radio_ssl_context(wifi.radio)
6763

6864
# If you need to use certificate/key pair authentication (e.g. X.509), you can load them in the
6965
# ssl context by uncommenting the lines below and adding the following keys to your settings.toml:
7066
# "device_cert_path" - Path to the Device Certificate
7167
# "device_key_path" - Path to the RSA Private Key
7268
# ssl_context.load_cert_chain(
73-
# certfile=os.getenv("device_cert_path"), keyfile=os.getenv("device_key_path")
69+
# certfile=getenv("device_cert_path"), keyfile=getenv("device_key_path")
7470
# )
7571

7672
# Set up a MiniMQTT Client
7773
client = MQTT.MQTT(
7874
broker="io.adafruit.com",
79-
port=1883,
8075
username=aio_username,
8176
password=aio_key,
8277
socket_pool=pool,
@@ -89,14 +84,14 @@ def on_message(client, topic, message):
8984
client.on_subscribe = subscribe
9085
client.on_unsubscribe = unsubscribe
9186
client.on_message = on_message
92-
client.add_topic_callback(aio_username + "/feeds/device.batterylevel", on_battery_msg)
87+
client.add_topic_callback(f"{aio_username}/feeds/device.batterylevel", on_battery_msg)
9388

9489
# Connect the client to the MQTT broker.
9590
print("Connecting to MQTT broker...")
9691
client.connect()
9792

9893
# Subscribe to all notifications on the device group
99-
client.subscribe(aio_username + "/groups/device", 1)
94+
client.subscribe(f"{aio_username}/groups/device", 1)
10095

10196
# Start a blocking message loop...
10297
# NOTE: NO code below this loop will execute

‎ruff.toml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
target-version = "py38"
66
line-length = 100
77

8+
# Enable preview features.
9+
preview = true
10+
811
[lint]
912
select = ["I", "PL", "UP"]
1013

@@ -16,7 +19,7 @@ extend-select = [
1619
"PLC2401", # non-ascii-name
1720
"PLC2801", # unnecessary-dunder-call
1821
"PLC3002", # unnecessary-direct-lambda-call
19-
"E999", # syntax-error
22+
# "E999", # syntax-error
2023
"PLE0101", # return-in-init
2124
"F706", # return-outside-function
2225
"F704", # yield-outside-function
@@ -27,6 +30,7 @@ extend-select = [
2730
"PLE0604", # invalid-all-object
2831
"PLE0605", # invalid-all-format
2932
"PLE0643", # potential-index-error
33+
"F821", # undefined name
3034
"PLE0704", # misplaced-bare-raise
3135
"PLE1141", # dict-iter-missing-items
3236
"PLE1142", # await-outside-async

‎tests/test_loop.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
#
33
# SPDX-License-Identifier: Unlicense
44

5+
# ruff: noqa: PLR6301 no-self-use
6+
57
"""loop() tests"""
68

79
import errno

‎tests/test_port_ssl.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
#
33
# SPDX-License-Identifier: Unlicense
44

5+
# ruff: noqa: PLR6301 no-self-use
6+
57
"""tests that verify the connect behavior w.r.t. port number and TLS"""
68

79
import socket

‎tests/test_subscribe.py

Lines changed: 69 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -29,47 +29,43 @@ def handle_subscribe(client, user_data, topic, qos):
2929
(
3030
"foo/bar",
3131
bytearray([0x90, 0x03, 0x00, 0x01, 0x00]), # SUBACK
32-
bytearray(
33-
[
34-
0x82, # fixed header
35-
0x0C, # remaining length
36-
0x00,
37-
0x01, # message ID
38-
0x00,
39-
0x07, # topic length
40-
0x66, # topic
41-
0x6F,
42-
0x6F,
43-
0x2F,
44-
0x62,
45-
0x61,
46-
0x72,
47-
0x00, # QoS
48-
]
49-
),
32+
bytearray([
33+
0x82, # fixed header
34+
0x0C, # remaining length
35+
0x00,
36+
0x01, # message ID
37+
0x00,
38+
0x07, # topic length
39+
0x66, # topic
40+
0x6F,
41+
0x6F,
42+
0x2F,
43+
0x62,
44+
0x61,
45+
0x72,
46+
0x00, # QoS
47+
]),
5048
),
5149
# same as before but with tuple
5250
(
5351
("foo/bar", 0),
5452
bytearray([0x90, 0x03, 0x00, 0x01, 0x00]), # SUBACK
55-
bytearray(
56-
[
57-
0x82, # fixed header
58-
0x0C, # remaining length
59-
0x00,
60-
0x01, # message ID
61-
0x00,
62-
0x07, # topic length
63-
0x66, # topic
64-
0x6F,
65-
0x6F,
66-
0x2F,
67-
0x62,
68-
0x61,
69-
0x72,
70-
0x00, # QoS
71-
]
72-
),
53+
bytearray([
54+
0x82, # fixed header
55+
0x0C, # remaining length
56+
0x00,
57+
0x01, # message ID
58+
0x00,
59+
0x07, # topic length
60+
0x66, # topic
61+
0x6F,
62+
0x6F,
63+
0x2F,
64+
0x62,
65+
0x61,
66+
0x72,
67+
0x00, # QoS
68+
]),
7369
),
7470
# remaining length is encoded as 2 bytes due to long topic name.
7571
(
@@ -93,47 +89,43 @@ def handle_subscribe(client, user_data, topic, qos):
9389
# SUBSCRIBE responded to by PUBLISH followed by SUBACK
9490
(
9591
"foo/bar",
96-
bytearray(
97-
[
98-
0x30, # PUBLISH
99-
0x0C,
100-
0x00,
101-
0x07,
102-
0x66,
103-
0x6F,
104-
0x6F,
105-
0x2F,
106-
0x62,
107-
0x61,
108-
0x72,
109-
0x66,
110-
0x6F,
111-
0x6F,
112-
0x90, # SUBACK
113-
0x03,
114-
0x00,
115-
0x01,
116-
0x00,
117-
]
118-
),
119-
bytearray(
120-
[
121-
0x82, # fixed header
122-
0x0C, # remaining length
123-
0x00,
124-
0x01, # message ID
125-
0x00,
126-
0x07, # topic length
127-
0x66, # topic
128-
0x6F,
129-
0x6F,
130-
0x2F,
131-
0x62,
132-
0x61,
133-
0x72,
134-
0x00, # QoS
135-
]
136-
),
92+
bytearray([
93+
0x30, # PUBLISH
94+
0x0C,
95+
0x00,
96+
0x07,
97+
0x66,
98+
0x6F,
99+
0x6F,
100+
0x2F,
101+
0x62,
102+
0x61,
103+
0x72,
104+
0x66,
105+
0x6F,
106+
0x6F,
107+
0x90, # SUBACK
108+
0x03,
109+
0x00,
110+
0x01,
111+
0x00,
112+
]),
113+
bytearray([
114+
0x82, # fixed header
115+
0x0C, # remaining length
116+
0x00,
117+
0x01, # message ID
118+
0x00,
119+
0x07, # topic length
120+
0x66, # topic
121+
0x6F,
122+
0x6F,
123+
0x2F,
124+
0x62,
125+
0x61,
126+
0x72,
127+
0x00, # QoS
128+
]),
137129
),
138130
# use list of topics for more coverage. If the range was (1, 10000), that would be
139131
# long enough to use 3 bytes for remaining length, however that would make the test

‎tests/test_unsubscribe.py

Lines changed: 50 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -32,23 +32,21 @@ def handle_unsubscribe(client, user_data, topic, pid):
3232
(
3333
"foo/bar",
3434
bytearray([0xB0, 0x02, 0x00, 0x01]),
35-
bytearray(
36-
[
37-
0xA2, # fixed header
38-
0x0B, # remaining length
39-
0x00, # message ID
40-
0x01,
41-
0x00, # topic length
42-
0x07,
43-
0x66, # topic
44-
0x6F,
45-
0x6F,
46-
0x2F,
47-
0x62,
48-
0x61,
49-
0x72,
50-
]
51-
),
35+
bytearray([
36+
0xA2, # fixed header
37+
0x0B, # remaining length
38+
0x00, # message ID
39+
0x01,
40+
0x00, # topic length
41+
0x07,
42+
0x66, # topic
43+
0x6F,
44+
0x6F,
45+
0x2F,
46+
0x62,
47+
0x61,
48+
0x72,
49+
]),
5250
),
5351
# remaining length is encoded as 2 bytes due to long topic name.
5452
(
@@ -71,45 +69,41 @@ def handle_unsubscribe(client, user_data, topic, pid):
7169
# UNSUBSCRIBE responded to by PUBLISH followed by UNSUBACK
7270
(
7371
"foo/bar",
74-
bytearray(
75-
[
76-
0x30, # PUBLISH
77-
0x0C,
78-
0x00,
79-
0x07,
80-
0x66,
81-
0x6F,
82-
0x6F,
83-
0x2F,
84-
0x62,
85-
0x61,
86-
0x72,
87-
0x66,
88-
0x6F,
89-
0x6F,
90-
0xB0, # UNSUBACK
91-
0x02,
92-
0x00,
93-
0x01,
94-
]
95-
),
96-
bytearray(
97-
[
98-
0xA2, # fixed header
99-
0x0B, # remaining length
100-
0x00,
101-
0x01, # message ID
102-
0x00,
103-
0x07, # topic length
104-
0x66, # topic
105-
0x6F,
106-
0x6F,
107-
0x2F,
108-
0x62,
109-
0x61,
110-
0x72,
111-
]
112-
),
72+
bytearray([
73+
0x30, # PUBLISH
74+
0x0C,
75+
0x00,
76+
0x07,
77+
0x66,
78+
0x6F,
79+
0x6F,
80+
0x2F,
81+
0x62,
82+
0x61,
83+
0x72,
84+
0x66,
85+
0x6F,
86+
0x6F,
87+
0xB0, # UNSUBACK
88+
0x02,
89+
0x00,
90+
0x01,
91+
]),
92+
bytearray([
93+
0xA2, # fixed header
94+
0x0B, # remaining length
95+
0x00,
96+
0x01, # message ID
97+
0x00,
98+
0x07, # topic length
99+
0x66, # topic
100+
0x6F,
101+
0x6F,
102+
0x2F,
103+
0x62,
104+
0x61,
105+
0x72,
106+
]),
113107
),
114108
# use list of topics for more coverage. If the range was (1, 10000), that would be
115109
# long enough to use 3 bytes for remaining length, however that would make the test

0 commit comments

Comments
 (0)
Please sign in to comment.