From 028300fcb46a7dda55a3184e69019b940de81c0d Mon Sep 17 00:00:00 2001 From: Justin Myers Date: Mon, 24 Feb 2025 13:54:22 -0800 Subject: [PATCH 1/3] Remove secrets usage --- README.rst | 27 +++++++------ .../azureiot_central_commands.py | 31 +++++++------- .../azureiot_central_notconnected.py | 31 +++++++------- .../azureiot_central_properties.py | 31 +++++++------- .../azureiot_central_simpletest.py | 31 +++++++------- .../azureiot_hub_directmethods.py | 25 ++++++------ .../azureiot_hub_messages.py | 25 ++++++------ .../azureiot_hub_simpletest.py | 25 ++++++------ .../azureiot_hub_twin_operations.py | 25 ++++++------ .../azureiot_central_commands.py | 23 ++++++----- .../azureiot_central_notconnected.py | 23 ++++++----- .../azureiot_central_properties.py | 23 ++++++----- .../azureiot_central_simpletest.py | 25 ++++++------ .../azureiot_hub_directmethods.py | 19 +++++---- .../azureiot_hub_messages.py | 19 +++++---- .../azureiot_hub_simpletest.py | 19 +++++---- .../azureiot_hub_twin_operations.py | 19 +++++---- examples/azureiot_secrets_example.py | 40 ------------------- examples/azureiot_settings_example.py | 38 ++++++++++++++++++ 19 files changed, 257 insertions(+), 242 deletions(-) delete mode 100644 examples/azureiot_secrets_example.py create mode 100644 examples/azureiot_settings_example.py diff --git a/README.rst b/README.rst index 1052c4d..95c1385 100644 --- a/README.rst +++ b/README.rst @@ -106,7 +106,7 @@ To interact with Azure IoT Hub, you will need to create a hub, and a register a - Open the `Azure Portal `_. - Follow the instructions in `Microsoft Docs `_ to create an Azure IoT Hub and register a device. -- Copy the devices Primary or secondary connection string, and add this to your ``secrets.py`` file. +- Copy the devices Primary or secondary connection string, and add this to your ``settings.toml`` file. You can find the device connection string by selecting the IoT Hub in the `Azure Portal `_, *selecting Explorer -> IoT devices*, then selecting your device. @@ -128,7 +128,7 @@ Then copy either the primary or secondary connection string using the copy butto from adafruit_azureiot import IoTHubDevice - device = IoTHubDevice(wifi, secrets["device_connection_string"]) + device = IoTHubDevice(wifi, device_connection_string) device.connect() Once the device is connected, you will regularly need to run a ``loop`` to poll for messages from the cloud. @@ -197,7 +197,7 @@ To use Azure IoT Central, you will need to create an Azure IoT Central app, crea - Head to `Azure IoT Central `__ - Follow the instructions in the `Microsoft Docs `__ to create an application. Every tier is free for up to 2 devices. - Follow the instructions in the `Microsoft Docs `__ to create a device template. -- Create a device based off the template, and select **Connect** to get the device connection details. Store the ID Scope, Device ID and either the primary or secondary device SAS key in your ``secrets.py`` file. +- Create a device based off the template, and select **Connect** to get the device connection details. Store the ID Scope, Device ID and either the primary or secondary device SAS key in your ``settings.toml`` file. .. image:: iot-central-connect-button.png :alt: The IoT Central connect button @@ -209,18 +209,19 @@ To use Azure IoT Central, you will need to create an Azure IoT Central app, crea *The connection details dialog* + +settings.toml: + .. code-block:: python - secrets = { - # WiFi settings - "ssid": "", - "password": "", + # WiFi settings + CIRCUITPY_WIFI_SSID="", + CIRCUITPY_WIFI_PASSWORD="", - # Azure IoT Central settings - "id_scope": "", - "device_id": "", - "device_sas_key": "" - } + # Azure IoT Central settings + id_scope="" + device_id="" + device_sas_key="" **Connect your device to your Azure IoT Central app** @@ -228,7 +229,7 @@ To use Azure IoT Central, you will need to create an Azure IoT Central app, crea from adafruit_azureiot import IoTCentralDevice - device = IoTCentralDevice(wifi, secrets["id_scope"], secrets["device_id"], secrets["device_sas_key"]) + device = IoTCentralDevice(wifi, id_scope, device_id, device_sas_key) device.connect() Once the device is connected, you will regularly need to run a ``loop`` to poll for messages from the cloud. diff --git a/examples/azureiot_esp32spi/azureiot_central_commands.py b/examples/azureiot_esp32spi/azureiot_central_commands.py index d11cc1f..d0922e0 100644 --- a/examples/azureiot_esp32spi/azureiot_central_commands.py +++ b/examples/azureiot_esp32spi/azureiot_central_commands.py @@ -1,6 +1,7 @@ # SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries # SPDX-License-Identifier: MIT +from os import getenv import time import board import busio @@ -10,12 +11,12 @@ from adafruit_esp32spi import adafruit_esp32spi, adafruit_esp32spi_wifimanager import adafruit_connection_manager -# 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") +id_scope = getenv("id_scope") +device_id = getenv("device_id") +device_sas_key = getenv("device_sas_key") # ESP32 Setup try: @@ -31,19 +32,21 @@ 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 +) print("Connecting to WiFi...") @@ -86,7 +89,7 @@ # # Next create a device using the device template, and select Connect to get the device connection # details. -# Add the connection details to your secrets.py file, using the following values: +# Add the connection details to your settings.toml file, using the following values: # # 'id_scope' - the devices ID scope # 'device_id' - the devices device id @@ -108,9 +111,9 @@ device = IoTCentralDevice( pool, ssl_context, - secrets["id_scope"], - secrets["device_id"], - secrets["device_sas_key"], + id_scope, + device_id, + device_sas_key, ) diff --git a/examples/azureiot_esp32spi/azureiot_central_notconnected.py b/examples/azureiot_esp32spi/azureiot_central_notconnected.py index 8cb7863..b03ab46 100644 --- a/examples/azureiot_esp32spi/azureiot_central_notconnected.py +++ b/examples/azureiot_esp32spi/azureiot_central_notconnected.py @@ -1,6 +1,7 @@ # SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries # SPDX-License-Identifier: MIT +from os import getenv import json import random import time @@ -12,12 +13,12 @@ from adafruit_esp32spi import adafruit_esp32spi, adafruit_esp32spi_wifimanager import adafruit_connection_manager -# 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") +id_scope = getenv("id_scope") +device_id = getenv("device_id") +device_sas_key = getenv("device_sas_key") # ESP32 Setup try: @@ -33,19 +34,21 @@ 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 +) print("Connecting to WiFi...") @@ -80,7 +83,7 @@ # # Next create a device using the device template, and select Connect to get the device connection # details. -# Add the connection details to your secrets.py file, using the following values: +# Add the connection details to your settings.toml file, using the following values: # # 'id_scope' - the devices ID scope # 'device_id' - the devices device id @@ -104,9 +107,9 @@ device = IoTCentralDevice( pool, ssl_context, - secrets["id_scope"], - secrets["device_id"], - secrets["device_sas_key"], + id_scope, + device_id, + device_sas_key, ) # don't connect diff --git a/examples/azureiot_esp32spi/azureiot_central_properties.py b/examples/azureiot_esp32spi/azureiot_central_properties.py index 43d131b..bb8d208 100644 --- a/examples/azureiot_esp32spi/azureiot_central_properties.py +++ b/examples/azureiot_esp32spi/azureiot_central_properties.py @@ -1,6 +1,7 @@ # SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries # SPDX-License-Identifier: MIT +from os import getenv import random import time import board @@ -11,12 +12,12 @@ from adafruit_esp32spi import adafruit_esp32spi, adafruit_esp32spi_wifimanager import adafruit_connection_manager -# 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") +id_scope = getenv("id_scope") +device_id = getenv("device_id") +device_sas_key = getenv("device_sas_key") # ESP32 Setup try: @@ -32,19 +33,21 @@ 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 +) print("Connecting to WiFi...") @@ -87,7 +90,7 @@ # # Next create a device using the device template, and select Connect to get the device connection # details. -# Add the connection details to your secrets.py file, using the following values: +# Add the connection details to your settings.toml file, using the following values: # # 'id_scope' - the devices ID scope # 'device_id' - the devices device id @@ -105,9 +108,9 @@ device = IoTCentralDevice( pool, ssl_context, - secrets["id_scope"], - secrets["device_id"], - secrets["device_sas_key"], + id_scope, + device_id, + device_sas_key, ) diff --git a/examples/azureiot_esp32spi/azureiot_central_simpletest.py b/examples/azureiot_esp32spi/azureiot_central_simpletest.py index ad66d2c..01faade 100644 --- a/examples/azureiot_esp32spi/azureiot_central_simpletest.py +++ b/examples/azureiot_esp32spi/azureiot_central_simpletest.py @@ -1,6 +1,7 @@ # SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries # SPDX-License-Identifier: MIT +from os import getenv import json import random import time @@ -12,12 +13,12 @@ from adafruit_esp32spi import adafruit_esp32spi, adafruit_esp32spi_wifimanager import adafruit_connection_manager -# 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") +id_scope = getenv("id_scope") +device_id = getenv("device_id") +device_sas_key = getenv("device_sas_key") # ESP32 Setup try: @@ -33,19 +34,21 @@ 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 +) print("Connecting to WiFi...") @@ -88,7 +91,7 @@ # # Next create a device using the device template, and select Connect to get the device connection # details. -# Add the connection details to your secrets.py file, using the following values: +# Add the connection details to your settings.toml file, using the following values: # # 'id_scope' - the devices ID scope # 'device_id' - the devices device id @@ -106,9 +109,9 @@ device = IoTCentralDevice( pool, ssl_context, - secrets["id_scope"], - secrets["device_id"], - secrets["device_sas_key"], + id_scope, + device_id, + device_sas_key, ) print("Connecting to Azure IoT Central...") diff --git a/examples/azureiot_esp32spi/azureiot_hub_directmethods.py b/examples/azureiot_esp32spi/azureiot_hub_directmethods.py index b329c0e..17466e9 100644 --- a/examples/azureiot_esp32spi/azureiot_hub_directmethods.py +++ b/examples/azureiot_esp32spi/azureiot_hub_directmethods.py @@ -1,6 +1,7 @@ # SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries # SPDX-License-Identifier: MIT +from os import getenv import time import board import busio @@ -10,12 +11,10 @@ from adafruit_esp32spi import adafruit_esp32spi, adafruit_esp32spi_wifimanager import adafruit_connection_manager -# 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 Azure keys, ensure these are setup in settings.toml +ssid = getenv("CIRCUITPY_WIFI_SSID") +password = getenv("CIRCUITPY_WIFI_PASSWORD") +device_connection_string = getenv("device_connection_string") # ESP32 Setup try: @@ -31,19 +30,21 @@ 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 +) print("Connecting to WiFi...") @@ -84,7 +85,7 @@ # if you are using the free tier # # Once you have a hub and a device, copy the device primary connection string. -# Add it to the secrets.py file in an entry called device_connection_string +# Add it to the settings.toml file in an entry called device_connection_string # # The adafruit-circuitpython-azureiot library depends on the following libraries: # @@ -99,7 +100,7 @@ pool = adafruit_connection_manager.get_radio_socketpool(esp) ssl_context = adafruit_connection_manager.get_radio_ssl_context(esp) # Create an IoT Hub device client and connect -device = IoTHubDevice(pool, ssl_context, secrets["device_connection_string"]) +device = IoTHubDevice(pool, ssl_context, device_connection_string) # Subscribe to direct method calls diff --git a/examples/azureiot_esp32spi/azureiot_hub_messages.py b/examples/azureiot_esp32spi/azureiot_hub_messages.py index 956a3a8..f242be5 100644 --- a/examples/azureiot_esp32spi/azureiot_hub_messages.py +++ b/examples/azureiot_esp32spi/azureiot_hub_messages.py @@ -1,6 +1,7 @@ # SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries # SPDX-License-Identifier: MIT +from os import getenv import json import random import time @@ -12,12 +13,10 @@ from adafruit_esp32spi import adafruit_esp32spi, adafruit_esp32spi_wifimanager import adafruit_connection_manager -# 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 Azure keys, ensure these are setup in settings.toml +ssid = getenv("CIRCUITPY_WIFI_SSID") +password = getenv("CIRCUITPY_WIFI_PASSWORD") +device_connection_string = getenv("device_connection_string") # ESP32 Setup try: @@ -33,19 +32,21 @@ 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 +) print("Connecting to WiFi...") @@ -86,7 +87,7 @@ # if you are using the free tier # # Once you have a hub and a device, copy the device primary connection string. -# Add it to the secrets.py file in an entry called device_connection_string +# Add it to the settings.toml file in an entry called device_connection_string # # The adafruit-circuitpython-azureiot library depends on the following libraries: # @@ -97,7 +98,7 @@ pool = adafruit_connection_manager.get_radio_socketpool(esp) ssl_context = adafruit_connection_manager.get_radio_ssl_context(esp) # Create an IoT Hub device client and connect -device = IoTHubDevice(pool, ssl_context, secrets["device_connection_string"]) +device = IoTHubDevice(pool, ssl_context, device_connection_string) # Subscribe to cloud to device messages diff --git a/examples/azureiot_esp32spi/azureiot_hub_simpletest.py b/examples/azureiot_esp32spi/azureiot_hub_simpletest.py index 20d7136..f92247b 100644 --- a/examples/azureiot_esp32spi/azureiot_hub_simpletest.py +++ b/examples/azureiot_esp32spi/azureiot_hub_simpletest.py @@ -1,6 +1,7 @@ # SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries # SPDX-License-Identifier: MIT +from os import getenv import json import random import time @@ -12,12 +13,10 @@ from adafruit_esp32spi import adafruit_esp32spi, adafruit_esp32spi_wifimanager import adafruit_connection_manager -# 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 Azure keys, ensure these are setup in settings.toml +ssid = getenv("CIRCUITPY_WIFI_SSID") +password = getenv("CIRCUITPY_WIFI_PASSWORD") +device_connection_string = getenv("device_connection_string") # ESP32 Setup try: @@ -33,19 +32,21 @@ 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 +) print("Connecting to WiFi...") @@ -86,7 +87,7 @@ # if you are using the free tier # # Once you have a hub and a device, copy the device primary connection string. -# Add it to the secrets.py file in an entry called device_connection_string +# Add it to the settings.toml file in an entry called device_connection_string # # The adafruit-circuitpython-azureiot library depends on the following libraries: # @@ -97,7 +98,7 @@ pool = adafruit_connection_manager.get_radio_socketpool(esp) ssl_context = adafruit_connection_manager.get_radio_ssl_context(esp) # Create an IoT Hub device client and connect -device = IoTHubDevice(pool, ssl_context, secrets["device_connection_string"]) +device = IoTHubDevice(pool, ssl_context, device_connection_string) print("Connecting to Azure IoT Hub...") diff --git a/examples/azureiot_esp32spi/azureiot_hub_twin_operations.py b/examples/azureiot_esp32spi/azureiot_hub_twin_operations.py index 3ed7842..1f7cdac 100644 --- a/examples/azureiot_esp32spi/azureiot_hub_twin_operations.py +++ b/examples/azureiot_esp32spi/azureiot_hub_twin_operations.py @@ -1,6 +1,7 @@ # SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries # SPDX-License-Identifier: MIT +from os import getenv import random import time import board @@ -11,12 +12,10 @@ from adafruit_esp32spi import adafruit_esp32spi, adafruit_esp32spi_wifimanager import adafruit_connection_manager -# 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 Azure keys, ensure these are setup in settings.toml +ssid = getenv("CIRCUITPY_WIFI_SSID") +password = getenv("CIRCUITPY_WIFI_PASSWORD") +device_connection_string = getenv("device_connection_string") # ESP32 Setup try: @@ -32,19 +31,21 @@ 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 +) print("Connecting to WiFi...") @@ -85,7 +86,7 @@ # if you are using the free tier # # Once you have a hub and a device, copy the device primary connection string. -# Add it to the secrets.py file in an entry called device_connection_string +# Add it to the settings.toml file in an entry called device_connection_string # # To us twins, you will need either a free or standard tier IoT Hub. Basic tier doesn't # support twins @@ -99,7 +100,7 @@ pool = adafruit_connection_manager.get_radio_socketpool(esp) ssl_context = adafruit_connection_manager.get_radio_ssl_context(esp) # Create an IoT Hub device client and connect -device = IoTHubDevice(pool, ssl_context, secrets["device_connection_string"]) +device = IoTHubDevice(pool, ssl_context, device_connection_string) # Subscribe to device twin desired property updates diff --git a/examples/azureiot_native_networking/azureiot_central_commands.py b/examples/azureiot_native_networking/azureiot_central_commands.py index 85b0476..3dea2ed 100644 --- a/examples/azureiot_native_networking/azureiot_central_commands.py +++ b/examples/azureiot_native_networking/azureiot_central_commands.py @@ -1,6 +1,7 @@ # SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries # SPDX-License-Identifier: MIT +from os import getenv import time import rtc @@ -11,15 +12,15 @@ from adafruit_azureiot import IoTCentralDevice from adafruit_azureiot.iot_mqtt import IoTResponse -# 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") +id_scope = getenv("id_scope") +device_id = getenv("device_id") +device_sas_key = getenv("device_sas_key") print("Connecting to WiFi...") -wifi.radio.connect(secrets["ssid"], secrets["password"]) +wifi.radio.connect(ssid, password) pool = adafruit_connection_manager.get_radio_socketpool(wifi.radio) ssl_context = adafruit_connection_manager.get_radio_ssl_context(wifi.radio) @@ -57,7 +58,7 @@ # # Next create a device using the device template, and select Connect to get the device connection # details. -# Add the connection details to your secrets.py file, using the following values: +# Add the connection details to your settings.toml file, using the following values: # # 'id_scope' - the devices ID scope # 'device_id' - the devices device id @@ -73,9 +74,9 @@ device = IoTCentralDevice( pool, ssl_context, - secrets["id_scope"], - secrets["device_id"], - secrets["device_sas_key"], + id_scope, + device_id, + device_sas_key, ) diff --git a/examples/azureiot_native_networking/azureiot_central_notconnected.py b/examples/azureiot_native_networking/azureiot_central_notconnected.py index b695dae..55a3c48 100644 --- a/examples/azureiot_native_networking/azureiot_central_notconnected.py +++ b/examples/azureiot_native_networking/azureiot_central_notconnected.py @@ -1,6 +1,7 @@ # SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries # SPDX-License-Identifier: MIT +from os import getenv import json import random import time @@ -15,15 +16,15 @@ IoTError, ) -# 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") +id_scope = getenv("id_scope") +device_id = getenv("device_id") +device_sas_key = getenv("device_sas_key") print("Connecting to WiFi...") -wifi.radio.connect(secrets["ssid"], secrets["password"]) +wifi.radio.connect(ssid, password) pool = adafruit_connection_manager.get_radio_socketpool(wifi.radio) ssl_context = adafruit_connection_manager.get_radio_ssl_context(wifi.radio) @@ -61,7 +62,7 @@ # # Next create a device using the device template, and select Connect to get the device connection # details. -# Add the connection details to your secrets.py file, using the following values: +# Add the connection details to your settings.toml file, using the following values: # # 'id_scope' - the devices ID scope # 'device_id' - the devices device id @@ -77,9 +78,9 @@ device = IoTCentralDevice( pool, ssl_context, - secrets["id_scope"], - secrets["device_id"], - secrets["device_sas_key"], + id_scope, + device_id, + device_sas_key, ) # don't connect diff --git a/examples/azureiot_native_networking/azureiot_central_properties.py b/examples/azureiot_native_networking/azureiot_central_properties.py index 7cbd9c0..3c49cec 100644 --- a/examples/azureiot_native_networking/azureiot_central_properties.py +++ b/examples/azureiot_native_networking/azureiot_central_properties.py @@ -1,6 +1,7 @@ # SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries # SPDX-License-Identifier: MIT +from os import getenv import random import time @@ -11,15 +12,15 @@ import adafruit_ntp from adafruit_azureiot import IoTCentralDevice -# 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") +id_scope = getenv("id_scope") +device_id = getenv("device_id") +device_sas_key = getenv("device_sas_key") print("Connecting to WiFi...") -wifi.radio.connect(secrets["ssid"], secrets["password"]) +wifi.radio.connect(ssid, password) pool = adafruit_connection_manager.get_radio_socketpool(wifi.radio) ssl_context = adafruit_connection_manager.get_radio_ssl_context(wifi.radio) @@ -57,7 +58,7 @@ # # Next create a device using the device template, and select Connect to get the device connection # details. -# Add the connection details to your secrets.py file, using the following values: +# Add the connection details to your settings.toml file, using the following values: # # 'id_scope' - the devices ID scope # 'device_id' - the devices device id @@ -73,9 +74,9 @@ device = IoTCentralDevice( pool, ssl_context, - secrets["id_scope"], - secrets["device_id"], - secrets["device_sas_key"], + id_scope, + device_id, + device_sas_key, ) diff --git a/examples/azureiot_native_networking/azureiot_central_simpletest.py b/examples/azureiot_native_networking/azureiot_central_simpletest.py index 8ce022d..018de9f 100644 --- a/examples/azureiot_native_networking/azureiot_central_simpletest.py +++ b/examples/azureiot_native_networking/azureiot_central_simpletest.py @@ -1,6 +1,7 @@ # SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries # SPDX-License-Identifier: MIT +from os import getenv import json import random import time @@ -12,15 +13,15 @@ import adafruit_ntp from adafruit_azureiot import IoTCentralDevice -# 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") +id_scope = getenv("id_scope") +device_id = getenv("device_id") +device_sas_key = getenv("device_sas_key") print("Connecting to WiFi...") -wifi.radio.connect(secrets["ssid"], secrets["password"]) +wifi.radio.connect(ssid, password) pool = adafruit_connection_manager.get_radio_socketpool(wifi.radio) ssl_context = adafruit_connection_manager.get_radio_ssl_context(wifi.radio) @@ -58,7 +59,7 @@ # # Next create a device using the device template, and select Connect to get the device connection # details. -# Add the connection details to your secrets.py file, using the following values: +# Add the connection details to your settings.toml file, using the following values: # # 'id_scope' - the devices ID scope # 'device_id' - the devices device id @@ -74,9 +75,9 @@ device = IoTCentralDevice( pool, ssl_context, - secrets["id_scope"], - secrets["device_id"], - secrets["device_sas_key"], + id_scope, + device_id, + device_sas_key, ) print("Connecting to Azure IoT Central...") @@ -103,7 +104,7 @@ print("Connection error, reconnecting\n", str(e)) wifi.radio.enabled = False wifi.radio.enabled = True - wifi.radio.connect(secrets["ssid"], secrets["password"]) + wifi.radio.connect(ssid, password) device.reconnect() continue time.sleep(1) diff --git a/examples/azureiot_native_networking/azureiot_hub_directmethods.py b/examples/azureiot_native_networking/azureiot_hub_directmethods.py index c0857f0..a61bb54 100644 --- a/examples/azureiot_native_networking/azureiot_hub_directmethods.py +++ b/examples/azureiot_native_networking/azureiot_hub_directmethods.py @@ -1,6 +1,7 @@ # SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries # SPDX-License-Identifier: MIT +from os import getenv import time import rtc @@ -11,15 +12,13 @@ from adafruit_azureiot import IoTHubDevice from adafruit_azureiot.iot_mqtt import IoTResponse -# 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, ensure these are setup in settings.toml +ssid = getenv("CIRCUITPY_WIFI_SSID") +password = getenv("CIRCUITPY_WIFI_PASSWORD") +device_connection_string = getenv("device_connection_string") print("Connecting to WiFi...") -wifi.radio.connect(secrets["ssid"], secrets["password"]) +wifi.radio.connect(ssid, password) pool = adafruit_connection_manager.get_radio_socketpool(wifi.radio) ssl_context = adafruit_connection_manager.get_radio_ssl_context(wifi.radio) @@ -55,7 +54,7 @@ # if you are using the free tier # # Once you have a hub and a device, copy the device primary connection string. -# Add it to the secrets.py file in an entry called device_connection_string +# Add it to the settings.toml file in an entry called device_connection_string # # The adafruit-circuitpython-azureiot library depends on the following libraries: # @@ -64,7 +63,7 @@ # Create an IoT Hub device client and connect -device = IoTHubDevice(pool, ssl_context, secrets["device_connection_string"]) +device = IoTHubDevice(pool, ssl_context, device_connection_string) # Subscribe to direct method calls @@ -96,7 +95,7 @@ def direct_method_invoked(method_name: str, payload) -> IoTResponse: # If we lose connectivity, reset the wifi and reconnect wifi.radio.enabled = False wifi.radio.enabled = True - wifi.radio.connect(secrets["ssid"], secrets["password"]) + wifi.radio.connect(ssid, password) device.reconnect() continue diff --git a/examples/azureiot_native_networking/azureiot_hub_messages.py b/examples/azureiot_native_networking/azureiot_hub_messages.py index cc9a57e..32dc3e1 100644 --- a/examples/azureiot_native_networking/azureiot_hub_messages.py +++ b/examples/azureiot_native_networking/azureiot_hub_messages.py @@ -1,6 +1,7 @@ # SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries # SPDX-License-Identifier: MIT +from os import getenv import json import random import time @@ -12,15 +13,13 @@ import adafruit_ntp from adafruit_azureiot import IoTHubDevice -# 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 Azure keys, ensure these are setup in settings.toml +ssid = getenv("CIRCUITPY_WIFI_SSID") +password = getenv("CIRCUITPY_WIFI_PASSWORD") +device_connection_string = getenv("device_connection_string") print("Connecting to WiFi...") -wifi.radio.connect(secrets["ssid"], secrets["password"]) +wifi.radio.connect(ssid, password) pool = adafruit_connection_manager.get_radio_socketpool(wifi.radio) ssl_context = adafruit_connection_manager.get_radio_ssl_context(wifi.radio) @@ -56,7 +55,7 @@ # if you are using the free tier # # Once you have a hub and a device, copy the device primary connection string. -# Add it to the secrets.py file in an entry called device_connection_string +# Add it to the settings.toml file in an entry called device_connection_string # # The adafruit-circuitpython-azureiot library depends on the following libraries: # @@ -65,7 +64,7 @@ # Create an IoT Hub device client and connect -device = IoTHubDevice(pool, ssl_context, secrets["device_connection_string"]) +device = IoTHubDevice(pool, ssl_context, device_connection_string) # Subscribe to cloud to device messages @@ -104,7 +103,7 @@ def cloud_to_device_message_received(body: str, properties: dict): # If we lose connectivity, reset the wifi and reconnect wifi.radio.enabled = False wifi.radio.enabled = True - wifi.radio.connect(secrets["ssid"], secrets["password"]) + wifi.radio.connect(ssid, password) device.reconnect() continue time.sleep(1) diff --git a/examples/azureiot_native_networking/azureiot_hub_simpletest.py b/examples/azureiot_native_networking/azureiot_hub_simpletest.py index 8da982d..1ebf496 100644 --- a/examples/azureiot_native_networking/azureiot_hub_simpletest.py +++ b/examples/azureiot_native_networking/azureiot_hub_simpletest.py @@ -1,6 +1,7 @@ # SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries # SPDX-License-Identifier: MIT +from os import getenv import json import random import time @@ -12,15 +13,13 @@ import adafruit_ntp from adafruit_azureiot import IoTHubDevice -# 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 Azure keys, ensure these are setup in settings.toml +ssid = getenv("CIRCUITPY_WIFI_SSID") +password = getenv("CIRCUITPY_WIFI_PASSWORD") +device_connection_string = getenv("device_connection_string") print("Connecting to WiFi...") -wifi.radio.connect(secrets["ssid"], secrets["password"]) +wifi.radio.connect(ssid, password) pool = adafruit_connection_manager.get_radio_socketpool(wifi.radio) ssl_context = adafruit_connection_manager.get_radio_ssl_context(wifi.radio) @@ -56,7 +55,7 @@ # if you are using the free tier # # Once you have a hub and a device, copy the device primary connection string. -# Add it to the secrets.py file in an entry called device_connection_string +# Add it to the settings.toml file in an entry called device_connection_string # # The adafruit-circuitpython-azureiot library depends on the following libraries: # @@ -65,7 +64,7 @@ # Create an IoT Hub device client and connect -device = IoTHubDevice(pool, ssl_context, secrets["device_connection_string"]) +device = IoTHubDevice(pool, ssl_context, device_connection_string) print("Connecting to Azure IoT Hub...") @@ -95,7 +94,7 @@ # If we lose connectivity, reset the wifi and reconnect wifi.radio.enabled = False wifi.radio.enabled = True - wifi.radio.connect(secrets["ssid"], secrets["password"]) + wifi.radio.connect(ssid, password) device.reconnect() continue time.sleep(1) diff --git a/examples/azureiot_native_networking/azureiot_hub_twin_operations.py b/examples/azureiot_native_networking/azureiot_hub_twin_operations.py index 90b3303..2bea634 100644 --- a/examples/azureiot_native_networking/azureiot_hub_twin_operations.py +++ b/examples/azureiot_native_networking/azureiot_hub_twin_operations.py @@ -1,6 +1,7 @@ # SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries # SPDX-License-Identifier: MIT +from os import getenv import random import time @@ -11,15 +12,13 @@ import adafruit_ntp from adafruit_azureiot import IoTHubDevice -# 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 Azure keys, ensure these are setup in settings.toml +ssid = getenv("CIRCUITPY_WIFI_SSID") +password = getenv("CIRCUITPY_WIFI_PASSWORD") +device_connection_string = getenv("device_connection_string") print("Connecting to WiFi...") -wifi.radio.connect(secrets["ssid"], secrets["password"]) +wifi.radio.connect(ssid, password) pool = adafruit_connection_manager.get_radio_socketpool(wifi.radio) ssl_context = adafruit_connection_manager.get_radio_ssl_context(wifi.radio) @@ -55,7 +54,7 @@ # if you are using the free tier # # Once you have a hub and a device, copy the device primary connection string. -# Add it to the secrets.py file in an entry called device_connection_string +# Add it to the settings.toml file in an entry called device_connection_string # # The adafruit-circuitpython-azureiot library depends on the following libraries: # @@ -64,7 +63,7 @@ # Create an IoT Hub device client and connect -device = IoTHubDevice(pool, ssl_context, secrets["device_connection_string"]) +device = IoTHubDevice(pool, ssl_context, device_connection_string) # Subscribe to device twin desired property updates @@ -113,7 +112,7 @@ def device_twin_desired_updated( # If we lose connectivity, reset the wifi and reconnect wifi.radio.enabled = False wifi.radio.enabled = True - wifi.radio.connect(secrets["ssid"], secrets["password"]) + wifi.radio.connect(ssid, password) device.reconnect() continue time.sleep(1) diff --git a/examples/azureiot_secrets_example.py b/examples/azureiot_secrets_example.py deleted file mode 100644 index 37e618c..0000000 --- a/examples/azureiot_secrets_example.py +++ /dev/null @@ -1,40 +0,0 @@ -# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries -# SPDX-License-Identifier: MIT - -# This file is where you keep secret settings, passwords, and tokens! -# If you put them in the code you risk committing that info or sharing it -# which would be not great. So, instead, keep it all in this one file and -# keep it a secret. - -# To find out how to hide any changes you make to this file from Git, check out -# this blog post: https://www.jimbobbennett.io/hiding-api-keys-from-git/ - -""" -Contains the secrets for your app including WiFi connection details. -DO NOT CHECK THIS INTO SOURCE CODE CONTROL!!!!11!!! -""" - -secrets = { - # WiFi settings - "ssid": "", - "password": "", - # Azure IoT Central settings - if you are connecting to Azure IoT Central, fill in these three - # values - # To get these values, select your device in Azure IoT Central, - # then select the Connect button - # A dialog will appear with these three values - # id_scope comes from the ID scope value - # device_id comes from the Device ID value - # key comes from either the Primary key or Secondary key - "id_scope": "", - "device_id": "", - "device_sas_key": "", - # Azure IoT Hub settings - if you are connecting to Azure IoT Hub, fill in this value - # To get this value, from the Azure Portal (https://aka.ms/AzurePortalHome), select your IoT - # Hub, then select Explorers -> IoT devices, select your device, then copy the entire primary - # or secondary connection string using the copy button next to the value and set this here. - # It will be in the format: - # HostName=.azure-devices.net;DeviceId=;SharedAccessKey= - # Note - you need the primary or secondary connection string, NOT the primary or secondary key - "device_connection_string": "", -} diff --git a/examples/azureiot_settings_example.py b/examples/azureiot_settings_example.py new file mode 100644 index 0000000..276e1cd --- /dev/null +++ b/examples/azureiot_settings_example.py @@ -0,0 +1,38 @@ +# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries +# SPDX-License-Identifier: MIT + +# This file is where you keep settings, passwords, and tokens! +# If you put them in the code you risk committing that info or sharing it +# which would be not great. So, instead, keep it all in this one file and +# keep it a private. + +# To find out how to hide any changes you make to this file from Git, check out +# this blog post: https://www.jimbobbennett.io/hiding-api-keys-from-git/ + +# Contains the passwords and tokens for your app including WiFi connection details. +# DO NOT CHECK THIS INTO SOURCE CODE CONTROL!!!!11!!! + +# WiFi settings +CIRCUITPY_WIFI_SSID = "" +CIRCUITPY_WIFI_PASSWORD = "" + +# Azure IoT Central settings - if you are connecting to Azure IoT Central, fill in these three +# values +# To get these values, select your device in Azure IoT Central, +# then select the Connect button +# A dialog will appear with these three values +# id_scope comes from the ID scope value +# device_id comes from the Device ID value +# key comes from either the Primary key or Secondary key +id_scope = "" +device_id = "" +device_sas_key = "" +# Azure IoT Hub settings - if you are connecting to Azure IoT Hub, fill in this value + +# To get this value, from the Azure Portal (https://aka.ms/AzurePortalHome), select your IoT +# Hub, then select Explorers -> IoT devices, select your device, then copy the entire primary +# or secondary connection string using the copy button next to the value and set this here. +# It will be in the format: +# HostName=.azure-devices.net;DeviceId=;SharedAccessKey= +# Note - you need the primary or secondary connection string, NOT the primary or secondary key +device_connection_string = "" From 3e4e3d8dd26dc96d948e76d040539e14c38ed92b Mon Sep 17 00:00:00 2001 From: Justin Myers Date: Mon, 24 Feb 2025 18:46:50 -0800 Subject: [PATCH 2/3] Update README.rst Co-authored-by: Dan Halbert --- README.rst | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.rst b/README.rst index 95c1385..18ef5f6 100644 --- a/README.rst +++ b/README.rst @@ -215,8 +215,8 @@ settings.toml: .. code-block:: python # WiFi settings - CIRCUITPY_WIFI_SSID="", - CIRCUITPY_WIFI_PASSWORD="", + CIRCUITPY_WIFI_SSID="" + CIRCUITPY_WIFI_PASSWORD="" # Azure IoT Central settings id_scope="" From 99e1de59efb20d8794ae4ab7b9cea0922b244755 Mon Sep 17 00:00:00 2001 From: Justin Myers Date: Mon, 24 Feb 2025 19:05:00 -0800 Subject: [PATCH 3/3] Update README --- README.rst | 25 +++++++++++++++++++------ 1 file changed, 19 insertions(+), 6 deletions(-) diff --git a/README.rst b/README.rst index 18ef5f6..10c2187 100644 --- a/README.rst +++ b/README.rst @@ -215,21 +215,34 @@ settings.toml: .. code-block:: python # WiFi settings - CIRCUITPY_WIFI_SSID="" - CIRCUITPY_WIFI_PASSWORD="" + CIRCUITPY_WIFI_SSID="Your WiFi ssid" + CIRCUITPY_WIFI_PASSWORD="Your WiFi password" # Azure IoT Central settings - id_scope="" - device_id="" - device_sas_key="" + id_scope="Your ID Scope" + device_id="Your Device ID" + device_sas_key="Your Primary Key" **Connect your device to your Azure IoT Central app** .. code-block:: python + import wifi + from os import getenv from adafruit_azureiot import IoTCentralDevice + import adafruit_connection_manager - device = IoTCentralDevice(wifi, id_scope, device_id, device_sas_key) + ssid = getenv("CIRCUITPY_WIFI_SSID") + password = getenv("CIRCUITPY_WIFI_PASSWORD") + id_scope = getenv("id_scope") + device_id = getenv("device_id") + device_sas_key = getenv("device_sas_key") + + wifi.radio.connect(ssid, password) + + pool = adafruit_connection_manager.get_radio_socketpool(wifi.radio) + + device = IoTCentralDevice(pool, id_scope, device_id, device_sas_key) device.connect() Once the device is connected, you will regularly need to run a ``loop`` to poll for messages from the cloud.