From 67f971fe3f247ac222c72593a97960145a14a4af Mon Sep 17 00:00:00 2001 From: Justin Myers Date: Mon, 24 Feb 2025 13:55:32 -0800 Subject: [PATCH 01/10] Remove secrets usage --- .../cellular/minimqtt_adafruitio_cellular.py | 23 ++++++----- .../cellular/minimqtt_simpletest_cellular.py | 35 +++++++++-------- .../cpython/minimqtt_adafruitio_cpython.py | 18 +++++---- .../cpython/minimqtt_simpletest_cpython.py | 24 ++++++------ .../esp32spi/minimqtt_adafruitio_esp32spi.py | 26 ++++++------- .../esp32spi/minimqtt_certificate_esp32spi.py | 38 +++++++++---------- .../minimqtt_pub_sub_blocking_esp32spi.py | 26 ++++++------- ...b_sub_blocking_topic_callbacks_esp32spi.py | 33 ++++++++-------- .../minimqtt_pub_sub_nonblocking_esp32spi.py | 22 +++++------ .../minimqtt_pub_sub_pyportal_esp32spi.py | 22 +++++------ .../esp32spi/minimqtt_simpletest_esp32spi.py | 33 ++++++++-------- examples/ethernet/minimqtt_adafruitio_eth.py | 15 ++++---- examples/ethernet/minimqtt_simpletest_eth.py | 27 +++++++------ examples/minimqtt_simpletest.py | 26 ++++++------- .../minimqtt_adafruitio_native_networking.py | 34 ++++++++--------- ...mqtt_pub_sub_blocking_native_networking.py | 30 +++++++-------- ...cking_topic_callbacks_native_networking.py | 34 ++++++++--------- 17 files changed, 227 insertions(+), 239 deletions(-) diff --git a/examples/cellular/minimqtt_adafruitio_cellular.py b/examples/cellular/minimqtt_adafruitio_cellular.py index 686062c1..6c63de9f 100755 --- a/examples/cellular/minimqtt_adafruitio_cellular.py +++ b/examples/cellular/minimqtt_adafruitio_cellular.py @@ -1,8 +1,8 @@ # SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries # SPDX-License-Identifier: MIT -import os import time +from os import getenv import adafruit_connection_manager import adafruit_fona.adafruit_fona_network as network @@ -14,12 +14,13 @@ import adafruit_minimqtt.adafruit_minimqtt as MQTT -# Add settings.toml to your filesystem CIRCUITPY_WIFI_SSID and CIRCUITPY_WIFI_PASSWORD keys -# with your GPRS credentials. Add your Adafruit IO username and key as well. -# DO NOT share that file or commit it into Git or other source control. - -aio_username = os.getenv("aio_username") -aio_key = os.getenv("aio_key") +# Get FONA details and Adafruit IO keys, ensure these are setup in settings.toml +# (visit io.adafruit.com if you need to create an account, or if you need your Adafruit IO key.) +apn = getenv("apn") +apn_username = getenv("apn_username") +apn_password = getenv("apn_password") +aio_username = getenv("ADAFRUIT_AIO_USERNAME") +aio_key = getenv("ADAFRUIT_AIO_KEY") ### Cellular ### @@ -44,7 +45,7 @@ def connected(client, userdata, flags, rc): # This function will be called when the client is connected # successfully to the broker. - print("Connected to Adafruit IO! Listening for topic changes on %s" % onoff_feed) + print(f"Connected to Adafruit IO! Listening for topic changes on {onoff_feed}") # Subscribe to all changes on the onoff_feed. client.subscribe(onoff_feed) @@ -61,9 +62,7 @@ def message(client, topic, message): # Initialize cellular data network -network = network.CELLULAR( - fona, (os.getenv("apn"), os.getenv("apn_username"), os.getenv("apn_password")) -) +network = network.CELLULAR(fona, (apn, apn_username, apn_password)) while not network.is_attached: print("Attaching to network...") @@ -105,7 +104,7 @@ def message(client, topic, message): mqtt_client.loop() # Send a new message - print("Sending photocell value: %d..." % photocell_val) + print(f"Sending photocell value: {photocell_val}...") mqtt_client.publish(photocell_feed, photocell_val) print("Sent!") photocell_val += 1 diff --git a/examples/cellular/minimqtt_simpletest_cellular.py b/examples/cellular/minimqtt_simpletest_cellular.py index c24ef907..35fc90cb 100644 --- a/examples/cellular/minimqtt_simpletest_cellular.py +++ b/examples/cellular/minimqtt_simpletest_cellular.py @@ -1,8 +1,8 @@ # SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries # SPDX-License-Identifier: MIT -import os import time +from os import getenv import adafruit_connection_manager import adafruit_fona.adafruit_fona_network as network @@ -14,12 +14,13 @@ import adafruit_minimqtt.adafruit_minimqtt as MQTT -# Add settings.toml to your filesystem CIRCUITPY_WIFI_SSID and CIRCUITPY_WIFI_PASSWORD keys -# with your GPRS credentials. Add your Adafruit IO username and key as well. -# DO NOT share that file or commit it into Git or other source control. - -aio_username = os.getenv("aio_username") -aio_key = os.getenv("aio_key") +# Get FONA details and Adafruit IO keys, ensure these are setup in settings.toml +# (visit io.adafruit.com if you need to create an account, or if you need your Adafruit IO key.) +apn = getenv("apn") +apn_username = getenv("apn_username") +apn_password = getenv("apn_password") +aio_username = getenv("ADAFRUIT_AIO_USERNAME") +aio_key = getenv("ADAFRUIT_AIO_KEY") # Create a serial connection for the FONA connection uart = busio.UART(board.TX, board.RX) @@ -70,9 +71,7 @@ def publish(client, userdata, topic, pid): # Initialize cellular data network -network = network.CELLULAR( - fona, (os.getenv("apn"), os.getenv("apn_username"), os.getenv("apn_password")) -) +network = network.CELLULAR(fona, (apn, apn_username, apn_password)) while not network.is_attached: print("Attaching to network...") @@ -89,9 +88,9 @@ def publish(client, userdata, topic, pid): # Set up a MiniMQTT Client client = MQTT.MQTT( - broker=os.getenv("broker"), - username=os.getenv("username"), - password=os.getenv("password"), + broker="io.adafruit.com", + username=aio_username, + password=aio_key, is_ssl=False, socket_pool=pool, ssl_context=ssl_context, @@ -104,17 +103,17 @@ def publish(client, userdata, topic, pid): client.on_unsubscribe = unsubscribe client.on_publish = publish -print("Attempting to connect to %s" % client.broker) +print(f"Attempting to connect to {client.broker}") client.connect() -print("Subscribing to %s" % mqtt_topic) +print(f"Subscribing to {mqtt_topic}") client.subscribe(mqtt_topic) -print("Publishing to %s" % mqtt_topic) +print(f"Publishing to {mqtt_topic}") client.publish(mqtt_topic, "Hello Broker!") -print("Unsubscribing from %s" % mqtt_topic) +print(f"Unsubscribing from {mqtt_topic}") client.unsubscribe(mqtt_topic) -print("Disconnecting from %s" % client.broker) +print(f"Disconnecting from {client.broker}") client.disconnect() diff --git a/examples/cpython/minimqtt_adafruitio_cpython.py b/examples/cpython/minimqtt_adafruitio_cpython.py index 1f350c46..60d94c36 100644 --- a/examples/cpython/minimqtt_adafruitio_cpython.py +++ b/examples/cpython/minimqtt_adafruitio_cpython.py @@ -1,20 +1,22 @@ # SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries # SPDX-License-Identifier: MIT -import os import socket import ssl import time +from os import getenv import adafruit_minimqtt.adafruit_minimqtt as MQTT -### Secrets File Setup ### +### Key Setup ### -# Add settings.toml to your filesystem. Add your Adafruit IO username and key as well. -# DO NOT share that file or commit it into Git or other source control. +# Add your Adafruit IO username and key to your env. +# example: +# export ADAFRUIT_AIO_USERNAME=your-aio-username +# export ADAFRUIT_AIO_KEY=your-aio-key -aio_username = os.getenv("aio_username") -aio_key = os.getenv("aio_key") +aio_username = getenv("ADAFRUIT_AIO_USERNAME") +aio_key = getenv("ADAFRUIT_AIO_KEY") ### Feeds ### @@ -31,7 +33,7 @@ def connected(client, userdata, flags, rc): # This function will be called when the client is connected # successfully to the broker. - print("Connected to Adafruit IO! Listening for topic changes on %s" % onoff_feed) + print(f"Connected to Adafruit IO! Listening for topic changes on {onoff_feed}") # Subscribe to all changes on the onoff_feed. client.subscribe(onoff_feed) @@ -72,7 +74,7 @@ def message(client, topic, message): mqtt_client.loop() # Send a new message - print("Sending photocell value: %d..." % photocell_val) + print(f"Sending photocell value: {photocell_val}...") mqtt_client.publish(photocell_feed, photocell_val) print("Sent!") photocell_val += 1 diff --git a/examples/cpython/minimqtt_simpletest_cpython.py b/examples/cpython/minimqtt_simpletest_cpython.py index a435b6a3..ef875534 100644 --- a/examples/cpython/minimqtt_simpletest_cpython.py +++ b/examples/cpython/minimqtt_simpletest_cpython.py @@ -1,17 +1,19 @@ # SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries # SPDX-License-Identifier: MIT -import os import socket import ssl +from os import getenv import adafruit_minimqtt.adafruit_minimqtt as MQTT -# Add settings.toml to your filesystems. Add your Adafruit IO username and key as well. -# DO NOT share that file or commit it into Git or other source control. +# Add your Adafruit IO username and key to your env. +# example: +# export ADAFRUIT_AIO_USERNAME=your-aio-username +# export ADAFRUIT_AIO_KEY=your-aio-key -aio_username = os.getenv("aio_username") -aio_key = os.getenv("aio_key") +aio_username = getenv("ADAFRUIT_AIO_USERNAME") +aio_key = getenv("ADAFRUIT_AIO_KEY") ### Topic Setup ### @@ -61,7 +63,7 @@ def message(client, topic, message): # Set up a MiniMQTT Client mqtt_client = MQTT.MQTT( - broker=os.getenv("broker"), + broker="io.adafruit.com", username=aio_username, password=aio_key, socket_pool=socket, @@ -76,17 +78,17 @@ def message(client, topic, message): mqtt_client.on_publish = publish mqtt_client.on_message = message -print("Attempting to connect to %s" % mqtt_client.broker) +print(f"Attempting to connect to {mqtt_client.broker}") mqtt_client.connect() -print("Subscribing to %s" % mqtt_topic) +print(f"Subscribing to {mqtt_topic}") mqtt_client.subscribe(mqtt_topic) -print("Publishing to %s" % mqtt_topic) +print(f"Publishing to {mqtt_topic}") mqtt_client.publish(mqtt_topic, "Hello Broker!") -print("Unsubscribing from %s" % mqtt_topic) +print(f"Unsubscribing from {mqtt_topic}") mqtt_client.unsubscribe(mqtt_topic) -print("Disconnecting from %s" % mqtt_client.broker) +print(f"Disconnecting from {mqtt_client.broker}") mqtt_client.disconnect() diff --git a/examples/esp32spi/minimqtt_adafruitio_esp32spi.py b/examples/esp32spi/minimqtt_adafruitio_esp32spi.py index 7266beb8..5c9e6856 100644 --- a/examples/esp32spi/minimqtt_adafruitio_esp32spi.py +++ b/examples/esp32spi/minimqtt_adafruitio_esp32spi.py @@ -1,8 +1,8 @@ # SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries # SPDX-License-Identifier: MIT -import os import time +from os import getenv import adafruit_connection_manager import board @@ -13,12 +13,12 @@ import adafruit_minimqtt.adafruit_minimqtt as MQTT -# Add settings.toml to your filesystem CIRCUITPY_WIFI_SSID and CIRCUITPY_WIFI_PASSWORD keys -# with your WiFi credentials. Add your Adafruit IO username and key as well. -# DO NOT share that file or commit it into Git or other source control. - -aio_username = os.getenv("aio_username") -aio_key = os.getenv("aio_key") +# Get WiFi details and Adafruit IO keys, ensure these are setup in settings.toml +# (visit io.adafruit.com if you need to create an account, or if you need your Adafruit IO key.) +ssid = getenv("CIRCUITPY_WIFI_SSID") +password = getenv("CIRCUITPY_WIFI_PASSWORD") +aio_username = getenv("ADAFRUIT_AIO_USERNAME") +aio_key = getenv("ADAFRUIT_AIO_KEY") # If you are using a board with pre-defined ESP32 Pins: esp32_cs = DigitalInOut(board.ESP_CS) @@ -33,16 +33,16 @@ spi = busio.SPI(board.SCK, board.MOSI, board.MISO) esp = adafruit_esp32spi.ESP_SPIcontrol(spi, esp32_cs, esp32_ready, esp32_reset) """Use below for Most Boards""" -status_light = neopixel.NeoPixel(board.NEOPIXEL, 1, brightness=0.2) # Uncomment for Most Boards +status_pixel = neopixel.NeoPixel(board.NEOPIXEL, 1, brightness=0.2) # Uncomment for Most Boards """Uncomment below for ItsyBitsy M4""" -# status_light = dotstar.DotStar(board.APA102_SCK, board.APA102_MOSI, 1, brightness=0.2) +# status_pixel = dotstar.DotStar(board.APA102_SCK, board.APA102_MOSI, 1, brightness=0.2) # Uncomment below for an externally defined RGB LED # import adafruit_rgbled # from adafruit_esp32spi import PWMOut # RED_LED = PWMOut.PWMOut(esp, 26) # GREEN_LED = PWMOut.PWMOut(esp, 27) # BLUE_LED = PWMOut.PWMOut(esp, 25) -# status_light = adafruit_rgbled.RGBLED(RED_LED, BLUE_LED, GREEN_LED) +# status_pixel = adafruit_rgbled.RGBLED(RED_LED, BLUE_LED, GREEN_LED) ### Feeds ### @@ -59,7 +59,7 @@ def connected(client, userdata, flags, rc): # This function will be called when the client is connected # successfully to the broker. - print("Connected to Adafruit IO! Listening for topic changes on %s" % onoff_feed) + print(f"Connected to Adafruit IO! Listening for topic changes on {onoff_feed}") # Subscribe to all changes on the onoff_feed. client.subscribe(onoff_feed) @@ -77,7 +77,7 @@ def message(client, topic, message): # Connect to WiFi print("Connecting to WiFi...") -esp.connect_AP(os.getenv("CIRCUITPY_WIFI_SSID"), os.getenv("CIRCUITPY_WIFI_PASSWORD")) +esp.connect_AP(ssid, password) print("Connected!") pool = adafruit_connection_manager.get_radio_socketpool(esp) @@ -107,7 +107,7 @@ def message(client, topic, message): mqtt_client.loop() # Send a new message - print("Sending photocell value: %d..." % photocell_val) + print(f"Sending photocell value: {photocell_val}...") mqtt_client.publish(photocell_feed, photocell_val) print("Sent!") photocell_val += 1 diff --git a/examples/esp32spi/minimqtt_certificate_esp32spi.py b/examples/esp32spi/minimqtt_certificate_esp32spi.py index 66c397c9..eade1211 100644 --- a/examples/esp32spi/minimqtt_certificate_esp32spi.py +++ b/examples/esp32spi/minimqtt_certificate_esp32spi.py @@ -10,14 +10,14 @@ import adafruit_minimqtt.adafruit_minimqtt as MQTT -### WiFi ### +# Get WiFi details and MQTT keys, ensure these are setup in settings.toml +ssid = getenv("CIRCUITPY_WIFI_SSID") +password = getenv("CIRCUITPY_WIFI_PASSWORD") +broker = getenv("broker") +username = getenv("username") +paswword = getenv("paswword") -# Get wifi details and more from a secrets.py file -try: - from secrets import secrets -except ImportError: - print("WiFi secrets are kept in secrets.py, please add them there!") - raise +### WiFi ### # If you are using a board with pre-defined ESP32 Pins: esp32_cs = DigitalInOut(board.ESP_CS) @@ -32,17 +32,17 @@ spi = busio.SPI(board.SCK, board.MOSI, board.MISO) esp = adafruit_esp32spi.ESP_SPIcontrol(spi, esp32_cs, esp32_ready, esp32_reset) """Use below for Most Boards""" -status_light = neopixel.NeoPixel(board.NEOPIXEL, 1, brightness=0.2) # Uncomment for Most Boards +status_pixel = neopixel.NeoPixel(board.NEOPIXEL, 1, brightness=0.2) # Uncomment for Most Boards """Uncomment below for ItsyBitsy M4""" -# status_light = dotstar.DotStar(board.APA102_SCK, board.APA102_MOSI, 1, brightness=0.2) +# status_pixel = dotstar.DotStar(board.APA102_SCK, board.APA102_MOSI, 1, brightness=0.2) # Uncomment below for an externally defined RGB LED # import adafruit_rgbled # from adafruit_esp32spi import PWMOut # RED_LED = PWMOut.PWMOut(esp, 26) # GREEN_LED = PWMOut.PWMOut(esp, 27) # BLUE_LED = PWMOut.PWMOut(esp, 25) -# status_light = adafruit_rgbled.RGBLED(RED_LED, BLUE_LED, GREEN_LED) -wifi = adafruit_esp32spi_wifimanager.ESPSPI_WiFiManager(esp, secrets, status_light) +# status_pixel = adafruit_rgbled.RGBLED(RED_LED, BLUE_LED, GREEN_LED) +wifi = adafruit_esp32spi_wifimanager.WiFiManager(esp, ssid, password, status_pixel=status_pixel) ### Topic Setup ### @@ -109,9 +109,9 @@ def publish(client, userdata, topic, pid): # Set up a MiniMQTT Client client = MQTT.MQTT( - broker=secrets["broker"], - username=secrets["user"], - password=secrets["pass"], + broker=broker, + username=username, + password=password, socket_pool=pool, ssl_context=ssl_context, ) @@ -123,17 +123,17 @@ def publish(client, userdata, topic, pid): client.on_unsubscribe = unsubscribe client.on_publish = publish -print("Attempting to connect to %s" % client.broker) +print(f"Attempting to connect to {client.broker}") client.connect() -print("Subscribing to %s" % mqtt_topic) +print(f"Subscribing to {mqtt_topic}") client.subscribe(mqtt_topic) -print("Publishing to %s" % mqtt_topic) +print(f"Publishing to {mqtt_topic}") client.publish(mqtt_topic, "Hello Broker!") -print("Unsubscribing from %s" % mqtt_topic) +print(f"Unsubscribing from {mqtt_topic}") client.unsubscribe(mqtt_topic) -print("Disconnecting from %s" % client.broker) +print(f"Disconnecting from {client.broker}") client.disconnect() diff --git a/examples/esp32spi/minimqtt_pub_sub_blocking_esp32spi.py b/examples/esp32spi/minimqtt_pub_sub_blocking_esp32spi.py index cb8bbb46..139fc844 100644 --- a/examples/esp32spi/minimqtt_pub_sub_blocking_esp32spi.py +++ b/examples/esp32spi/minimqtt_pub_sub_blocking_esp32spi.py @@ -1,8 +1,8 @@ # SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries # SPDX-License-Identifier: MIT -import os import time +from os import getenv import adafruit_connection_manager import board @@ -13,12 +13,12 @@ import adafruit_minimqtt.adafruit_minimqtt as MQTT -# Add settings.toml to your filesystem CIRCUITPY_WIFI_SSID and CIRCUITPY_WIFI_PASSWORD keys -# with your WiFi credentials. Add your Adafruit IO username and key as well. -# DO NOT share that file or commit it into Git or other source control. - -aio_username = os.getenv("aio_username") -aio_key = os.getenv("aio_key") +# Get WiFi details and Adafruit IO keys, ensure these are setup in settings.toml +# (visit io.adafruit.com if you need to create an account, or if you need your Adafruit IO key.) +ssid = getenv("CIRCUITPY_WIFI_SSID") +password = getenv("CIRCUITPY_WIFI_PASSWORD") +aio_username = getenv("ADAFRUIT_AIO_USERNAME") +aio_key = getenv("ADAFRUIT_AIO_KEY") # If you are using a board with pre-defined ESP32 Pins: esp32_cs = DigitalInOut(board.ESP_CS) @@ -33,16 +33,16 @@ spi = busio.SPI(board.SCK, board.MOSI, board.MISO) esp = adafruit_esp32spi.ESP_SPIcontrol(spi, esp32_cs, esp32_ready, esp32_reset) """Use below for Most Boards""" -status_light = neopixel.NeoPixel(board.NEOPIXEL, 1, brightness=0.2) # Uncomment for Most Boards +status_pixel = neopixel.NeoPixel(board.NEOPIXEL, 1, brightness=0.2) # Uncomment for Most Boards """Uncomment below for ItsyBitsy M4""" -# status_light = dotstar.DotStar(board.APA102_SCK, board.APA102_MOSI, 1, brightness=0.2) +# status_pixel = dotstar.DotStar(board.APA102_SCK, board.APA102_MOSI, 1, brightness=0.2) # Uncomment below for an externally defined RGB LED # import adafruit_rgbled # from adafruit_esp32spi import PWMOut # RED_LED = PWMOut.PWMOut(esp, 26) # GREEN_LED = PWMOut.PWMOut(esp, 27) # BLUE_LED = PWMOut.PWMOut(esp, 25) -# status_light = adafruit_rgbled.RGBLED(RED_LED, BLUE_LED, GREEN_LED) +# status_pixel = adafruit_rgbled.RGBLED(RED_LED, BLUE_LED, GREEN_LED) ### Adafruit IO Setup ### @@ -56,7 +56,7 @@ def connected(client, userdata, flags, rc): # This function will be called when the client is connected # successfully to the broker. - print("Connected to MQTT broker! Listening for topic changes on %s" % default_topic) + print(f"Connected to MQTT broker! Listening for topic changes on {default_topic}") # Subscribe to all changes on the default_topic feed. client.subscribe(default_topic) @@ -77,7 +77,7 @@ def message(client, topic, message): # Connect to WiFi print("Connecting to WiFi...") -esp.connect_AP(os.getenv("CIRCUITPY_WIFI_SSID"), os.getenv("CIRCUITPY_WIFI_PASSWORD")) +esp.connect_AP(ssid, password) print("Connected!") pool = adafruit_connection_manager.get_radio_socketpool(esp) @@ -111,7 +111,7 @@ def message(client, topic, message): print("Failed to get data, retrying\n", e) esp.reset() time.sleep(1) - esp.connect_AP(os.getenv("CIRCUITPY_WIFI_SSID"), os.getenv("CIRCUITPY_WIFI_PASSWORD")) + esp.connect_AP(ssid, password) mqtt_client.reconnect() continue time.sleep(1) diff --git a/examples/esp32spi/minimqtt_pub_sub_blocking_topic_callbacks_esp32spi.py b/examples/esp32spi/minimqtt_pub_sub_blocking_topic_callbacks_esp32spi.py index 1b8d8f0d..f0b175d1 100644 --- a/examples/esp32spi/minimqtt_pub_sub_blocking_topic_callbacks_esp32spi.py +++ b/examples/esp32spi/minimqtt_pub_sub_blocking_topic_callbacks_esp32spi.py @@ -1,8 +1,8 @@ # SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries # SPDX-License-Identifier: MIT -import os import time +from os import getenv import adafruit_connection_manager import board @@ -13,14 +13,13 @@ import adafruit_minimqtt.adafruit_minimqtt as MQTT -### WiFi ### +# Get WiFi details and broker keys, ensure these are setup in settings.toml +ssid = getenv("CIRCUITPY_WIFI_SSID") +password = getenv("CIRCUITPY_WIFI_PASSWORD") +broker = getenv("broker") +broker_port = getenv("broker_port") -# Get wifi details and more from a secrets.py file -try: - from secrets import secrets -except ImportError: - print("WiFi secrets are kept in secrets.py, please add them there!") - raise +### WiFi ### # If you are using a board with pre-defined ESP32 Pins: esp32_cs = DigitalInOut(board.ESP_CS) @@ -35,17 +34,17 @@ spi = busio.SPI(board.SCK, board.MOSI, board.MISO) esp = adafruit_esp32spi.ESP_SPIcontrol(spi, esp32_cs, esp32_ready, esp32_reset) """Use below for Most Boards""" -status_light = neopixel.NeoPixel(board.NEOPIXEL, 1, brightness=0.2) # Uncomment for Most Boards +status_pixel = neopixel.NeoPixel(board.NEOPIXEL, 1, brightness=0.2) # Uncomment for Most Boards """Uncomment below for ItsyBitsy M4""" -# status_light = dotstar.DotStar(board.APA102_SCK, board.APA102_MOSI, 1, brightness=0.2) +# status_pixel = dotstar.DotStar(board.APA102_SCK, board.APA102_MOSI, 1, brightness=0.2) # Uncomment below for an externally defined RGB LED # import adafruit_rgbled # from adafruit_esp32spi import PWMOut # RED_LED = PWMOut.PWMOut(esp, 26) # GREEN_LED = PWMOut.PWMOut(esp, 27) # BLUE_LED = PWMOut.PWMOut(esp, 25) -# status_light = adafruit_rgbled.RGBLED(RED_LED, BLUE_LED, GREEN_LED) -wifi = adafruit_esp32spi_wifimanager.ESPSPI_WiFiManager(esp, secrets, status_light) +# status_pixel = adafruit_rgbled.RGBLED(RED_LED, BLUE_LED, GREEN_LED) +wifi = adafruit_esp32spi_wifimanager.WiFiManager(esp, ssid, password, status_pixel=status_pixel) ### Code ### @@ -76,7 +75,7 @@ def on_battery_msg(client, topic, message): # Method called when device/batteryLife has a new value print(f"Battery level: {message}v") - # client.remove_topic_callback(secrets["aio_username"] + "/feeds/device.batterylevel") + # client.remove_topic_callback(aio_username + "/feeds/device.batterylevel") def on_message(client, topic, message): @@ -94,8 +93,8 @@ def on_message(client, topic, message): # Set up a MiniMQTT Client client = MQTT.MQTT( - broker=os.getenv("broker"), - port=os.getenv("broker_port"), + broker=broker, + port=broker_port, socket_pool=pool, ssl_context=ssl_context, ) @@ -106,14 +105,14 @@ def on_message(client, topic, message): client.on_subscribe = subscribe client.on_unsubscribe = unsubscribe client.on_message = on_message -client.add_topic_callback(secrets["aio_username"] + "/feeds/device.batterylevel", on_battery_msg) +client.add_topic_callback(aio_username + "/feeds/device.batterylevel", on_battery_msg) # Connect the client to the MQTT broker. print("Connecting to MQTT broker...") client.connect() # Subscribe to all notifications on the device group -client.subscribe(secrets["aio_username"] + "/groups/device", 1) +client.subscribe(aio_username + "/groups/device", 1) # Start a blocking message loop... # NOTE: NO code below this loop will execute diff --git a/examples/esp32spi/minimqtt_pub_sub_nonblocking_esp32spi.py b/examples/esp32spi/minimqtt_pub_sub_nonblocking_esp32spi.py index 642d824c..fcd8cb67 100644 --- a/examples/esp32spi/minimqtt_pub_sub_nonblocking_esp32spi.py +++ b/examples/esp32spi/minimqtt_pub_sub_nonblocking_esp32spi.py @@ -1,8 +1,8 @@ # SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries # SPDX-License-Identifier: MIT -import os import time +from os import getenv import adafruit_connection_manager import board @@ -13,12 +13,12 @@ import adafruit_minimqtt.adafruit_minimqtt as MQTT -# Add settings.toml to your filesystem CIRCUITPY_WIFI_SSID and CIRCUITPY_WIFI_PASSWORD keys -# with your WiFi credentials. Add your Adafruit IO username and key as well. -# DO NOT share that file or commit it into Git or other source control. - -aio_username = os.getenv("aio_username") -aio_key = os.getenv("aio_key") +# Get WiFi details and Adafruit IO keys, ensure these are setup in settings.toml +# (visit io.adafruit.com if you need to create an account, or if you need your Adafruit IO key.) +ssid = getenv("CIRCUITPY_WIFI_SSID") +password = getenv("CIRCUITPY_WIFI_PASSWORD") +aio_username = getenv("ADAFRUIT_AIO_USERNAME") +aio_key = getenv("ADAFRUIT_AIO_KEY") # If you are using a board with pre-defined ESP32 Pins: esp32_cs = DigitalInOut(board.ESP_CS) @@ -33,16 +33,16 @@ spi = busio.SPI(board.SCK, board.MOSI, board.MISO) esp = adafruit_esp32spi.ESP_SPIcontrol(spi, esp32_cs, esp32_ready, esp32_reset) """Use below for Most Boards""" -status_light = neopixel.NeoPixel(board.NEOPIXEL, 1, brightness=0.2) # Uncomment for Most Boards +status_pixel = neopixel.NeoPixel(board.NEOPIXEL, 1, brightness=0.2) # Uncomment for Most Boards """Uncomment below for ItsyBitsy M4""" -# status_light = dotstar.DotStar(board.APA102_SCK, board.APA102_MOSI, 1, brightness=0.2) +# status_pixel = dotstar.DotStar(board.APA102_SCK, board.APA102_MOSI, 1, brightness=0.2) # Uncomment below for an externally defined RGB LED # import adafruit_rgbled # from adafruit_esp32spi import PWMOut # RED_LED = PWMOut.PWMOut(esp, 26) # GREEN_LED = PWMOut.PWMOut(esp, 27) # BLUE_LED = PWMOut.PWMOut(esp, 25) -# status_light = adafruit_rgbled.RGBLED(RED_LED, BLUE_LED, GREEN_LED) +# status_pixel = adafruit_rgbled.RGBLED(RED_LED, BLUE_LED, GREEN_LED) ### Adafruit IO Setup ### @@ -76,7 +76,7 @@ def message(client, topic, message): # Connect to WiFi print("Connecting to WiFi...") -esp.connect_AP(os.getenv("CIRCUITPY_WIFI_SSID"), os.getenv("CIRCUITPY_WIFI_PASSWORD")) +esp.connect_AP(ssid, password) print("Connected!") pool = adafruit_connection_manager.get_radio_socketpool(esp) diff --git a/examples/esp32spi/minimqtt_pub_sub_pyportal_esp32spi.py b/examples/esp32spi/minimqtt_pub_sub_pyportal_esp32spi.py index 5f726d19..33933a5d 100644 --- a/examples/esp32spi/minimqtt_pub_sub_pyportal_esp32spi.py +++ b/examples/esp32spi/minimqtt_pub_sub_pyportal_esp32spi.py @@ -1,8 +1,8 @@ # SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries # SPDX-License-Identifier: MIT -import os import time +from os import getenv import adafruit_connection_manager import adafruit_pyportal @@ -11,12 +11,10 @@ pyportal = adafruit_pyportal.PyPortal() -# Add settings.toml to your filesystem CIRCUITPY_WIFI_SSID and CIRCUITPY_WIFI_PASSWORD keys -# with your WiFi credentials. Add your Adafruit IO username and key as well. -# DO NOT share that file or commit it into Git or other source control. - -aio_username = os.getenv("aio_username") -aio_key = os.getenv("aio_key") +# Get Adafruit IO keys, ensure these are setup in settings.toml +# (visit io.adafruit.com if you need to create an account, or if you need your Adafruit IO key.) +aio_username = getenv("ADAFRUIT_AIO_USERNAME") +aio_key = getenv("ADAFRUIT_AIO_KEY") # ------------- MQTT Topic Setup ------------- # mqtt_topic = "test/topic" @@ -27,7 +25,7 @@ def connected(client, userdata, flags, rc): # This function will be called when the client is connected # successfully to the broker. - print("Subscribing to %s" % (mqtt_topic)) + print(f"Subscribing to {mqtt_topic}") client.subscribe(mqtt_topic) @@ -55,9 +53,9 @@ def message(client, topic, message): # Set up a MiniMQTT Client mqtt_client = MQTT.MQTT( - broker=os.getenv("broker"), - username=os.getenv("username"), - password=os.getenv("password"), + broker="io.adafruit.com", + username=aio_username, + password=aio_key, is_ssl=False, socket_pool=pool, ssl_context=ssl_context, @@ -77,7 +75,7 @@ def message(client, topic, message): mqtt_client.loop() # Send a new message - print("Sending photocell value: %d" % photocell_val) + print(f"Sending photocell value: {photocell_val}") mqtt_client.publish(mqtt_topic, photocell_val) photocell_val += 1 time.sleep(1) diff --git a/examples/esp32spi/minimqtt_simpletest_esp32spi.py b/examples/esp32spi/minimqtt_simpletest_esp32spi.py index 37dae061..8de8f446 100644 --- a/examples/esp32spi/minimqtt_simpletest_esp32spi.py +++ b/examples/esp32spi/minimqtt_simpletest_esp32spi.py @@ -1,7 +1,7 @@ # SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries # SPDX-License-Identifier: MIT -import os +from os import getenv import adafruit_connection_manager import board @@ -11,12 +11,12 @@ import adafruit_minimqtt.adafruit_minimqtt as MQTT -# Add settings.toml to your filesystem CIRCUITPY_WIFI_SSID and CIRCUITPY_WIFI_PASSWORD keys -# with your WiFi credentials. Add your Adafruit IO username and key as well. -# DO NOT share that file or commit it into Git or other source control. - -aio_username = os.getenv("aio_username") -aio_key = os.getenv("aio_key") +# Get WiFi details and Adafruit IO keys, ensure these are setup in settings.toml +# (visit io.adafruit.com if you need to create an account, or if you need your Adafruit IO key.) +ssid = getenv("CIRCUITPY_WIFI_SSID") +password = getenv("CIRCUITPY_WIFI_PASSWORD") +aio_username = getenv("ADAFRUIT_AIO_USERNAME") +aio_key = getenv("ADAFRUIT_AIO_KEY") # If you are using a board with pre-defined ESP32 Pins: esp32_cs = DigitalInOut(board.ESP_CS) @@ -34,7 +34,7 @@ print("Connecting to AP...") while not esp.is_connected: try: - esp.connect_AP(os.getenv("ssid"), os.getenv("password")) + esp.connect_AP(ssid, password) except RuntimeError as e: print("could not connect to AP, retrying: ", e) continue @@ -91,10 +91,9 @@ def message(client, topic, message): # Set up a MiniMQTT Client mqtt_client = MQTT.MQTT( - broker=os.getenv("broker"), - port=os.getenv("port"), - username=os.getenv("username"), - password=os.getenv("password"), + broker="io.adafruit.com", + username=aio_username, + password=aio_key, socket_pool=pool, ssl_context=ssl_context, ) @@ -107,17 +106,17 @@ def message(client, topic, message): mqtt_client.on_publish = publish mqtt_client.on_message = message -print("Attempting to connect to %s" % mqtt_client.broker) +print(f"Attempting to connect to {mqtt_client.broker}") mqtt_client.connect() -print("Subscribing to %s" % mqtt_topic) +print(f"Subscribing to {mqtt_topic}") mqtt_client.subscribe(mqtt_topic) -print("Publishing to %s" % mqtt_topic) +print(f"Publishing to {mqtt_topic}") mqtt_client.publish(mqtt_topic, "Hello Broker!") -print("Unsubscribing from %s" % mqtt_topic) +print(f"Unsubscribing from {mqtt_topic}") mqtt_client.unsubscribe(mqtt_topic) -print("Disconnecting from %s" % mqtt_client.broker) +print(f"Disconnecting from {mqtt_client.broker}") mqtt_client.disconnect() diff --git a/examples/ethernet/minimqtt_adafruitio_eth.py b/examples/ethernet/minimqtt_adafruitio_eth.py index 5cac4183..b18d286d 100755 --- a/examples/ethernet/minimqtt_adafruitio_eth.py +++ b/examples/ethernet/minimqtt_adafruitio_eth.py @@ -1,8 +1,8 @@ # SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries # SPDX-License-Identifier: MIT -import os import time +from os import getenv import adafruit_connection_manager import board @@ -12,11 +12,10 @@ import adafruit_minimqtt.adafruit_minimqtt as MQTT -# Add settings.toml to your filesystem. Add your Adafruit IO username and key as well. -# DO NOT share that file or commit it into Git or other source control. - -aio_username = os.getenv("aio_username") -aio_key = os.getenv("aio_key") +# Get Adafruit IO keys, ensure these are setup in settings.toml +# (visit io.adafruit.com if you need to create an account, or if you need your Adafruit IO key.) +aio_username = getenv("ADAFRUIT_AIO_USERNAME") +aio_key = getenv("ADAFRUIT_AIO_KEY") cs = DigitalInOut(board.D10) spi_bus = busio.SPI(board.SCK, MOSI=board.MOSI, MISO=board.MISO) @@ -39,7 +38,7 @@ def connected(client, userdata, flags, rc): # This function will be called when the client is connected # successfully to the broker. - print("Connected to Adafruit IO! Listening for topic changes on %s" % onoff_feed) + print(f"Connected to Adafruit IO! Listening for topic changes on {onoff_feed}") # Subscribe to all changes on the onoff_feed. client.subscribe(onoff_feed) @@ -84,7 +83,7 @@ def message(client, topic, message): mqtt_client.loop() # Send a new message - print("Sending photocell value: %d..." % photocell_val) + print(f"Sending photocell value: {photocell_val}...") mqtt_client.publish(photocell_feed, photocell_val) print("Sent!") photocell_val += 1 diff --git a/examples/ethernet/minimqtt_simpletest_eth.py b/examples/ethernet/minimqtt_simpletest_eth.py index 35535800..39d86dd7 100644 --- a/examples/ethernet/minimqtt_simpletest_eth.py +++ b/examples/ethernet/minimqtt_simpletest_eth.py @@ -1,7 +1,7 @@ # SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries # SPDX-License-Identifier: MIT -import os +from os import getenv import adafruit_connection_manager import board @@ -11,11 +11,10 @@ import adafruit_minimqtt.adafruit_minimqtt as MQTT -# Add settings.toml to your filesystem. Add your Adafruit IO username and key as well. -# DO NOT share that file or commit it into Git or other source control. - -aio_username = os.getenv("aio_username") -aio_key = os.getenv("aio_key") +# Get Adafruit IO keys, ensure these are setup in settings.toml +# (visit io.adafruit.com if you need to create an account, or if you need your Adafruit IO key.) +aio_username = getenv("ADAFRUIT_AIO_USERNAME") +aio_key = getenv("ADAFRUIT_AIO_KEY") cs = DigitalInOut(board.D10) spi_bus = busio.SPI(board.SCK, MOSI=board.MOSI, MISO=board.MISO) @@ -70,9 +69,9 @@ def publish(client, userdata, topic, pid): # Set up a MiniMQTT Client # NOTE: We'll need to connect insecurely for ethernet configurations. client = MQTT.MQTT( - broker=os.getenv("broker"), - username=os.getenv("username"), - password=os.getenv("password"), + broker="io.adafruit.com", + username=aio_username, + password=aio_key, is_ssl=False, socket_pool=pool, ssl_context=ssl_context, @@ -85,17 +84,17 @@ def publish(client, userdata, topic, pid): client.on_unsubscribe = unsubscribe client.on_publish = publish -print("Attempting to connect to %s" % client.broker) +print(f"Attempting to connect to {client.broker}") client.connect() -print("Subscribing to %s" % mqtt_topic) +print(f"Subscribing to {mqtt_topic}") client.subscribe(mqtt_topic) -print("Publishing to %s" % mqtt_topic) +print(f"Publishing to {mqtt_topic}") client.publish(mqtt_topic, "Hello Broker!") -print("Unsubscribing from %s" % mqtt_topic) +print(f"Unsubscribing from {mqtt_topic}") client.unsubscribe(mqtt_topic) -print("Disconnecting from %s" % client.broker) +print(f"Disconnecting from {client.broker}") client.disconnect() diff --git a/examples/minimqtt_simpletest.py b/examples/minimqtt_simpletest.py index c1027d9a..b5086ba0 100644 --- a/examples/minimqtt_simpletest.py +++ b/examples/minimqtt_simpletest.py @@ -1,7 +1,7 @@ # SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries # SPDX-License-Identifier: MIT -import os +from os import getenv import adafruit_connection_manager import board @@ -11,12 +11,12 @@ import adafruit_minimqtt.adafruit_minimqtt as MQTT -# Add settings.toml to your filesystem CIRCUITPY_WIFI_SSID and CIRCUITPY_WIFI_PASSWORD keys -# with your WiFi credentials. Add your Adafruit IO username and key as well. -# DO NOT share that file or commit it into Git or other source control. - -aio_username = os.getenv("aio_username") -aio_key = os.getenv("aio_key") +# Get WiFi details and Adafruit IO keys, ensure these are setup in settings.toml +# (visit io.adafruit.com if you need to create an account, or if you need your Adafruit IO key.) +ssid = getenv("CIRCUITPY_WIFI_SSID") +password = getenv("CIRCUITPY_WIFI_PASSWORD") +aio_username = getenv("ADAFRUIT_AIO_USERNAME") +aio_key = getenv("ADAFRUIT_AIO_KEY") # If you are using a board with pre-defined ESP32 Pins: esp32_cs = DigitalInOut(board.ESP_CS) @@ -34,7 +34,7 @@ print("Connecting to AP...") while not esp.is_connected: try: - esp.connect_AP(os.getenv("CIRCUITPY_WIFI_SSID"), os.getenv("CIRCUITPY_WIFI_PASSWORD")) + esp.connect_AP(ssid, password) except RuntimeError as e: print("could not connect to AP, retrying: ", e) continue @@ -107,17 +107,17 @@ def message(client, topic, message): mqtt_client.on_publish = publish mqtt_client.on_message = message -print("Attempting to connect to %s" % mqtt_client.broker) +print(f"Attempting to connect to {mqtt_client.broker}") mqtt_client.connect() -print("Subscribing to %s" % mqtt_topic) +print(f"Subscribing to {mqtt_topic}") mqtt_client.subscribe(mqtt_topic) -print("Publishing to %s" % mqtt_topic) +print(f"Publishing to {mqtt_topic}") mqtt_client.publish(mqtt_topic, "Hello Broker!") -print("Unsubscribing from %s" % mqtt_topic) +print(f"Unsubscribing from {mqtt_topic}") mqtt_client.unsubscribe(mqtt_topic) -print("Disconnecting from %s" % mqtt_client.broker) +print(f"Disconnecting from {mqtt_client.broker}") mqtt_client.disconnect() diff --git a/examples/native_networking/minimqtt_adafruitio_native_networking.py b/examples/native_networking/minimqtt_adafruitio_native_networking.py index facee071..873b2d7d 100644 --- a/examples/native_networking/minimqtt_adafruitio_native_networking.py +++ b/examples/native_networking/minimqtt_adafruitio_native_networking.py @@ -1,28 +1,24 @@ # SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries # SPDX-License-Identifier: MIT -import os -import ssl import time +from os import getenv -import socketpool +import adafruit_connection_manager import wifi import adafruit_minimqtt.adafruit_minimqtt as MQTT -# Add settings.toml to your filesystem CIRCUITPY_WIFI_SSID and CIRCUITPY_WIFI_PASSWORD keys -# with your WiFi credentials. DO NOT share that file or commit it into Git or other -# source control. +# Get WiFi details and Adafruit IO keys, ensure these are setup in settings.toml +# (visit io.adafruit.com if you need to create an account, or if you need your Adafruit IO key.) +ssid = getenv("CIRCUITPY_WIFI_SSID") +password = getenv("CIRCUITPY_WIFI_PASSWORD") +aio_username = getenv("ADAFRUIT_AIO_USERNAME") +aio_key = getenv("ADAFRUIT_AIO_KEY") -# Set your Adafruit IO Username, Key and Port in settings.toml -# (visit io.adafruit.com if you need to create an account, -# or if you need your Adafruit IO key.) -aio_username = os.getenv("aio_username") -aio_key = os.getenv("aio_key") - -print(f"Connecting to {os.getenv('CIRCUITPY_WIFI_SSID')}") -wifi.radio.connect(os.getenv("CIRCUITPY_WIFI_SSID"), os.getenv("CIRCUITPY_WIFI_PASSWORD")) -print(f"Connected to {os.getenv('CIRCUITPY_WIFI_SSID')}!") +print(f"Connecting to {ssid}") +wifi.radio.connect(ssid, password) +print(f"Connected to {ssid}!") ### Feeds ### # Setup a feed named 'photocell' for publishing to a feed @@ -54,16 +50,16 @@ def message(client, topic, message): print(f"New message on topic {topic}: {message}") -# Create a socket pool -pool = socketpool.SocketPool(wifi.radio) -ssl_context = ssl.create_default_context() +# Create a socket pool and ssl_context +pool = adafruit_connection_manager.get_radio_socketpool(wifi.radio) +ssl_context = adafruit_connection_manager.get_radio_ssl_context(wifi.radio) # If you need to use certificate/key pair authentication (e.g. X.509), you can load them in the # ssl context by uncommenting the lines below and adding the following keys to your settings.toml: # "device_cert_path" - Path to the Device Certificate # "device_key_path" - Path to the RSA Private Key # ssl_context.load_cert_chain( -# certfile=os.getenv("device_cert_path"), keyfile=os.getenv("device_key_path") +# certfile=getenv("device_cert_path"), keyfile=getenv("device_key_path") # ) # Set up a MiniMQTT Client diff --git a/examples/native_networking/minimqtt_pub_sub_blocking_native_networking.py b/examples/native_networking/minimqtt_pub_sub_blocking_native_networking.py index b59dff80..fe02c38f 100644 --- a/examples/native_networking/minimqtt_pub_sub_blocking_native_networking.py +++ b/examples/native_networking/minimqtt_pub_sub_blocking_native_networking.py @@ -1,11 +1,10 @@ # SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries # SPDX-License-Identifier: MIT -import os -import ssl import time +from os import getenv -import socketpool +import adafruit_connection_manager import wifi import adafruit_minimqtt.adafruit_minimqtt as MQTT @@ -14,15 +13,16 @@ # with your WiFi credentials. DO NOT share that file or commit it into Git or other # source control. -# Set your Adafruit IO Username, Key and Port in settings.toml -# (visit io.adafruit.com if you need to create an account, -# or if you need your Adafruit IO key.) -aio_username = os.getenv("aio_username") -aio_key = os.getenv("aio_key") +# Get WiFi details and Adafruit IO keys, ensure these are setup in settings.toml +# (visit io.adafruit.com if you need to create an account, or if you need your Adafruit IO key.) +ssid = getenv("CIRCUITPY_WIFI_SSID") +password = getenv("CIRCUITPY_WIFI_PASSWORD") +aio_username = getenv("ADAFRUIT_AIO_USERNAME") +aio_key = getenv("ADAFRUIT_AIO_KEY") -print("Connecting to %s" % os.getenv("CIRCUITPY_WIFI_SSID")) -wifi.radio.connect(os.getenv("CIRCUITPY_WIFI_SSID"), os.getenv("CIRCUITPY_WIFI_PASSWORD")) -print("Connected to %s!" % os.getenv("CIRCUITPY_WIFI_SSID")) +print(f"Connecting to {ssid}") +wifi.radio.connect(ssid, password) +print(f"Connected to {ssid}!") ### Adafruit IO Setup ### @@ -54,16 +54,16 @@ def message(client, topic, message): print(f"New message on topic {topic}: {message}") -# Create a socket pool -pool = socketpool.SocketPool(wifi.radio) -ssl_context = ssl.create_default_context() +# Create a socket pool and ssl_context +pool = adafruit_connection_manager.get_radio_socketpool(wifi.radio) +ssl_context = adafruit_connection_manager.get_radio_ssl_context(wifi.radio) # If you need to use certificate/key pair authentication (e.g. X.509), you can load them in the # ssl context by uncommenting the lines below and adding the following keys to your settings.toml: # "device_cert_path" - Path to the Device Certificate # "device_key_path" - Path to the RSA Private Key # ssl_context.load_cert_chain( -# certfile=os.getenv("device_cert_path"), keyfile=os.getenv("device_key_path") +# certfile=getenv("device_cert_path"), keyfile=getenv("device_key_path") # ) # Set up a MiniMQTT Client diff --git a/examples/native_networking/minimqtt_pub_sub_blocking_topic_callbacks_native_networking.py b/examples/native_networking/minimqtt_pub_sub_blocking_topic_callbacks_native_networking.py index 07f0f9d2..7dceba7a 100644 --- a/examples/native_networking/minimqtt_pub_sub_blocking_topic_callbacks_native_networking.py +++ b/examples/native_networking/minimqtt_pub_sub_blocking_topic_callbacks_native_networking.py @@ -1,28 +1,24 @@ # SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries # SPDX-License-Identifier: MIT -import os -import ssl import time +from os import getenv -import socketpool +import adafruit_connection_manager import wifi import adafruit_minimqtt.adafruit_minimqtt as MQTT -# Add settings.toml to your filesystem CIRCUITPY_WIFI_SSID and CIRCUITPY_WIFI_PASSWORD keys -# with your WiFi credentials. DO NOT share that file or commit it into Git or other -# source control. +# Get WiFi details and Adafruit IO keys, ensure these are setup in settings.toml +# (visit io.adafruit.com if you need to create an account, or if you need your Adafruit IO key.) +ssid = getenv("CIRCUITPY_WIFI_SSID") +password = getenv("CIRCUITPY_WIFI_PASSWORD") +aio_username = getenv("ADAFRUIT_AIO_USERNAME") +aio_key = getenv("ADAFRUIT_AIO_KEY") -# Set your Adafruit IO Username, Key and Port in settings.toml -# (visit io.adafruit.com if you need to create an account, -# or if you need your Adafruit IO key.) -aio_username = os.getenv("aio_username") -aio_key = os.getenv("aio_key") - -print("Connecting to %s" % os.getenv("CIRCUITPY_WIFI_SSID")) -wifi.radio.connect(os.getenv("CIRCUITPY_WIFI_SSID"), os.getenv("CIRCUITPY_WIFI_PASSWORD")) -print("Connected to %s!" % os.getenv("CIRCUITPY_WIFI_SSID")) +print(f"Connecting to {ssid}") +wifi.radio.connect(ssid, password) +print(f"Connected to {ssid}!") ### Code ### @@ -61,16 +57,16 @@ def on_message(client, topic, message): print(f"New message on topic {topic}: {message}") -# Create a socket pool -pool = socketpool.SocketPool(wifi.radio) -ssl_context = ssl.create_default_context() +# Create a socket pool and ssl_context +pool = adafruit_connection_manager.get_radio_socketpool(wifi.radio) +ssl_context = adafruit_connection_manager.get_radio_ssl_context(wifi.radio) # If you need to use certificate/key pair authentication (e.g. X.509), you can load them in the # ssl context by uncommenting the lines below and adding the following keys to your settings.toml: # "device_cert_path" - Path to the Device Certificate # "device_key_path" - Path to the RSA Private Key # ssl_context.load_cert_chain( -# certfile=os.getenv("device_cert_path"), keyfile=os.getenv("device_key_path") +# certfile=getenv("device_cert_path"), keyfile=getenv("device_key_path") # ) # Set up a MiniMQTT Client From ce1a94d4e90819c017ddc58adef5d1fc2228f6dc Mon Sep 17 00:00:00 2001 From: Justin Myers Date: Sat, 1 Mar 2025 08:01:55 -0800 Subject: [PATCH 02/10] PR comments --- examples/cellular/minimqtt_adafruitio_cellular.py | 4 ++-- examples/cellular/minimqtt_simpletest_cellular.py | 3 ++- examples/cpython/minimqtt_adafruitio_cpython.py | 4 ++-- examples/cpython/minimqtt_simpletest_cpython.py | 7 +++++-- examples/esp32spi/minimqtt_adafruitio_esp32spi.py | 4 ++-- .../minimqtt_pub_sub_blocking_esp32spi.py | 2 +- ...t_pub_sub_blocking_topic_callbacks_esp32spi.py | 15 ++++++++++----- .../minimqtt_pub_sub_nonblocking_esp32spi.py | 2 +- .../minimqtt_pub_sub_pyportal_esp32spi.py | 3 ++- examples/esp32spi/minimqtt_simpletest_esp32spi.py | 7 ++++--- examples/ethernet/minimqtt_adafruitio_eth.py | 4 ++-- examples/ethernet/minimqtt_simpletest_eth.py | 3 ++- examples/minimqtt_simpletest.py | 4 ++-- .../minimqtt_adafruitio_native_networking.py | 4 ++-- ...minimqtt_pub_sub_blocking_native_networking.py | 3 +-- ..._blocking_topic_callbacks_native_networking.py | 7 +++---- 16 files changed, 43 insertions(+), 33 deletions(-) diff --git a/examples/cellular/minimqtt_adafruitio_cellular.py b/examples/cellular/minimqtt_adafruitio_cellular.py index 6c63de9f..f2f3061b 100755 --- a/examples/cellular/minimqtt_adafruitio_cellular.py +++ b/examples/cellular/minimqtt_adafruitio_cellular.py @@ -33,10 +33,10 @@ ### Feeds ### # Setup a feed named 'photocell' for publishing to a feed -photocell_feed = aio_username + "/feeds/photocell" +photocell_feed = f"{aio_username}/feeds/photocell" # Setup a feed named 'onoff' for subscribing to changes -onoff_feed = aio_username + "/feeds/onoff" +onoff_feed = f"{aio_username}/feeds/onoff" ### Code ### diff --git a/examples/cellular/minimqtt_simpletest_cellular.py b/examples/cellular/minimqtt_simpletest_cellular.py index 35fc90cb..fb83e292 100644 --- a/examples/cellular/minimqtt_simpletest_cellular.py +++ b/examples/cellular/minimqtt_simpletest_cellular.py @@ -21,6 +21,7 @@ apn_password = getenv("apn_password") aio_username = getenv("ADAFRUIT_AIO_USERNAME") aio_key = getenv("ADAFRUIT_AIO_KEY") +broker = getenv("broker", "io.adafruit.com") # Create a serial connection for the FONA connection uart = busio.UART(board.TX, board.RX) @@ -88,7 +89,7 @@ def publish(client, userdata, topic, pid): # Set up a MiniMQTT Client client = MQTT.MQTT( - broker="io.adafruit.com", + broker=broker, username=aio_username, password=aio_key, is_ssl=False, diff --git a/examples/cpython/minimqtt_adafruitio_cpython.py b/examples/cpython/minimqtt_adafruitio_cpython.py index 60d94c36..5fa03556 100644 --- a/examples/cpython/minimqtt_adafruitio_cpython.py +++ b/examples/cpython/minimqtt_adafruitio_cpython.py @@ -21,10 +21,10 @@ ### Feeds ### # Setup a feed named 'photocell' for publishing to a feed -photocell_feed = aio_username + "/feeds/photocell" +photocell_feed = f"{aio_username}/feeds/photocell" # Setup a feed named 'onoff' for subscribing to changes -onoff_feed = aio_username + "/feeds/onoff" +onoff_feed = f"{aio_username}/feeds/onoff" ### Code ### diff --git a/examples/cpython/minimqtt_simpletest_cpython.py b/examples/cpython/minimqtt_simpletest_cpython.py index ef875534..3254e2d2 100644 --- a/examples/cpython/minimqtt_simpletest_cpython.py +++ b/examples/cpython/minimqtt_simpletest_cpython.py @@ -8,12 +8,15 @@ import adafruit_minimqtt.adafruit_minimqtt as MQTT # Add your Adafruit IO username and key to your env. +# (visit io.adafruit.com if you need to create an account, or if you need your Adafruit IO key.) # example: # export ADAFRUIT_AIO_USERNAME=your-aio-username # export ADAFRUIT_AIO_KEY=your-aio-key +# export broker=io.adafruit.com aio_username = getenv("ADAFRUIT_AIO_USERNAME") aio_key = getenv("ADAFRUIT_AIO_KEY") +broker = getenv("broker", "io.adafruit.com") ### Topic Setup ### @@ -23,7 +26,7 @@ # Adafruit IO-style Topic # Use this topic if you'd like to connect to io.adafruit.com -# mqtt_topic = aio_username + "/feeds/temperature" +# mqtt_topic = f"{aio_username}/feeds/temperature" ### Code ### @@ -63,7 +66,7 @@ def message(client, topic, message): # Set up a MiniMQTT Client mqtt_client = MQTT.MQTT( - broker="io.adafruit.com", + broker=broker, username=aio_username, password=aio_key, socket_pool=socket, diff --git a/examples/esp32spi/minimqtt_adafruitio_esp32spi.py b/examples/esp32spi/minimqtt_adafruitio_esp32spi.py index 5c9e6856..7a52f5ee 100644 --- a/examples/esp32spi/minimqtt_adafruitio_esp32spi.py +++ b/examples/esp32spi/minimqtt_adafruitio_esp32spi.py @@ -47,10 +47,10 @@ ### Feeds ### # Setup a feed named 'photocell' for publishing to a feed -photocell_feed = aio_username + "/feeds/photocell" +photocell_feed = f"{aio_username}/feeds/photocell" # Setup a feed named 'onoff' for subscribing to changes -onoff_feed = aio_username + "/feeds/onoff" +onoff_feed = f"{aio_username}/feeds/onoff" ### Code ### diff --git a/examples/esp32spi/minimqtt_pub_sub_blocking_esp32spi.py b/examples/esp32spi/minimqtt_pub_sub_blocking_esp32spi.py index 139fc844..98e9cffa 100644 --- a/examples/esp32spi/minimqtt_pub_sub_blocking_esp32spi.py +++ b/examples/esp32spi/minimqtt_pub_sub_blocking_esp32spi.py @@ -47,7 +47,7 @@ ### Adafruit IO Setup ### # Setup a feed named `testfeed` for publishing. -default_topic = aio_username + "/feeds/testfeed" +default_topic = f"{aio_username}/feeds/testfeed" ### Code ### diff --git a/examples/esp32spi/minimqtt_pub_sub_blocking_topic_callbacks_esp32spi.py b/examples/esp32spi/minimqtt_pub_sub_blocking_topic_callbacks_esp32spi.py index f0b175d1..3301891d 100644 --- a/examples/esp32spi/minimqtt_pub_sub_blocking_topic_callbacks_esp32spi.py +++ b/examples/esp32spi/minimqtt_pub_sub_blocking_topic_callbacks_esp32spi.py @@ -14,10 +14,13 @@ import adafruit_minimqtt.adafruit_minimqtt as MQTT # Get WiFi details and broker keys, ensure these are setup in settings.toml +# (visit io.adafruit.com if you need to create an account, or if you need your Adafruit IO key.) ssid = getenv("CIRCUITPY_WIFI_SSID") password = getenv("CIRCUITPY_WIFI_PASSWORD") -broker = getenv("broker") -broker_port = getenv("broker_port") +aio_username = getenv("ADAFRUIT_AIO_USERNAME") +aio_key = getenv("ADAFRUIT_AIO_KEY") +broker = getenv("broker", "io.adafruit.com") +broker_port = int(getenv("broker_port", "8883")) # Port 1883 insecure, 8883 secure ### WiFi ### @@ -75,7 +78,7 @@ def on_battery_msg(client, topic, message): # Method called when device/batteryLife has a new value print(f"Battery level: {message}v") - # client.remove_topic_callback(aio_username + "/feeds/device.batterylevel") + # client.remove_topic_callback(f"{aio_username}/feeds/device.batterylevel") def on_message(client, topic, message): @@ -95,6 +98,8 @@ def on_message(client, topic, message): client = MQTT.MQTT( broker=broker, port=broker_port, + username=aio_username, + password=aio_key, socket_pool=pool, ssl_context=ssl_context, ) @@ -105,14 +110,14 @@ def on_message(client, topic, message): client.on_subscribe = subscribe client.on_unsubscribe = unsubscribe client.on_message = on_message -client.add_topic_callback(aio_username + "/feeds/device.batterylevel", on_battery_msg) +client.add_topic_callback(f"{aio_username}/feeds/device.batterylevel", on_battery_msg) # Connect the client to the MQTT broker. print("Connecting to MQTT broker...") client.connect() # Subscribe to all notifications on the device group -client.subscribe(aio_username + "/groups/device", 1) +client.subscribe(f"{aio_username}/groups/device", 1) # Start a blocking message loop... # NOTE: NO code below this loop will execute diff --git a/examples/esp32spi/minimqtt_pub_sub_nonblocking_esp32spi.py b/examples/esp32spi/minimqtt_pub_sub_nonblocking_esp32spi.py index fcd8cb67..e396c53e 100644 --- a/examples/esp32spi/minimqtt_pub_sub_nonblocking_esp32spi.py +++ b/examples/esp32spi/minimqtt_pub_sub_nonblocking_esp32spi.py @@ -47,7 +47,7 @@ ### Adafruit IO Setup ### # Setup a feed named `testfeed` for publishing. -default_topic = aio_username + "/feeds/testfeed" +default_topic = f"{aio_username}/feeds/testfeed" ### Code ### diff --git a/examples/esp32spi/minimqtt_pub_sub_pyportal_esp32spi.py b/examples/esp32spi/minimqtt_pub_sub_pyportal_esp32spi.py index 33933a5d..cacb37ab 100644 --- a/examples/esp32spi/minimqtt_pub_sub_pyportal_esp32spi.py +++ b/examples/esp32spi/minimqtt_pub_sub_pyportal_esp32spi.py @@ -15,6 +15,7 @@ # (visit io.adafruit.com if you need to create an account, or if you need your Adafruit IO key.) aio_username = getenv("ADAFRUIT_AIO_USERNAME") aio_key = getenv("ADAFRUIT_AIO_KEY") +broker = getenv("broker", "io.adafruit.com") # ------------- MQTT Topic Setup ------------- # mqtt_topic = "test/topic" @@ -53,7 +54,7 @@ def message(client, topic, message): # Set up a MiniMQTT Client mqtt_client = MQTT.MQTT( - broker="io.adafruit.com", + broker=broker, username=aio_username, password=aio_key, is_ssl=False, diff --git a/examples/esp32spi/minimqtt_simpletest_esp32spi.py b/examples/esp32spi/minimqtt_simpletest_esp32spi.py index 8de8f446..f17f44dd 100644 --- a/examples/esp32spi/minimqtt_simpletest_esp32spi.py +++ b/examples/esp32spi/minimqtt_simpletest_esp32spi.py @@ -17,6 +17,7 @@ password = getenv("CIRCUITPY_WIFI_PASSWORD") aio_username = getenv("ADAFRUIT_AIO_USERNAME") aio_key = getenv("ADAFRUIT_AIO_KEY") +broker = getenv("broker", "io.adafruit.com") # If you are using a board with pre-defined ESP32 Pins: esp32_cs = DigitalInOut(board.ESP_CS) @@ -38,7 +39,7 @@ except RuntimeError as e: print("could not connect to AP, retrying: ", e) continue -print("Connected to", str(esp.ssid, "utf-8"), "\tRSSI:", esp.rssi) +print("Connected to", esp.ap_info.ssid, "\tRSSI:", esp.ap_info.rssi) ### Topic Setup ### @@ -48,7 +49,7 @@ # Adafruit IO-style Topic # Use this topic if you'd like to connect to io.adafruit.com -# mqtt_topic = aio_username + '/feeds/temperature' +# mqtt_topic = f"{aio_username}/feeds/temperature" ### Code ### @@ -91,7 +92,7 @@ def message(client, topic, message): # Set up a MiniMQTT Client mqtt_client = MQTT.MQTT( - broker="io.adafruit.com", + broker=broker, username=aio_username, password=aio_key, socket_pool=pool, diff --git a/examples/ethernet/minimqtt_adafruitio_eth.py b/examples/ethernet/minimqtt_adafruitio_eth.py index b18d286d..6474be24 100755 --- a/examples/ethernet/minimqtt_adafruitio_eth.py +++ b/examples/ethernet/minimqtt_adafruitio_eth.py @@ -26,10 +26,10 @@ ### Feeds ### # Setup a feed named 'photocell' for publishing to a feed -photocell_feed = aio_username + "/feeds/photocell" +photocell_feed = f"{aio_username}/feeds/photocell" # Setup a feed named 'onoff' for subscribing to changes -onoff_feed = aio_username + "/feeds/onoff" +onoff_feed = f"{aio_username}/feeds/onoff" ### Code ### diff --git a/examples/ethernet/minimqtt_simpletest_eth.py b/examples/ethernet/minimqtt_simpletest_eth.py index 39d86dd7..5130f496 100644 --- a/examples/ethernet/minimqtt_simpletest_eth.py +++ b/examples/ethernet/minimqtt_simpletest_eth.py @@ -15,6 +15,7 @@ # (visit io.adafruit.com if you need to create an account, or if you need your Adafruit IO key.) aio_username = getenv("ADAFRUIT_AIO_USERNAME") aio_key = getenv("ADAFRUIT_AIO_KEY") +broker = getenv("broker", "io.adafruit.com") cs = DigitalInOut(board.D10) spi_bus = busio.SPI(board.SCK, MOSI=board.MOSI, MISO=board.MISO) @@ -69,7 +70,7 @@ def publish(client, userdata, topic, pid): # Set up a MiniMQTT Client # NOTE: We'll need to connect insecurely for ethernet configurations. client = MQTT.MQTT( - broker="io.adafruit.com", + broker=broker, username=aio_username, password=aio_key, is_ssl=False, diff --git a/examples/minimqtt_simpletest.py b/examples/minimqtt_simpletest.py index b5086ba0..9746eab8 100644 --- a/examples/minimqtt_simpletest.py +++ b/examples/minimqtt_simpletest.py @@ -38,7 +38,7 @@ except RuntimeError as e: print("could not connect to AP, retrying: ", e) continue -print("Connected to", str(esp.ssid, "utf-8"), "\tRSSI:", esp.rssi) +print("Connected to", esp.ap_info.ssid, "\tRSSI:", esp.ap_info.rssi) ### Topic Setup ### @@ -48,7 +48,7 @@ # Adafruit IO-style Topic # Use this topic if you'd like to connect to io.adafruit.com -mqtt_topic = aio_username + "/feeds/temperature" +mqtt_topic = f"{aio_username}/feeds/temperature" ### Code ### diff --git a/examples/native_networking/minimqtt_adafruitio_native_networking.py b/examples/native_networking/minimqtt_adafruitio_native_networking.py index 873b2d7d..85dc0b32 100644 --- a/examples/native_networking/minimqtt_adafruitio_native_networking.py +++ b/examples/native_networking/minimqtt_adafruitio_native_networking.py @@ -15,6 +15,7 @@ password = getenv("CIRCUITPY_WIFI_PASSWORD") aio_username = getenv("ADAFRUIT_AIO_USERNAME") aio_key = getenv("ADAFRUIT_AIO_KEY") +broker = getenv("broker", "io.adafruit.com") print(f"Connecting to {ssid}") wifi.radio.connect(ssid, password) @@ -64,8 +65,7 @@ def message(client, topic, message): # Set up a MiniMQTT Client mqtt_client = MQTT.MQTT( - broker="io.adafruit.com", - port=1883, + broker=broker, username=aio_username, password=aio_key, socket_pool=pool, diff --git a/examples/native_networking/minimqtt_pub_sub_blocking_native_networking.py b/examples/native_networking/minimqtt_pub_sub_blocking_native_networking.py index fe02c38f..9c345154 100644 --- a/examples/native_networking/minimqtt_pub_sub_blocking_native_networking.py +++ b/examples/native_networking/minimqtt_pub_sub_blocking_native_networking.py @@ -27,7 +27,7 @@ ### Adafruit IO Setup ### # Setup a feed named `testfeed` for publishing. -default_topic = aio_username + "/feeds/testfeed" +default_topic = f"{aio_username}/feeds/testfeed" ### Code ### @@ -69,7 +69,6 @@ def message(client, topic, message): # Set up a MiniMQTT Client mqtt_client = MQTT.MQTT( broker="io.adafruit.com", - port=1883, username=aio_username, password=aio_key, socket_pool=pool, diff --git a/examples/native_networking/minimqtt_pub_sub_blocking_topic_callbacks_native_networking.py b/examples/native_networking/minimqtt_pub_sub_blocking_topic_callbacks_native_networking.py index 7dceba7a..8867925e 100644 --- a/examples/native_networking/minimqtt_pub_sub_blocking_topic_callbacks_native_networking.py +++ b/examples/native_networking/minimqtt_pub_sub_blocking_topic_callbacks_native_networking.py @@ -49,7 +49,7 @@ def on_battery_msg(client, topic, message): # Method called when device/batteryLife has a new value print(f"Battery level: {message}v") - # client.remove_topic_callback(aio_username + "/feeds/device.batterylevel") + # client.remove_topic_callback(f"{aio_username}/feeds/device.batterylevel") def on_message(client, topic, message): @@ -72,7 +72,6 @@ def on_message(client, topic, message): # Set up a MiniMQTT Client client = MQTT.MQTT( broker="io.adafruit.com", - port=1883, username=aio_username, password=aio_key, socket_pool=pool, @@ -85,14 +84,14 @@ def on_message(client, topic, message): client.on_subscribe = subscribe client.on_unsubscribe = unsubscribe client.on_message = on_message -client.add_topic_callback(aio_username + "/feeds/device.batterylevel", on_battery_msg) +client.add_topic_callback(f"{aio_username}/feeds/device.batterylevel", on_battery_msg) # Connect the client to the MQTT broker. print("Connecting to MQTT broker...") client.connect() # Subscribe to all notifications on the device group -client.subscribe(aio_username + "/groups/device", 1) +client.subscribe(f"{aio_username}/groups/device", 1) # Start a blocking message loop... # NOTE: NO code below this loop will execute From 8a6253d41989f343baf4033b3d819612a6022361 Mon Sep 17 00:00:00 2001 From: Justin Myers Date: Wed, 5 Mar 2025 21:12:08 -0800 Subject: [PATCH 03/10] Missing import --- examples/esp32spi/minimqtt_certificate_esp32spi.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/examples/esp32spi/minimqtt_certificate_esp32spi.py b/examples/esp32spi/minimqtt_certificate_esp32spi.py index eade1211..5dcd7cd7 100644 --- a/examples/esp32spi/minimqtt_certificate_esp32spi.py +++ b/examples/esp32spi/minimqtt_certificate_esp32spi.py @@ -1,6 +1,8 @@ # SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries # SPDX-License-Identifier: MIT +from os import getenv + import adafruit_connection_manager import board import busio From 538c2eb010e4fdb31be184def7428c3761fef0ed Mon Sep 17 00:00:00 2001 From: Tyeth Gundry Date: Thu, 6 Mar 2025 20:21:55 +0000 Subject: [PATCH 04/10] Update ruff-pre-commit, reorder format after fix, and enable error again https://github.com/astral-sh/ruff-pre-commit/blob/2c8dce6094fa2b4b668e74f694ca63ceffd38614/README.md?plain=1#L63-L65 --- .pre-commit-config.yaml | 2 +- examples/esp32spi/minimqtt_certificate_esp32spi.py | 2 -- 2 files changed, 1 insertion(+), 3 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index f27b7860..c099b9e7 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -11,7 +11,7 @@ repos: - id: end-of-file-fixer - id: trailing-whitespace - repo: https://github.com/astral-sh/ruff-pre-commit - rev: v0.3.4 + rev: v0.9.9 hooks: - id: ruff-format - id: ruff diff --git a/examples/esp32spi/minimqtt_certificate_esp32spi.py b/examples/esp32spi/minimqtt_certificate_esp32spi.py index 5dcd7cd7..eade1211 100644 --- a/examples/esp32spi/minimqtt_certificate_esp32spi.py +++ b/examples/esp32spi/minimqtt_certificate_esp32spi.py @@ -1,8 +1,6 @@ # SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries # SPDX-License-Identifier: MIT -from os import getenv - import adafruit_connection_manager import board import busio From ddebed1b5d2c89e7c97385aa4833addfcbc48a51 Mon Sep 17 00:00:00 2001 From: Tyeth Gundry Date: Thu, 6 Mar 2025 20:24:04 +0000 Subject: [PATCH 05/10] Drop rule E999 (now included for free) --- ruff.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ruff.toml b/ruff.toml index db37c83e..a245da32 100644 --- a/ruff.toml +++ b/ruff.toml @@ -16,7 +16,7 @@ extend-select = [ "PLC2401", # non-ascii-name "PLC2801", # unnecessary-dunder-call "PLC3002", # unnecessary-direct-lambda-call - "E999", # syntax-error + # "E999", # syntax-error "PLE0101", # return-in-init "F706", # return-outside-function "F704", # yield-outside-function From 054a4d08bb39db8770ff79a3770b2d73e3349e77 Mon Sep 17 00:00:00 2001 From: Tyeth Gundry Date: Thu, 6 Mar 2025 20:29:16 +0000 Subject: [PATCH 06/10] Reorder and add rule F821 Undefined Name --- .pre-commit-config.yaml | 2 +- ruff.toml | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index c099b9e7..029d1e95 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -13,9 +13,9 @@ repos: - repo: https://github.com/astral-sh/ruff-pre-commit rev: v0.9.9 hooks: - - id: ruff-format - id: ruff args: ["--fix"] + - id: ruff-format - repo: https://github.com/fsfe/reuse-tool rev: v3.0.1 hooks: diff --git a/ruff.toml b/ruff.toml index a245da32..3fed4e9c 100644 --- a/ruff.toml +++ b/ruff.toml @@ -27,6 +27,7 @@ extend-select = [ "PLE0604", # invalid-all-object "PLE0605", # invalid-all-format "PLE0643", # potential-index-error + "F821", # undefined name "PLE0704", # misplaced-bare-raise "PLE1141", # dict-iter-missing-items "PLE1142", # await-outside-async From 88754672d61c3b6178309f9d18c9455c78c40400 Mon Sep 17 00:00:00 2001 From: Tyeth Gundry Date: Thu, 6 Mar 2025 20:31:54 +0000 Subject: [PATCH 07/10] Enable preview features in ruff --- ruff.toml | 3 +++ 1 file changed, 3 insertions(+) diff --git a/ruff.toml b/ruff.toml index 3fed4e9c..0cbd6c66 100644 --- a/ruff.toml +++ b/ruff.toml @@ -5,6 +5,9 @@ target-version = "py38" line-length = 100 +# Enable preview features. +preview = true + [lint] select = ["I", "PL", "UP"] From fb2b101c740fa8d380e831b5793809e31fdc8834 Mon Sep 17 00:00:00 2001 From: Tyeth Gundry Date: Thu, 6 Mar 2025 20:41:21 +0000 Subject: [PATCH 08/10] Ignore class length but keep rule active + remove error --- adafruit_minimqtt/adafruit_minimqtt.py | 2 +- examples/esp32spi/minimqtt_certificate_esp32spi.py | 2 ++ 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/adafruit_minimqtt/adafruit_minimqtt.py b/adafruit_minimqtt/adafruit_minimqtt.py index 75148ed4..55eb0f72 100644 --- a/adafruit_minimqtt/adafruit_minimqtt.py +++ b/adafruit_minimqtt/adafruit_minimqtt.py @@ -111,7 +111,7 @@ def __init__(self) -> None: setattr(NullLogger, log_level, self.nothing) -class MQTT: +class MQTT: # noqa: PLR0904 # too-many-public-methods """MQTT Client for CircuitPython. :param str broker: MQTT Broker URL or IP Address. diff --git a/examples/esp32spi/minimqtt_certificate_esp32spi.py b/examples/esp32spi/minimqtt_certificate_esp32spi.py index eade1211..5dcd7cd7 100644 --- a/examples/esp32spi/minimqtt_certificate_esp32spi.py +++ b/examples/esp32spi/minimqtt_certificate_esp32spi.py @@ -1,6 +1,8 @@ # SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries # SPDX-License-Identifier: MIT +from os import getenv + import adafruit_connection_manager import board import busio From a9ce58da3985c5bb71864e69aa770e8ebdc5f627 Mon Sep 17 00:00:00 2001 From: Tyeth Gundry Date: Thu, 6 Mar 2025 20:46:07 +0000 Subject: [PATCH 09/10] format noqa line --- adafruit_minimqtt/adafruit_minimqtt.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/adafruit_minimqtt/adafruit_minimqtt.py b/adafruit_minimqtt/adafruit_minimqtt.py index 55eb0f72..6cfcaf88 100644 --- a/adafruit_minimqtt/adafruit_minimqtt.py +++ b/adafruit_minimqtt/adafruit_minimqtt.py @@ -111,7 +111,7 @@ def __init__(self) -> None: setattr(NullLogger, log_level, self.nothing) -class MQTT: # noqa: PLR0904 # too-many-public-methods +class MQTT: # noqa: PLR0904 # too-many-public-methods """MQTT Client for CircuitPython. :param str broker: MQTT Broker URL or IP Address. From 29e12f6f6e03651a1418c1e5ae5693dd5ef45e55 Mon Sep 17 00:00:00 2001 From: Justin Myers Date: Thu, 6 Mar 2025 13:05:59 -0800 Subject: [PATCH 10/10] ruff ignores --- adafruit_minimqtt/adafruit_minimqtt.py | 2 + tests/test_loop.py | 2 + tests/test_port_ssl.py | 2 + tests/test_subscribe.py | 146 ++++++++++++------------- tests/test_unsubscribe.py | 106 +++++++++--------- 5 files changed, 125 insertions(+), 133 deletions(-) diff --git a/adafruit_minimqtt/adafruit_minimqtt.py b/adafruit_minimqtt/adafruit_minimqtt.py index 6cfcaf88..f088fdef 100644 --- a/adafruit_minimqtt/adafruit_minimqtt.py +++ b/adafruit_minimqtt/adafruit_minimqtt.py @@ -29,6 +29,8 @@ """ +# ruff: noqa: PLR6104,PLR6201,PLR6301 non-augmented-assignment,literal-membership,no-self-use + import errno import struct import time diff --git a/tests/test_loop.py b/tests/test_loop.py index 834a0d4f..7d1677ae 100644 --- a/tests/test_loop.py +++ b/tests/test_loop.py @@ -2,6 +2,8 @@ # # SPDX-License-Identifier: Unlicense +# ruff: noqa: PLR6301 no-self-use + """loop() tests""" import errno diff --git a/tests/test_port_ssl.py b/tests/test_port_ssl.py index 196f8c73..6156a6ca 100644 --- a/tests/test_port_ssl.py +++ b/tests/test_port_ssl.py @@ -2,6 +2,8 @@ # # SPDX-License-Identifier: Unlicense +# ruff: noqa: PLR6301 no-self-use + """tests that verify the connect behavior w.r.t. port number and TLS""" import socket diff --git a/tests/test_subscribe.py b/tests/test_subscribe.py index f7b037b9..90e5b21f 100644 --- a/tests/test_subscribe.py +++ b/tests/test_subscribe.py @@ -29,47 +29,43 @@ def handle_subscribe(client, user_data, topic, qos): ( "foo/bar", bytearray([0x90, 0x03, 0x00, 0x01, 0x00]), # SUBACK - bytearray( - [ - 0x82, # fixed header - 0x0C, # remaining length - 0x00, - 0x01, # message ID - 0x00, - 0x07, # topic length - 0x66, # topic - 0x6F, - 0x6F, - 0x2F, - 0x62, - 0x61, - 0x72, - 0x00, # QoS - ] - ), + bytearray([ + 0x82, # fixed header + 0x0C, # remaining length + 0x00, + 0x01, # message ID + 0x00, + 0x07, # topic length + 0x66, # topic + 0x6F, + 0x6F, + 0x2F, + 0x62, + 0x61, + 0x72, + 0x00, # QoS + ]), ), # same as before but with tuple ( ("foo/bar", 0), bytearray([0x90, 0x03, 0x00, 0x01, 0x00]), # SUBACK - bytearray( - [ - 0x82, # fixed header - 0x0C, # remaining length - 0x00, - 0x01, # message ID - 0x00, - 0x07, # topic length - 0x66, # topic - 0x6F, - 0x6F, - 0x2F, - 0x62, - 0x61, - 0x72, - 0x00, # QoS - ] - ), + bytearray([ + 0x82, # fixed header + 0x0C, # remaining length + 0x00, + 0x01, # message ID + 0x00, + 0x07, # topic length + 0x66, # topic + 0x6F, + 0x6F, + 0x2F, + 0x62, + 0x61, + 0x72, + 0x00, # QoS + ]), ), # remaining length is encoded as 2 bytes due to long topic name. ( @@ -93,47 +89,43 @@ def handle_subscribe(client, user_data, topic, qos): # SUBSCRIBE responded to by PUBLISH followed by SUBACK ( "foo/bar", - bytearray( - [ - 0x30, # PUBLISH - 0x0C, - 0x00, - 0x07, - 0x66, - 0x6F, - 0x6F, - 0x2F, - 0x62, - 0x61, - 0x72, - 0x66, - 0x6F, - 0x6F, - 0x90, # SUBACK - 0x03, - 0x00, - 0x01, - 0x00, - ] - ), - bytearray( - [ - 0x82, # fixed header - 0x0C, # remaining length - 0x00, - 0x01, # message ID - 0x00, - 0x07, # topic length - 0x66, # topic - 0x6F, - 0x6F, - 0x2F, - 0x62, - 0x61, - 0x72, - 0x00, # QoS - ] - ), + bytearray([ + 0x30, # PUBLISH + 0x0C, + 0x00, + 0x07, + 0x66, + 0x6F, + 0x6F, + 0x2F, + 0x62, + 0x61, + 0x72, + 0x66, + 0x6F, + 0x6F, + 0x90, # SUBACK + 0x03, + 0x00, + 0x01, + 0x00, + ]), + bytearray([ + 0x82, # fixed header + 0x0C, # remaining length + 0x00, + 0x01, # message ID + 0x00, + 0x07, # topic length + 0x66, # topic + 0x6F, + 0x6F, + 0x2F, + 0x62, + 0x61, + 0x72, + 0x00, # QoS + ]), ), # use list of topics for more coverage. If the range was (1, 10000), that would be # long enough to use 3 bytes for remaining length, however that would make the test diff --git a/tests/test_unsubscribe.py b/tests/test_unsubscribe.py index 1dfbb856..0f9ed2ff 100644 --- a/tests/test_unsubscribe.py +++ b/tests/test_unsubscribe.py @@ -32,23 +32,21 @@ def handle_unsubscribe(client, user_data, topic, pid): ( "foo/bar", bytearray([0xB0, 0x02, 0x00, 0x01]), - bytearray( - [ - 0xA2, # fixed header - 0x0B, # remaining length - 0x00, # message ID - 0x01, - 0x00, # topic length - 0x07, - 0x66, # topic - 0x6F, - 0x6F, - 0x2F, - 0x62, - 0x61, - 0x72, - ] - ), + bytearray([ + 0xA2, # fixed header + 0x0B, # remaining length + 0x00, # message ID + 0x01, + 0x00, # topic length + 0x07, + 0x66, # topic + 0x6F, + 0x6F, + 0x2F, + 0x62, + 0x61, + 0x72, + ]), ), # remaining length is encoded as 2 bytes due to long topic name. ( @@ -71,45 +69,41 @@ def handle_unsubscribe(client, user_data, topic, pid): # UNSUBSCRIBE responded to by PUBLISH followed by UNSUBACK ( "foo/bar", - bytearray( - [ - 0x30, # PUBLISH - 0x0C, - 0x00, - 0x07, - 0x66, - 0x6F, - 0x6F, - 0x2F, - 0x62, - 0x61, - 0x72, - 0x66, - 0x6F, - 0x6F, - 0xB0, # UNSUBACK - 0x02, - 0x00, - 0x01, - ] - ), - bytearray( - [ - 0xA2, # fixed header - 0x0B, # remaining length - 0x00, - 0x01, # message ID - 0x00, - 0x07, # topic length - 0x66, # topic - 0x6F, - 0x6F, - 0x2F, - 0x62, - 0x61, - 0x72, - ] - ), + bytearray([ + 0x30, # PUBLISH + 0x0C, + 0x00, + 0x07, + 0x66, + 0x6F, + 0x6F, + 0x2F, + 0x62, + 0x61, + 0x72, + 0x66, + 0x6F, + 0x6F, + 0xB0, # UNSUBACK + 0x02, + 0x00, + 0x01, + ]), + bytearray([ + 0xA2, # fixed header + 0x0B, # remaining length + 0x00, + 0x01, # message ID + 0x00, + 0x07, # topic length + 0x66, # topic + 0x6F, + 0x6F, + 0x2F, + 0x62, + 0x61, + 0x72, + ]), ), # use list of topics for more coverage. If the range was (1, 10000), that would be # long enough to use 3 bytes for remaining length, however that would make the test