Skip to content

Commit b58345d

Browse files
committed
Update samples and docs
1 parent 957f945 commit b58345d

15 files changed

+775
-26
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ ipython_config.py
107107
# pyenv
108108
# For a library or package, you might want to ignore these files since the code is
109109
# intended to run in multiple environments; otherwise, check them in:
110-
# .python-version
110+
.python-version
111111

112112
# pipenv
113113
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.

README.rst

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,9 @@ 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+
===================
68+
6669
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:
6770

6871
.. code-block:: python
@@ -74,6 +77,23 @@ To use this library, you will need to create an ESP32_SPI WifiManager, connected
7477
time.sleep(5)
7578
ntp.set_time()
7679
80+
Native Networking
81+
=================
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:
83+
84+
.. code-block:: python
85+
86+
pool = socketpool.SocketPool(wifi.radio)
87+
requests = adafruit_requests.Session(pool, ssl.create_default_context())
88+
response = requests.get("https://io.adafruit.com/api/v2/time/seconds")
89+
if response:
90+
if response.status_code == 200:
91+
r = rtc.RTC()
92+
r.datetime = time.localtime(int(response.text))
93+
print(f"System Time: {r.datetime}")
94+
else:
95+
print("Setting time failed")
96+
7797
Azure IoT Hub
7898
-------------
7999

@@ -172,7 +192,7 @@ To use Azure IoT Central, you will need to create an Azure IoT Central app, crea
172192
- Head to `Azure IoT Central <https://apps.azureiotcentral.com/?WT.mc_id=academic-3168-jabenn>`__
173193
- Follow the instructions in the `Microsoft Docs <https://docs.microsoft.com/azure/iot-central/core/quick-deploy-iot-central?WT.mc_id=academic-3168-jabenn>`__ to create an application. Every tier is free for up to 2 devices.
174194
- Follow the instructions in the `Microsoft Docs <https://docs.microsoft.com/azure/iot-central/core/quick-create-simulated-device?WT.mc_id=academic-3168-jabenn>`__ to create a device template.
175-
- 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 Key in your ``secrets.py`` file.
195+
- 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.
176196

177197
.. image:: iot-central-connect-button.png
178198
:alt: The IoT Central connect button
@@ -194,7 +214,7 @@ To use Azure IoT Central, you will need to create an Azure IoT Central app, crea
194214
# Azure IoT Central settings
195215
"id_scope": "",
196216
"device_id": "",
197-
"key": ""
217+
"device_sas_key": ""
198218
}
199219
200220
**Connect your device to your Azure IoT Central app**

docs/examples.rst

Lines changed: 72 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,107 @@
1-
IoT Hub
1+
IoT Hub ESP32SPI Networking
22
------------
33

44
Ensure your IoT Hub device works with this simple test.
55

6-
.. literalinclude:: ../examples/azureiot_hub_simpletest.py
7-
:caption: examples/azureiot_hub_simpletest.py
6+
.. literalinclude:: ../examples/esp32spi/azureiot_hub_simpletest.py
7+
:caption: examples/esp32spi/azureiot_hub_simpletest.py
88
:linenos:
99

1010
Handle direct methods.
1111

12-
.. literalinclude:: ../examples/azureiot_hub_directmethods.py
13-
:caption: examples/azureiot_hub_directmethods.py
12+
.. literalinclude:: ../examples/esp32spi/azureiot_hub_directmethods.py
13+
:caption: examples/esp32spi/azureiot_hub_directmethods.py
1414
:linenos:
1515

1616
Send device to cloud messages, and handle cloud to device messages.
1717

18-
.. literalinclude:: ../examples/azureiot_hub_messages.py
19-
:caption: examples/azureiot_hub_messages.py
18+
.. literalinclude:: ../examples/esp32spi/azureiot_hub_messages.py
19+
:caption: examples/esp32spi/azureiot_hub_messages.py
2020
:linenos:
2121

2222
Update the reported properties of the devices device twin, and receive updates to desired properties.
2323

24-
.. literalinclude:: ../examples/azureiot_hub_twin_operations.py
25-
:caption: examples/azureiot_hub_twin_operations.py
24+
.. literalinclude:: ../examples/esp32spi/azureiot_hub_twin_operations.py
25+
:caption: examples/esp32spi/azureiot_hub_twin_operations.py
2626
:linenos:
2727

28-
IoT Central
28+
IoT Central ESP32SPI Networking
2929
------------
3030

3131
Ensure your IoT Central device works with this simple test.
3232

33-
.. literalinclude:: ../examples/azureiot_central_simpletest.py
34-
:caption: examples/azureiot_central_simpletest.py
33+
.. literalinclude:: ../examples/esp32spi/azureiot_central_simpletest.py
34+
:caption: examples/esp32spi/azureiot_central_simpletest.py
3535
:linenos:
3636

