IoT device configuration

Setting up IoT hub

First things first, let\’s create an IoT hub. This is the easiest step. From the Azure portal, we can create our IoT hub:

\"\"
Setting up IoT hub in the Azure portal

Once the IoT hub is deployed, we\’ll need to get the connection string to connect to it securely. To get this, we\’ll first create a device ID for the console app we\’ll write next. To do this, in the Azure portal, go to the IoT hub blade, and select IoT devices and hit the new button at the top.

\"\"

Once the device ID is created, select the device and then copy either of the connection strings:

\"\"

That\’s all we need to do right now with IoT hub. We\’ll revisit IoT hub later when we implement a service to update the configuration of the devices. But first, let\’s connect a device to IoT.

Writing the Python code to connect to IoT Hub

We\’ll start our development by writing a small console app. To do this, we\’ll need the connection string. For this demo, I\’ll store the connection string in a file, not hard code it in the code.

sudo mkdir /var/secrets
echo \'CONNECTION_STRING\' | sudo tee /var/secrets/device_iot_conn_string

With that out of the way, let\’s install the required IoT Hub package:

pip3 install azure-iot-device
pip3 install azure-iot-hub

Let\’s now create our Python code to connect to IoT Hub. This code is also available on GitHub. The code is straightforward, and contains 2 methods: It has a method to listen to updates from IoT Hub, and a method to initialize the IoT client:

import time
import json
import threading
from azure.iot.device import IoTHubModuleClient

with open(\'/var/secrets/device_iot_conn_string\', \'r\') as f:
    CONNECTION_STRING = f.readline()

# uncomment line below if you want to see the connection string.
# print(CONNECTION_STRING)

def twin_update_listener(client):
    while True:
        patch = client.receive_twin_desired_properties_patch()  # blocking call
        print(\"\")
        print(\"Twin desired properties patch received:\")
        print(patch)
        print(\"Now is the time to execute business logic.\")


def iothub_client_sample_run():
    try:
        module_client = IoTHubModuleClient.create_from_connection_string(CONNECTION_STRING)

        twin_update_listener_thread = threading.Thread(target=twin_update_listener, args=(module_client,))
        twin_update_listener_thread.daemon = True
        twin_update_listener_thread.start()

        while True:
            time.sleep(1000000)

    except KeyboardInterrupt:
        print(\"IoTHubModuleClient sample stopped\")


if __name__ == \'__main__\':
    print ( \"Starting the IoT Hub Python sample...\" )
    print ( \"IoTHubModuleClient waiting for commands, press Ctrl-C to exit\" )

    iothub_client_sample_run()

We can run this code using:

python3 device.py

This will start the device. This won\’t send/receive anything just yet, since it\’s only listening to updates from the device configuration service.

\"\"

Keep this console app running, so you can see the update apply once we finish the next steps.

Let\’s now create a console app to update the device twin to update its properties.

Updating the digital twin

To update the digital twin, we\’ll write a new console app that will update the twin. This console app will need a different connection string, namely to access the IoT Hub service. To get to this connection string, go to the IoT Hub blade

Verifying updates in the digital twin

To see that the update was applied, we can navigate to the digital twin view and see the status of our device. To get there, first open the device view and select the device we created:

\"\"

From there, select \”Device twin\” from the top hand navigation:

\"\"

And there you can see when the last update was applied, and how the update got applied:

\"\"


Posted

in

by

Tags:

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *