Get started with device twins (Python)

Device twins are JSON documents that store device state information (metadata, configurations, and conditions). IoT Hub persists a device twin for each device that connects to it.

Note

The features described in this article are available only in the standard tier of IoT Hub. For more information about the basic and standard IoT Hub tiers, see Choose the right IoT Hub tier.

Use device twins to:

  • Store device metadata from your solution back end.

  • Report current state information such as available capabilities and conditions (for example, the connectivity method used) from your device app.

  • Synchronize the state of long-running workflows (such as firmware and configuration updates) between a device app and a back-end app.

  • Query your device metadata, configuration, or state.

Device twins are designed for synchronization and for querying device configurations and conditions. More information on when to use device twins can be found in Understand device twins.

Device twins are stored in an IoT hub and contain:

  • tags, device metadata accessible only by the solution back end;

  • desired properties, JSON objects modifiable by the solution back end and observable by the device app; and

  • reported properties, JSON objects modifiable by the device app and readable by the solution back end. Tags and properties cannot contain arrays, but objects can be nested.

Device twin image showing functionality

Additionally, the solution back end can query device twins based on all the above data. Refer to Understand device twins for more information about device twins, and to the IoT Hub query language reference for querying.

This tutorial shows you how to:

  • Create a back-end app that adds tags to a device twin, and a simulated device app that reports its connectivity channel as a reported property on the device twin.

  • Query devices from your back-end app using filters on the tags and properties previously created.

At the end of this tutorial, you will have two Python console apps:

  • AddTagsAndQuery.py, a Python back-end app, which adds tags and queries device twins.

  • ReportConnectivity.py, a Python app, which simulates a device that connects to your IoT hub with the device identity created earlier, and reports its connectivity condition.

Note

The article Azure IoT SDKs provides information about the Azure IoT SDKs that you can use to build both device and back-end apps.

To complete this tutorial you need the following:

Note

The pip packages for azure-iothub-service-client and azure-iothub-device-client are currently available only for Windows OS. For Linux/Mac OS, please refer to the Linux and Mac OS-specific sections on the Prepare your development environment for Python post.

Create an IoT hub

This section describes how to create an IoT hub using the Azure portal.

  1. Log in to the Azure portal.

  2. Choose +Create a resource, then Search the Marketplace for the IoT Hub.

  3. Select IoT Hub and click the Create button. You see the first screen for creating an IoT hub.

    Create a hub in the Azure portal

    Fill in the fields.

    Subscription: Select the subscription to use for your IoT hub.

    Resource Group: You can create a new resource group or use an existing one. To create a new one, click Create new and fill in the name you want to use. To use an existing resource group, click Use existing and select the resource group from the dropdown list. For more information, see Manage Azure Resource Manager resource groups.

    Region: This is the region in which you want your hub to be located. Select the location closest to you from the dropdown list.

    IoT Hub Name: Put in the name for your IoT Hub. This name must be globally unique. If the name you enter is available, a green check mark appears.

    Important

    Because the IoT hub will be publicly discoverable as a DNS endpoint, be sure to avoid entering any sensitive or personally identifiable information when you name it.

  4. Click Next: Size and scale to continue creating your IoT hub.

    Set the size and scale for a new IoT hub using the Azure portal

    On this screen, you can take the defaults and just click Review + create at the bottom.

    Pricing and scale tier: You can choose from several tiers depending on how many features you want and how many messages you send through your solution per day. The free tier is intended for testing and evaluation. It allows 500 devices to be connected to the IoT hub and up to 8,000 messages per day. Each Azure subscription can create one IoT Hub in the free tier.

    IoT Hub units: The number of messages allowed per unit per day depends on your hub's pricing tier. For example, if you want the IoT hub to support ingress of 700,000 messages, you choose two S1 tier units.

    For details about the other tier options, see Choosing the right IoT Hub tier.

    Advanced / Device-to-cloud partitions: This property relates the device-to-cloud messages to the number of simultaneous readers of the messages. Most IoT hubs only need four partitions.

  5. Click Review + create to review your choices. You see something similar to this screen.

    Review information for creating the new IoT hub

  6. Click Create to create your new IoT hub. Creating the hub takes a few minutes.

Retrieve connection string for IoT hub

After your hub has been created, retrieve the connection string for the hub. This is used to connect devices and applications to your hub.

  1. Click on your hub to see the IoT Hub pane with Settings, and so on. Click Shared access policies.

  2. In Shared access policies, select the iothubowner policy.

  3. Under Shared access keys, copy the Connection string -- primary key to be used later.

    Show how to retrieve the connection string

    For more information, see Access control in the "IoT Hub developer guide."

Register a new device in the IoT hub

In this section, you create a device identity in the identity registry in your IoT hub. A device cannot connect to IoT hub unless it has an entry in the identity registry. For more information, see the "Identity registry" section of the IoT Hub developer guide

  1. In your IoT hub navigation menu, open IoT Devices, then select Add to register a new device in your IoT hub.

    Create device identity in portal

  2. Provide a name for your new device, such as myDeviceId, and select Save. This action creates a new device identity for your IoT hub.

    Add a new device

    Important

    The device ID may be visible in the logs collected for customer support and troubleshooting, so make sure to avoid any sensitive information while naming it.

  3. After the device is created, open the device from the list in the IoT devices pane. Copy the Connection string---primary key to use later.

    Device connection string

Note

The IoT Hub identity registry only stores device identities to enable secure access to the IoT hub. It stores device IDs and keys to use as security credentials, and an enabled/disabled flag that you can use to disable access for an individual device. If your application needs to store other device-specific metadata, it should use an application-specific store. For more information, see IoT Hub developer guide.

Create the service app

In this section, you create a Python console app that adds location metadata to the device twin associated with your {Device ID}. It then queries the device twins stored in the IoT hub selecting the devices located in Redmond, and then the ones that are reporting a cellular connection.

  1. Open a command prompt and install the Azure IoT Hub Service SDK for Python. Close the command prompt after you install the SDK.

    pip install azure-iothub-service-client
    
  2. Using a text editor, create a new AddTagsAndQuery.py file.

  3. Add the following code to import the required modules from the service SDK:

    import sys
    import iothub_service_client
    from iothub_service_client import IoTHubRegistryManager, IoTHubRegistryManagerAuthMethod
    from iothub_service_client import IoTHubDeviceTwin, IoTHubError
    
  4. Add the following code, replacing the placeholder for [IoTHub Connection String] and [Device Id] with the connection string for the IoT hub and the device ID you created in the previous sections.

    CONNECTION_STRING = "[IoTHub Connection String]"
    DEVICE_ID = "[Device Id]"
    
    UPDATE_JSON = "{\"properties\":{\"desired\":{\"location\":\"Redmond\"}}}"
    
    UPDATE_JSON_SEARCH = "\"location\":\"Redmond\""
    UPDATE_JSON_CLIENT_SEARCH = "\"connectivity\":\"cellular\""
    
  5. Add the following code to the AddTagsAndQuery.py file:

    def iothub_service_sample_run():
       try:
           iothub_registry_manager = IoTHubRegistryManager(CONNECTION_STRING)
    
           iothub_registry_statistics = iothub_registry_manager.get_statistics()
           print ( "Total device count                       : {0}".format(iothub_registry_statistics.totalDeviceCount) )
           print ( "Enabled device count                     : {0}".format(iothub_registry_statistics.enabledDeviceCount) )
           print ( "Disabled device count                    : {0}".format(iothub_registry_statistics.disabledDeviceCount) )
           print ( "" )
    
           number_of_devices = iothub_registry_statistics.totalDeviceCount
           dev_list = iothub_registry_manager.get_device_list(number_of_devices)
    
           iothub_twin_method = IoTHubDeviceTwin(CONNECTION_STRING)
    
           for device in range(0, number_of_devices):
               if dev_list[device].deviceId == DEVICE_ID:
                   twin_info = iothub_twin_method.update_twin(dev_list[device].deviceId, UPDATE_JSON)
    
           print ( "Devices in Redmond: " )
           for device in range(0, number_of_devices):
               twin_info = iothub_twin_method.get_twin(dev_list[device].deviceId)
    
               if twin_info.find(UPDATE_JSON_SEARCH) > -1:
                   print ( dev_list[device].deviceId )
    
           print ( "" )
    
           print ( "Devices in Redmond using cellular network: " )
           for device in range(0, number_of_devices):
               twin_info = iothub_twin_method.get_twin(dev_list[device].deviceId)
    
               if twin_info.find(UPDATE_JSON_SEARCH) > -1:
                   if twin_info.find(UPDATE_JSON_CLIENT_SEARCH) > -1:
                       print ( dev_list[device].deviceId )
    
       except IoTHubError as iothub_error:
           print ( "Unexpected error {0}".format(iothub_error) )
           return
       except KeyboardInterrupt:
           print ( "IoTHub sample stopped" )
    

    The Registry object exposes all the methods required to interact with device twins from the service. The code first initializes the Registry object, then updates the device twin for deviceId, and finally runs two queries. The first selects only the device twins of devices located in the Redmond43 plant, and the second refines the query to select only the devices that are also connected through cellular network.

  6. Add the following code at the end of AddTagsAndQuery.py to implement the iothub_service_sample_run function:

    if __name__ == '__main__':
        print ( "Starting the IoT Hub Device Twins Python service sample..." )
    
        iothub_service_sample_run()
    
  7. Run the application with:

    python AddTagsAndQuery.py
    

    You should see one device in the results for the query asking for all devices located in Redmond43 and none for the query that restricts the results to devices that use a cellular network.

    first query showing all devices in Redmond

In the next section, you create a device app that reports the connectivity information and changes the result of the query in the previous section.

Create the device app

In this section, you create a Python console app that connects to your hub as your {Device ID}, and then updates its device twin's reported properties to contain the information that it is connected using a cellular network.

  1. Open a command prompt and install the Azure IoT Hub Service SDK for Python. Close the command prompt after you install the SDK.

    pip install azure-iothub-device-client
    
  2. Using a text editor, create a new ReportConnectivity.py file.

  3. Add the following code to import the required modules from the service SDK:

    import time
    import iothub_client
    from iothub_client import IoTHubClient, IoTHubClientError, IoTHubTransportProvider, IoTHubClientResult, IoTHubError
    
  4. Add the following code, replacing the placeholder for [IoTHub Device Connection String] with the connection string for the IoT hub device you created in the previous sections.

    CONNECTION_STRING = "[IoTHub Device Connection String]"
    
    # choose HTTP, AMQP, AMQP_WS or MQTT as transport protocol
    PROTOCOL = IoTHubTransportProvider.MQTT
    
    TIMER_COUNT = 5
    TWIN_CONTEXT = 0
    SEND_REPORTED_STATE_CONTEXT = 0
    
  5. Add the following code to the ReportConnectivity.py file to implement the device twins functionality:

    def device_twin_callback(update_state, payload, user_context):
        print ( "" )
        print ( "Twin callback called with:" )
        print ( "    updateStatus: %s" % update_state )
        print ( "    payload: %s" % payload )
    
    def send_reported_state_callback(status_code, user_context):
        print ( "" )
        print ( "Confirmation for reported state called with:" )
        print ( "    status_code: %d" % status_code )
    
    def iothub_client_init():
        client = IoTHubClient(CONNECTION_STRING, PROTOCOL)
    
        if client.protocol == IoTHubTransportProvider.MQTT or client.protocol == IoTHubTransportProvider.MQTT_WS:
            client.set_device_twin_callback(
                device_twin_callback, TWIN_CONTEXT)
    
        return client
    
    def iothub_client_sample_run():
        try:
            client = iothub_client_init()
    
            if client.protocol == IoTHubTransportProvider.MQTT:
                print ( "Sending data as reported property..." )
    
                reported_state = "{\"connectivity\":\"cellular\"}"
    
                client.send_reported_state(reported_state, len(reported_state), send_reported_state_callback, SEND_REPORTED_STATE_CONTEXT)
    
            while True:
                print ( "Press Ctrl-C to exit" )
    
                status_counter = 0
                while status_counter <= TIMER_COUNT:
                    status = client.get_send_status()
                    time.sleep(10)
                    status_counter += 1	
        except IoTHubError as iothub_error:
            print ( "Unexpected error %s from IoTHub" % iothub_error )
            return
        except KeyboardInterrupt:
            print ( "IoTHubClient sample stopped" )
    

    The Client object exposes all the methods you require to interact with device twins from the device. The previous code, after it initializes the Client object, retrieves the device twin for your device and updates its reported property with the connectivity information.

  6. Add the following code at the end of ReportConnectivity.py to implement the iothub_client_sample_run function:

    if __name__ == '__main__':
        print ( "Starting the IoT Hub Device Twins Python client sample..." )
    
        iothub_client_sample_run()
    
  7. Run the device app:

    python ReportConnectivity.py
    

    You should see confirmation the device twins were updated.

    update twins

  8. Now that the device reported its connectivity information, it should appear in both queries. Go back and run the queries again:

    python AddTagsAndQuery.py
    

    This time your {Device ID} should appear in both query results.

    second query

Next steps

In this tutorial, you configured a new IoT hub in the Azure portal, and then created a device identity in the IoT hub's identity registry. You added device metadata as tags from a back-end app, and wrote a simulated device app to report device connectivity information in the device twin. You also learned how to query this information using the registry.

Use the following resources to learn how to: