Skip to content

Commit 4b72e22

Browse files
authored
Merge pull request #37 from askpatrickw/add-ssl-context
Fix miniMQTT compatibility and native_networking examples
2 parents fe80aad + 79c50ef commit 4b72e22

20 files changed

+64
-22
lines changed

README.rst

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,8 @@ To create an Azure IoT Hub instance or an Azure IoT Central app, you will need a
6363

6464
- If you are not a student, head to `aka.ms/FreeAz <https://aka.ms/FreeAz>`_ and sign up to get $200 of credit for 30 days, as well as free tiers of a load of services. You will need a credit card for validation only, your card will not be charged.
6565

66-
ESP32SPI Networking
67-
===================
66+
ESP32 AirLift Networking
67+
========================
6868

6969
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 via the `Adafruit CircuitPython NTP <https://github.com/adafruit/Adafruit_CircuitPython_NTP>`_ library with the following code:
7070

@@ -79,7 +79,7 @@ To use this library, you will need to create an ESP32_SPI WifiManager, connected
7979
8080
Native Networking
8181
=================
82-
To use this library, with boards that have native networking support, you need to be connected to a network. 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 by using the `Adafruit IoT Time Service <https://io.adafruit.com/api/docs/#time>`_ via the `Requests library <https://github.com/adafruit/Adafruit_CircuitPython_Requests/>_` with the following code:
82+
To use this library, with boards that have native networking support, you need to be connected to a network. 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 by using the `Adafruit IoT Time API <https://io.adafruit.com/api/docs/#time>`_ via the `Adafruit Requests library <https://github.com/adafruit/Adafruit_CircuitPython_Requests>`_ with the following code:
8383

8484
.. code-block:: python
8585

