An Azure device twin is a JSON document that stores all of the information about a device. Each device in your IoT Hub has a device twin that you can access. In code.py, device twins are used to check the connection status of each device. The connection status is represented by circles in the top right-hand corners of each device's telemetry box on the Raspberry Pi's display.
IoTHubRegistryManager()
is used to authenticate access to the IoT Hub's device twins.
# connect to device twin status_client = IoTHubRegistryManager(status_connection)
A device twin is accessed for each of the three devices using a for
statement. If the device is connected, then it's circle is updated to be green. If the device is disconnected, then its circle is updated to be red.
# iterate through the status circles for d in range(0, 3): # get connection status of all 3 devices using device twin twin = status_client.get_twin(devices[d]) # if a device is connected, make the status circle green if twin.connection_state == 'Connected': status_circles[d].fill = 0x00FF00 # if a device is disconnected, make the status circle red if twin.connection_state == 'Disconnected': status_circles[d].fill = 0xFF0000
Text editor powered by tinymce.