3737
Handle commands.
3838

39-
.. literalinclude:: ../examples/azureiot_central_commands.py
40-
:caption: examples/azureiot_central_commands.py
39+
.. literalinclude:: ../examples/esp32spi/azureiot_central_commands.py
40+
:caption: examples/esp32spi/azureiot_central_commands.py
4141
:linenos:
4242

4343
Update the properties of the device, and receive updates to properties.
4444

45-
.. literalinclude:: ../examples/azureiot_central_properties.py
46-
:caption: examples/azureiot_central_properties.py
45+
.. literalinclude:: ../examples/esp32spi/azureiot_central_properties.py
46+
:caption: examples/esp32spi/azureiot_central_properties.py
4747
:linenos:
4848

4949
Handle connection errors.
5050

51-
.. literalinclude:: ../examples/azureiot_central_notconnected.py
52-
:caption: examples/azureiot_central_notconnected.py
51+
.. literalinclude:: ../examples/esp32spi/azureiot_central_notconnected.py
52+
:caption: examples/esp32spi/azureiot_central_notconnected.py
53+
:linenos:
54+
55+
IoT Hub Native Networking
56+
------------
57+
58+
Ensure your IoT Hub device works with this simple test.
59+
60+
.. literalinclude:: ../examples/native_networking/azureiot_hub_simpletest.py
61+
:caption: examples/native_networking/azureiot_hub_simpletest.py
62+
:linenos:
63+
64+
Handle direct methods.
65+
66+
.. literalinclude:: ../examples/native_networking/azureiot_hub_directmethods.py
67+
:caption: examples/native_networking/azureiot_hub_directmethods.py
68+
:linenos:
69+
70+
Send device to cloud messages, and handle cloud to device messages.
71+
72+
.. literalinclude:: ../examples/native_networking/azureiot_hub_messages.py
73+
:caption: examples/native_networking/azureiot_hub_messages.py
74+
:linenos:
75+
76+
Update the reported properties of the devices device twin, and receive updates to desired properties.
77+
78+
.. literalinclude:: ../examples/native_networking/azureiot_hub_twin_operations.py
79+
:caption: examples/native_networking/azureiot_hub_twin_operations.py
80+
:linenos:
81+
82+
IoT Central Native Networking
83+
------------
84+
85+
Ensure your IoT Central device works with this simple test.
86+
87+
.. literalinclude:: ../examples/native_networking/azureiot_central_simpletest.py
88+
:caption: examples/native_networking/azureiot_central_simpletest.py
89+
:linenos:
90+
91+
Handle commands.
92+
93+
.. literalinclude:: ../examples/native_networking/azureiot_central_commands.py
94+
:caption: examples/native_networking/azureiot_central_commands.py
95+
:linenos:
96+
97+
Update the properties of the device, and receive updates to properties.
98+
99+
.. literalinclude:: ../examples/native_networking/azureiot_central_properties.py
100+
:caption: examples/native_networking/azureiot_central_properties.py
101+
:linenos:
102+
103+
Handle connection errors.
104+
105+
.. literalinclude:: ../examples/native_networking/azureiot_central_notconnected.py
106+
:caption: examples/native_networking/azureiot_central_notconnected.py
53107
:linenos:

examples/ esp32spi /azureiot_central_commands.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@
8484
#
8585
# 'id_scope' - the devices ID scope
8686
# 'device_id' - the devices device id
87-
# 'key' - the devices primary key
87+
# 'device_sas_key' - the devices primary key
8888
#
8989
# The adafruit-circuitpython-azureiot library depends on the following libraries:
9090
#

examples/ esp32spi /azureiot_central_notconnected.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@
8686
#
8787
# 'id_scope' - the devices ID scope
8888
# 'device_id' - the devices device id
89-
# 'key' - the devices primary key
89+
# 'device_sas_key' - the devices primary key
9090
#
9191
# The adafruit-circuitpython-azureiot library depends on the following libraries:
9292
#

examples/ esp32spi /azureiot_central_properties.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@
8585
#
8686
# 'id_scope' - the devices ID scope
8787
# 'device_id' - the devices device id
88-
# 'key' - the devices primary key
88+
# 'device_sas_key' - the devices primary key
8989
#
9090
# The adafruit-circuitpython-azureiot library depends on the following libraries:
9191
#

