diff --git a/examples/adafruit_io_http/adafruit_io_analog_in.py b/examples/adafruit_io_http/adafruit_io_analog_in.py index fe327e5..bbbda7d 100644 --- a/examples/adafruit_io_http/adafruit_io_analog_in.py +++ b/examples/adafruit_io_http/adafruit_io_analog_in.py @@ -3,6 +3,7 @@ # Example of publishing the value of an ADC to Adafruit IO # adafruit_circuitpython_adafruitio with an esp32spi_socket +from os import getenv import time import board import busio @@ -13,15 +14,12 @@ import adafruit_requests from adafruit_io.adafruit_io import IO_HTTP, AdafruitIO_RequestError -# 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 +# Get WiFi details and Adafruit IO keys, ensure these are setup in settings.toml +# (visit io.adafruit.com if you need to create an account, or if you need your Adafruit IO key.) +ssid = getenv("CIRCUITPY_WIFI_SSID") +password = getenv("CIRCUITPY_WIFI_PASSWORD") +aio_username = getenv("ADAFRUIT_AIO_USERNAME") +aio_key = getenv("ADAFRUIT_AIO_KEY") # If you are using a board with pre-defined ESP32 Pins: esp32_cs = DigitalInOut(board.ESP_CS) @@ -39,7 +37,7 @@ print("Connecting to AP...") while not esp.is_connected: try: - esp.connect_AP(secrets["ssid"], secrets["password"]) + esp.connect_AP(ssid, password) except RuntimeError as e: print("could not connect to AP, retrying: ", e) continue @@ -50,12 +48,6 @@ ssl_context = adafruit_connection_manager.get_radio_ssl_context(esp) requests = adafruit_requests.Session(pool, ssl_context) -# 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"] - # Initialize an Adafruit IO HTTP API object io = IO_HTTP(aio_username, aio_key, requests) diff --git a/examples/adafruit_io_http/adafruit_io_batch_cpython.py b/examples/adafruit_io_http/adafruit_io_batch_cpython.py index 16835a9..4423fad 100644 --- a/examples/adafruit_io_http/adafruit_io_batch_cpython.py +++ b/examples/adafruit_io_http/adafruit_io_batch_cpython.py @@ -3,38 +3,19 @@ # adafruit_circuitpython_adafruitio usage for batch data with a CPython socket. import datetime +from os import getenv import socket import ssl from random import randint import adafruit_requests from adafruit_io.adafruit_io import IO_HTTP, AdafruitIO_RequestError -# Add a secrets.py to your filesystem that has a dictionary called secrets with "aio_username" -# and "aio_key" entries with your IO credentials, or set environment variables/defaults below. -# *** 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: - import os - - secrets = { - "aio_username": os.getenv("ADAFRUIT_AIO_USERNAME", "Your_Username_Here"), - "aio_key": os.getenv("ADAFRUIT_AIO_KEY", "Your_Adafruit_IO_Key_Here"), - } - if ( - secrets["aio_key"] == "Your_Adafruit_IO_Key_Here" - or secrets["aio_username"] == "Your_Username_Here" - ): - print("Adafruit IO 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"] - +# 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") requests = adafruit_requests.Session(socket, ssl.create_default_context()) # Initialize an Adafruit IO HTTP API object diff --git a/examples/adafruit_io_http/adafruit_io_create_and_get_feed.py b/examples/adafruit_io_http/adafruit_io_create_and_get_feed.py index 7b687f6..0eac01b 100644 --- a/examples/adafruit_io_http/adafruit_io_create_and_get_feed.py +++ b/examples/adafruit_io_http/adafruit_io_create_and_get_feed.py @@ -4,37 +4,28 @@ Example using create_and_get_feed. Creates a new feed if it does not exist and sends to it, or sends to an existing feed once it has been created. """ -import ssl +from os import getenv import adafruit_requests -import socketpool import wifi import microcontroller +import adafruit_connection_manager from adafruit_io.adafruit_io import IO_HTTP -# Add a secrets.py to your filesystem that has a dictionary called secrets with "ssid" and -# "password" keys with your WiFi credentials, and "aio_username" and "aio_key" keys with your -# Adafruit IO 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 and Adafruit IO credentials 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") -# Connect to Wi-Fi using credentials from secrets.py -wifi.radio.connect(secrets["ssid"], secrets["password"]) -print("Connected to {}!".format(secrets["ssid"])) +# Connect to Wi-Fi using credentials from settings.toml +wifi.radio.connect(ssid, password) +print("Connected to {}!".format(ssid)) print("IP:", wifi.radio.ipv4_address) -pool = socketpool.SocketPool(wifi.radio) -requests = adafruit_requests.Session(pool, ssl.create_default_context()) - -# Obtain Adafruit IO credentials from secrets.py -aio_username = secrets["aio_username"] -aio_key = secrets["aio_key"] +pool = adafruit_connection_manager.get_radio_socketpool(wifi.radio) +ssl_context = adafruit_connection_manager.get_radio_ssl_context(wifi.radio) +requests = adafruit_requests.Session(pool, ssl_context) # Initialize an Adafruit IO HTTP API object io = IO_HTTP(aio_username, aio_key, requests) diff --git a/examples/adafruit_io_http/adafruit_io_digital_out.py b/examples/adafruit_io_http/adafruit_io_digital_out.py index 6a1ee04..5385b39 100644 --- a/examples/adafruit_io_http/adafruit_io_digital_out.py +++ b/examples/adafruit_io_http/adafruit_io_digital_out.py @@ -4,6 +4,7 @@ # Turn on and off a LED from your Adafruit IO Dashboard. # adafruit_circuitpython_adafruitio with an esp32spi_socket import time +from os import getenv import board import busio from digitalio import DigitalInOut, Direction @@ -12,15 +13,12 @@ import adafruit_requests from adafruit_io.adafruit_io import IO_HTTP, AdafruitIO_RequestError -# 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 +# Get WiFi details and Adafruit IO keys, ensure these are setup in settings.toml +# (visit io.adafruit.com if you need to create an account, or if you need your Adafruit IO key.) +ssid = getenv("CIRCUITPY_WIFI_SSID") +password = getenv("CIRCUITPY_WIFI_PASSWORD") +aio_username = getenv("ADAFRUIT_AIO_USERNAME") +aio_key = getenv("ADAFRUIT_AIO_KEY") # If you are using a board with pre-defined ESP32 Pins: esp32_cs = DigitalInOut(board.ESP_CS) @@ -38,7 +36,7 @@ print("Connecting to AP...") while not esp.is_connected: try: - esp.connect_AP(secrets["ssid"], secrets["password"]) + esp.connect_AP(ssid, password) except RuntimeError as e: print("could not connect to AP, retrying: ", e) continue @@ -49,12 +47,6 @@ ssl_context = adafruit_connection_manager.get_radio_ssl_context(esp) requests = adafruit_requests.Session(pool, ssl_context) -# 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"] - # Initialize an Adafruit IO HTTP API object io = IO_HTTP(aio_username, aio_key, requests) diff --git a/examples/adafruit_io_http/adafruit_io_feeds.py b/examples/adafruit_io_http/adafruit_io_feeds.py index 7637a06..ea4874c 100644 --- a/examples/adafruit_io_http/adafruit_io_feeds.py +++ b/examples/adafruit_io_http/adafruit_io_feeds.py @@ -4,6 +4,7 @@ # Adafruit IO HTTP API - Feed Interactions # Documentation: https://io.adafruit.com/api/docs/#feeds # adafruit_circuitpython_adafruitio with an esp32spi_socket +from os import getenv import board import busio from digitalio import DigitalInOut @@ -12,15 +13,12 @@ import adafruit_requests from adafruit_io.adafruit_io import IO_HTTP -# 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 +# Get WiFi details and Adafruit IO keys, ensure these are setup in settings.toml +# (visit io.adafruit.com if you need to create an account, or if you need your Adafruit IO key.) +ssid = getenv("CIRCUITPY_WIFI_SSID") +password = getenv("CIRCUITPY_WIFI_PASSWORD") +aio_username = getenv("ADAFRUIT_AIO_USERNAME") +aio_key = getenv("ADAFRUIT_AIO_KEY") # If you are using a board with pre-defined ESP32 Pins: esp32_cs = DigitalInOut(board.ESP_CS) @@ -38,7 +36,7 @@ print("Connecting to AP...") while not esp.is_connected: try: - esp.connect_AP(secrets["ssid"], secrets["password"]) + esp.connect_AP(ssid, password) except RuntimeError as e: print("could not connect to AP, retrying: ", e) continue @@ -49,12 +47,6 @@ ssl_context = adafruit_connection_manager.get_radio_ssl_context(esp) requests = adafruit_requests.Session(pool, ssl_context) -# 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"] - # Initialize an Adafruit IO HTTP API object io = IO_HTTP(aio_username, aio_key, requests) diff --git a/examples/adafruit_io_http/adafruit_io_groups.py b/examples/adafruit_io_http/adafruit_io_groups.py index 2e1754b..5911f33 100644 --- a/examples/adafruit_io_http/adafruit_io_groups.py +++ b/examples/adafruit_io_http/adafruit_io_groups.py @@ -4,6 +4,7 @@ # Adafruit IO HTTP API - Group Interactions # Documentation: https://io.adafruit.com/api/docs/#groups # adafruit_circuitpython_adafruitio with an esp32spi_socket +from os import getenv import adafruit_datetime as datetime import board import busio @@ -13,28 +14,12 @@ import adafruit_requests from adafruit_io.adafruit_io import IO_HTTP - -# Add a secrets.py to your filesystem that has a dictionary called secrets with "ssid" and -# "password" keys with your WiFi credentials, along with "aio_username" and "aio_key" for -# your Adafruit IO user/key. 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: - import os - - if os.getenv("ADAFRUIT_AIO_USERNAME") and os.getenv("ADAFRUIT_AIO_KEY"): - secrets = { - "aio_username": os.getenv("ADAFRUIT_AIO_USERNAME", "Your_Username_Here"), - "aio_key": os.getenv("ADAFRUIT_AIO_KEY", "Your_Adafruit_IO_Key_Here"), - "ssid": os.getenv("CIRCUITPY_WIFI_SSID", ""), - "password": os.getenv("CIRCUITPY_WIFI_PASSWORD", ""), - } - else: - print( - "WiFi + Adafruit IO 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") # If you are using a board with pre-defined ESP32 Pins: esp32_cs = DigitalInOut(board.ESP_CS) @@ -52,14 +37,14 @@ print("Connecting to AP...") while not esp.is_connected: try: - esp.connect_AP(secrets["ssid"], secrets["password"]) + esp.connect_AP(ssid, password) except RuntimeError as e: print("could not connect to AP, retrying: ", e) continue print("Connected to", str(esp.ssid, "utf-8"), "\tRSSI:", esp.rssi) # If you are using a wifi based mcu use this instead of esp code above, remove the from -# adafruit_esp32spi import line, optionally esp.connect(secrets["ssid"], secrets["password"]) +# adafruit_esp32spi import line, optionally esp.connect(ssid, password) # import wifi # esp = wifi.radio @@ -71,19 +56,8 @@ # If you are testing on python with blinka, use real requests below and comment out above: # import os, datetime, requests as real_requests # from adafruit_io.adafruit_io import IO_HTTP -# secrets = { -# "aio_username": os.getenv("ADAFRUIT_AIO_USERNAME"), -# "aio_key": os.getenv("ADAFRUIT_AIO_KEY"), -# } # requests = real_requests.Session() - -# 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"] - # Initialize an Adafruit IO HTTP API object io = IO_HTTP(aio_username, aio_key, requests) diff --git a/examples/adafruit_io_http/adafruit_io_metadata.py b/examples/adafruit_io_http/adafruit_io_metadata.py index 9bf27d5..de6c339 100644 --- a/examples/adafruit_io_http/adafruit_io_metadata.py +++ b/examples/adafruit_io_http/adafruit_io_metadata.py @@ -3,6 +3,7 @@ # Adafruit IO HTTP API - Sending values with optional metadata # adafruit_circuitpython_adafruitio with an esp32spi_socket +from os import getenv import board import busio from digitalio import DigitalInOut @@ -11,15 +12,12 @@ import adafruit_requests from adafruit_io.adafruit_io import IO_HTTP, AdafruitIO_RequestError -# 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 +# Get WiFi details and Adafruit IO keys, ensure these are setup in settings.toml +# (visit io.adafruit.com if you need to create an account, or if you need your Adafruit IO key.) +ssid = getenv("CIRCUITPY_WIFI_SSID") +password = getenv("CIRCUITPY_WIFI_PASSWORD") +aio_username = getenv("ADAFRUIT_AIO_USERNAME") +aio_key = getenv("ADAFRUIT_AIO_KEY") # If you are using a board with pre-defined ESP32 Pins: esp32_cs = DigitalInOut(board.ESP_CS) @@ -37,7 +35,7 @@ print("Connecting to AP...") while not esp.is_connected: try: - esp.connect_AP(secrets["ssid"], secrets["password"]) + esp.connect_AP(ssid, password) except RuntimeError as e: print("could not connect to AP, retrying: ", e) continue @@ -48,12 +46,6 @@ ssl_context = adafruit_connection_manager.get_radio_ssl_context(esp) requests = adafruit_requests.Session(pool, ssl_context) -# 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"] - # Initialize an Adafruit IO HTTP API object io = IO_HTTP(aio_username, aio_key, requests) diff --git a/examples/adafruit_io_http/adafruit_io_randomizer.py b/examples/adafruit_io_http/adafruit_io_randomizer.py index 920bf01..a7b46db 100644 --- a/examples/adafruit_io_http/adafruit_io_randomizer.py +++ b/examples/adafruit_io_http/adafruit_io_randomizer.py @@ -4,6 +4,7 @@ # Example for using Adafruit IO's random data (randomizer) service # adafruit_circuitpython_adafruitio with an esp32spi_socket import time +from os import getenv import board import busio from digitalio import DigitalInOut @@ -12,15 +13,12 @@ import adafruit_requests from adafruit_io.adafruit_io import IO_HTTP -# 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 +# Get WiFi details and Adafruit IO keys, ensure these are setup in settings.toml +# (visit io.adafruit.com if you need to create an account, or if you need your Adafruit IO key.) +ssid = getenv("CIRCUITPY_WIFI_SSID") +password = getenv("CIRCUITPY_WIFI_PASSWORD") +aio_username = getenv("ADAFRUIT_AIO_USERNAME") +aio_key = getenv("ADAFRUIT_AIO_KEY") # If you are using a board with pre-defined ESP32 Pins: esp32_cs = DigitalInOut(board.ESP_CS) @@ -38,7 +36,7 @@ print("Connecting to AP...") while not esp.is_connected: try: - esp.connect_AP(secrets["ssid"], secrets["password"]) + esp.connect_AP(ssid, password) except RuntimeError as e: print("could not connect to AP, retrying: ", e) continue @@ -49,12 +47,6 @@ ssl_context = adafruit_connection_manager.get_radio_ssl_context(esp) requests = adafruit_requests.Session(pool, ssl_context) -# 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"] - # Initialize an Adafruit IO HTTP API object io = IO_HTTP(aio_username, aio_key, requests) diff --git a/examples/adafruit_io_http/adafruit_io_simpletest.py b/examples/adafruit_io_http/adafruit_io_simpletest.py index 836e0be..1d0b8a1 100644 --- a/examples/adafruit_io_http/adafruit_io_simpletest.py +++ b/examples/adafruit_io_http/adafruit_io_simpletest.py @@ -2,36 +2,27 @@ # SPDX-License-Identifier: MIT # adafruit_circuitpython_adafruitio usage with native wifi networking -import ssl +from os import getenv from random import randint import adafruit_requests -import socketpool import wifi +import adafruit_connection_manager from adafruit_io.adafruit_io import IO_HTTP, AdafruitIO_RequestError -# 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"]) +# Get WiFi details and Adafruit IO keys, ensure these are setup in settings.toml +# (visit io.adafruit.com if you need to create an account, or if you need your Adafruit IO key.) +ssid = getenv("CIRCUITPY_WIFI_SSID") +password = getenv("CIRCUITPY_WIFI_PASSWORD") +aio_username = getenv("ADAFRUIT_AIO_USERNAME") +aio_key = getenv("ADAFRUIT_AIO_KEY") +print("Connecting to %s" % ssid) +wifi.radio.connect(ssid, password) +print("Connected to %s!" % ssid) -pool = socketpool.SocketPool(wifi.radio) -requests = adafruit_requests.Session(pool, ssl.create_default_context()) +pool = adafruit_connection_manager.get_radio_socketpool(wifi.radio) +ssl_context = adafruit_connection_manager.get_radio_ssl_context(wifi.radio) +requests = adafruit_requests.Session(pool, ssl_context) # Initialize an Adafruit IO HTTP API object io = IO_HTTP(aio_username, aio_key, requests) diff --git a/examples/adafruit_io_http/adafruit_io_simpletest_cpython.py b/examples/adafruit_io_http/adafruit_io_simpletest_cpython.py index 52f5031..dd5ac50 100644 --- a/examples/adafruit_io_http/adafruit_io_simpletest_cpython.py +++ b/examples/adafruit_io_http/adafruit_io_simpletest_cpython.py @@ -2,28 +2,19 @@ # SPDX-License-Identifier: MIT # adafruit_circuitpython_adafruitio usage with a CPython socket +from os import getenv import socket import ssl from random import randint import adafruit_requests from adafruit_io.adafruit_io import IO_HTTP, AdafruitIO_RequestError -# 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"] - +# 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") requests = adafruit_requests.Session(socket, ssl.create_default_context()) # Initialize an Adafruit IO HTTP API object diff --git a/examples/adafruit_io_http/adafruit_io_simpletest_esp32spi.py b/examples/adafruit_io_http/adafruit_io_simpletest_esp32spi.py index 0fe2df4..a7e0ac0 100644 --- a/examples/adafruit_io_http/adafruit_io_simpletest_esp32spi.py +++ b/examples/adafruit_io_http/adafruit_io_simpletest_esp32spi.py @@ -2,6 +2,7 @@ # SPDX-License-Identifier: MIT # adafruit_circuitpython_adafruitio usage with an esp32spi_socket +from os import getenv from random import randint import board import busio @@ -11,15 +12,12 @@ import adafruit_requests from adafruit_io.adafruit_io import IO_HTTP, AdafruitIO_RequestError -# 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 +# Get WiFi details and Adafruit IO keys, ensure these are setup in settings.toml +# (visit io.adafruit.com if you need to create an account, or if you need your Adafruit IO key.) +ssid = getenv("CIRCUITPY_WIFI_SSID") +password = getenv("CIRCUITPY_WIFI_PASSWORD") +aio_username = getenv("ADAFRUIT_AIO_USERNAME") +aio_key = getenv("ADAFRUIT_AIO_KEY") # If you are using a board with pre-defined ESP32 Pins: esp32_cs = DigitalInOut(board.ESP_CS) @@ -37,7 +35,7 @@ print("Connecting to AP...") while not esp.is_connected: try: - esp.connect_AP(secrets["ssid"], secrets["password"]) + esp.connect_AP(ssid, password) except RuntimeError as e: print("could not connect to AP, retrying: ", e) continue @@ -48,12 +46,6 @@ ssl_context = adafruit_connection_manager.get_radio_ssl_context(esp) requests = adafruit_requests.Session(pool, ssl_context) -# 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"] - # Initialize an Adafruit IO HTTP API object io = IO_HTTP(aio_username, aio_key, requests) diff --git a/examples/adafruit_io_http/adafruit_io_temperature.py b/examples/adafruit_io_http/adafruit_io_temperature.py index 989260e..969ad90 100644 --- a/examples/adafruit_io_http/adafruit_io_temperature.py +++ b/examples/adafruit_io_http/adafruit_io_temperature.py @@ -4,6 +4,7 @@ # Example of sending ADT7410 sensor temperature values to IO # adafruit_circuitpython_adafruitio with an esp32spi_socket import time +from os import getenv import board import busio from digitalio import DigitalInOut @@ -13,15 +14,12 @@ import adafruit_adt7410 from adafruit_io.adafruit_io import IO_HTTP, AdafruitIO_RequestError -# 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 +# Get WiFi details and Adafruit IO keys, ensure these are setup in settings.toml +# (visit io.adafruit.com if you need to create an account, or if you need your Adafruit IO key.) +ssid = getenv("CIRCUITPY_WIFI_SSID") +password = getenv("CIRCUITPY_WIFI_PASSWORD") +aio_username = getenv("ADAFRUIT_AIO_USERNAME") +aio_key = getenv("ADAFRUIT_AIO_KEY") # If you are using a board with pre-defined ESP32 Pins: esp32_cs = DigitalInOut(board.ESP_CS) @@ -39,7 +37,7 @@ print("Connecting to AP...") while not esp.is_connected: try: - esp.connect_AP(secrets["ssid"], secrets["password"]) + esp.connect_AP(ssid, password) except RuntimeError as e: print("could not connect to AP, retrying: ", e) continue @@ -50,12 +48,6 @@ ssl_context = adafruit_connection_manager.get_radio_ssl_context(esp) requests = adafruit_requests.Session(pool, ssl_context) -# 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"] - # Initialize an Adafruit IO HTTP API object io = IO_HTTP(aio_username, aio_key, requests) diff --git a/examples/adafruit_io_http/adafruit_io_user_info.py b/examples/adafruit_io_http/adafruit_io_user_info.py index 3351311..fe9a0f6 100644 --- a/examples/adafruit_io_http/adafruit_io_user_info.py +++ b/examples/adafruit_io_http/adafruit_io_user_info.py @@ -2,37 +2,27 @@ # SPDX-License-Identifier: MIT # retrieve user rate info via adafruit_circuitpython_adafruitio with native wifi networking -import ssl +from os import getenv import time # pylint: disable=unused-import import adafruit_requests -import socketpool import wifi +import adafruit_connection_manager from adafruit_io.adafruit_io import IO_HTTP -# 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. +# 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") -# 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 +print("Connecting to %s" % ssid) +wifi.radio.connect(ssid, password) +print("Connected to %s!" % ssid) -# 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"]) - - -pool = socketpool.SocketPool(wifi.radio) -requests = adafruit_requests.Session(pool, ssl.create_default_context()) +pool = adafruit_connection_manager.get_radio_socketpool(wifi.radio) +ssl_context = adafruit_connection_manager.get_radio_ssl_context(wifi.radio) +requests = adafruit_requests.Session(pool, ssl_context) # Initialize an Adafruit IO HTTP API object io = IO_HTTP(aio_username, aio_key, requests) diff --git a/examples/adafruit_io_http/adafruit_io_weather.py b/examples/adafruit_io_http/adafruit_io_weather.py index 376cf83..5731858 100644 --- a/examples/adafruit_io_http/adafruit_io_weather.py +++ b/examples/adafruit_io_http/adafruit_io_weather.py @@ -3,6 +3,7 @@ # Example of using the Adafruit IO+ Weather Service # adafruit_circuitpython_adafruitio with an esp32spi_socket +from os import getenv import board import busio from digitalio import DigitalInOut @@ -11,15 +12,12 @@ import adafruit_requests from adafruit_io.adafruit_io import IO_HTTP -# 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 +# Get WiFi details and Adafruit IO keys, ensure these are setup in settings.toml +# (visit io.adafruit.com if you need to create an account, or if you need your Adafruit IO key.) +ssid = getenv("CIRCUITPY_WIFI_SSID") +password = getenv("CIRCUITPY_WIFI_PASSWORD") +aio_username = getenv("ADAFRUIT_AIO_USERNAME") +aio_key = getenv("ADAFRUIT_AIO_KEY") # If you are using a board with pre-defined ESP32 Pins: esp32_cs = DigitalInOut(board.ESP_CS) @@ -37,7 +35,7 @@ print("Connecting to AP...") while not esp.is_connected: try: - esp.connect_AP(secrets["ssid"], secrets["password"]) + esp.connect_AP(ssid, password) except RuntimeError as e: print("could not connect to AP, retrying: ", e) continue @@ -48,12 +46,6 @@ ssl_context = adafruit_connection_manager.get_radio_ssl_context(esp) requests = adafruit_requests.Session(pool, ssl_context) -# 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"] - # Initialize an Adafruit IO HTTP API object io = IO_HTTP(aio_username, aio_key, requests) diff --git a/examples/adafruit_io_mqtt/adafruit_io_feed_callback.py b/examples/adafruit_io_mqtt/adafruit_io_feed_callback.py index 442ca91..594fe71 100644 --- a/examples/adafruit_io_mqtt/adafruit_io_feed_callback.py +++ b/examples/adafruit_io_mqtt/adafruit_io_feed_callback.py @@ -2,7 +2,7 @@ # SPDX-License-Identifier: MIT import time - +from os import getenv import board import busio from digitalio import DigitalInOut @@ -13,14 +13,14 @@ import adafruit_minimqtt.adafruit_minimqtt as MQTT from adafruit_io.adafruit_io import IO_MQTT -### 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 secrets.py file -try: - from secrets import secrets -except ImportError: - print("WiFi secrets are kept in secrets.py, please add them there!") - raise +### WiFi ### # If you are using a board with pre-defined ESP32 Pins: esp32_cs = DigitalInOut(board.ESP_CS) @@ -35,19 +35,21 @@ spi = busio.SPI(board.SCK, board.MOSI, board.MISO) esp = adafruit_esp32spi.ESP_SPIcontrol(spi, esp32_cs, esp32_ready, esp32_reset) """Use below for Most Boards""" -status_light = neopixel.NeoPixel( +status_pixel = neopixel.NeoPixel( board.NEOPIXEL, 1, brightness=0.2 ) # Uncomment for Most Boards """Uncomment below for ItsyBitsy M4""" -# status_light = dotstar.DotStar(board.APA102_SCK, board.APA102_MOSI, 1, brightness=0.2) +# status_pixel = dotstar.DotStar(board.APA102_SCK, board.APA102_MOSI, 1, brightness=0.2) # Uncomment below for an externally defined RGB LED # import adafruit_rgbled # from adafruit_esp32spi import PWMOut # RED_LED = PWMOut.PWMOut(esp, 26) # GREEN_LED = PWMOut.PWMOut(esp, 27) # BLUE_LED = PWMOut.PWMOut(esp, 25) -# status_light = adafruit_rgbled.RGBLED(RED_LED, BLUE_LED, GREEN_LED) -wifi = adafruit_esp32spi_wifimanager.ESPSPI_WiFiManager(esp, secrets, status_light) +# status_pixel = adafruit_rgbled.RGBLED(RED_LED, BLUE_LED, GREEN_LED) +wifi = adafruit_esp32spi_wifimanager.WiFiManager( + esp, ssid, password, status_pixel=status_pixel +) # Define callback functions which will be called when certain events happen. @@ -110,8 +112,8 @@ def on_battery_msg(client, topic, message): mqtt_client = MQTT.MQTT( broker="io.adafruit.com", port=1883, - username=secrets["aio_username"], - password=secrets["aio_key"], + username=aio_username, + password=aio_key, socket_pool=pool, ssl_context=ssl_context, ) diff --git a/examples/adafruit_io_mqtt/adafruit_io_groups.py b/examples/adafruit_io_mqtt/adafruit_io_groups.py index 0634ac1..7294fd7 100755 --- a/examples/adafruit_io_mqtt/adafruit_io_groups.py +++ b/examples/adafruit_io_mqtt/adafruit_io_groups.py @@ -4,6 +4,7 @@ # Subscribing to an Adafruit IO Group # and Publishing to the feeds in the group import time +from os import getenv from random import randint import board @@ -16,14 +17,14 @@ import adafruit_minimqtt.adafruit_minimqtt as MQTT from adafruit_io.adafruit_io import IO_MQTT -### 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 secrets.py file -try: - from secrets import secrets -except ImportError: - print("WiFi secrets are kept in secrets.py, please add them there!") - raise +### WiFi ### # If you are using a board with pre-defined ESP32 Pins: esp32_cs = DigitalInOut(board.ESP_CS) @@ -38,19 +39,21 @@ spi = busio.SPI(board.SCK, board.MOSI, board.MISO) esp = adafruit_esp32spi.ESP_SPIcontrol(spi, esp32_cs, esp32_ready, esp32_reset) """Use below for Most Boards""" -status_light = neopixel.NeoPixel( +status_pixel = neopixel.NeoPixel( board.NEOPIXEL, 1, brightness=0.2 ) # Uncomment for Most Boards """Uncomment below for ItsyBitsy M4""" -# status_light = dotstar.DotStar(board.APA102_SCK, board.APA102_MOSI, 1, brightness=0.2) +# status_pixel = dotstar.DotStar(board.APA102_SCK, board.APA102_MOSI, 1, brightness=0.2) # Uncomment below for an externally defined RGB LED # import adafruit_rgbled # from adafruit_esp32spi import PWMOut # RED_LED = PWMOut.PWMOut(esp, 26) # GREEN_LED = PWMOut.PWMOut(esp, 27) # BLUE_LED = PWMOut.PWMOut(esp, 25) -# status_light = adafruit_rgbled.RGBLED(RED_LED, BLUE_LED, GREEN_LED) -wifi = adafruit_esp32spi_wifimanager.ESPSPI_WiFiManager(esp, secrets, status_light) +# status_pixel = adafruit_rgbled.RGBLED(RED_LED, BLUE_LED, GREEN_LED) +wifi = adafruit_esp32spi_wifimanager.WiFiManager( + esp, ssid, password, status_pixel=status_pixel +) # Define callback functions which will be called when certain events happen. @@ -101,8 +104,8 @@ def message(client, feed_id, payload): mqtt_client = MQTT.MQTT( broker="io.adafruit.com", port=1883, - username=secrets["aio_username"], - password=secrets["aio_key"], + username=aio_username, + password=aio_key, socket_pool=pool, ssl_context=ssl_context, ) diff --git a/examples/adafruit_io_mqtt/adafruit_io_location.py b/examples/adafruit_io_mqtt/adafruit_io_location.py index 4b78463..b2a1d8c 100755 --- a/examples/adafruit_io_mqtt/adafruit_io_location.py +++ b/examples/adafruit_io_mqtt/adafruit_io_location.py @@ -4,6 +4,7 @@ # Example of tagging data with location values # and sending it to an Adafruit IO feed. import time +from os import getenv import board import busio from digitalio import DigitalInOut @@ -11,19 +12,17 @@ from adafruit_esp32spi import adafruit_esp32spi from adafruit_esp32spi import adafruit_esp32spi_wifimanager import neopixel - - import adafruit_minimqtt.adafruit_minimqtt as MQTT from adafruit_io.adafruit_io import IO_MQTT -### 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 secrets.py file -try: - from secrets import secrets -except ImportError: - print("WiFi secrets are kept in secrets.py, please add them there!") - raise +### WiFi ### # If you are using a board with pre-defined ESP32 Pins: esp32_cs = DigitalInOut(board.ESP_CS) @@ -38,19 +37,21 @@ spi = busio.SPI(board.SCK, board.MOSI, board.MISO) esp = adafruit_esp32spi.ESP_SPIcontrol(spi, esp32_cs, esp32_ready, esp32_reset) """Use below for Most Boards""" -status_light = neopixel.NeoPixel( +status_pixel = neopixel.NeoPixel( board.NEOPIXEL, 1, brightness=0.2 ) # Uncomment for Most Boards """Uncomment below for ItsyBitsy M4""" -# status_light = dotstar.DotStar(board.APA102_SCK, board.APA102_MOSI, 1, brightness=0.2) +# status_pixel = dotstar.DotStar(board.APA102_SCK, board.APA102_MOSI, 1, brightness=0.2) # Uncomment below for an externally defined RGB LED # import adafruit_rgbled # from adafruit_esp32spi import PWMOut # RED_LED = PWMOut.PWMOut(esp, 26) # GREEN_LED = PWMOut.PWMOut(esp, 27) # BLUE_LED = PWMOut.PWMOut(esp, 25) -# status_light = adafruit_rgbled.RGBLED(RED_LED, BLUE_LED, GREEN_LED) -wifi = adafruit_esp32spi_wifimanager.ESPSPI_WiFiManager(esp, secrets, status_light) +# status_pixel = adafruit_rgbled.RGBLED(RED_LED, BLUE_LED, GREEN_LED) +wifi = adafruit_esp32spi_wifimanager.WiFiManager( + esp, ssid, password, status_pixel=status_pixel +) # Define callback functions which will be called when certain events happen. @@ -101,8 +102,8 @@ def message(client, feed_id, payload): mqtt_client = MQTT.MQTT( broker="io.adafruit.com", port=1883, - username=secrets["aio_username"], - password=secrets["aio_key"], + username=aio_username, + password=aio_key, socket_pool=pool, ssl_context=ssl_context, ) diff --git a/examples/adafruit_io_mqtt/adafruit_io_pubsub_rp2040.py b/examples/adafruit_io_mqtt/adafruit_io_pubsub_rp2040.py index 0fba44a..d4c1258 100644 --- a/examples/adafruit_io_mqtt/adafruit_io_pubsub_rp2040.py +++ b/examples/adafruit_io_mqtt/adafruit_io_pubsub_rp2040.py @@ -2,6 +2,7 @@ # SPDX-License-Identifier: MIT import time +from os import getenv from microcontroller import cpu import board import busio @@ -12,14 +13,14 @@ import adafruit_minimqtt.adafruit_minimqtt as MQTT from adafruit_io.adafruit_io import IO_MQTT -### 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 secrets.py file -try: - from secrets import secrets -except ImportError: - print("WiFi secrets are kept in secrets.py, please add them there!") - raise +### WiFi ### # Raspberry Pi RP2040 esp32_cs = DigitalInOut(board.GP13) @@ -29,7 +30,7 @@ spi = busio.SPI(board.GP10, board.GP11, board.GP12) esp = adafruit_esp32spi.ESP_SPIcontrol(spi, esp32_cs, esp32_ready, esp32_reset) -wifi = adafruit_esp32spi_wifimanager.ESPSPI_WiFiManager(esp, secrets) +wifi = adafruit_esp32spi_wifimanager.WiFiManager(esp, ssid, password) # Configure the RP2040 Pico LED Pin as an output led_pin = DigitalInOut(board.LED) @@ -88,8 +89,8 @@ def on_led_msg(client, topic, message): mqtt_client = MQTT.MQTT( broker="io.adafruit.com", port=1883, - username=secrets["aio_username"], - password=secrets["aio_key"], + username=aio_username, + password=aio_key, socket_pool=pool, ssl_context=ssl_context, ) diff --git a/examples/adafruit_io_mqtt/adafruit_io_simpletest_cellular.py b/examples/adafruit_io_mqtt/adafruit_io_simpletest_cellular.py index 634de5b..3c0b314 100644 --- a/examples/adafruit_io_mqtt/adafruit_io_simpletest_cellular.py +++ b/examples/adafruit_io_mqtt/adafruit_io_simpletest_cellular.py @@ -6,6 +6,7 @@ # to subscribe to an Adafruit IO feed and publish random data # to be received by the feed. import time +from os import getenv from random import randint import board @@ -20,12 +21,13 @@ import adafruit_minimqtt.adafruit_minimqtt as MQTT from adafruit_io.adafruit_io import IO_MQTT -# Get MQTT details and more from a secrets.py file -try: - from secrets import secrets -except ImportError: - print("MQTT secrets are kept in secrets.py, please add them there!") - raise +# Get Fona details and Adafruit IO keys, ensure these are setup in settings.toml +# (visit io.adafruit.com if you need to create an account, or if you need your Adafruit IO key.) +apn = getenv("apn") +apn_username = getenv("apn_username") +apn_password = getenv("apn_password") +aio_username = getenv("ADAFRUIT_AIO_USERNAME") +aio_key = getenv("ADAFRUIT_AIO_KEY") # Create a serial connection for the FONA connection uart = busio.UART(board.TX, board.RX) @@ -35,7 +37,7 @@ fona = FONA(uart, rst) # initialize gsm -gsm = GSM(fona, (secrets["apn"], secrets["apn_username"], secrets["apn_password"])) +gsm = GSM(fona, (apn, apn_username, apn_password)) while not gsm.is_attached: print("Attaching to network...") @@ -100,8 +102,8 @@ def message(client, feed_id, payload): mqtt_client = MQTT.MQTT( broker="io.adafruit.com", port=1883, - username=secrets["aio_username"], - password=secrets["aio_key"], + username=aio_username, + password=aio_key, socket_pool=pool, ssl_context=ssl_context, ) diff --git a/examples/adafruit_io_mqtt/adafruit_io_simpletest_esp32s2.py b/examples/adafruit_io_mqtt/adafruit_io_simpletest_esp32s2.py index e032926..e308c27 100644 --- a/examples/adafruit_io_mqtt/adafruit_io_simpletest_esp32s2.py +++ b/examples/adafruit_io_mqtt/adafruit_io_simpletest_esp32s2.py @@ -1,47 +1,27 @@ # SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries # SPDX-License-Identifier: MIT import time +from os import getenv from random import randint -import os -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 -### 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") -# 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: - if os.getenv("AIO_USERNAME") and os.getenv("AIO_KEY"): - secrets = { - "aio_username": os.getenv("AIO_USERNAME"), - "aio_key": os.getenv("AIO_KEY"), - "ssid": os.getenv("CIRCUITPY_WIFI_SSID"), - "password": os.getenv("CIRCUITPY_WIFI_PASSWORD"), - } - else: - from secrets import secrets -except ImportError: - print( - "WiFi + Adafruit IO secrets are kept in secrets.py or settings.toml, 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"] +### WiFi ### if not wifi.radio.connected: - print("Connecting to %s" % secrets["ssid"]) - wifi.radio.connect(secrets["ssid"], secrets["password"]) - print("Connected to %s!" % secrets["ssid"]) + print("Connecting to %s" % ssid) + wifi.radio.connect(ssid, password) + print("Connected to %s!" % ssid) # Define callback functions which will be called when certain events happen. @@ -91,17 +71,18 @@ def message(client, feed_id, payload): print("Feed {0} received new value: {1}".format(feed_id, payload)) -# 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", port=8883, - username=secrets["aio_username"], - password=secrets["aio_key"], + username=aio_username, + password=aio_key, socket_pool=pool, - ssl_context=ssl.create_default_context(), + ssl_context=ssl_context, is_ssl=True, ) diff --git a/examples/adafruit_io_mqtt/adafruit_io_simpletest_eth.py b/examples/adafruit_io_mqtt/adafruit_io_simpletest_eth.py index 07ff0f7..b502983 100755 --- a/examples/adafruit_io_mqtt/adafruit_io_simpletest_eth.py +++ b/examples/adafruit_io_mqtt/adafruit_io_simpletest_eth.py @@ -6,6 +6,7 @@ # to subscribe to an Adafruit IO feed and publish random data # to be received by the feed. import time +from os import getenv from random import randint import board @@ -17,12 +18,12 @@ import adafruit_minimqtt.adafruit_minimqtt as MQTT from adafruit_io.adafruit_io import IO_MQTT -# Get MQTT details and more from a secrets.py file -try: - from secrets import secrets -except ImportError: - print("MQTT 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") cs = DigitalInOut(board.D10) spi_bus = busio.SPI(board.SCK, MOSI=board.MOSI, MISO=board.MISO) @@ -85,8 +86,8 @@ def message(client, feed_id, payload): mqtt_client = MQTT.MQTT( broker="io.adafruit.com", port=1883, - username=secrets["aio_username"], - password=secrets["aio_key"], + username=aio_username, + password=aio_key, socket_pool=pool, ssl_context=ssl_context, ) diff --git a/examples/adafruit_io_mqtt/adafruit_io_time.py b/examples/adafruit_io_mqtt/adafruit_io_time.py index d457865..e2b69a6 100755 --- a/examples/adafruit_io_mqtt/adafruit_io_time.py +++ b/examples/adafruit_io_mqtt/adafruit_io_time.py @@ -5,6 +5,7 @@ # for obtaining the current server time, if you don't have # access to a RTC module. import time +from os import getenv import board import busio from digitalio import DigitalInOut @@ -12,18 +13,17 @@ from adafruit_esp32spi import adafruit_esp32spi from adafruit_esp32spi import adafruit_esp32spi_wifimanager import neopixel - import adafruit_minimqtt.adafruit_minimqtt as MQTT from adafruit_io.adafruit_io import IO_MQTT -### 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 secrets.py file -try: - from secrets import secrets -except ImportError: - print("WiFi secrets are kept in secrets.py, please add them there!") - raise +### WiFi ### # If you are using a board with pre-defined ESP32 Pins: esp32_cs = DigitalInOut(board.ESP_CS) @@ -38,19 +38,21 @@ spi = busio.SPI(board.SCK, board.MOSI, board.MISO) esp = adafruit_esp32spi.ESP_SPIcontrol(spi, esp32_cs, esp32_ready, esp32_reset) """Use below for Most Boards""" -status_light = neopixel.NeoPixel( +status_pixel = neopixel.NeoPixel( board.NEOPIXEL, 1, brightness=0.2 ) # Uncomment for Most Boards """Uncomment below for ItsyBitsy M4""" -# status_light = dotstar.DotStar(board.APA102_SCK, board.APA102_MOSI, 1, brightness=0.2) +# status_pixel = dotstar.DotStar(board.APA102_SCK, board.APA102_MOSI, 1, brightness=0.2) # Uncomment below for an externally defined RGB LED # import adafruit_rgbled # from adafruit_esp32spi import PWMOut # RED_LED = PWMOut.PWMOut(esp, 26) # GREEN_LED = PWMOut.PWMOut(esp, 27) # BLUE_LED = PWMOut.PWMOut(esp, 25) -# status_light = adafruit_rgbled.RGBLED(RED_LED, BLUE_LED, GREEN_LED) -wifi = adafruit_esp32spi_wifimanager.ESPSPI_WiFiManager(esp, secrets, status_light) +# status_pixel = adafruit_rgbled.RGBLED(RED_LED, BLUE_LED, GREEN_LED) +wifi = adafruit_esp32spi_wifimanager.WiFiManager( + esp, ssid, password, status_pixel=status_pixel +) # Define callback functions which will be called when certain events happen. @@ -115,8 +117,8 @@ def message(client, feed_id, payload): mqtt_client = MQTT.MQTT( broker="io.adafruit.com", port=1883, - username=secrets["aio_username"], - password=secrets["aio_key"], + username=aio_username, + password=aio_key, socket_pool=pool, ssl_context=ssl_context, ) diff --git a/examples/adafruit_io_simpletest.py b/examples/adafruit_io_simpletest.py index 2dc56b3..702f3ca 100755 --- a/examples/adafruit_io_simpletest.py +++ b/examples/adafruit_io_simpletest.py @@ -6,9 +6,8 @@ # to subscribe to an Adafruit IO feed and publish random data # to be received by the feed. import time +from os import getenv from random import randint - - import board import busio from digitalio import DigitalInOut @@ -19,14 +18,14 @@ import adafruit_minimqtt.adafruit_minimqtt as MQTT from adafruit_io.adafruit_io import IO_MQTT -### 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 secrets.py file -try: - from secrets import secrets -except ImportError: - print("WiFi secrets are kept in secrets.py, please add them there!") - raise +### WiFi ### # If you are using a board with pre-defined ESP32 Pins: esp32_cs = DigitalInOut(board.ESP_CS) @@ -41,19 +40,21 @@ spi = busio.SPI(board.SCK, board.MOSI, board.MISO) esp = adafruit_esp32spi.ESP_SPIcontrol(spi, esp32_cs, esp32_ready, esp32_reset) """Use below for Most Boards""" -status_light = neopixel.NeoPixel( +status_pixel = neopixel.NeoPixel( board.NEOPIXEL, 1, brightness=0.2 ) # Uncomment for Most Boards """Uncomment below for ItsyBitsy M4""" -# status_light = dotstar.DotStar(board.APA102_SCK, board.APA102_MOSI, 1, brightness=0.2) +# status_pixel = dotstar.DotStar(board.APA102_SCK, board.APA102_MOSI, 1, brightness=0.2) # Uncomment below for an externally defined RGB LED # import adafruit_rgbled # from adafruit_esp32spi import PWMOut # RED_LED = PWMOut.PWMOut(esp, 26) # GREEN_LED = PWMOut.PWMOut(esp, 27) # BLUE_LED = PWMOut.PWMOut(esp, 25) -# status_light = adafruit_rgbled.RGBLED(RED_LED, BLUE_LED, GREEN_LED) -wifi = adafruit_esp32spi_wifimanager.ESPSPI_WiFiManager(esp, secrets, status_light) +# status_pixel = adafruit_rgbled.RGBLED(RED_LED, BLUE_LED, GREEN_LED) +wifi = adafruit_esp32spi_wifimanager.WiFiManager( + esp, ssid, password, status_pixel=status_pixel +) # Define callback functions which will be called when certain events happen. @@ -113,8 +114,8 @@ def message(client, feed_id, payload): mqtt_client = MQTT.MQTT( broker="io.adafruit.com", port=1883, - username=secrets["aio_username"], - password=secrets["aio_key"], + username=aio_username, + password=aio_key, socket_pool=pool, ssl_context=ssl_context, )