adafruit_azureiot/__init__.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,16 @@
1919
* Adafruit CircuitPython firmware for the supported boards:
2020
https://github.com/adafruit/circuitpython/releases
2121
22-
**ESP32SPI Peripheral Networking**
22+
**With ESP32 Airlift Networking**
2323
2424
* Adafruit's Bus Device library: https://github.com/adafruit/Adafruit_CircuitPython_BusDevice
25-
* Adafruit's ESP32SPI Networking library: https://github.com/adafruit/Adafruit_CircuitPython_ESP32SPI
25+
* Adafruit's ESP32SPI library: https://github.com/adafruit/Adafruit_CircuitPython_ESP32SPI
26+
* Adafruit's NTP library: https://github.com/adafruit/Adafruit_CircuitPython_NTP
2627
27-
**Native Networking**
28+
**With Native Networking**
2829
2930
* CircuitPython's Wifi Module: https://docs.circuitpython.org/en/latest/shared-bindings/wifi/index.html
30-
* Adafruit's CircuitPython Requests Library: https://github.com/adafruit/Adafruit_CircuitPython_Requests/
31+
* Adafruit's Requests Library: https://github.com/adafruit/Adafruit_CircuitPython_Requests/
3132
"""
3233

3334
from .iot_error import IoTError

adafruit_azureiot/device_registration.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ def __init__(
5353
logger: Logger = None,
5454
):
5555
"""Creates an instance of the device registration service
56+
5657
:param socket: The network socket
5758
:param str id_scope: The ID scope of the device to register
5859
:param str device_id: The device ID of the device to register
@@ -163,6 +164,7 @@ def register_device(self, expiry: int) -> str:
163164
"""
164165
Registers the device with the IoT Central device registration service.
165166
Returns the hostname of the IoT hub to use over MQTT
167+
166168
:param int expiry: The expiry time for the registration
167169
:returns: The underlying IoT Hub that this device should connect to
168170
:rtype: str

adafruit_azureiot/iot_error.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ class IoTError(Exception):
2020

2121
def __init__(self, message: str):
2222
"""Create the IoT Error
23+
2324
:param str message: The error message
2425
"""
2526
super().__init__(message)

adafruit_azureiot/iot_mqtt.py

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
import adafruit_minimqtt.adafruit_minimqtt as MQTT
2121
import adafruit_logging as logging
22+
from adafruit_logging import Logger
2223

2324
from .iot_error import IoTError
2425
from .keys import compute_derived_symmetric_key
@@ -31,6 +32,7 @@ class IoTResponse:
3132

3233
def __init__(self, code: int, message: str):
3334
"""Creates an IoT Response object
35+
3436
:param int code: The HTTP response code for this method call, for example 200 if the method was handled successfully
3537
:param str message: The HTTP response message for this method call
3638
"""
@@ -43,17 +45,20 @@ class IoTMQTTCallback:
4345

4446
def message_sent(self, data: str) -> None:
4547
"""Called when a message is sent to the cloud
48+
4649
:param str data: The data send with the message
4750
"""
4851

4952
def connection_status_change(self, connected: bool) -> None:
5053
"""Called when the connection status changes
54+
5155
:param bool connected: True if the device is connected, otherwise false
5256
"""
5357

5458
# pylint: disable=W0613, R0201
5559
def direct_method_invoked(self, method_name: str, payload: str) -> IoTResponse:
5660
"""Called when a direct method is invoked
61+
5762
:param str method_name: The name of the method that was invoked
5863
:param str payload: The payload with the message
5964
:returns: A response with a code and status to show if the method was correctly handled
@@ -64,6 +69,7 @@ def direct_method_invoked(self, method_name: str, payload: str) -> IoTResponse:
6469
# pylint: disable=C0103
6570
def cloud_to_device_message_received(self, body: str, properties: dict) -> None:
6671
"""Called when a cloud to device message is received
72+
6773
:param str body: The body of the message
6874
:param dict properties: The propreties sent with the mesage
6975
"""
@@ -72,6 +78,7 @@ def device_twin_desired_updated(
7278
self, desired_property_name: str, desired_property_value, desired_version: int
7379
) -> None:
7480
"""Called when the device twin desired properties are updated
81+
7582
:param str desired_property_name: The name of the desired property that was updated
7683
:param desired_property_value: The value of the desired property that was updated
7784
:param int desired_version: The version of the desired property that was updated
@@ -84,6 +91,7 @@ def device_twin_reported_updated(
8491
reported_version: int,
8592
) -> None:
8693
"""Called when the device twin reported values are updated
94+
8795
:param str reported_property_name: The name of the reported property that was updated
8896
:param reported_property_value: The value of the reported property that was updated
8997
:param int reported_version: The version of the reported property that was updated
@@ -319,17 +327,18 @@ def __init__(
319327
device_id: str,
320328
device_sas_key: str,
321329
token_expires: int = 21600,
322-
logger: logging = None,
330+
logger: Logger = None,
323331
):
324332
"""Create the Azure IoT MQTT client
333+
325334
:param IoTMQTTCallback callback: A callback class
326335
:param socket: The socket to communicate over
327336
:param iface: The network interface to communicate over
328337
:param str hostname: The hostname of the MQTT broker to connect to, get this by registering the device
329338
:param str device_id: The device ID of the device to register
330339
:param str device_sas_key: The primary or secondary key of the device to register
331340
:param int token_expires: The number of seconds till the token expires, defaults to 6 hours
332-
:param adafruit_logging logger: The logger
341+
:param Logger logger: The logger
333342
"""
334343
self._callback = callback
335344
self._socket = socket
@@ -372,6 +381,7 @@ def _subscribe_to_twin_topics(self):
372381

373382
def connect(self) -> bool:
374383
"""Connects to the MQTT broker
384+
375385
:returns: True if the connection is successful, otherwise False
376386
:rtype: bool
377387
"""
@@ -426,6 +436,7 @@ def reconnect(self) -> None:
426436

427437
def is_connected(self) -> bool:
428438
"""Gets if there is an open connection to the MQTT broker
439+
429440
:returns: True if there is an open connection, False if not
430441
:rtype: bool
431442
"""
@@ -443,6 +454,7 @@ def send_device_to_cloud_message(
443454
self, message, system_properties: dict = None
444455
) -> None:
445456
"""Send a device to cloud message from this device to Azure IoT Hub
457+
446458
:param message: The message data as a JSON string or a dictionary
447459
:param system_properties: System properties to send with the message
448460
:raises: ValueError if the message is not a string or dictionary
@@ -472,6 +484,7 @@ def send_device_to_cloud_message(
472484

473485
def send_twin_patch(self, patch) -> None:
474486
"""Send a patch for the reported properties of the device twin
487+
475488
:param patch: The patch as a JSON string or a dictionary
476489
:raises: IoTError if the data is not a string or dictionary
477490
:raises RuntimeError: if the internet connection is not responding or is unable to connect