examples/ esp32spi /azureiot_central_simpletest.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@
8686
#
8787
# 'id_scope' - the devices ID scope
8888
# 'device_id' - the devices device id
89-
# 'key' - the devices primary key
89+
# 'device_sas_key' - the devices primary key
9090
#
9191
# The adafruit-circuitpython-azureiot library depends on the following libraries:
9292
#
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
2+
# SPDX-License-Identifier: MIT
3+
4+
import ssl
5+
import time
6+
7+
import rtc
8+
import socketpool
9+
import wifi
10+
11+
import adafruit_requests
12+
from adafruit_azureiot import IoTCentralDevice
13+
from adafruit_azureiot.iot_mqtt import IoTResponse
14+
15+
# Get wifi details and more from a secrets.py file
16+
try:
17+
from secrets import secrets
18+
except ImportError:
19+
print("WiFi secrets are kept in secrets.py, please add them there!")
20+
raise
21+
22+
print("Connecting to WiFi...")
23+
wifi.radio.connect(secrets["ssid"], secrets["password"])
24+
25+
print("Connected to WiFi!")
26+
27+
if time.localtime().tm_year < 2022:
28+
print("Setting System Time in UTC")
29+
pool = socketpool.SocketPool(wifi.radio)
30+
requests = adafruit_requests.Session(pool, ssl.create_default_context())
31+
response = requests.get("https://io.adafruit.com/api/v2/time/seconds")
32+
if response:
33+
if response.status_code == 200:
34+
r = rtc.RTC()
35+
r.datetime = time.localtime(int(response.text))
36+
print(f"System Time: {r.datetime}")
37+
else:
38+
print("Setting time failed")
39+
else:
40+
print("Year seems good, skipping set time.")
41+
42+
# To use Azure IoT Central, you will need to create an IoT Central app.
43+
# You can either create a free tier app that will live for 7 days without an Azure subscription,
44+
# Or a standard tier app that will last for ever with an Azure subscription.
45+
# The standard tiers are free for up to 2 devices
46+
#
47+
# If you don't have an Azure subscription:
48+
#
49+
# If you are a student, head to https://aka.ms/FreeStudentAzure and sign up, validating with your
50+
# student email address. This will give you $100 of Azure credit and free tiers of a load of
51+
# service, renewable each year you are a student
52+
#
53+
# If you are not a student, head to https://aka.ms/FreeAz and sign up to get $200 of credit for 30
54+
# days, as well as free tiers of a load of services
55+
#
56+
# Create an Azure IoT Central app by following these instructions: https://aka.ms/CreateIoTCentralApp
57+
# Add a device template with telemetry, properties and commands, as well as a view to visualize the
58+
# telemetry and execute commands, and a form to set properties.
59+
#
60+
# Next create a device using the device template, and select Connect to get the device connection details.
61+
# Add the connection details to your secrets.py file, using the following values:
62+
#
63+
# 'id_scope' - the devices ID scope
64+
# 'device_id' - the devices device id
65+
# 'device_sas_key' - the devices primary key
66+
#
67+
# The adafruit-circuitpython-azureiot library depends on the following libraries:
68+
#
69+
# From the Adafruit CircuitPython Bundle (https://github.com/adafruit/Adafruit_CircuitPython_Bundle):
70+
# * adafruit-circuitpython-minimqtt
71+
# * adafruit-circuitpython-requests
72+
73+
74+
# Create an IoT Hub device client and connect
75+
esp = None
76+
pool = socketpool.SocketPool(wifi.radio)
77+
device = IoTCentralDevice(
78+
pool, esp, secrets["id_scope"], secrets["device_id"], secrets["device_sas_key"]
79+
)
80+
81+
# Subscribe to commands
82+
# Commands can be sent from the devices Dashboard in IoT Central, assuming
83+
# the device template and view has been set up with the commands
84+
# Command handlers need to return a response to show if the command was handled
85+
# successfully or not, returning an HTTP status code and message
86+
def command_executed(command_name: str, payload) -> IoTResponse:
87+
print("Command", command_name, "executed with payload", str(payload))
88+
# return a status code and message to indicate if the command was handled correctly
89+
return IoTResponse(200, "OK")
90+
91+
92+
# Subscribe to the command execute event
93+
device.on_command_executed = command_executed
94+
95+
96+
print("Connecting to Azure IoT Central...")
97+
device.connect()
98+
99+
print("Connected to Azure IoT Central!")
100+
101+
message_counter = 60
102+
103+
while True:
104+
try:
105+
# Poll every second for messages from the cloud
106+
device.loop()
107+
except (ValueError, RuntimeError) as e:
108+
print("Connection error, reconnecting\n", str(e))
109+
# If we lose connectivity, reset the wifi and reconnect
110+
wifi.reset()
111+
wifi.connect()
112+
device.reconnect()
113+
continue
114+
115+
time.sleep(1)

0 commit comments

Comments
 (0)