From 9255b59e5ff3a9d10c56fd43b745b2d937309630 Mon Sep 17 00:00:00 2001 From: Justin Myers Date: Mon, 24 Feb 2025 13:54:45 -0800 Subject: [PATCH 1/2] Remove secrets usage --- README.rst | 35 ++++++------ examples/dash_display_advancedtest.py | 50 ++++++----------- .../battery_daughter.py | 40 ++++++-------- .../door_daughter.py | 42 ++++++--------- .../neopixel_daughter.py | 42 ++++++--------- .../neopixel_setter_daughter.py | 42 +++++---------- .../relay_daughter.py | 42 ++++++--------- .../relay_hi_daughter.py | 42 ++++++--------- .../temp_humid_daughter.py | 54 +++++++------------ examples/dash_display_simpletest.py | 50 ++++++----------- 10 files changed, 157 insertions(+), 282 deletions(-) diff --git a/README.rst b/README.rst index abbdd03..03f230c 100644 --- a/README.rst +++ b/README.rst @@ -43,12 +43,11 @@ Usage Example .. code :: python3 import time - import ssl import board from digitalio import DigitalInOut, Direction, Pull import touchio - import socketpool import wifi + import adafruit_connection_manager import adafruit_minimqtt.adafruit_minimqtt as MQTT from adafruit_io.adafruit_io import IO_MQTT from adafruit_dash_display import Hub @@ -68,34 +67,30 @@ Usage Example back = touchio.TouchIn(board.CAP7) submit = touchio.TouchIn(board.CAP8) - try: - from secrets import secrets - except ImportError: - print("WiFi secrets are kept in secrets.py, please add them there!") - raise + # 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") display = board.DISPLAY - # Set your Adafruit IO Username and Key in secrets.py - # (visit io.adafruit.com if you need to create an account, - # or if you need your Adafruit IO key.) - aio_username = secrets["aio_username"] - aio_key = secrets["aio_key"] - - print("Connecting to %s" % secrets["ssid"]) - wifi.radio.connect(secrets["ssid"], secrets["password"]) - print("Connected to %s!" % secrets["ssid"]) + print(f"Connecting to {ssid}") + wifi.radio.connect(ssid, password) + print(f"Connected to {ssid}!") # Create a socket pool - pool = socketpool.SocketPool(wifi.radio) + pool = adafruit_connection_manager.get_radio_socketpool(wifi.radio) + ssl_context = adafruit_connection_manager.get_radio_ssl_context(wifi.radio) # Initialize a new MQTT Client object mqtt_client = MQTT.MQTT( broker="io.adafruit.com", - username=secrets["aio_username"], - password=secrets["aio_key"], + username=aio_username, + password=aio_username, socket_pool=pool, - ssl_context=ssl.create_default_context(), + ssl_context=ssl_context, ) # Initialize an Adafruit IO MQTT Client diff --git a/examples/dash_display_advancedtest.py b/examples/dash_display_advancedtest.py index 34b4c07..74ccfd6 100644 --- a/examples/dash_display_advancedtest.py +++ b/examples/dash_display_advancedtest.py @@ -2,7 +2,6 @@ # SPDX-License-Identifier: MIT import time -import ssl from os import getenv import displayio import board @@ -10,8 +9,8 @@ from adafruit_display_text.label import Label import terminalio import touchio -import socketpool import wifi +import adafruit_connection_manager import adafruit_minimqtt.adafruit_minimqtt as MQTT from adafruit_io.adafruit_io import IO_MQTT from adafruit_dash_display import Hub @@ -31,24 +30,12 @@ back = touchio.TouchIn(board.CAP7) submit = touchio.TouchIn(board.CAP8) -# Get wifi details and more from a settings.toml file -# tokens used by this Demo: CIRCUITPY_WIFI_SSID, CIRCUITPY_WIFI_PASSWORD -# CIRCUITPY_AIO_USERNAME, CIRCUITPY_AIO_KEY -secrets = {} -for token in ["SSID", "PASSWORD"]: - if getenv("CIRCUITPY_WIFI_" + token): - secrets[token.lower()] = getenv("CIRCUITPY_WIFI_" + token) -for token in ["AIO_USERNAME", "AIO_KEY"]: - if getenv("CIRCUITPY_" + token): - secrets[token.lower()] = getenv("CIRCUITPY_" + token) - -if not secrets: - try: - # Fallback on secrets.py until depreciation is over and option is removed - from secrets import secrets - except ImportError: - print("WiFi secrets are kept in settings.toml, please add them there!") - raise +# 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") rgb_group = displayio.Group() R_label = Label( @@ -185,26 +172,21 @@ def pub_lamp(lamp): display = board.DISPLAY -# Set your Adafruit IO Username and Key in settings.toml -# (visit io.adafruit.com if you need to create an account, -# or if you need your Adafruit IO key.) -aio_username = secrets["aio_username"] -aio_key = secrets["aio_key"] +print(f"Connecting to {ssid}") +wifi.radio.connect(ssid, password) +print(f"Connected to {ssid}!") -print("Connecting to %s" % secrets["ssid"]) -wifi.radio.connect(secrets["ssid"], secrets["password"]) -print("Connected to %s!" % secrets["ssid"]) - -# Create a socket pool -pool = socketpool.SocketPool(wifi.radio) +# 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) # Initialize a new MQTT Client object mqtt_client = MQTT.MQTT( broker="io.adafruit.com", - username=secrets["aio_username"], - password=secrets["aio_key"], + username=aio_username, + password=aio_username, socket_pool=pool, - ssl_context=ssl.create_default_context(), + ssl_context=ssl_context, ) # Initialize an Adafruit IO MQTT Client diff --git a/examples/dash_display_client_examples/battery_daughter.py b/examples/dash_display_client_examples/battery_daughter.py index ced128a..a151363 100644 --- a/examples/dash_display_client_examples/battery_daughter.py +++ b/examples/dash_display_client_examples/battery_daughter.py @@ -21,26 +21,14 @@ displayio.release_displays() -### WiFi ### +# 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") -# Get wifi details and more from a settings.toml file -# tokens used by this Demo: CIRCUITPY_WIFI_SSID, CIRCUITPY_WIFI_PASSWORD -# CIRCUITPY_AIO_USERNAME, CIRCUITPY_AIO_KEY -secrets = {} -for token in ["SSID", "PASSWORD"]: - if getenv("CIRCUITPY_WIFI_" + token): - secrets[token.lower()] = getenv("CIRCUITPY_WIFI_" + token) -for token in ["AIO_USERNAME", "AIO_KEY"]: - if getenv("CIRCUITPY_" + token): - secrets[token.lower()] = getenv("CIRCUITPY_" + token) - -if not secrets: - try: - # Fallback on secrets.py until depreciation is over and option is removed - from secrets import secrets - except ImportError: - print("WiFi secrets are kept in settings.toml, please add them there!") - raise +### WiFi ### # If you are using a board with pre-defined ESP32 Pins: esp32_cs = DigitalInOut(board.ESP_CS) @@ -60,18 +48,20 @@ 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) +status_pixel = neopixel.NeoPixel(board.NEOPIXEL, 1, brightness=0.2) """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 (including Arduino Nano Connect)""" # 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) -wifi = adafruit_esp32spi_wifimanager.ESPSPI_WiFiManager(esp, secrets, status_light) +wifi = adafruit_esp32spi_wifimanager.WiFiManager( + esp, ssid, password, status_pixel=status_pixel +) # Define callback functions which will be called when certain events happen. @@ -100,8 +90,8 @@ def message(client, feed_id, payload): # Initialize a new MQTT Client object mqtt_client = MQTT.MQTT( broker="io.adafruit.com", - username=secrets["aio_username"], - password=secrets["aio_key"], + username=aio_username, + password=aio_username, socket_pool=pool, ssl_context=ssl_context, ) diff --git a/examples/dash_display_client_examples/door_daughter.py b/examples/dash_display_client_examples/door_daughter.py index ff95493..7920125 100644 --- a/examples/dash_display_client_examples/door_daughter.py +++ b/examples/dash_display_client_examples/door_daughter.py @@ -13,32 +13,20 @@ from digitalio import DigitalInOut, Direction, Pull from adafruit_debouncer import Debouncer -### WiFi ### - -# Get wifi details and more from a settings.toml file -# tokens used by this Demo: CIRCUITPY_WIFI_SSID, CIRCUITPY_WIFI_PASSWORD -# CIRCUITPY_AIO_USERNAME, CIRCUITPY_AIO_KEY -secrets = {} -for token in ["SSID", "PASSWORD"]: - if getenv("CIRCUITPY_WIFI_" + token): - secrets[token.lower()] = getenv("CIRCUITPY_WIFI_" + token) -for token in ["AIO_USERNAME", "AIO_KEY"]: - if getenv("CIRCUITPY_" + token): - secrets[token.lower()] = getenv("CIRCUITPY_" + token) - -if not secrets: - try: - # Fallback on secrets.py until depreciation is over and option is removed - from secrets import secrets - except ImportError: - print("WiFi secrets are kept in settings.toml, please add them there!") - raise +# 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") switch_pin = DigitalInOut(board.D10) switch_pin.direction = Direction.INPUT switch_pin.pull = Pull.UP switch = Debouncer(switch_pin) +### WiFi ### + # If you are using a board with pre-defined ESP32 Pins: esp32_cs = DigitalInOut(board.ESP_CS) esp32_ready = DigitalInOut(board.ESP_BUSY) @@ -57,18 +45,20 @@ 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) +status_pixel = neopixel.NeoPixel(board.NEOPIXEL, 1, brightness=0.2) """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 (including Arduino Nano Connect)""" # 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) -wifi = adafruit_esp32spi_wifimanager.ESPSPI_WiFiManager(esp, secrets, status_light) +wifi = adafruit_esp32spi_wifimanager.WiFiManager( + esp, ssid, password, status_pixel=status_pixel +) # Connect to WiFi print("Connecting to WiFi...") @@ -81,8 +71,8 @@ # Initialize a new MQTT Client object mqtt_client = MQTT.MQTT( broker="io.adafruit.com", - username=secrets["aio_username"], - password=secrets["aio_key"], + username=aio_username, + password=aio_username, socket_pool=pool, ssl_context=ssl_context, ) diff --git a/examples/dash_display_client_examples/neopixel_daughter.py b/examples/dash_display_client_examples/neopixel_daughter.py index f0d287e..1c9c390 100644 --- a/examples/dash_display_client_examples/neopixel_daughter.py +++ b/examples/dash_display_client_examples/neopixel_daughter.py @@ -12,29 +12,17 @@ from adafruit_io.adafruit_io import IO_MQTT from digitalio import DigitalInOut -### WiFi ### - -# Get wifi details and more from a settings.toml file -# tokens used by this Demo: CIRCUITPY_WIFI_SSID, CIRCUITPY_WIFI_PASSWORD -# CIRCUITPY_AIO_USERNAME, CIRCUITPY_AIO_KEY -secrets = {} -for token in ["SSID", "PASSWORD"]: - if getenv("CIRCUITPY_WIFI_" + token): - secrets[token.lower()] = getenv("CIRCUITPY_WIFI_" + token) -for token in ["AIO_USERNAME", "AIO_KEY"]: - if getenv("CIRCUITPY_" + token): - secrets[token.lower()] = getenv("CIRCUITPY_" + token) - -if not secrets: - try: - # Fallback on secrets.py until depreciation is over and option is removed - from secrets import secrets - except ImportError: - print("WiFi secrets are kept in settings.toml, please add them there!") - raise +# 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") pixels = neopixel.NeoPixel(board.D5, 300) +### WiFi ### + # If you are using a board with pre-defined ESP32 Pins: esp32_cs = DigitalInOut(board.ESP_CS) esp32_ready = DigitalInOut(board.ESP_BUSY) @@ -53,18 +41,20 @@ 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) +status_pixel = neopixel.NeoPixel(board.NEOPIXEL, 1, brightness=0.2) """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 (including Arduino Nano Connect)""" # 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) -wifi = adafruit_esp32spi_wifimanager.ESPSPI_WiFiManager(esp, secrets, status_light) +wifi = adafruit_esp32spi_wifimanager.WiFiManager( + esp, ssid, password, status_pixel=status_pixel +) # Define callback functions which will be called when certain events happen. @@ -98,8 +88,8 @@ def on_neopixel(client, topic, message): # Initialize a new MQTT Client object mqtt_client = MQTT.MQTT( broker="io.adafruit.com", - username=secrets["aio_username"], - password=secrets["aio_key"], + username=aio_username, + password=aio_username, socket_pool=pool, ssl_context=ssl_context, ) diff --git a/examples/dash_display_client_examples/neopixel_setter_daughter.py b/examples/dash_display_client_examples/neopixel_setter_daughter.py index d356019..0d6c749 100644 --- a/examples/dash_display_client_examples/neopixel_setter_daughter.py +++ b/examples/dash_display_client_examples/neopixel_setter_daughter.py @@ -93,24 +93,12 @@ group.append(rect) print(len(group)) -# Get wifi details and more from a settings.toml file -# tokens used by this Demo: CIRCUITPY_WIFI_SSID, CIRCUITPY_WIFI_PASSWORD -# CIRCUITPY_AIO_USERNAME, CIRCUITPY_AIO_KEY -secrets = {} -for token in ["SSID", "PASSWORD"]: - if getenv("CIRCUITPY_WIFI_" + token): - secrets[token.lower()] = getenv("CIRCUITPY_WIFI_" + token) -for token in ["AIO_USERNAME", "AIO_KEY"]: - if getenv("CIRCUITPY_" + token): - secrets[token.lower()] = getenv("CIRCUITPY_" + token) - -if not secrets: - try: - # Fallback on secrets.py until depreciation is over and option is removed - from secrets import secrets - except ImportError: - print("WiFi secrets are kept in settings.toml, please add them there!") - raise +# 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") # PyPortal ESP32 Setup esp32_cs = DigitalInOut(board.ESP_CS) @@ -124,27 +112,23 @@ 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) +status_pixel = neopixel.NeoPixel(board.NEOPIXEL, 1, brightness=0.2) """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 (including Arduino Nano Connect)""" # 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) -# Set your Adafruit IO Username and Key in settings.toml -# (visit io.adafruit.com if you need to create an account, -# or if you need your Adafruit IO key.) -ADAFRUIT_IO_USER = secrets["aio_username"] -ADAFRUIT_IO_KEY = secrets["aio_key"] +wifi = adafruit_esp32spi_wifimanager.WiFiManager( + esp, ssid, password, status_pixel=status_pixel +) # Create an instance of the Adafruit IO HTTP client -io = IO_HTTP(ADAFRUIT_IO_USER, ADAFRUIT_IO_KEY, wifi) +io = IO_HTTP(aio_username, aio_key, wifi) try: # Get the 'temperature' feed from Adafruit IO diff --git a/examples/dash_display_client_examples/relay_daughter.py b/examples/dash_display_client_examples/relay_daughter.py index a93aae6..b3ccf18 100644 --- a/examples/dash_display_client_examples/relay_daughter.py +++ b/examples/dash_display_client_examples/relay_daughter.py @@ -12,30 +12,18 @@ from adafruit_io.adafruit_io import IO_MQTT from digitalio import DigitalInOut, Direction -### WiFi ### - -# Get wifi details and more from a settings.toml file -# tokens used by this Demo: CIRCUITPY_WIFI_SSID, CIRCUITPY_WIFI_PASSWORD -# CIRCUITPY_AIO_USERNAME, CIRCUITPY_AIO_KEY -secrets = {} -for token in ["SSID", "PASSWORD"]: - if getenv("CIRCUITPY_WIFI_" + token): - secrets[token.lower()] = getenv("CIRCUITPY_WIFI_" + token) -for token in ["AIO_USERNAME", "AIO_KEY"]: - if getenv("CIRCUITPY_" + token): - secrets[token.lower()] = getenv("CIRCUITPY_" + token) - -if not secrets: - try: - # Fallback on secrets.py until depreciation is over and option is removed - from secrets import secrets - except ImportError: - print("WiFi secrets are kept in settings.toml, please add them there!") - raise +# 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") RELAY = DigitalInOut(board.D10) RELAY.direction = Direction.OUTPUT +### WiFi ### + # If you are using a board with pre-defined ESP32 Pins: esp32_cs = DigitalInOut(board.ESP_CS) esp32_ready = DigitalInOut(board.ESP_BUSY) @@ -55,18 +43,20 @@ 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) +status_pixel = neopixel.NeoPixel(board.NEOPIXEL, 1, brightness=0.2) """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 (including Arduino Nano Connect)""" # 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) -wifi = adafruit_esp32spi_wifimanager.ESPSPI_WiFiManager(esp, secrets, status_light) +wifi = adafruit_esp32spi_wifimanager.WiFiManager( + esp, ssid, password, status_pixel=status_pixel +) # Define callback functions which will be called when certain events happen. @@ -95,8 +85,8 @@ def on_lamp(client, topic, message): # Initialize a new MQTT Client object mqtt_client = MQTT.MQTT( broker="io.adafruit.com", - username=secrets["aio_username"], - password=secrets["aio_key"], + username=aio_username, + password=aio_username, socket_pool=pool, ssl_context=ssl_context, ) diff --git a/examples/dash_display_client_examples/relay_hi_daughter.py b/examples/dash_display_client_examples/relay_hi_daughter.py index 2dae472..a967605 100644 --- a/examples/dash_display_client_examples/relay_hi_daughter.py +++ b/examples/dash_display_client_examples/relay_hi_daughter.py @@ -12,30 +12,18 @@ from adafruit_io.adafruit_io import IO_MQTT from digitalio import DigitalInOut, Direction -### WiFi ### - -# Get wifi details and more from a settings.toml file -# tokens used by this Demo: CIRCUITPY_WIFI_SSID, CIRCUITPY_WIFI_PASSWORD -# CIRCUITPY_AIO_USERNAME, CIRCUITPY_AIO_KEY -secrets = {} -for token in ["SSID", "PASSWORD"]: - if getenv("CIRCUITPY_WIFI_" + token): - secrets[token.lower()] = getenv("CIRCUITPY_WIFI_" + token) -for token in ["AIO_USERNAME", "AIO_KEY"]: - if getenv("CIRCUITPY_" + token): - secrets[token.lower()] = getenv("CIRCUITPY_" + token) - -if not secrets: - try: - # Fallback on secrets.py until depreciation is over and option is removed - from secrets import secrets - except ImportError: - print("WiFi secrets are kept in settings.toml, please add them there!") - raise +# 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") RELAY = DigitalInOut(board.D10) RELAY.direction = Direction.OUTPUT +### WiFi ### + # If you are using a board with pre-defined ESP32 Pins: esp32_cs = DigitalInOut(board.ESP_CS) esp32_ready = DigitalInOut(board.ESP_BUSY) @@ -55,18 +43,20 @@ 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) +status_pixel = neopixel.NeoPixel(board.NEOPIXEL, 1, brightness=0.2) """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 (including Arduino Nano Connect)""" # 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) -wifi = adafruit_esp32spi_wifimanager.ESPSPI_WiFiManager(esp, secrets, status_light) +wifi = adafruit_esp32spi_wifimanager.WiFiManager( + esp, ssid, password, status_pixel=status_pixel +) # Define callback functions which will be called when certain events happen. @@ -98,8 +88,8 @@ def on_hi(client, topic, message): # Initialize a new MQTT Client object mqtt_client = MQTT.MQTT( broker="io.adafruit.com", - username=secrets["aio_username"], - password=secrets["aio_key"], + username=aio_username, + password=aio_username, socket_pool=pool, ssl_context=ssl_context, ) diff --git a/examples/dash_display_client_examples/temp_humid_daughter.py b/examples/dash_display_client_examples/temp_humid_daughter.py index bd4a5fa..f4b76ed 100644 --- a/examples/dash_display_client_examples/temp_humid_daughter.py +++ b/examples/dash_display_client_examples/temp_humid_daughter.py @@ -2,9 +2,8 @@ # SPDX-License-Identifier: MIT from os import getenv -import ssl -import socketpool import wifi +import adafruit_connection_manager import adafruit_minimqtt.adafruit_minimqtt as MQTT from adafruit_io.adafruit_io import IO_MQTT @@ -13,8 +12,6 @@ import board import adafruit_sht4x -### WiFi ### - # Add a settings.toml to your filesystem that has token variables "CIRCUITPY_WIFI_SSID" and # "CIRCUITPY_WIFI_PASSWORD" with your WiFi credentials. DO NOT share that file or commit it # into Git or other source control. @@ -22,46 +19,31 @@ magtag = MagTag() -# Get wifi details and more from a settings.toml file -# tokens used by this Demo: CIRCUITPY_WIFI_SSID, CIRCUITPY_WIFI_PASSWORD -# CIRCUITPY_AIO_USERNAME, CIRCUITPY_AIO_KEY -secrets = {} -for token in ["SSID", "PASSWORD"]: - if getenv("CIRCUITPY_WIFI_" + token): - secrets[token.lower()] = getenv("CIRCUITPY_WIFI_" + token) -for token in ["AIO_USERNAME", "AIO_KEY"]: - if getenv("CIRCUITPY_" + token): - secrets[token.lower()] = getenv("CIRCUITPY_" + token) - -if not secrets: - try: - # Fallback on secrets.py until depreciation is over and option is removed - from secrets import secrets - except ImportError: - print("WiFi secrets are kept in settings.toml, please add them there!") - raise - -# Set your Adafruit IO Username and Key in settings.toml -# (visit io.adafruit.com if you need to create an account, -# or if you need your Adafruit IO key.) -aio_username = secrets["aio_username"] -aio_key = secrets["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") + +### WiFi ### try: - print("Connecting to %s" % secrets["ssid"]) - wifi.radio.connect(secrets["ssid"], secrets["password"]) - print("Connected to %s!" % secrets["ssid"]) + print(f"Connecting to {ssid}") + wifi.radio.connect(ssid, password) + print(f"Connected to {ssid}!") - # Create a socket pool - pool = socketpool.SocketPool(wifi.radio) + # 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) # Initialize a new MQTT Client object mqtt_client = MQTT.MQTT( broker="io.adafruit.com", - username=secrets["aio_username"], - password=secrets["aio_key"], + username=aio_username, + password=aio_username, socket_pool=pool, - ssl_context=ssl.create_default_context(), + ssl_context=ssl_context, ) # Initialize an Adafruit IO MQTT Client diff --git a/examples/dash_display_simpletest.py b/examples/dash_display_simpletest.py index 772ce1b..a7a2e11 100644 --- a/examples/dash_display_simpletest.py +++ b/examples/dash_display_simpletest.py @@ -2,13 +2,12 @@ # SPDX-License-Identifier: MIT import time -import ssl from os import getenv import board from digitalio import DigitalInOut, Direction, Pull import touchio -import socketpool import wifi +import adafruit_connection_manager import adafruit_minimqtt.adafruit_minimqtt as MQTT from adafruit_io.adafruit_io import IO_MQTT from adafruit_dash_display import Hub @@ -28,47 +27,30 @@ back = touchio.TouchIn(board.CAP7) submit = touchio.TouchIn(board.CAP8) -# Get wifi details and more from a settings.toml file -# tokens used by this Demo: CIRCUITPY_WIFI_SSID, CIRCUITPY_WIFI_PASSWORD -# CIRCUITPY_AIO_USERNAME, CIRCUITPY_AIO_KEY -secrets = {} -for token in ["SSID", "PASSWORD"]: - if getenv("CIRCUITPY_WIFI_" + token): - secrets[token.lower()] = getenv("CIRCUITPY_WIFI_" + token) -for token in ["AIO_USERNAME", "AIO_KEY"]: - if getenv("CIRCUITPY_" + token): - secrets[token.lower()] = getenv("CIRCUITPY_" + token) - -if not secrets: - try: - # Fallback on secrets.py until depreciation is over and option is removed - from secrets import secrets - except ImportError: - print("WiFi secrets are kept in settings.toml, please add them there!") - raise +# 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") display = board.DISPLAY -# Set your Adafruit IO Username and Key in settings.toml -# (visit io.adafruit.com if you need to create an account, -# or if you need your Adafruit IO key.) -aio_username = secrets["aio_username"] -aio_key = secrets["aio_key"] +print(f"Connecting to {ssid}") +wifi.radio.connect(ssid, password) +print(f"Connected to {ssid}!") -print("Connecting to %s" % secrets["ssid"]) -wifi.radio.connect(secrets["ssid"], secrets["password"]) -print("Connected to %s!" % secrets["ssid"]) - -# Create a socket pool -pool = socketpool.SocketPool(wifi.radio) +# 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) # Initialize a new MQTT Client object mqtt_client = MQTT.MQTT( broker="io.adafruit.com", - username=secrets["aio_username"], - password=secrets["aio_key"], + username=aio_username, + password=aio_username, socket_pool=pool, - ssl_context=ssl.create_default_context(), + ssl_context=ssl_context, ) # Initialize an Adafruit IO MQTT Client From 9c29b782e346560a0f592d0682dc512494e62ace Mon Sep 17 00:00:00 2001 From: Justin Myers Date: Mon, 24 Feb 2025 17:20:10 -0800 Subject: [PATCH 2/2] Apply suggestions from code review Co-authored-by: Dan Halbert --- README.rst | 2 +- examples/dash_display_advancedtest.py | 2 +- examples/dash_display_client_examples/battery_daughter.py | 2 +- examples/dash_display_client_examples/door_daughter.py | 2 +- examples/dash_display_client_examples/neopixel_daughter.py | 2 +- examples/dash_display_client_examples/relay_daughter.py | 2 +- examples/dash_display_client_examples/relay_hi_daughter.py | 2 +- examples/dash_display_client_examples/temp_humid_daughter.py | 2 +- examples/dash_display_simpletest.py | 2 +- 9 files changed, 9 insertions(+), 9 deletions(-) diff --git a/README.rst b/README.rst index 03f230c..e1d339d 100644 --- a/README.rst +++ b/README.rst @@ -88,7 +88,7 @@ Usage Example mqtt_client = MQTT.MQTT( broker="io.adafruit.com", username=aio_username, - password=aio_username, + password=aio_key, socket_pool=pool, ssl_context=ssl_context, ) diff --git a/examples/dash_display_advancedtest.py b/examples/dash_display_advancedtest.py index 74ccfd6..259689b 100644 --- a/examples/dash_display_advancedtest.py +++ b/examples/dash_display_advancedtest.py @@ -184,7 +184,7 @@ def pub_lamp(lamp): mqtt_client = MQTT.MQTT( broker="io.adafruit.com", username=aio_username, - password=aio_username, + password=aio_key, socket_pool=pool, ssl_context=ssl_context, ) diff --git a/examples/dash_display_client_examples/battery_daughter.py b/examples/dash_display_client_examples/battery_daughter.py index a151363..51a340e 100644 --- a/examples/dash_display_client_examples/battery_daughter.py +++ b/examples/dash_display_client_examples/battery_daughter.py @@ -91,7 +91,7 @@ def message(client, feed_id, payload): mqtt_client = MQTT.MQTT( broker="io.adafruit.com", username=aio_username, - password=aio_username, + password=aio_key, socket_pool=pool, ssl_context=ssl_context, ) diff --git a/examples/dash_display_client_examples/door_daughter.py b/examples/dash_display_client_examples/door_daughter.py index 7920125..01b3966 100644 --- a/examples/dash_display_client_examples/door_daughter.py +++ b/examples/dash_display_client_examples/door_daughter.py @@ -72,7 +72,7 @@ mqtt_client = MQTT.MQTT( broker="io.adafruit.com", username=aio_username, - password=aio_username, + password=aio_key, socket_pool=pool, ssl_context=ssl_context, ) diff --git a/examples/dash_display_client_examples/neopixel_daughter.py b/examples/dash_display_client_examples/neopixel_daughter.py index 1c9c390..33a73a3 100644 --- a/examples/dash_display_client_examples/neopixel_daughter.py +++ b/examples/dash_display_client_examples/neopixel_daughter.py @@ -89,7 +89,7 @@ def on_neopixel(client, topic, message): mqtt_client = MQTT.MQTT( broker="io.adafruit.com", username=aio_username, - password=aio_username, + password=aio_key, socket_pool=pool, ssl_context=ssl_context, ) diff --git a/examples/dash_display_client_examples/relay_daughter.py b/examples/dash_display_client_examples/relay_daughter.py index b3ccf18..b3e6706 100644 --- a/examples/dash_display_client_examples/relay_daughter.py +++ b/examples/dash_display_client_examples/relay_daughter.py @@ -86,7 +86,7 @@ def on_lamp(client, topic, message): mqtt_client = MQTT.MQTT( broker="io.adafruit.com", username=aio_username, - password=aio_username, + password=aio_key, socket_pool=pool, ssl_context=ssl_context, ) diff --git a/examples/dash_display_client_examples/relay_hi_daughter.py b/examples/dash_display_client_examples/relay_hi_daughter.py index a967605..9d8c0a5 100644 --- a/examples/dash_display_client_examples/relay_hi_daughter.py +++ b/examples/dash_display_client_examples/relay_hi_daughter.py @@ -89,7 +89,7 @@ def on_hi(client, topic, message): mqtt_client = MQTT.MQTT( broker="io.adafruit.com", username=aio_username, - password=aio_username, + password=aio_key, socket_pool=pool, ssl_context=ssl_context, ) diff --git a/examples/dash_display_client_examples/temp_humid_daughter.py b/examples/dash_display_client_examples/temp_humid_daughter.py index f4b76ed..f69ad2b 100644 --- a/examples/dash_display_client_examples/temp_humid_daughter.py +++ b/examples/dash_display_client_examples/temp_humid_daughter.py @@ -41,7 +41,7 @@ mqtt_client = MQTT.MQTT( broker="io.adafruit.com", username=aio_username, - password=aio_username, + password=aio_key, socket_pool=pool, ssl_context=ssl_context, ) diff --git a/examples/dash_display_simpletest.py b/examples/dash_display_simpletest.py index a7a2e11..ec0a25e 100644 --- a/examples/dash_display_simpletest.py +++ b/examples/dash_display_simpletest.py @@ -48,7 +48,7 @@ mqtt_client = MQTT.MQTT( broker="io.adafruit.com", username=aio_username, - password=aio_username, + password=aio_key, socket_pool=pool, ssl_context=ssl_context, )