Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix ESP32SPI #65

Merged
merged 2 commits into from
May 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 2 additions & 6 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -49,16 +49,14 @@ This driver depends on:

* `Adafruit CircuitPython <https://github.com/adafruit/circuitpython>`_
* `Adafruit CircuitPython BinASCII <https://github.com/adafruit/Adafruit_CircuitPython_Binascii>`_
* `Adafruit CircuitPython ConnectionManager <https://github.com/adafruit/Adafruit_CircuitPython_ConnectionManager/>`_
* `Adafruit CircuitPython Logging <https://github.com/adafruit/Adafruit_CircuitPython_Logging>`_
* `Adafruit CircuitPython MiniMQTT <https://github.com/adafruit/Adafruit_CircuitPython_MiniMQTT>`_
* `Adafruit CircuitPython Requests <https://github.com/adafruit/Adafruit_CircuitPython_Requests>`_

Please ensure all dependencies are available on the CircuitPython filesystem.
This is easily achieved by downloading
`the Adafruit library and driver bundle <https://github.com/adafruit/Adafruit_CircuitPython_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
=============
Expand All @@ -74,8 +72,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
Expand All @@ -96,7 +92,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
Expand Down
3 changes: 0 additions & 3 deletions adafruit_azureiot/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
13 changes: 6 additions & 7 deletions adafruit_azureiot/device_registration.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
"""

import json
import ssl
import time

import adafruit_logging as logging
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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())
Expand Down
13 changes: 6 additions & 7 deletions adafruit_azureiot/iot_mqtt.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@

import gc
import json
import ssl
import time

import adafruit_minimqtt.adafruit_minimqtt as MQTT
Expand Down Expand Up @@ -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())
Expand Down Expand Up @@ -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,
Expand All @@ -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
Expand Down
11 changes: 8 additions & 3 deletions examples/azureiot_esp32spi/azureiot_central_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -96,16 +96,21 @@
#
# 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

# 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"],
)


Expand Down
11 changes: 8 additions & 3 deletions examples/azureiot_esp32spi/azureiot_central_notconnected.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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,
Expand All @@ -99,9 +98,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
Expand Down
11 changes: 8 additions & 3 deletions examples/azureiot_esp32spi/azureiot_central_properties.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -97,12 +97,17 @@
#
# 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)
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"],
)


Expand Down
11 changes: 8 additions & 3 deletions examples/azureiot_esp32spi/azureiot_central_simpletest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -98,12 +98,17 @@
#
# 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)
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...")
Expand Down
7 changes: 4 additions & 3 deletions examples/azureiot_esp32spi/azureiot_hub_directmethods.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -90,15 +90,16 @@
#
# 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

# 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
Expand Down
7 changes: 4 additions & 3 deletions examples/azureiot_esp32spi/azureiot_hub_messages.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -92,11 +92,12 @@
#
# 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)
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
Expand Down
7 changes: 4 additions & 3 deletions examples/azureiot_esp32spi/azureiot_hub_simpletest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -92,11 +92,12 @@
#
# 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)
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...")

Expand Down
7 changes: 4 additions & 3 deletions examples/azureiot_esp32spi/azureiot_hub_twin_operations.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -94,11 +94,12 @@
#
# 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)
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
Expand Down
Loading
Loading