From 98c94a27177e97ccb1b63272f1f333272ae47140 Mon Sep 17 00:00:00 2001 From: Liz Date: Tue, 25 Apr 2023 11:05:54 -0400 Subject: [PATCH 01/11] Updating examples to use settings.toml Updating the esp32spi and native networking examples to use settings.toml --- .../esp32spi/minimqtt_adafruitio_esp32spi.py | 25 ++++++------- .../minimqtt_adafruitio_native_networking.py | 36 +++++++++---------- 2 files changed, 27 insertions(+), 34 deletions(-) diff --git a/examples/esp32spi/minimqtt_adafruitio_esp32spi.py b/examples/esp32spi/minimqtt_adafruitio_esp32spi.py index 7141e8bc..7edddcb8 100644 --- a/examples/esp32spi/minimqtt_adafruitio_esp32spi.py +++ b/examples/esp32spi/minimqtt_adafruitio_esp32spi.py @@ -1,25 +1,23 @@ # SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries # SPDX-License-Identifier: MIT +import os import time import board import busio from digitalio import DigitalInOut import neopixel from adafruit_esp32spi import adafruit_esp32spi -from adafruit_esp32spi import adafruit_esp32spi_wifimanager import adafruit_esp32spi.adafruit_esp32spi_socket as socket import adafruit_minimqtt.adafruit_minimqtt as MQTT -### WiFi ### +# 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. -# 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 +aio_username = os.getenv('aio_username') +aio_key = os.getenv('aio_key') # If you are using a board with pre-defined ESP32 Pins: esp32_cs = DigitalInOut(board.ESP_CS) @@ -46,15 +44,14 @@ # 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) ### Feeds ### # Setup a feed named 'photocell' for publishing to a feed -photocell_feed = secrets["aio_username"] + "/feeds/photocell" +photocell_feed = aio_username + "/feeds/photocell" # Setup a feed named 'onoff' for subscribing to changes -onoff_feed = secrets["aio_username"] + "/feeds/onoff" +onoff_feed = aio_username + "/feeds/onoff" ### Code ### @@ -81,7 +78,7 @@ def message(client, topic, message): # Connect to WiFi print("Connecting to WiFi...") -wifi.connect() +esp.connect_AP(os.getenv('CIRCUITPY_WIFI_SSID'), os.getenv('CIRCUITPY_WIFI_PASSWORD')) print("Connected!") # Initialize MQTT interface with the esp interface @@ -90,8 +87,8 @@ def message(client, topic, message): # Set up a MiniMQTT Client mqtt_client = MQTT.MQTT( broker="io.adafruit.com", - username=secrets["aio_username"], - password=secrets["aio_key"], + username=aio_username, + password=aio_key, ) # Setup the callback methods above diff --git a/examples/native_networking/minimqtt_adafruitio_native_networking.py b/examples/native_networking/minimqtt_adafruitio_native_networking.py index 21661d31..7e42abcc 100644 --- a/examples/native_networking/minimqtt_adafruitio_native_networking.py +++ b/examples/native_networking/minimqtt_adafruitio_native_networking.py @@ -1,38 +1,34 @@ # SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries # SPDX-License-Identifier: MIT +import os import time import ssl import socketpool import wifi import adafruit_minimqtt.adafruit_minimqtt as MQTT -# Add a secrets.py to your filesystem that has a dictionary called secrets with "ssid" and -# "password" keys with your WiFi credentials. DO NOT share that file or commit it into Git or other +# 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. -# pylint: disable=no-name-in-module,wrong-import-order -try: - from secrets import secrets -except ImportError: - print("WiFi secrets are kept in secrets.py, please add them there!") - raise - -# Set your Adafruit IO Username and Key in secrets.py + +# 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 = secrets["aio_username"] -aio_key = secrets["aio_key"] +aio_username = os.getenv('aio_username') +aio_key = os.getenv('aio_key') +aio_port = os.getenv('port') -print("Connecting to %s" % secrets["ssid"]) -wifi.radio.connect(secrets["ssid"], secrets["password"]) -print("Connected to %s!" % secrets["ssid"]) +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')) ### Feeds ### # Setup a feed named 'photocell' for publishing to a feed -photocell_feed = secrets["aio_username"] + "/feeds/photocell" +photocell_feed = aio_username + "/feeds/photocell" # Setup a feed named 'onoff' for subscribing to changes -onoff_feed = secrets["aio_username"] + "/feeds/onoff" +onoff_feed = aio_username + "/feeds/onoff" ### Code ### @@ -63,9 +59,9 @@ def message(client, topic, message): # Set up a MiniMQTT Client mqtt_client = MQTT.MQTT( broker="io.adafruit.com", - port=secrets["port"], - username=secrets["aio_username"], - password=secrets["aio_key"], + port=aio_port, + username=aio_username, + password=aio_key, socket_pool=pool, ssl_context=ssl.create_default_context(), ) From e5f953009fd3b96184a910b1559a7a768fb55bd5 Mon Sep 17 00:00:00 2001 From: Liz Date: Tue, 25 Apr 2023 11:10:23 -0400 Subject: [PATCH 02/11] black --- examples/esp32spi/minimqtt_adafruitio_esp32spi.py | 7 ++++--- .../minimqtt_adafruitio_native_networking.py | 15 +++++++++------ 2 files changed, 13 insertions(+), 9 deletions(-) diff --git a/examples/esp32spi/minimqtt_adafruitio_esp32spi.py b/examples/esp32spi/minimqtt_adafruitio_esp32spi.py index 7edddcb8..68bf3dd6 100644 --- a/examples/esp32spi/minimqtt_adafruitio_esp32spi.py +++ b/examples/esp32spi/minimqtt_adafruitio_esp32spi.py @@ -16,8 +16,8 @@ # 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') +aio_username = os.getenv("aio_username") +aio_key = os.getenv("aio_key") # If you are using a board with pre-defined ESP32 Pins: esp32_cs = DigitalInOut(board.ESP_CS) @@ -55,6 +55,7 @@ ### Code ### + # Define callback methods which are called when events occur # pylint: disable=unused-argument, redefined-outer-name def connected(client, userdata, flags, rc): @@ -78,7 +79,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(os.getenv("CIRCUITPY_WIFI_SSID"), os.getenv("CIRCUITPY_WIFI_PASSWORD")) print("Connected!") # Initialize MQTT interface with the esp interface diff --git a/examples/native_networking/minimqtt_adafruitio_native_networking.py b/examples/native_networking/minimqtt_adafruitio_native_networking.py index 7e42abcc..a4bce577 100644 --- a/examples/native_networking/minimqtt_adafruitio_native_networking.py +++ b/examples/native_networking/minimqtt_adafruitio_native_networking.py @@ -15,13 +15,15 @@ # 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') -aio_port = os.getenv('port') +aio_username = os.getenv("aio_username") +aio_key = os.getenv("aio_key") +aio_port = os.getenv("port") -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("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")) ### Feeds ### # Setup a feed named 'photocell' for publishing to a feed @@ -32,6 +34,7 @@ ### Code ### + # Define callback methods which are called when events occur # pylint: disable=unused-argument, redefined-outer-name def connected(client, userdata, flags, rc): From 1ad80850f76afb792b0cc7a822d61d2caebc5659 Mon Sep 17 00:00:00 2001 From: Liz Date: Tue, 25 Apr 2023 11:19:47 -0400 Subject: [PATCH 03/11] updating simpletest --- examples/minimqtt_simpletest.py | 40 ++++++++++++++++----------------- 1 file changed, 20 insertions(+), 20 deletions(-) diff --git a/examples/minimqtt_simpletest.py b/examples/minimqtt_simpletest.py index 2f1c49b5..17927443 100644 --- a/examples/minimqtt_simpletest.py +++ b/examples/minimqtt_simpletest.py @@ -1,30 +1,29 @@ # SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries # SPDX-License-Identifier: MIT +import os import ssl import socketpool import wifi import adafruit_minimqtt.adafruit_minimqtt as MQTT -# Add a secrets.py to your filesystem that has a dictionary called secrets with "ssid" and -# "password" keys with your WiFi credentials. DO NOT share that file or commit it into Git or other +# 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. -# pylint: disable=no-name-in-module,wrong-import-order -try: - from secrets import secrets -except ImportError: - print("WiFi secrets are kept in secrets.py, please add them there!") - raise - -# Set your Adafruit IO Username and Key in secrets.py + +# 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("Connecting to %s" % secrets["ssid"]) -wifi.radio.connect(secrets["ssid"], secrets["password"]) -print("Connected to %s!" % secrets["ssid"]) +aio_username = os.getenv("aio_username") +aio_key = os.getenv("aio_key") +aio_broker = os.getenv("broker") +aio_port = os.getenv("port") + +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")) ### Topic Setup ### @@ -36,6 +35,7 @@ # Use this topic if you'd like to connect to io.adafruit.com # mqtt_topic = secrets["aio_username"] + '/feeds/temperature' + ### Code ### # Define callback methods which are called when events occur # pylint: disable=unused-argument, redefined-outer-name @@ -77,10 +77,10 @@ def message(client, topic, message): # Set up a MiniMQTT Client mqtt_client = MQTT.MQTT( - broker=secrets["broker"], - port=secrets["port"], - username=secrets["aio_username"], - password=secrets["aio_key"], + broker=aio_broker, + port=aio_port, + username=os.getenv("CIRCUITPY_WIFI_SSID"), + password=os.getenv("CIRCUITPY_WIFI_PASSWORD"), socket_pool=pool, ssl_context=ssl.create_default_context(), ) From 466120471e538dd37b183e30033263eee6a11aa7 Mon Sep 17 00:00:00 2001 From: Liz Date: Tue, 25 Apr 2023 11:33:26 -0400 Subject: [PATCH 04/11] updating pub sub examples --- .../minimqtt_pub_sub_blocking_esp32spi.py | 28 ++++++++++--------- .../minimqtt_pub_sub_nonblocking_esp32spi.py | 22 +++++++-------- 2 files changed, 25 insertions(+), 25 deletions(-) diff --git a/examples/esp32spi/minimqtt_pub_sub_blocking_esp32spi.py b/examples/esp32spi/minimqtt_pub_sub_blocking_esp32spi.py index 734816ea..7f19067a 100644 --- a/examples/esp32spi/minimqtt_pub_sub_blocking_esp32spi.py +++ b/examples/esp32spi/minimqtt_pub_sub_blocking_esp32spi.py @@ -1,25 +1,23 @@ # SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries # SPDX-License-Identifier: MIT +import os import time import board import busio from digitalio import DigitalInOut import neopixel from adafruit_esp32spi import adafruit_esp32spi -from adafruit_esp32spi import adafruit_esp32spi_wifimanager import adafruit_esp32spi.adafruit_esp32spi_socket as socket import adafruit_minimqtt.adafruit_minimqtt as MQTT -### WiFi ### +# 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. -# 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 +aio_username = os.getenv("aio_username") +aio_key = os.getenv("aio_key") # If you are using a board with pre-defined ESP32 Pins: esp32_cs = DigitalInOut(board.ESP_CS) @@ -46,15 +44,15 @@ # 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) ### Adafruit IO Setup ### # Setup a feed named `testfeed` for publishing. -default_topic = secrets["user"] + "/feeds/testfeed" +default_topic = aio_username + "/feeds/testfeed" ### Code ### + # Define callback methods which are called when events occur # pylint: disable=unused-argument, redefined-outer-name def connected(client, userdata, flags, rc): @@ -81,7 +79,7 @@ def message(client, topic, message): # Connect to WiFi print("Connecting to WiFi...") -wifi.connect() +esp.connect_AP(os.getenv("CIRCUITPY_WIFI_SSID"), os.getenv("CIRCUITPY_WIFI_PASSWORD")) print("Connected!") # Initialize MQTT interface with the esp interface @@ -89,7 +87,7 @@ def message(client, topic, message): # Set up a MiniMQTT Client mqtt_client = MQTT.MQTT( - broker=secrets["broker"], username=secrets["user"], password=secrets["pass"] + broker=os.getenv("broker"), username=aio_username, password=aio_key ) # Setup the callback methods above @@ -109,7 +107,11 @@ def message(client, topic, message): mqtt_client.loop() except (ValueError, RuntimeError) as e: print("Failed to get data, retrying\n", e) - wifi.reset() + esp.reset() + time.sleep(1) + esp.connect_AP( + os.getenv("CIRCUITPY_WIFI_SSID"), os.getenv("CIRCUITPY_WIFI_PASSWORD") + ) mqtt_client.reconnect() continue time.sleep(1) diff --git a/examples/esp32spi/minimqtt_pub_sub_nonblocking_esp32spi.py b/examples/esp32spi/minimqtt_pub_sub_nonblocking_esp32spi.py index 6ff98d27..525e01b9 100644 --- a/examples/esp32spi/minimqtt_pub_sub_nonblocking_esp32spi.py +++ b/examples/esp32spi/minimqtt_pub_sub_nonblocking_esp32spi.py @@ -1,25 +1,23 @@ # SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries # SPDX-License-Identifier: MIT +import os import time import board import busio from digitalio import DigitalInOut import neopixel from adafruit_esp32spi import adafruit_esp32spi -from adafruit_esp32spi import adafruit_esp32spi_wifimanager import adafruit_esp32spi.adafruit_esp32spi_socket as socket import adafruit_minimqtt.adafruit_minimqtt as MQTT -### WiFi ### +# 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. -# 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 +aio_username = os.getenv("aio_username") +aio_key = os.getenv("aio_key") # If you are using a board with pre-defined ESP32 Pins: esp32_cs = DigitalInOut(board.ESP_CS) @@ -46,12 +44,12 @@ # 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) ### Adafruit IO Setup ### # Setup a feed named `testfeed` for publishing. -default_topic = secrets["user"] + "/feeds/testfeed" +default_topic = aio_username + "/feeds/testfeed" + ### Code ### # Define callback methods which are called when events occur @@ -80,7 +78,7 @@ def message(client, topic, message): # Connect to WiFi print("Connecting to WiFi...") -wifi.connect() +esp.connect_AP(os.getenv("CIRCUITPY_WIFI_SSID"), os.getenv("CIRCUITPY_WIFI_PASSWORD")) print("Connected!") # Initialize MQTT interface with the esp interface @@ -88,7 +86,7 @@ def message(client, topic, message): # Set up a MiniMQTT Client mqtt_client = MQTT.MQTT( - broker=secrets["broker"], username=secrets["user"], password=secrets["pass"] + broker=os.getenv("broker"), username=aio_username, password=aio_key ) # Setup the callback methods above From 8212c464ddf75d3fa5d3232937a675436c90f501 Mon Sep 17 00:00:00 2001 From: Liz Date: Tue, 25 Apr 2023 11:50:37 -0400 Subject: [PATCH 05/11] updating mqtt_client --- examples/esp32spi/minimqtt_pub_sub_blocking_esp32spi.py | 2 +- examples/esp32spi/minimqtt_pub_sub_nonblocking_esp32spi.py | 2 +- examples/minimqtt_simpletest.py | 6 ++---- .../minimqtt_adafruitio_native_networking.py | 7 +++---- 4 files changed, 7 insertions(+), 10 deletions(-) diff --git a/examples/esp32spi/minimqtt_pub_sub_blocking_esp32spi.py b/examples/esp32spi/minimqtt_pub_sub_blocking_esp32spi.py index 7f19067a..a4e508c9 100644 --- a/examples/esp32spi/minimqtt_pub_sub_blocking_esp32spi.py +++ b/examples/esp32spi/minimqtt_pub_sub_blocking_esp32spi.py @@ -87,7 +87,7 @@ def message(client, topic, message): # Set up a MiniMQTT Client mqtt_client = MQTT.MQTT( - broker=os.getenv("broker"), username=aio_username, password=aio_key + broker="io.adafruit.com", username=aio_username, password=aio_key ) # Setup the callback methods above diff --git a/examples/esp32spi/minimqtt_pub_sub_nonblocking_esp32spi.py b/examples/esp32spi/minimqtt_pub_sub_nonblocking_esp32spi.py index 525e01b9..9bf3755b 100644 --- a/examples/esp32spi/minimqtt_pub_sub_nonblocking_esp32spi.py +++ b/examples/esp32spi/minimqtt_pub_sub_nonblocking_esp32spi.py @@ -86,7 +86,7 @@ def message(client, topic, message): # Set up a MiniMQTT Client mqtt_client = MQTT.MQTT( - broker=os.getenv("broker"), username=aio_username, password=aio_key + broker="io.adafruit.com", username=aio_username, password=aio_key ) # Setup the callback methods above diff --git a/examples/minimqtt_simpletest.py b/examples/minimqtt_simpletest.py index 17927443..2faa8067 100644 --- a/examples/minimqtt_simpletest.py +++ b/examples/minimqtt_simpletest.py @@ -16,8 +16,6 @@ # or if you need your Adafruit IO key.) aio_username = os.getenv("aio_username") aio_key = os.getenv("aio_key") -aio_broker = os.getenv("broker") -aio_port = os.getenv("port") print("Connecting to %s" % os.getenv("CIRCUITPY_WIFI_SSID")) wifi.radio.connect( @@ -77,8 +75,8 @@ def message(client, topic, message): # Set up a MiniMQTT Client mqtt_client = MQTT.MQTT( - broker=aio_broker, - port=aio_port, + broker="io.adafruit.com", + port=8883, username=os.getenv("CIRCUITPY_WIFI_SSID"), password=os.getenv("CIRCUITPY_WIFI_PASSWORD"), socket_pool=pool, diff --git a/examples/native_networking/minimqtt_adafruitio_native_networking.py b/examples/native_networking/minimqtt_adafruitio_native_networking.py index a4bce577..d4e892ba 100644 --- a/examples/native_networking/minimqtt_adafruitio_native_networking.py +++ b/examples/native_networking/minimqtt_adafruitio_native_networking.py @@ -17,7 +17,6 @@ # or if you need your Adafruit IO key.) aio_username = os.getenv("aio_username") aio_key = os.getenv("aio_key") -aio_port = os.getenv("port") print("Connecting to %s" % os.getenv("CIRCUITPY_WIFI_SSID")) wifi.radio.connect( @@ -62,9 +61,9 @@ def message(client, topic, message): # Set up a MiniMQTT Client mqtt_client = MQTT.MQTT( broker="io.adafruit.com", - port=aio_port, - username=aio_username, - password=aio_key, + port=8883, + username=os.getenv("CIRCUITPY_WIFI_SSID"), + password=os.getenv("CIRCUITPY_WIFI_PASSWORD"), socket_pool=pool, ssl_context=ssl.create_default_context(), ) From eca04848c226b396a9355a9bdce7e0b7265dda4d Mon Sep 17 00:00:00 2001 From: Liz Date: Tue, 25 Apr 2023 11:52:50 -0400 Subject: [PATCH 06/11] fixing simpletest --- examples/minimqtt_simpletest.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/examples/minimqtt_simpletest.py b/examples/minimqtt_simpletest.py index 2faa8067..397cbbe9 100644 --- a/examples/minimqtt_simpletest.py +++ b/examples/minimqtt_simpletest.py @@ -31,7 +31,7 @@ # Adafruit IO-style Topic # Use this topic if you'd like to connect to io.adafruit.com -# mqtt_topic = secrets["aio_username"] + '/feeds/temperature' +# mqtt_topic = aio_username + '/feeds/temperature' ### Code ### @@ -77,8 +77,8 @@ def message(client, topic, message): mqtt_client = MQTT.MQTT( broker="io.adafruit.com", port=8883, - username=os.getenv("CIRCUITPY_WIFI_SSID"), - password=os.getenv("CIRCUITPY_WIFI_PASSWORD"), + username=aio_username, + password=aio_key, socket_pool=pool, ssl_context=ssl.create_default_context(), ) From 0794d69018d27325cd7baf25bd265e32cc35f54e Mon Sep 17 00:00:00 2001 From: Liz Date: Fri, 7 Jul 2023 10:53:52 -0400 Subject: [PATCH 07/11] Updating examples for settings dot toml Retested examples using a Feather ESP32-S2 for native and a Metro M7 for esp32spi --- .../esp32spi/minimqtt_simpletest_esp32spi.py | 1 + examples/minimqtt_simpletest.py | 58 ++++++++++++------- .../minimqtt_adafruitio_native_networking.py | 16 ++--- ...mqtt_pub_sub_blocking_native_networking.py | 38 ++++++------ ...cking_topic_callbacks_native_networking.py | 44 +++++++------- 5 files changed, 83 insertions(+), 74 deletions(-) diff --git a/examples/esp32spi/minimqtt_simpletest_esp32spi.py b/examples/esp32spi/minimqtt_simpletest_esp32spi.py index 254253dc..96a71474 100644 --- a/examples/esp32spi/minimqtt_simpletest_esp32spi.py +++ b/examples/esp32spi/minimqtt_simpletest_esp32spi.py @@ -57,6 +57,7 @@ ### Code ### + # Define callback methods which are called when events occur # pylint: disable=unused-argument, redefined-outer-name def connect(mqtt_client, userdata, flags, rc): diff --git a/examples/minimqtt_simpletest.py b/examples/minimqtt_simpletest.py index 397cbbe9..5bc6524f 100644 --- a/examples/minimqtt_simpletest.py +++ b/examples/minimqtt_simpletest.py @@ -2,39 +2,57 @@ # SPDX-License-Identifier: MIT import os -import ssl -import socketpool -import wifi +import board +import busio +from digitalio import DigitalInOut +from adafruit_esp32spi import adafruit_esp32spi +import adafruit_esp32spi.adafruit_esp32spi_socket as socket 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. +# 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. -# 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 = 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")) +# If you are using a board with pre-defined ESP32 Pins: +esp32_cs = DigitalInOut(board.ESP_CS) +esp32_ready = DigitalInOut(board.ESP_BUSY) +esp32_reset = DigitalInOut(board.ESP_RESET) + +# If you have an externally connected ESP32: +# esp32_cs = DigitalInOut(board.D9) +# esp32_ready = DigitalInOut(board.D10) +# esp32_reset = DigitalInOut(board.D5) + +spi = busio.SPI(board.SCK, board.MOSI, board.MISO) +esp = adafruit_esp32spi.ESP_SPIcontrol(spi, esp32_cs, esp32_ready, esp32_reset) + +print("Connecting to AP...") +while not esp.is_connected: + try: + esp.connect_AP( + os.getenv("CIRCUITPY_WIFI_SSID"), os.getenv("CIRCUITPY_WIFI_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) ### Topic Setup ### # MQTT Topic # Use this topic if you'd like to connect to a standard MQTT broker -mqtt_topic = "test/topic" +# mqtt_topic = "test/topic" # 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 = aio_username + "/feeds/temperature" ### Code ### + + # Define callback methods which are called when events occur # pylint: disable=unused-argument, redefined-outer-name def connect(mqtt_client, userdata, flags, rc): @@ -66,21 +84,17 @@ def publish(mqtt_client, userdata, topic, pid): def message(client, topic, message): - # Method called when a client's subscribed feed has a new value. print("New message on topic {0}: {1}".format(topic, message)) -# Create a socket pool -pool = socketpool.SocketPool(wifi.radio) +socket.set_interface(esp) +MQTT.set_socket(socket, esp) # Set up a MiniMQTT Client mqtt_client = MQTT.MQTT( broker="io.adafruit.com", - port=8883, username=aio_username, password=aio_key, - socket_pool=pool, - ssl_context=ssl.create_default_context(), ) # Connect callback handlers to mqtt_client diff --git a/examples/native_networking/minimqtt_adafruitio_native_networking.py b/examples/native_networking/minimqtt_adafruitio_native_networking.py index d4e892ba..9e7c89e3 100644 --- a/examples/native_networking/minimqtt_adafruitio_native_networking.py +++ b/examples/native_networking/minimqtt_adafruitio_native_networking.py @@ -18,11 +18,11 @@ aio_username = os.getenv("aio_username") aio_key = os.getenv("aio_key") -print("Connecting to %s" % os.getenv("CIRCUITPY_WIFI_SSID")) +print(f"Connecting to {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"Connected to {os.getenv('CIRCUITPY_WIFI_SSID')}!") ### Feeds ### # Setup a feed named 'photocell' for publishing to a feed @@ -39,7 +39,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) @@ -52,7 +52,7 @@ def disconnected(client, userdata, rc): def message(client, topic, message): # This method is called when a topic the client is subscribed to # has a new message. - print("New message on topic {0}: {1}".format(topic, message)) + print(f"New message on topic {topic}: {message}") # Create a socket pool @@ -61,9 +61,9 @@ def message(client, topic, message): # Set up a MiniMQTT Client mqtt_client = MQTT.MQTT( broker="io.adafruit.com", - port=8883, - username=os.getenv("CIRCUITPY_WIFI_SSID"), - password=os.getenv("CIRCUITPY_WIFI_PASSWORD"), + port=1883, + username=aio_username, + password=aio_key, socket_pool=pool, ssl_context=ssl.create_default_context(), ) @@ -83,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/native_networking/minimqtt_pub_sub_blocking_native_networking.py b/examples/native_networking/minimqtt_pub_sub_blocking_native_networking.py index 58dbc7f7..bbcab99e 100644 --- a/examples/native_networking/minimqtt_pub_sub_blocking_native_networking.py +++ b/examples/native_networking/minimqtt_pub_sub_blocking_native_networking.py @@ -1,36 +1,34 @@ # SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries # SPDX-License-Identifier: MIT +import os import time import ssl import socketpool import wifi import adafruit_minimqtt.adafruit_minimqtt as MQTT -# Add a secrets.py to your filesystem that has a dictionary called secrets with "ssid" and -# "password" keys with your WiFi credentials. DO NOT share that file or commit it into Git or other +# 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. -# pylint: disable=no-name-in-module,wrong-import-order -try: - from secrets import secrets -except ImportError: - print("WiFi secrets are kept in secrets.py, please add them there!") - raise - -# Set your Adafruit IO Username and Key in secrets.py + +# 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 = secrets["aio_username"] -aio_key = secrets["aio_key"] +aio_username = os.getenv("aio_username") +aio_key = os.getenv("aio_key") -print("Connecting to %s" % secrets["ssid"]) -wifi.radio.connect(secrets["ssid"], secrets["password"]) -print("Connected to %s!" % secrets["ssid"]) +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")) ### Adafruit IO Setup ### # Setup a feed named `testfeed` for publishing. -default_topic = secrets["aio_username"] + "/feeds/testfeed" +default_topic = aio_username + "/feeds/testfeed" + ### Code ### # Define callback methods which are called when events occur @@ -62,10 +60,10 @@ def message(client, topic, message): # Set up a MiniMQTT Client mqtt_client = MQTT.MQTT( - broker=secrets["broker"], - port=secrets["port"], - username=secrets["aio_username"], - password=secrets["aio_key"], + broker="io.adafruit.com", + port=1883, + username=aio_username, + password=aio_key, socket_pool=pool, ssl_context=ssl.create_default_context(), ) 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 2a2eddf3..7f18f55b 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,34 +1,32 @@ # SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries # SPDX-License-Identifier: MIT +import os import time import ssl import socketpool import wifi import adafruit_minimqtt.adafruit_minimqtt as MQTT -# Add a secrets.py to your filesystem that has a dictionary called secrets with "ssid" and -# "password" keys with your WiFi credentials. DO NOT share that file or commit it into Git or other +# 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. -# pylint: disable=no-name-in-module,wrong-import-order -try: - from secrets import secrets -except ImportError: - print("WiFi secrets are kept in secrets.py, please add them there!") - raise - -# Set your Adafruit IO Username and Key in secrets.py + +# 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 = secrets["aio_username"] -aio_key = secrets["aio_key"] +aio_username = os.getenv("aio_username") +aio_key = os.getenv("aio_key") -print("Connecting to %s" % secrets["ssid"]) -wifi.radio.connect(secrets["ssid"], secrets["password"]) -print("Connected to %s!" % secrets["ssid"]) +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")) ### Code ### + # Define callback methods which are called when events occur # pylint: disable=unused-argument, redefined-outer-name def connected(client, userdata, flags, rc): @@ -56,7 +54,7 @@ def on_battery_msg(client, topic, message): # Method called when device/batteryLife has a new value print("Battery level: {}v".format(message)) - # 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): @@ -69,10 +67,10 @@ def on_message(client, topic, message): # Set up a MiniMQTT Client client = MQTT.MQTT( - broker=secrets["broker"], - port=secrets["port"], - username=secrets["aio_username"], - password=secrets["aio_key"], + broker="io.adafruit.com", + port=1883, + username=aio_username, + password=aio_key, socket_pool=pool, ssl_context=ssl.create_default_context(), ) @@ -83,16 +81,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 From e8532e043252e0e9e178231d8d0898e57a43c565 Mon Sep 17 00:00:00 2001 From: Liz Date: Fri, 7 Jul 2023 11:06:09 -0400 Subject: [PATCH 08/11] black --- examples/esp32spi/minimqtt_pub_sub_nonblocking_esp32spi.py | 6 +++--- .../minimqtt_pub_sub_blocking_native_networking.py | 4 ++-- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/examples/esp32spi/minimqtt_pub_sub_nonblocking_esp32spi.py b/examples/esp32spi/minimqtt_pub_sub_nonblocking_esp32spi.py index 9bf3755b..d0ae3feb 100644 --- a/examples/esp32spi/minimqtt_pub_sub_nonblocking_esp32spi.py +++ b/examples/esp32spi/minimqtt_pub_sub_nonblocking_esp32spi.py @@ -57,7 +57,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) @@ -73,7 +73,7 @@ def message(client, topic, message): :param str topic: The topic of the feed with a new value. :param str message: The new value """ - print("New message on topic {0}: {1}".format(topic, message)) + print(f"New message on topic {topic}: {message}") # Connect to WiFi @@ -103,7 +103,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(default_topic, photocell_val) photocell_val += 1 time.sleep(0.5) 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 bbcab99e..384d4df1 100644 --- a/examples/native_networking/minimqtt_pub_sub_blocking_native_networking.py +++ b/examples/native_networking/minimqtt_pub_sub_blocking_native_networking.py @@ -36,7 +36,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) @@ -52,7 +52,7 @@ def message(client, topic, message): :param str topic: The topic of the feed with a new value. :param str message: The new value """ - print("New message on topic {0}: {1}".format(topic, message)) + print(f"New message on topic {topic}: {message}") # Create a socket pool From 91a0f13048cebcc3b39150cebd61a938e2e9b79b Mon Sep 17 00:00:00 2001 From: Liz Date: Fri, 7 Jul 2023 11:24:36 -0400 Subject: [PATCH 09/11] black --- examples/esp32spi/minimqtt_pub_sub_nonblocking_esp32spi.py | 2 +- .../minimqtt_pub_sub_blocking_native_networking.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/examples/esp32spi/minimqtt_pub_sub_nonblocking_esp32spi.py b/examples/esp32spi/minimqtt_pub_sub_nonblocking_esp32spi.py index d0ae3feb..758d805e 100644 --- a/examples/esp32spi/minimqtt_pub_sub_nonblocking_esp32spi.py +++ b/examples/esp32spi/minimqtt_pub_sub_nonblocking_esp32spi.py @@ -1,4 +1,4 @@ -# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries +# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries # SPDX-License-Identifier: MIT import os 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 384d4df1..55684dcf 100644 --- a/examples/native_networking/minimqtt_pub_sub_blocking_native_networking.py +++ b/examples/native_networking/minimqtt_pub_sub_blocking_native_networking.py @@ -1,4 +1,4 @@ -# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries +# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries # SPDX-License-Identifier: MIT import os From 1da654e232d2b8e23a5dae806ac529bda4d3fa2b Mon Sep 17 00:00:00 2001 From: Liz Date: Fri, 7 Jul 2023 11:25:50 -0400 Subject: [PATCH 10/11] black --- examples/esp32spi/minimqtt_pub_sub_nonblocking_esp32spi.py | 2 +- .../minimqtt_pub_sub_blocking_native_networking.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/examples/esp32spi/minimqtt_pub_sub_nonblocking_esp32spi.py b/examples/esp32spi/minimqtt_pub_sub_nonblocking_esp32spi.py index 758d805e..d0ae3feb 100644 --- a/examples/esp32spi/minimqtt_pub_sub_nonblocking_esp32spi.py +++ b/examples/esp32spi/minimqtt_pub_sub_nonblocking_esp32spi.py @@ -1,4 +1,4 @@ -# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries +# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries # SPDX-License-Identifier: MIT import os 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 55684dcf..384d4df1 100644 --- a/examples/native_networking/minimqtt_pub_sub_blocking_native_networking.py +++ b/examples/native_networking/minimqtt_pub_sub_blocking_native_networking.py @@ -1,4 +1,4 @@ -# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries +# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries # SPDX-License-Identifier: MIT import os From 6b96ab3c5cc97b21a307419275f10a73c157e5ad Mon Sep 17 00:00:00 2001 From: foamyguy Date: Fri, 7 Jul 2023 12:52:58 -0500 Subject: [PATCH 11/11] merge main and code format --- examples/esp32spi/minimqtt_pub_sub_nonblocking_esp32spi.py | 1 - .../minimqtt_pub_sub_blocking_native_networking.py | 1 - 2 files changed, 2 deletions(-) diff --git a/examples/esp32spi/minimqtt_pub_sub_nonblocking_esp32spi.py b/examples/esp32spi/minimqtt_pub_sub_nonblocking_esp32spi.py index 77e84bf9..d0ae3feb 100644 --- a/examples/esp32spi/minimqtt_pub_sub_nonblocking_esp32spi.py +++ b/examples/esp32spi/minimqtt_pub_sub_nonblocking_esp32spi.py @@ -51,7 +51,6 @@ default_topic = aio_username + "/feeds/testfeed" - ### Code ### # Define callback methods which are called when events occur # pylint: disable=unused-argument, redefined-outer-name 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 569a58c6..0ba76dfb 100644 --- a/examples/native_networking/minimqtt_pub_sub_blocking_native_networking.py +++ b/examples/native_networking/minimqtt_pub_sub_blocking_native_networking.py @@ -30,7 +30,6 @@ default_topic = aio_username + "/feeds/testfeed" - ### Code ### # Define callback methods which are called when events occur # pylint: disable=unused-argument, redefined-outer-name