From 98182a6e4fa8dfaf5b3fb7e8d55913113e406631 Mon Sep 17 00:00:00 2001 From: Justin Myers Date: Tue, 30 Apr 2024 19:54:08 -0700 Subject: [PATCH 1/7] Fix ESP32SPI --- README.rst | 6 ++---- adafruit_azureiot/device_registration.py | 13 ++++++------- adafruit_azureiot/iot_mqtt.py | 13 ++++++------- .../azureiot_esp32spi/azureiot_central_commands.py | 10 ++++++++-- .../azureiot_central_notconnected.py | 10 ++++++++-- .../azureiot_central_properties.py | 10 ++++++++-- .../azureiot_central_simpletest.py | 10 ++++++++-- .../azureiot_hub_directmethods.py | 6 ++++-- .../azureiot_esp32spi/azureiot_hub_messages.py | 6 ++++-- .../azureiot_esp32spi/azureiot_hub_simpletest.py | 6 ++++-- .../azureiot_hub_twin_operations.py | 6 ++++-- .../azureiot_central_commands.py | 14 +++++++++----- .../azureiot_central_notconnected.py | 14 +++++++++----- .../azureiot_central_properties.py | 14 +++++++++----- .../azureiot_central_simpletest.py | 14 +++++++++----- .../azureiot_hub_directmethods.py | 10 +++++----- .../azureiot_hub_messages.py | 10 +++++----- .../azureiot_hub_simpletest.py | 10 +++++----- .../azureiot_hub_twin_operations.py | 10 +++++----- 19 files changed, 118 insertions(+), 74 deletions(-) diff --git a/README.rst b/README.rst index 0bed32c..9439579 100644 --- a/README.rst +++ b/README.rst @@ -58,7 +58,7 @@ Please ensure all dependencies are available on the CircuitPython filesystem. This is easily achieved by downloading `the Adafruit library and driver bundle `_. -**Board Compatibility:** The following built-in modules must be available: gc, json, ssl, time +**Board Compatibility:** The following built-in modules must be available: gc, json, time Usage Example ============= @@ -74,8 +74,6 @@ To create an Azure IoT Hub instance or an Azure IoT Central app, you will need a ESP32 AirLift Networking ======================== -*NOTE* currently the ESP32 AirLift is not supported due to the requirment of `ssl`, which is only on boards with native WiFi. - To use this library, you will need to create an ESP32_SPI WifiManager, connected to WiFi. You will also need to set the current time, as this is used to generate time-based authentication keys. One way to do this is with the following code: .. code-block:: python @@ -96,7 +94,7 @@ To use this library, with boards that have native networking support, you need t .. code-block:: python - pool = socketpool.SocketPool(wifi.radio) + pool = adafruit_connection_manager.get_radio_socketpool(wifi.radio) ntp = adafruit_ntp.NTP(pool, tz_offset=0) # NOTE: This changes the system time so make sure you aren't assuming that time diff --git a/adafruit_azureiot/device_registration.py b/adafruit_azureiot/device_registration.py index b616d3c..b4532a1 100644 --- a/adafruit_azureiot/device_registration.py +++ b/adafruit_azureiot/device_registration.py @@ -14,7 +14,6 @@ """ import json -import ssl import time import adafruit_logging as logging @@ -45,8 +44,8 @@ class DeviceRegistration: # pylint: disable=R0913 def __init__( self, - socket, - iface, + socket_pool, + ssl_context, id_scope: str, device_id: str, device_sas_key: str, @@ -74,8 +73,8 @@ def __init__( self._operation_id = None self._hostname = None - self._socket = socket - self._iface = iface + self._socket_pool = socket_pool + self._ssl_context = ssl_context # pylint: disable=W0613 # pylint: disable=C0103 @@ -202,8 +201,8 @@ def register_device(self, expiry: int) -> str: client_id=self._device_id, is_ssl=True, keep_alive=120, - socket_pool=self._socket, - ssl_context=ssl.create_default_context(), + socket_pool=self._socket_pool, + ssl_context=self._ssl_context, ) self._mqtt.enable_logger(logging, self._logger.getEffectiveLevel()) diff --git a/adafruit_azureiot/iot_mqtt.py b/adafruit_azureiot/iot_mqtt.py index 0d13800..c47f785 100644 --- a/adafruit_azureiot/iot_mqtt.py +++ b/adafruit_azureiot/iot_mqtt.py @@ -14,7 +14,6 @@ import gc import json -import ssl import time import adafruit_minimqtt.adafruit_minimqtt as MQTT @@ -139,8 +138,8 @@ def _create_mqtt_client(self) -> None: client_id=self._device_id, is_ssl=True, keep_alive=120, - socket_pool=self._socket, - ssl_context=ssl.create_default_context(), + socket_pool=self._socket_pool, + ssl_context=self._ssl_context, ) self._mqtts.enable_logger(logging, self._logger.getEffectiveLevel()) @@ -326,8 +325,8 @@ def _get_device_settings(self) -> None: def __init__( self, callback: IoTMQTTCallback, - socket, - iface, + socket_pool, + ssl_context, hostname: str, device_id: str, device_sas_key: str, @@ -347,8 +346,8 @@ def __init__( :param Logger logger: The logger """ self._callback = callback - self._socket = socket - self._iface = iface + self._socket_pool = socket_pool + self._ssl_context = ssl_context self._auth_response_received = False self._mqtts = None self._device_id = device_id diff --git a/examples/azureiot_esp32spi/azureiot_central_commands.py b/examples/azureiot_esp32spi/azureiot_central_commands.py index c829a32..08194cb 100644 --- a/examples/azureiot_esp32spi/azureiot_central_commands.py +++ b/examples/azureiot_esp32spi/azureiot_central_commands.py @@ -8,7 +8,7 @@ import neopixel import rtc from adafruit_esp32spi import adafruit_esp32spi, adafruit_esp32spi_wifimanager -import adafruit_esp32spi.adafruit_esp32spi_socket as socket +import adafruit_connection_manager # Get wifi details and more from a secrets.py file try: @@ -103,9 +103,15 @@ # pylint: enable=wrong-import-position +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 = IoTCentralDevice( - socket, esp, secrets["id_scope"], secrets["device_id"], secrets["device_sas_key"] + pool, + ssl_context, + secrets["id_scope"], + secrets["device_id"], + secrets["device_sas_key"], ) diff --git a/examples/azureiot_esp32spi/azureiot_central_notconnected.py b/examples/azureiot_esp32spi/azureiot_central_notconnected.py index f78afd1..fe2bf62 100644 --- a/examples/azureiot_esp32spi/azureiot_central_notconnected.py +++ b/examples/azureiot_esp32spi/azureiot_central_notconnected.py @@ -10,7 +10,7 @@ import neopixel import rtc from adafruit_esp32spi import adafruit_esp32spi, adafruit_esp32spi_wifimanager -import adafruit_esp32spi.adafruit_esp32spi_socket as socket +import adafruit_connection_manager # Get wifi details and more from a secrets.py file try: @@ -99,9 +99,15 @@ # pylint: enable=wrong-import-position +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 = IoTCentralDevice( - socket, esp, secrets["id_scope"], secrets["device_id"], secrets["device_sas_key"] + pool, + ssl_context, + secrets["id_scope"], + secrets["device_id"], + secrets["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 826cdf2..1d7e942 100644 --- a/examples/azureiot_esp32spi/azureiot_central_properties.py +++ b/examples/azureiot_esp32spi/azureiot_central_properties.py @@ -9,7 +9,7 @@ import neopixel import rtc from adafruit_esp32spi import adafruit_esp32spi, adafruit_esp32spi_wifimanager -import adafruit_esp32spi.adafruit_esp32spi_socket as socket +import adafruit_connection_manager # Get wifi details and more from a secrets.py file try: @@ -100,9 +100,15 @@ # * adafruit-circuitpython-requests from adafruit_azureiot import IoTCentralDevice # pylint: disable=wrong-import-position +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 = IoTCentralDevice( - socket, esp, secrets["id_scope"], secrets["device_id"], secrets["device_sas_key"] + pool, + ssl_context, + secrets["id_scope"], + secrets["device_id"], + secrets["device_sas_key"], ) diff --git a/examples/azureiot_esp32spi/azureiot_central_simpletest.py b/examples/azureiot_esp32spi/azureiot_central_simpletest.py index 546b5d0..a710d23 100644 --- a/examples/azureiot_esp32spi/azureiot_central_simpletest.py +++ b/examples/azureiot_esp32spi/azureiot_central_simpletest.py @@ -10,7 +10,7 @@ import neopixel import rtc from adafruit_esp32spi import adafruit_esp32spi, adafruit_esp32spi_wifimanager -import adafruit_esp32spi.adafruit_esp32spi_socket as socket +import adafruit_connection_manager # Get wifi details and more from a secrets.py file try: @@ -101,9 +101,15 @@ # * adafruit-circuitpython-requests from adafruit_azureiot import IoTCentralDevice # pylint: disable=wrong-import-position +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 = IoTCentralDevice( - socket, esp, secrets["id_scope"], secrets["device_id"], secrets["device_sas_key"] + pool, + ssl_context, + secrets["id_scope"], + secrets["device_id"], + secrets["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 1a9e079..2c020ae 100644 --- a/examples/azureiot_esp32spi/azureiot_hub_directmethods.py +++ b/examples/azureiot_esp32spi/azureiot_hub_directmethods.py @@ -8,7 +8,7 @@ import neopixel import rtc from adafruit_esp32spi import adafruit_esp32spi, adafruit_esp32spi_wifimanager -import adafruit_esp32spi.adafruit_esp32spi_socket as socket +import adafruit_connection_manager # Get wifi details and more from a secrets.py file try: @@ -97,8 +97,10 @@ # pylint: enable=wrong-import-position +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(socket, esp, secrets["device_connection_string"]) +device = IoTHubDevice(pool, ssl_context, secrets["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 999de2e..36f05d4 100644 --- a/examples/azureiot_esp32spi/azureiot_hub_messages.py +++ b/examples/azureiot_esp32spi/azureiot_hub_messages.py @@ -10,7 +10,7 @@ import neopixel import rtc from adafruit_esp32spi import adafruit_esp32spi, adafruit_esp32spi_wifimanager -import adafruit_esp32spi.adafruit_esp32spi_socket as socket +import adafruit_connection_manager # Get wifi details and more from a secrets.py file try: @@ -95,8 +95,10 @@ # * adafruit-circuitpython-requests from adafruit_azureiot import IoTHubDevice # pylint: disable=wrong-import-position +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(socket, esp, secrets["device_connection_string"]) +device = IoTHubDevice(pool, ssl_context, secrets["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 2e16a57..7d4d94c 100644 --- a/examples/azureiot_esp32spi/azureiot_hub_simpletest.py +++ b/examples/azureiot_esp32spi/azureiot_hub_simpletest.py @@ -10,7 +10,7 @@ import neopixel import rtc from adafruit_esp32spi import adafruit_esp32spi, adafruit_esp32spi_wifimanager -import adafruit_esp32spi.adafruit_esp32spi_socket as socket +import adafruit_connection_manager # Get wifi details and more from a secrets.py file try: @@ -95,8 +95,10 @@ # * adafruit-circuitpython-requests from adafruit_azureiot import IoTHubDevice # pylint: disable=wrong-import-position +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(socket, esp, secrets["device_connection_string"]) +device = IoTHubDevice(pool, ssl_context, secrets["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 41944b8..81c479a 100644 --- a/examples/azureiot_esp32spi/azureiot_hub_twin_operations.py +++ b/examples/azureiot_esp32spi/azureiot_hub_twin_operations.py @@ -9,7 +9,7 @@ import neopixel import rtc from adafruit_esp32spi import adafruit_esp32spi, adafruit_esp32spi_wifimanager -import adafruit_esp32spi.adafruit_esp32spi_socket as socket +import adafruit_connection_manager # Get wifi details and more from a secrets.py file try: @@ -97,8 +97,10 @@ # * adafruit-circuitpython-requests from adafruit_azureiot import IoTHubDevice # pylint: disable=wrong-import-position +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(socket, esp, secrets["device_connection_string"]) +device = IoTHubDevice(pool, ssl_context, secrets["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 07e151e..425bbb9 100644 --- a/examples/azureiot_native_networking/azureiot_central_commands.py +++ b/examples/azureiot_native_networking/azureiot_central_commands.py @@ -4,9 +4,9 @@ import time import rtc -import socketpool import wifi +import adafruit_connection_manager import adafruit_ntp from adafruit_azureiot import IoTCentralDevice from adafruit_azureiot.iot_mqtt import IoTResponse @@ -21,11 +21,13 @@ print("Connecting to WiFi...") wifi.radio.connect(secrets["ssid"], secrets["password"]) +pool = adafruit_connection_manager.get_radio_socketpool(wifi.radio) +ssl_context = adafruit_connection_manager.get_radio_ssl_context(wifi.radio) + print("Connected to WiFi!") if time.localtime().tm_year < 2022: print("Setting System Time in UTC") - pool = socketpool.SocketPool(wifi.radio) ntp = adafruit_ntp.NTP(pool, tz_offset=0) # NOTE: This changes the system time so make sure you aren't assuming that time @@ -69,10 +71,12 @@ # Create an IoT Hub device client and connect -esp = None -pool = socketpool.SocketPool(wifi.radio) device = IoTCentralDevice( - pool, esp, secrets["id_scope"], secrets["device_id"], secrets["device_sas_key"] + pool, + ssl_context, + secrets["id_scope"], + secrets["device_id"], + secrets["device_sas_key"], ) diff --git a/examples/azureiot_native_networking/azureiot_central_notconnected.py b/examples/azureiot_native_networking/azureiot_central_notconnected.py index 5ad0f47..1cb63c9 100644 --- a/examples/azureiot_native_networking/azureiot_central_notconnected.py +++ b/examples/azureiot_native_networking/azureiot_central_notconnected.py @@ -6,9 +6,9 @@ import time import rtc -import socketpool import wifi +import adafruit_connection_manager import adafruit_ntp from adafruit_azureiot import ( IoTCentralDevice, @@ -25,11 +25,13 @@ print("Connecting to WiFi...") wifi.radio.connect(secrets["ssid"], secrets["password"]) +pool = adafruit_connection_manager.get_radio_socketpool(wifi.radio) +ssl_context = adafruit_connection_manager.get_radio_ssl_context(wifi.radio) + print("Connected to WiFi!") if time.localtime().tm_year < 2022: print("Setting System Time in UTC") - pool = socketpool.SocketPool(wifi.radio) ntp = adafruit_ntp.NTP(pool, tz_offset=0) # NOTE: This changes the system time so make sure you aren't assuming that time @@ -73,10 +75,12 @@ # Create an IoT Hub device client and connect -esp = None -pool = socketpool.SocketPool(wifi.radio) device = IoTCentralDevice( - pool, esp, secrets["id_scope"], secrets["device_id"], secrets["device_sas_key"] + pool, + ssl_context, + secrets["id_scope"], + secrets["device_id"], + secrets["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 91a0a33..58015a5 100644 --- a/examples/azureiot_native_networking/azureiot_central_properties.py +++ b/examples/azureiot_native_networking/azureiot_central_properties.py @@ -5,9 +5,9 @@ import time import rtc -import socketpool import wifi +import adafruit_connection_manager import adafruit_ntp from adafruit_azureiot import IoTCentralDevice @@ -21,11 +21,13 @@ print("Connecting to WiFi...") wifi.radio.connect(secrets["ssid"], secrets["password"]) +pool = adafruit_connection_manager.get_radio_socketpool(wifi.radio) +ssl_context = adafruit_connection_manager.get_radio_ssl_context(wifi.radio) + print("Connected to WiFi!") if time.localtime().tm_year < 2022: print("Setting System Time in UTC") - pool = socketpool.SocketPool(wifi.radio) ntp = adafruit_ntp.NTP(pool, tz_offset=0) # NOTE: This changes the system time so make sure you aren't assuming that time @@ -69,10 +71,12 @@ # Create an IoT Hub device client and connect -esp = None -pool = socketpool.SocketPool(wifi.radio) device = IoTCentralDevice( - pool, esp, secrets["id_scope"], secrets["device_id"], secrets["device_sas_key"] + pool, + ssl_context, + secrets["id_scope"], + secrets["device_id"], + secrets["device_sas_key"], ) diff --git a/examples/azureiot_native_networking/azureiot_central_simpletest.py b/examples/azureiot_native_networking/azureiot_central_simpletest.py index 0595783..cb56138 100644 --- a/examples/azureiot_native_networking/azureiot_central_simpletest.py +++ b/examples/azureiot_native_networking/azureiot_central_simpletest.py @@ -6,9 +6,9 @@ import time import rtc -import socketpool import wifi +import adafruit_connection_manager import adafruit_ntp from adafruit_azureiot import IoTCentralDevice @@ -22,11 +22,13 @@ print("Connecting to WiFi...") wifi.radio.connect(secrets["ssid"], secrets["password"]) +pool = adafruit_connection_manager.get_radio_socketpool(wifi.radio) +ssl_context = adafruit_connection_manager.get_radio_ssl_context(wifi.radio) + print("Connected to WiFi!") if time.localtime().tm_year < 2022: print("Setting System Time in UTC") - pool = socketpool.SocketPool(wifi.radio) ntp = adafruit_ntp.NTP(pool, tz_offset=0) # NOTE: This changes the system time so make sure you aren't assuming that time @@ -70,10 +72,12 @@ # Create an IoT Hub device client and connect -esp = None -pool = socketpool.SocketPool(wifi.radio) device = IoTCentralDevice( - pool, esp, secrets["id_scope"], secrets["device_id"], secrets["device_sas_key"] + pool, + ssl_context, + secrets["id_scope"], + secrets["device_id"], + secrets["device_sas_key"], ) print("Connecting to Azure IoT Central...") diff --git a/examples/azureiot_native_networking/azureiot_hub_directmethods.py b/examples/azureiot_native_networking/azureiot_hub_directmethods.py index 9589cf9..518f666 100644 --- a/examples/azureiot_native_networking/azureiot_hub_directmethods.py +++ b/examples/azureiot_native_networking/azureiot_hub_directmethods.py @@ -3,10 +3,10 @@ import time -import socketpool import rtc import wifi +import adafruit_connection_manager import adafruit_ntp from adafruit_azureiot import IoTHubDevice from adafruit_azureiot.iot_mqtt import IoTResponse @@ -21,11 +21,13 @@ print("Connecting to WiFi...") wifi.radio.connect(secrets["ssid"], secrets["password"]) +pool = adafruit_connection_manager.get_radio_socketpool(wifi.radio) +ssl_context = adafruit_connection_manager.get_radio_ssl_context(wifi.radio) + print("Connected to WiFi!") if time.localtime().tm_year < 2022: print("Setting System Time in UTC") - pool = socketpool.SocketPool(wifi.radio) ntp = adafruit_ntp.NTP(pool, tz_offset=0) # NOTE: This changes the system time so make sure you aren't assuming that time @@ -62,10 +64,8 @@ # * adafruit-circuitpython-requests -esp = None -pool = socketpool.SocketPool(wifi.radio) # Create an IoT Hub device client and connect -device = IoTHubDevice(pool, esp, secrets["device_connection_string"]) +device = IoTHubDevice(pool, ssl_context, secrets["device_connection_string"]) # Subscribe to direct method calls diff --git a/examples/azureiot_native_networking/azureiot_hub_messages.py b/examples/azureiot_native_networking/azureiot_hub_messages.py index 1baea14..1ea36bf 100644 --- a/examples/azureiot_native_networking/azureiot_hub_messages.py +++ b/examples/azureiot_native_networking/azureiot_hub_messages.py @@ -5,10 +5,10 @@ import random import time -import socketpool import rtc import wifi +import adafruit_connection_manager import adafruit_ntp from adafruit_azureiot import IoTHubDevice @@ -22,11 +22,13 @@ print("Connecting to WiFi...") wifi.radio.connect(secrets["ssid"], secrets["password"]) +pool = adafruit_connection_manager.get_radio_socketpool(wifi.radio) +ssl_context = adafruit_connection_manager.get_radio_ssl_context(wifi.radio) + print("Connected to WiFi!") if time.localtime().tm_year < 2022: print("Setting System Time in UTC") - pool = socketpool.SocketPool(wifi.radio) ntp = adafruit_ntp.NTP(pool, tz_offset=0) # NOTE: This changes the system time so make sure you aren't assuming that time @@ -63,10 +65,8 @@ # * adafruit-circuitpython-requests -esp = None -pool = socketpool.SocketPool(wifi.radio) # Create an IoT Hub device client and connect -device = IoTHubDevice(pool, esp, secrets["device_connection_string"]) +device = IoTHubDevice(pool, ssl_context, secrets["device_connection_string"]) # Subscribe to cloud to device messages diff --git a/examples/azureiot_native_networking/azureiot_hub_simpletest.py b/examples/azureiot_native_networking/azureiot_hub_simpletest.py index acc7a94..4cb6069 100644 --- a/examples/azureiot_native_networking/azureiot_hub_simpletest.py +++ b/examples/azureiot_native_networking/azureiot_hub_simpletest.py @@ -5,10 +5,10 @@ import random import time -import socketpool import rtc import wifi +import adafruit_connection_manager import adafruit_ntp from adafruit_azureiot import IoTHubDevice @@ -22,11 +22,13 @@ print("Connecting to WiFi...") wifi.radio.connect(secrets["ssid"], secrets["password"]) +pool = adafruit_connection_manager.get_radio_socketpool(wifi.radio) +ssl_context = adafruit_connection_manager.get_radio_ssl_context(wifi.radio) + print("Connected to WiFi!") if time.localtime().tm_year < 2022: print("Setting System Time in UTC") - pool = socketpool.SocketPool(wifi.radio) ntp = adafruit_ntp.NTP(pool, tz_offset=0) # NOTE: This changes the system time so make sure you aren't assuming that time @@ -63,10 +65,8 @@ # * adafruit-circuitpython-requests -esp = None -pool = socketpool.SocketPool(wifi.radio) # Create an IoT Hub device client and connect -device = IoTHubDevice(pool, esp, secrets["device_connection_string"]) +device = IoTHubDevice(pool, ssl_context, secrets["device_connection_string"]) print("Connecting to Azure IoT Hub...") diff --git a/examples/azureiot_native_networking/azureiot_hub_twin_operations.py b/examples/azureiot_native_networking/azureiot_hub_twin_operations.py index 3984527..b878d74 100644 --- a/examples/azureiot_native_networking/azureiot_hub_twin_operations.py +++ b/examples/azureiot_native_networking/azureiot_hub_twin_operations.py @@ -4,10 +4,10 @@ import random import time -import socketpool import rtc import wifi +import adafruit_connection_manager import adafruit_ntp from adafruit_azureiot import IoTHubDevice @@ -21,11 +21,13 @@ print("Connecting to WiFi...") wifi.radio.connect(secrets["ssid"], secrets["password"]) +pool = adafruit_connection_manager.get_radio_socketpool(wifi.radio) +ssl_context = adafruit_connection_manager.get_radio_ssl_context(wifi.radio) + print("Connected to WiFi!") if time.localtime().tm_year < 2022: print("Setting System Time in UTC") - pool = socketpool.SocketPool(wifi.radio) ntp = adafruit_ntp.NTP(pool, tz_offset=0) # NOTE: This changes the system time so make sure you aren't assuming that time @@ -62,10 +64,8 @@ # * adafruit-circuitpython-requests -esp = None -pool = socketpool.SocketPool(wifi.radio) # Create an IoT Hub device client and connect -device = IoTHubDevice(pool, esp, secrets["device_connection_string"]) +device = IoTHubDevice(pool, ssl_context, secrets["device_connection_string"]) # Subscribe to device twin desired property updates From 922b05cab8ee12f2b7499357bdc1937d5de6efad Mon Sep 17 00:00:00 2001 From: Justin Myers Date: Thu, 2 May 2024 07:24:31 -0700 Subject: [PATCH 2/7] Remove invalid dependency info --- README.rst | 2 -- adafruit_azureiot/__init__.py | 3 --- examples/azureiot_esp32spi/azureiot_central_commands.py | 1 - examples/azureiot_esp32spi/azureiot_central_notconnected.py | 1 - examples/azureiot_esp32spi/azureiot_central_properties.py | 1 - examples/azureiot_esp32spi/azureiot_central_simpletest.py | 1 - examples/azureiot_esp32spi/azureiot_hub_directmethods.py | 1 - examples/azureiot_esp32spi/azureiot_hub_messages.py | 1 - examples/azureiot_esp32spi/azureiot_hub_simpletest.py | 1 - examples/azureiot_esp32spi/azureiot_hub_twin_operations.py | 1 - .../azureiot_native_networking/azureiot_central_commands.py | 1 - .../azureiot_central_notconnected.py | 1 - .../azureiot_native_networking/azureiot_central_properties.py | 1 - .../azureiot_native_networking/azureiot_central_simpletest.py | 1 - .../azureiot_native_networking/azureiot_hub_directmethods.py | 1 - examples/azureiot_native_networking/azureiot_hub_messages.py | 1 - examples/azureiot_native_networking/azureiot_hub_simpletest.py | 1 - .../azureiot_native_networking/azureiot_hub_twin_operations.py | 1 - requirements.txt | 1 - 19 files changed, 22 deletions(-) diff --git a/README.rst b/README.rst index 9439579..1052c4d 100644 --- a/README.rst +++ b/README.rst @@ -49,10 +49,8 @@ This driver depends on: * `Adafruit CircuitPython `_ * `Adafruit CircuitPython BinASCII `_ -* `Adafruit CircuitPython ConnectionManager `_ * `Adafruit CircuitPython Logging `_ * `Adafruit CircuitPython MiniMQTT `_ -* `Adafruit CircuitPython Requests `_ Please ensure all dependencies are available on the CircuitPython filesystem. This is easily achieved by downloading diff --git a/adafruit_azureiot/__init__.py b/adafruit_azureiot/__init__.py index 43d901d..efff896 100644 --- a/adafruit_azureiot/__init__.py +++ b/adafruit_azureiot/__init__.py @@ -21,15 +21,12 @@ **With ESP32 Airlift Networking** -* Adafruit's Bus Device library: https://github.com/adafruit/Adafruit_CircuitPython_BusDevice * Adafruit's ESP32SPI library: https://github.com/adafruit/Adafruit_CircuitPython_ESP32SPI -* Adafruit's NTP library: https://github.com/adafruit/Adafruit_CircuitPython_NTP **With Native Networking** * CircuitPython's Wifi Module: https://docs.circuitpython.org/en/latest/shared-bindings/wifi/index.html -* Adafruit's Requests Library: https://github.com/adafruit/Adafruit_CircuitPython_Requests/ """ from .iot_error import IoTError diff --git a/examples/azureiot_esp32spi/azureiot_central_commands.py b/examples/azureiot_esp32spi/azureiot_central_commands.py index 08194cb..d11cc1f 100644 --- a/examples/azureiot_esp32spi/azureiot_central_commands.py +++ b/examples/azureiot_esp32spi/azureiot_central_commands.py @@ -96,7 +96,6 @@ # # From the Adafruit CircuitPython Bundle https://github.com/adafruit/Adafruit_CircuitPython_Bundle: # * adafruit-circuitpython-minimqtt -# * adafruit-circuitpython-requests # pylint: disable=wrong-import-position from adafruit_azureiot import IoTCentralDevice from adafruit_azureiot.iot_mqtt import IoTResponse diff --git a/examples/azureiot_esp32spi/azureiot_central_notconnected.py b/examples/azureiot_esp32spi/azureiot_central_notconnected.py index fe2bf62..8cb7863 100644 --- a/examples/azureiot_esp32spi/azureiot_central_notconnected.py +++ b/examples/azureiot_esp32spi/azureiot_central_notconnected.py @@ -90,7 +90,6 @@ # # From the Adafruit CircuitPython Bundle https://github.com/adafruit/Adafruit_CircuitPython_Bundle: # * adafruit-circuitpython-minimqtt -# * adafruit-circuitpython-requests # pylint: disable=wrong-import-position from adafruit_azureiot import ( IoTCentralDevice, diff --git a/examples/azureiot_esp32spi/azureiot_central_properties.py b/examples/azureiot_esp32spi/azureiot_central_properties.py index 1d7e942..43d131b 100644 --- a/examples/azureiot_esp32spi/azureiot_central_properties.py +++ b/examples/azureiot_esp32spi/azureiot_central_properties.py @@ -97,7 +97,6 @@ # # From the Adafruit CircuitPython Bundle https://github.com/adafruit/Adafruit_CircuitPython_Bundle: # * adafruit-circuitpython-minimqtt -# * adafruit-circuitpython-requests from adafruit_azureiot import IoTCentralDevice # pylint: disable=wrong-import-position pool = adafruit_connection_manager.get_radio_socketpool(esp) diff --git a/examples/azureiot_esp32spi/azureiot_central_simpletest.py b/examples/azureiot_esp32spi/azureiot_central_simpletest.py index a710d23..ad66d2c 100644 --- a/examples/azureiot_esp32spi/azureiot_central_simpletest.py +++ b/examples/azureiot_esp32spi/azureiot_central_simpletest.py @@ -98,7 +98,6 @@ # # From the Adafruit CircuitPython Bundle https://github.com/adafruit/Adafruit_CircuitPython_Bundle: # * adafruit-circuitpython-minimqtt -# * adafruit-circuitpython-requests from adafruit_azureiot import IoTCentralDevice # pylint: disable=wrong-import-position pool = adafruit_connection_manager.get_radio_socketpool(esp) diff --git a/examples/azureiot_esp32spi/azureiot_hub_directmethods.py b/examples/azureiot_esp32spi/azureiot_hub_directmethods.py index 2c020ae..b329c0e 100644 --- a/examples/azureiot_esp32spi/azureiot_hub_directmethods.py +++ b/examples/azureiot_esp32spi/azureiot_hub_directmethods.py @@ -90,7 +90,6 @@ # # From the Adafruit CircuitPython Bundle https://github.com/adafruit/Adafruit_CircuitPython_Bundle: # * adafruit-circuitpython-minimqtt -# * adafruit-circuitpython-requests # pylint: disable=wrong-import-position from adafruit_azureiot import IoTHubDevice from adafruit_azureiot.iot_mqtt import IoTResponse diff --git a/examples/azureiot_esp32spi/azureiot_hub_messages.py b/examples/azureiot_esp32spi/azureiot_hub_messages.py index 36f05d4..956a3a8 100644 --- a/examples/azureiot_esp32spi/azureiot_hub_messages.py +++ b/examples/azureiot_esp32spi/azureiot_hub_messages.py @@ -92,7 +92,6 @@ # # From the Adafruit CircuitPython Bundle https://github.com/adafruit/Adafruit_CircuitPython_Bundle: # * adafruit-circuitpython-minimqtt -# * adafruit-circuitpython-requests from adafruit_azureiot import IoTHubDevice # pylint: disable=wrong-import-position pool = adafruit_connection_manager.get_radio_socketpool(esp) diff --git a/examples/azureiot_esp32spi/azureiot_hub_simpletest.py b/examples/azureiot_esp32spi/azureiot_hub_simpletest.py index 7d4d94c..20d7136 100644 --- a/examples/azureiot_esp32spi/azureiot_hub_simpletest.py +++ b/examples/azureiot_esp32spi/azureiot_hub_simpletest.py @@ -92,7 +92,6 @@ # # From the Adafruit CircuitPython Bundle https://github.com/adafruit/Adafruit_CircuitPython_Bundle: # * adafruit-circuitpython-minimqtt -# * adafruit-circuitpython-requests from adafruit_azureiot import IoTHubDevice # pylint: disable=wrong-import-position pool = adafruit_connection_manager.get_radio_socketpool(esp) diff --git a/examples/azureiot_esp32spi/azureiot_hub_twin_operations.py b/examples/azureiot_esp32spi/azureiot_hub_twin_operations.py index 81c479a..3ed7842 100644 --- a/examples/azureiot_esp32spi/azureiot_hub_twin_operations.py +++ b/examples/azureiot_esp32spi/azureiot_hub_twin_operations.py @@ -94,7 +94,6 @@ # # From the Adafruit CircuitPython Bundle https://github.com/adafruit/Adafruit_CircuitPython_Bundle: # * adafruit-circuitpython-minimqtt -# * adafruit-circuitpython-requests from adafruit_azureiot import IoTHubDevice # pylint: disable=wrong-import-position pool = adafruit_connection_manager.get_radio_socketpool(esp) diff --git a/examples/azureiot_native_networking/azureiot_central_commands.py b/examples/azureiot_native_networking/azureiot_central_commands.py index 425bbb9..85b0476 100644 --- a/examples/azureiot_native_networking/azureiot_central_commands.py +++ b/examples/azureiot_native_networking/azureiot_central_commands.py @@ -67,7 +67,6 @@ # # From the Adafruit CircuitPython Bundle https://github.com/adafruit/Adafruit_CircuitPython_Bundle: # * adafruit-circuitpython-minimqtt -# * adafruit-circuitpython-requests # Create an IoT Hub device client and connect diff --git a/examples/azureiot_native_networking/azureiot_central_notconnected.py b/examples/azureiot_native_networking/azureiot_central_notconnected.py index 1cb63c9..b695dae 100644 --- a/examples/azureiot_native_networking/azureiot_central_notconnected.py +++ b/examples/azureiot_native_networking/azureiot_central_notconnected.py @@ -71,7 +71,6 @@ # # From the Adafruit CircuitPython Bundle https://github.com/adafruit/Adafruit_CircuitPython_Bundle: # * adafruit-circuitpython-minimqtt -# * adafruit-circuitpython-requests # Create an IoT Hub device client and connect diff --git a/examples/azureiot_native_networking/azureiot_central_properties.py b/examples/azureiot_native_networking/azureiot_central_properties.py index 58015a5..7cbd9c0 100644 --- a/examples/azureiot_native_networking/azureiot_central_properties.py +++ b/examples/azureiot_native_networking/azureiot_central_properties.py @@ -67,7 +67,6 @@ # # From the Adafruit CircuitPython Bundle https://github.com/adafruit/Adafruit_CircuitPython_Bundle: # * adafruit-circuitpython-minimqtt -# * adafruit-circuitpython-requests # Create an IoT Hub device client and connect diff --git a/examples/azureiot_native_networking/azureiot_central_simpletest.py b/examples/azureiot_native_networking/azureiot_central_simpletest.py index cb56138..8ce022d 100644 --- a/examples/azureiot_native_networking/azureiot_central_simpletest.py +++ b/examples/azureiot_native_networking/azureiot_central_simpletest.py @@ -68,7 +68,6 @@ # # From the Adafruit CircuitPython Bundle https://github.com/adafruit/Adafruit_CircuitPython_Bundle: # * adafruit-circuitpython-minimqtt -# * adafruit-circuitpython-requests # Create an IoT Hub device client and connect diff --git a/examples/azureiot_native_networking/azureiot_hub_directmethods.py b/examples/azureiot_native_networking/azureiot_hub_directmethods.py index 518f666..c0857f0 100644 --- a/examples/azureiot_native_networking/azureiot_hub_directmethods.py +++ b/examples/azureiot_native_networking/azureiot_hub_directmethods.py @@ -61,7 +61,6 @@ # # From the Adafruit CircuitPython Bundle https://github.com/adafruit/Adafruit_CircuitPython_Bundle: # * adafruit-circuitpython-minimqtt -# * adafruit-circuitpython-requests # Create an IoT Hub device client and connect diff --git a/examples/azureiot_native_networking/azureiot_hub_messages.py b/examples/azureiot_native_networking/azureiot_hub_messages.py index 1ea36bf..cc9a57e 100644 --- a/examples/azureiot_native_networking/azureiot_hub_messages.py +++ b/examples/azureiot_native_networking/azureiot_hub_messages.py @@ -62,7 +62,6 @@ # # From the Adafruit CircuitPython Bundle https://github.com/adafruit/Adafruit_CircuitPython_Bundle: # * adafruit-circuitpython-minimqtt -# * adafruit-circuitpython-requests # Create an IoT Hub device client and connect diff --git a/examples/azureiot_native_networking/azureiot_hub_simpletest.py b/examples/azureiot_native_networking/azureiot_hub_simpletest.py index 4cb6069..8da982d 100644 --- a/examples/azureiot_native_networking/azureiot_hub_simpletest.py +++ b/examples/azureiot_native_networking/azureiot_hub_simpletest.py @@ -62,7 +62,6 @@ # # From the Adafruit CircuitPython Bundle https://github.com/adafruit/Adafruit_CircuitPython_Bundle: # * adafruit-circuitpython-minimqtt -# * adafruit-circuitpython-requests # Create an IoT Hub device client and connect diff --git a/examples/azureiot_native_networking/azureiot_hub_twin_operations.py b/examples/azureiot_native_networking/azureiot_hub_twin_operations.py index b878d74..90b3303 100644 --- a/examples/azureiot_native_networking/azureiot_hub_twin_operations.py +++ b/examples/azureiot_native_networking/azureiot_hub_twin_operations.py @@ -61,7 +61,6 @@ # # From the Adafruit CircuitPython Bundle https://github.com/adafruit/Adafruit_CircuitPython_Bundle: # * adafruit-circuitpython-minimqtt -# * adafruit-circuitpython-requests # Create an IoT Hub device client and connect diff --git a/requirements.txt b/requirements.txt index 228aa2a..abf61e9 100644 --- a/requirements.txt +++ b/requirements.txt @@ -5,5 +5,4 @@ Adafruit-Blinka adafruit-circuitpython-logging>=4.0.1 adafruit-circuitpython-minimqtt -adafruit-circuitpython-requests adafruit-circuitpython-binascii From 2f6562f9a3a0b84eb3f387ccc5dd9ed72f2c59b0 Mon Sep 17 00:00:00 2001 From: foamyguy Date: Mon, 7 Oct 2024 09:24:05 -0500 Subject: [PATCH 3/7] remove deprecated get_html_theme_path() call Signed-off-by: foamyguy --- docs/conf.py | 1 - 1 file changed, 1 deletion(-) diff --git a/docs/conf.py b/docs/conf.py index 33bdb2d..4bd1f17 100644 --- a/docs/conf.py +++ b/docs/conf.py @@ -109,7 +109,6 @@ import sphinx_rtd_theme html_theme = "sphinx_rtd_theme" -html_theme_path = [sphinx_rtd_theme.get_html_theme_path(), "."] # Add any paths that contain custom static files (such as style sheets) here, # relative to this directory. They are copied after the builtin static files, From 24b9f950a02556e0cc492270a30c680d76875ecb Mon Sep 17 00:00:00 2001 From: foamyguy Date: Tue, 14 Jan 2025 11:32:34 -0600 Subject: [PATCH 4/7] add sphinx configuration to rtd.yaml Signed-off-by: foamyguy --- .readthedocs.yaml | 3 +++ 1 file changed, 3 insertions(+) diff --git a/.readthedocs.yaml b/.readthedocs.yaml index 33c2a61..88bca9f 100644 --- a/.readthedocs.yaml +++ b/.readthedocs.yaml @@ -8,6 +8,9 @@ # Required version: 2 +sphinx: + configuration: docs/conf.py + build: os: ubuntu-20.04 tools: From 028300fcb46a7dda55a3184e69019b940de81c0d Mon Sep 17 00:00:00 2001 From: Justin Myers Date: Mon, 24 Feb 2025 13:54:22 -0800 Subject: [PATCH 5/7] 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 6/7] 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 7/7] 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.