diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index f27b7860..029d1e95 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -11,11 +11,11 @@ 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 args: ["--fix"] + - id: ruff-format - repo: https://github.com/fsfe/reuse-tool rev: v3.0.1 hooks: diff --git a/adafruit_minimqtt/adafruit_minimqtt.py b/adafruit_minimqtt/adafruit_minimqtt.py index 75148ed4..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 @@ -111,7 +113,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/cellular/minimqtt_adafruitio_cellular.py b/examples/cellular/minimqtt_adafruitio_cellular.py index 686062c1..f2f3061b 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 ### @@ -32,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 ### @@ -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..fb83e292 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,14 @@ 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") +broker = getenv("broker", "io.adafruit.com") # Create a serial connection for the FONA connection uart = busio.UART(board.TX, board.RX) @@ -70,9 +72,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 +89,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=broker, + username=aio_username, + password=aio_key, is_ssl=False, socket_pool=pool, ssl_context=ssl_context, @@ -104,17 +104,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..5fa03556 100644 --- a/examples/cpython/minimqtt_adafruitio_cpython.py +++ b/examples/cpython/minimqtt_adafruitio_cpython.py @@ -1,28 +1,30 @@ # 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 ### # 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 ### @@ -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..3254e2d2 100644 --- a/examples/cpython/minimqtt_simpletest_cpython.py +++ b/examples/cpython/minimqtt_simpletest_cpython.py @@ -1,17 +1,22 @@ # 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. +# (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 = os.getenv("aio_username") -aio_key = os.getenv("aio_key") +aio_username = getenv("ADAFRUIT_AIO_USERNAME") +aio_key = getenv("ADAFRUIT_AIO_KEY") +broker = getenv("broker", "io.adafruit.com") ### Topic Setup ### @@ -21,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 ### @@ -61,7 +66,7 @@ def message(client, topic, message): # Set up a MiniMQTT Client mqtt_client = MQTT.MQTT( - broker=os.getenv("broker"), + broker=broker, username=aio_username, password=aio_key, socket_pool=socket, @@ -76,17 +81,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..7a52f5ee 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,24 +33,24 @@ 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 ### # 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 ### @@ -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..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 @@ -10,14 +12,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 +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) ### Topic Setup ### @@ -109,9 +111,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 +125,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..98e9cffa 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,21 +33,21 @@ 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 ### # Setup a feed named `testfeed` for publishing. -default_topic = aio_username + "/feeds/testfeed" +default_topic = f"{aio_username}/feeds/testfeed" ### Code ### @@ -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..3301891d 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,16 @@ import adafruit_minimqtt.adafruit_minimqtt as MQTT -### WiFi ### +# 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") +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 -# 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 +37,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 +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(secrets["aio_username"] + "/feeds/device.batterylevel") + # client.remove_topic_callback(f"{aio_username}/feeds/device.batterylevel") def on_message(client, topic, message): @@ -94,8 +96,10 @@ 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, + username=aio_username, + password=aio_key, socket_pool=pool, ssl_context=ssl_context, ) @@ -106,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(secrets["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(secrets["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 642d824c..e396c53e 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,21 +33,21 @@ 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 ### # Setup a feed named `testfeed` for publishing. -default_topic = aio_username + "/feeds/testfeed" +default_topic = f"{aio_username}/feeds/testfeed" ### Code ### @@ -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..cacb37ab 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,11 @@ 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") +broker = getenv("broker", "io.adafruit.com") # ------------- MQTT Topic Setup ------------- # mqtt_topic = "test/topic" @@ -27,7 +26,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 +54,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=broker, + username=aio_username, + password=aio_key, is_ssl=False, socket_pool=pool, ssl_context=ssl_context, @@ -77,7 +76,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..f17f44dd 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,13 @@ 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") +broker = getenv("broker", "io.adafruit.com") # If you are using a board with pre-defined ESP32 Pins: esp32_cs = DigitalInOut(board.ESP_CS) @@ -34,11 +35,11 @@ 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 -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,10 +92,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=broker, + username=aio_username, + password=aio_key, socket_pool=pool, ssl_context=ssl_context, ) @@ -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/ethernet/minimqtt_adafruitio_eth.py b/examples/ethernet/minimqtt_adafruitio_eth.py index 5cac4183..6474be24 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) @@ -27,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 ### @@ -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..5130f496 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,11 @@ 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") +broker = getenv("broker", "io.adafruit.com") cs = DigitalInOut(board.D10) spi_bus = busio.SPI(board.SCK, MOSI=board.MOSI, MISO=board.MISO) @@ -70,9 +70,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=broker, + username=aio_username, + password=aio_key, is_ssl=False, socket_pool=pool, ssl_context=ssl_context, @@ -85,17 +85,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..9746eab8 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,11 +34,11 @@ 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 -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 ### @@ -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..85dc0b32 100644 --- a/examples/native_networking/minimqtt_adafruitio_native_networking.py +++ b/examples/native_networking/minimqtt_adafruitio_native_networking.py @@ -1,28 +1,25 @@ # 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. - -# 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')}!") +# 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") +broker = getenv("broker", "io.adafruit.com") + +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,22 +51,21 @@ 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 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 b59dff80..9c345154 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,20 +13,21 @@ # 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 ### # Setup a feed named `testfeed` for publishing. -default_topic = aio_username + "/feeds/testfeed" +default_topic = f"{aio_username}/feeds/testfeed" ### Code ### @@ -54,22 +54,21 @@ 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 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 07f0f9d2..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 @@ -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 ### @@ -53,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): @@ -61,22 +57,21 @@ 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 client = MQTT.MQTT( broker="io.adafruit.com", - port=1883, username=aio_username, password=aio_key, socket_pool=pool, @@ -89,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 diff --git a/ruff.toml b/ruff.toml index db37c83e..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"] @@ -16,7 +19,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 @@ -27,6 +30,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 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