adafruit_azureiot/iotcentral_device.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import json
1616
import time
1717
import adafruit_logging as logging
18+
from adafruit_logging import Logger
1819
from .device_registration import DeviceRegistration
1920
from .iot_error import IoTError
2021
from .iot_mqtt import IoTMQTT, IoTMQTTCallback, IoTResponse
@@ -25,6 +26,7 @@ class IoTCentralDevice(IoTMQTTCallback):
2526

2627
def connection_status_change(self, connected: bool) -> None:
2728
"""Called when the connection status changes
29+
2830
:param bool connected: True if the device is connected, otherwise false
2931
"""
3032
if self.on_connection_status_changed is not None:
@@ -34,6 +36,7 @@ def connection_status_change(self, connected: bool) -> None:
3436
# pylint: disable=W0613, R0201
3537
def direct_method_called(self, method_name: str, payload: str) -> IoTResponse:
3638
"""Called when a direct method is invoked
39+
3740
:param str method_name: The name of the method that was invoked
3841
:param str payload: The payload with the message
3942
:returns: A response with a code and status to show if the method was correctly handled
@@ -49,6 +52,7 @@ def device_twin_desired_updated(
4952
self, desired_property_name: str, desired_property_value, desired_version: int
5053
) -> None:
5154
"""Called when the device twin desired properties are updated
55+
5256
:param str desired_property_name: The name of the desired property that was updated
5357
:param desired_property_value: The value of the desired property that was updated
5458
:param int desired_version: The version of the desired property that was updated
@@ -69,6 +73,7 @@ def device_twin_reported_updated(
6973
reported_version: int,
7074
) -> None:
7175
"""Called when the device twin reported values are updated
76+
7277
:param str reported_property_name: The name of the reported property that was updated
7378
:param reported_property_value: The value of the reported property that was updated
7479
:param int reported_version: The version of the reported property that was updated
@@ -88,16 +93,17 @@ def __init__(
8893
device_id: str,
8994
device_sas_key: str,
9095
token_expires: int = 21600,
91-
logger: logging = None,
96+
logger: Logger = None,
9297
):
9398
"""Create the Azure IoT Central device client
99+
94100
:param socket: The network socket
95101
:param iface: The network interface
96102
:param str id_scope: The ID Scope of the device in IoT Central
97103
:param str device_id: The device ID of the device in IoT Central
98104
:param str device_sas_key: The primary or secondary key of the device in IoT Central
99105
:param int token_expires: The number of seconds till the token expires, defaults to 6 hours
100-
:param adafruit_logging logger: The logger
106+
:param Logger logger: The logger
101107
"""
102108
self._socket = socket
103109
self._iface = iface
@@ -132,6 +138,7 @@ def property_changed(_property_name: str, property_value, version: int) -> None
132138

133139
def connect(self) -> None:
134140
"""Connects to Azure IoT Central
141+
135142
:raises DeviceRegistrationError: if the device cannot be registered successfully
136143
:raises RuntimeError: if the internet connection is not responding or is unable to connect
137144
"""
@@ -166,6 +173,7 @@ def connect(self) -> None:
166173

167174
def disconnect(self) -> None:
168175
"""Disconnects from the MQTT broker
176+
169177
:raises IoTError: if there is no open connection to the MQTT broker
170178
"""
171179
if self._mqtt is None:
@@ -182,13 +190,15 @@ def reconnect(self) -> None:
182190

183191
def is_connected(self) -> bool:
184192
"""Gets if there is an open connection to the MQTT broker
193+
185194
:returns: True if there is an open connection, False if not
186195
:rtype: bool
187196
"""
188197
return self._mqtt.is_connected() if self._mqtt is not None else False
189198

190199
def loop(self) -> None:
191200
"""Listens for MQTT messages
201+
192202
:raises IoTError: if there is no open connection to the MQTT broker
193203
"""
194204
if self._mqtt is None:
@@ -198,6 +208,7 @@ def loop(self) -> None:
198208

199209
def send_property(self, property_name: str, value) -> None:
200210
"""Updates the value of a writable property
211+
201212
:param str property_name: The name of the property to write to
202213
:param value: The value to set on the property
203214
:raises IoTError: if there is no open connection to the MQTT broker
@@ -211,6 +222,7 @@ def send_property(self, property_name: str, value) -> None:
211222

212223
def send_telemetry(self, data) -> None:
213224
"""Sends telemetry to the IoT Central app
225+
214226
:param data: The telemetry data to send
215227
:raises IoTError: if there is no open connection to the MQTT broker
216228
"""

0 commit comments

Comments
 (0)