Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
getDeviceAndModule.py
# code based on
# https://docs.microsoft.com/en-us/azure/iot-hub/iot-hub-python-twin-getstarted
import sys
from time import sleep
from azure.iot.hub import IoTHubRegistryManager
from azure.iot.hub.models import Twin, TwinProperties, QuerySpecification, QueryResult
# connect using IoTHub connection string and device id with the module identity name
IOTHUB_CONNECTION_STRING = "IOT HUB Connection String"
DEVICE_ID = 'Device Id'
MODULE_ID = 'DoorController'
# create IoTHub Registry Manager
iothub_registry_manager = IoTHubRegistryManager(IOTHUB_CONNECTION_STRING)
# get twin reference https://docs.microsoft.com/en-us/rest/api/iothub/service/devices/get-twin
twin = iothub_registry_manager.get_twin(DEVICE_ID)
print(type(twin))
# get modules
# https://docs.microsoft.com/en-us/rest/api/iothub/service/modules/get-modules-on-device#module
modules = iothub_registry_manager.get_modules(DEVICE_ID)
print(type(modules))
# iterate through modules
for module in modules:
print(type(module))
print(module.module_id)
print(module.connection_state)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment