diff --git a/examples/esp32spi/minimqtt_adafruitio_esp32spi.py b/examples/esp32spi/minimqtt_adafruitio_esp32spi.py index 53242b6..68bf3dd 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 ### @@ -82,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 @@ -91,8 +88,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/esp32spi/minimqtt_pub_sub_blocking_esp32spi.py b/examples/esp32spi/minimqtt_pub_sub_blocking_esp32spi.py index 8c323ba..a4e508c 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,12 +44,11 @@ # 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 ### @@ -82,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 @@ -90,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="io.adafruit.com", username=aio_username, password=aio_key ) # Setup the callback methods above @@ -110,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 8a5ce8b..d0ae3fe 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,11 @@ # 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 ### @@ -60,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) @@ -76,12 +73,12 @@ 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 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 +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="io.adafruit.com", username=aio_username, password=aio_key ) # Setup the callback methods above @@ -106,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/minimqtt_simpletest.py b/examples/minimqtt_simpletest.py index 9defad6..f9ab277 100644 --- a/examples/minimqtt_simpletest.py +++ b/examples/minimqtt_simpletest.py @@ -1,43 +1,59 @@ # SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries # SPDX-License-Identifier: MIT -import ssl -import socketpool -import wifi +import os +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 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 -# 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 -# (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"]) +# 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") + +# 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 = secrets["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): @@ -69,21 +85,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=secrets["broker"], - port=secrets["port"], - username=secrets["aio_username"], - password=secrets["aio_key"], - socket_pool=pool, - ssl_context=ssl.create_default_context(), + broker="io.adafruit.com", + username=aio_username, + password=aio_key, ) # 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 75720b3..512b83e 100644 --- a/examples/native_networking/minimqtt_adafruitio_native_networking.py +++ b/examples/native_networking/minimqtt_adafruitio_native_networking.py @@ -1,38 +1,35 @@ # 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(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')}!") ### 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 ### @@ -42,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) @@ -55,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 @@ -74,9 +71,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=1883, + username=aio_username, + password=aio_key, socket_pool=pool, ssl_context=ssl_context, ) @@ -96,7 +93,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 b296eac..0ba76df 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,33 @@ # 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 ### @@ -39,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) @@ -55,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 @@ -73,10 +70,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_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 f38b627..82b9ce1 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,31 +1,28 @@ # 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 ### @@ -57,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): @@ -80,10 +77,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_context, ) @@ -94,16 +91,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