diff --git a/examples/aws_iot_native_networking.py b/examples/aws_iot_native_networking.py index 4f81593..a4a33f1 100644 --- a/examples/aws_iot_native_networking.py +++ b/examples/aws_iot_native_networking.py @@ -1,30 +1,32 @@ # SPDX-FileCopyrightText: 2023 ladyada for Adafruit Industries # SPDX-License-Identifier: MIT +from os import getenv import time -import ssl import json -import socketpool import wifi +import adafruit_connection_manager import adafruit_minimqtt.adafruit_minimqtt as MQTT from adafruit_aws_iot import MQTT_CLIENT -# Add a secrets.py to your filesystem that has a dictionary called "secrets". DO NOT share that -# file or commit it into Git or other source control. The "secrets" dictionary should have the -# following keys: -# "ssid" - Your WiFi ssid -# "password" - Your WiFi password -# "device_cert_path" - Path to the Device Certificate from AWS IoT (".cert.pem") -# "device_key_path" - Path to the RSA Private Key from AWS IoT (".private.key") -# "broker" - The endpoint for the AWS IoT broker (".iot..amazonaws.com") -# "client_id" - The client id. Your device's Policy needs to allow this client ("basicPubSub") -# -# 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 +# Add a settings.toml to your filesystem. DO NOT share that file or commit it into +# Git or other source control. The file should have the following settings: +""" +CIRCUITPY_WIFI_SSID="Your WiFi ssid" +CIRCUITPY_WIFI_PASSWORD="Your WiFi password" +device_cert_path=".cert.pem" # Path to the Device Certificate from AWS IoT +device_key_path=".private.key" # Path to the RSA Private Key from AWS IoT +broker=".iot..amazonaws.com" # The endpoint for the AWS IoT broker +client_id="client_id" # The client id. Your device's Policy needs to allow this client +""" + +# Get WiFi details and AWS keys, ensure these are setup in settings.toml +ssid = getenv("CIRCUITPY_WIFI_SSID") +password = getenv("CIRCUITPY_WIFI_PASSWORD") +device_cert_path = getenv("device_cert_path") +device_key_path = getenv("device_key_path") +broker = getenv("broker") +client_id = getenv("client_id") ### Code ### @@ -76,23 +78,21 @@ def message(client, topic, msg): print("Message from {}: {}".format(topic, msg)) -print("Connecting to %s" % secrets["ssid"]) -wifi.radio.connect(secrets["ssid"], secrets["password"]) -print("Connected to %s!" % secrets["ssid"]) +print(f"Connecting to {ssid}") +wifi.radio.connect(ssid, password) +print(f"Connected to {ssid}!") # Create a socket pool -pool = socketpool.SocketPool(wifi.radio) -ssl_context = ssl.create_default_context() +pool = adafruit_connection_manager.get_radio_socketpool(wifi.radio) +ssl_context = adafruit_connection_manager.get_radio_ssl_context(wifi.radio) # Set AWS Device Certificate and AWS RSA Private Key -ssl_context.load_cert_chain( - certfile=secrets["device_cert_path"], keyfile=secrets["device_key_path"] -) +ssl_context.load_cert_chain(certfile=device_cert_path, keyfile=device_key_path) # Set up a MiniMQTT Client mqtt_client = MQTT.MQTT( - broker=secrets["broker"], - client_id=secrets["client_id"], + broker=broker, + client_id=client_id, is_ssl=True, socket_pool=pool, ssl_context=ssl_context, @@ -109,7 +109,7 @@ def message(client, topic, msg): aws_iot.on_publish = publish aws_iot.on_message = message -print("Attempting to connect to %s" % mqtt_client.broker) +print(f"Attempting to connect to {mqtt_client.broker}") aws_iot.connect() # Start a blocking message loop... diff --git a/examples/aws_iot_shadows.py b/examples/aws_iot_shadows.py index 7c33445..008bea9 100644 --- a/examples/aws_iot_shadows.py +++ b/examples/aws_iot_shadows.py @@ -1,6 +1,7 @@ # SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries # SPDX-License-Identifier: MIT +from os import getenv import time import json import board @@ -13,14 +14,11 @@ import adafruit_minimqtt.adafruit_minimqtt as MQTT from adafruit_aws_iot import MQTT_CLIENT -### WiFi ### - -# 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 +# Get WiFi details and AWS keys, ensure these are setup in settings.toml +ssid = getenv("CIRCUITPY_WIFI_SSID") +password = getenv("CIRCUITPY_WIFI_PASSWORD") +broker = getenv("broker") +client_id = getenv("client_id") # Get device certificate try: @@ -38,6 +36,8 @@ print("Certificate (private.pem.key) not found on CIRCUITPY filesystem.") raise +### WiFi ### + # If you are using a board with pre-defined ESP32 Pins: esp32_cs = DigitalInOut(board.ESP_CS) esp32_ready = DigitalInOut(board.ESP_BUSY) @@ -57,19 +57,21 @@ ), "Please update nina-fw to >=1.4.0." # 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 +) ### Code ### @@ -139,8 +141,8 @@ def message(client, topic, msg): # Set up a new MiniMQTT Client client = MQTT.MQTT( - broker=secrets["broker"], - client_id=secrets["client_id"], + broker=broker, + client_id=client_id, is_ssl=True, socket_pool=pool, ssl_context=ssl_context, @@ -157,7 +159,7 @@ def message(client, topic, msg): aws_iot.on_publish = publish aws_iot.on_message = message -print("Attempting to connect to %s" % client.broker) +print(f"Attempting to connect to {client.broker}") aws_iot.connect() # Pump the message loop forever, all events diff --git a/examples/aws_iot_simpletest.py b/examples/aws_iot_simpletest.py index e920152..0598dab 100644 --- a/examples/aws_iot_simpletest.py +++ b/examples/aws_iot_simpletest.py @@ -1,6 +1,7 @@ # SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries # SPDX-License-Identifier: MIT +from os import getenv import time import json import board @@ -13,14 +14,11 @@ import adafruit_minimqtt.adafruit_minimqtt as MQTT from adafruit_aws_iot import MQTT_CLIENT -### WiFi ### - -# 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 +# Get WiFi details and AWS keys, ensure these are setup in settings.toml +ssid = getenv("CIRCUITPY_WIFI_SSID") +password = getenv("CIRCUITPY_WIFI_PASSWORD") +broker = getenv("broker") +client_id = getenv("client_id") # Get device certificate try: @@ -38,6 +36,8 @@ print("Certificate (private.pem.key) not found on CIRCUITPY filesystem.") raise +### WiFi ### + # If you are using a board with pre-defined ESP32 Pins: esp32_cs = DigitalInOut(board.ESP_CS) esp32_ready = DigitalInOut(board.ESP_BUSY) @@ -57,19 +57,21 @@ ), "Please update nina-fw to >=1.4.0." # 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 +) ### Code ### @@ -136,8 +138,8 @@ def message(client, topic, msg): # Set up a new MiniMQTT Client client = MQTT.MQTT( - broker=secrets["broker"], - client_id=secrets["client_id"], + broker=broker, + client_id=client_id, is_ssl=True, socket_pool=pool, ssl_context=ssl_context, @@ -154,7 +156,7 @@ def message(client, topic, msg): aws_iot.on_publish = publish aws_iot.on_message = message -print("Attempting to connect to %s" % client.broker) +print(f"Attempting to connect to {client.broker}") aws_iot.connect() # Pump the message loop forever, all events