Skip to content

Instantly share code, notes, and snippets.

@elbruno
Created September 13, 2021 15:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save elbruno/963d3edf4094bea22b9bbafe4e42a50f to your computer and use it in GitHub Desktop.
Save elbruno/963d3edf4094bea22b9bbafe4e42a50f to your computer and use it in GitHub Desktop.
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