Skip to content

Commit 77489e4

Browse files
author
brentru
committed
add example of fetching, modifying, and returning a device twin property
1 parent 2bd864b commit 77489e4

File tree

1 file changed

+50
-0
lines changed

1 file changed

+50
-0
lines changed

examples/azureiot_devicetwin.py

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import board
2+
import busio
3+
from digitalio import DigitalInOut
4+
from adafruit_esp32spi import adafruit_esp32spi, adafruit_esp32spi_wifimanager
5+
import neopixel
6+
from adafruit_azureiot import IOT_Hub
7+
8+
# Get wifi details and more from a secrets.py file
9+
try:
10+
from secrets import secrets
11+
except ImportError:
12+
print("WiFi secrets are kept in secrets.py, please add them there!")
13+
raise
14+
15+
# ESP32 Setup
16+
try:
17+
esp32_cs = DigitalInOut(board.ESP_CS)
18+
esp32_ready = DigitalInOut(board.ESP_BUSY)
19+
esp32_reset = DigitalInOut(board.ESP_RESET)
20+
except AttributeError:
21+
esp32_cs = DigitalInOut(board.D9)
22+
esp32_ready = DigitalInOut(board.D10)
23+
esp32_reset = DigitalInOut(board.D5)
24+
spi = busio.SPI(board.SCK, board.MOSI, board.MISO)
25+
esp = adafruit_esp32spi.ESP_SPIcontrol(spi, esp32_cs, esp32_ready, esp32_reset)
26+
status_light = neopixel.NeoPixel(board.NEOPIXEL, 1, brightness=0.2) # Uncomment for Most Boards
27+
"""Uncomment below for ItsyBitsy M4"""
28+
#status_light = dotstar.DotStar(board.APA102_SCK, board.APA102_MOSI, 1, brightness=0.2)
29+
wifi = adafruit_esp32spi_wifimanager.ESPSPI_WiFiManager(esp, secrets, status_light)
30+
31+
# Create an instance of the Azure IoT Hub
32+
hub = IOT_Hub(wifi, secrets['azure_iot_hub'], secrets['azure_iot_sas'])
33+
34+
# Set a device id (This name must match a device connected to your Azure IoT Hub)
35+
device_id = 'Blinka'
36+
37+
# Get a Device Twin
38+
device_twin = hub.get_device_twin(device_id)
39+
# Filter out the device's name from the twin's properties
40+
device_name = device_twin['properties']['desired']['deviceName']
41+
print(device_name)
42+
43+
# Update a Device Twin's Properties
44+
data = {"properties":{"desired": {"deviceName": "{{BasementTemperatureLoggerFeather}}"}}}
45+
hub.update_device_twin(device_id, data)
46+
47+
# And read the updated device twin information
48+
device_twin = hub.get_device_twin(device_id)
49+
device_name = device_twin['properties']['desired']['deviceName']
50+
print(device_name)

0 commit comments

Comments
 (0)