Skip to content

Contact sales

By filling out this form and clicking submit, you acknowledge our privacy policy.
  • Labs icon Lab
  • A Cloud Guru
Azure icon
Labs

Automatic Device Management with Device Twins in Azure

Managing one or two IoT devices is really no big deal. But what if you had 100 devices? 1000? Then things get a little more interesting. And by interesting, we mean excruciatingly painful. But help is on the way, in the form of Automatic Device Management in Azure IoT Hub. We can use this handy feature to manage properties and tags on hundreds of IoT devices at the same time!

Azure icon
Labs

Path Info

Level
Clock icon Intermediate
Duration
Clock icon 1h 0m
Published
Clock icon Jun 12, 2020

Contact sales

By filling out this form and clicking submit, you acknowledge our privacy policy.

Table of Contents

  1. Challenge

    Register and Configure the Device with an Azure IoT Hub

    Configure Cloud Shell

    1. Access Azure Cloud Shell using the credentials provided in the lab.
    2. Choose the Bash environment.
    3. Select Show advanced settings.
    4. Use the existing storage account that resides in the resource group location and the existing cloudshell file share name.
    5. Install the Azure IoT extension for CLI:
      az extension add --name azure-iot
      

    Register and Configure the IoT device

    The following steps are configured with the CLI in Cloud Shell. Note that XXXXX is the five-character suffix for the lab resources.

    1. Register the device in the IoT Hub identity registry:

      az iot hub device-identity create --device-id IoTDevice1 --hub-name iothub-XXXXX
      
    2. Obtain the connection string for the device application:

      az iot hub device-identity connection-string show --hub-name iothub-XXXXX --device-id IoTDevice1 --output table
      
    3. Copy the connection string to your clipboard and paste it into a text document.

    4. Add tags to the IoT device twin:

      az iot hub device-twin update --device-id IoTDevice1 --hub-name iothub-ycb3m --set tags='{"location":{"state":"TX","city":"Austin"}}'
      
  2. Challenge

    Create an IoT Device Management Configuration

    Create the IoT device management configuration

    The following steps will be configured in the Azure Portal. Note that XXXXX is the five-character suffix for the lab resources.

    1. Access the Azure Portal using the credentials provided in the lab.
    2. Click on the IoT Hub provisioned for this lab. It will be named IoTHub-XXXXX.
    3. Click on IoT device configuration in the IoTHub-XXXXX pane.
    4. Click + Add Device Configuration to create the configuration with the following settings:
      • Name and Label: - Name: This is descriptive, so name it anything. It has to be lowercase and the only special characters that can be used are '-+%_\*. - Labels: You can specify a key/tag pair for this configuration, but it is not required.
        • Twin Settings:
          • Device Twin Property: Use properties.desired.settings.
          • Device Twin Property Content: Use { "gatelocked": false, "gateclosed": false }.
        • Metrics:
          • Metric Name: Use nowReporting.
          • Metric Criteria: Use SELECT deviceId FROM devices WHERE properties.reported.settings.status = 'Success'
        • Target Devices:.
          • Priority: Use 10.
          • Target Condition: Use tags.location.city = 'Austin'.

    Click Review + create and then Create to create the deployment.

    Verify That the Configuration Was Applied

    Once the configuration has been created, verify that it has been deployed to the test device.

    1. Click on IoT devices in the IoTHub-XXXXX pane and click on the device created in the first objective.
    2. Click on Device Twin to view the twin. Verify that the desired properties now reside within the twin.

    Device Desired Properties!

  3. Challenge

    Download and Configure the Node.js Application

    1. Access Azure Cloud Shell using the credentials provided in the lab.

    2. Download the sample Node.js application by cloning the Git repository for this course:

      git clone https://github.com/linuxacademy/content-AZ-220.git
      
    3. Change to the directory for the Device Twin lab:

      cd content-AZ-220/samples/DeviceTwins/
      
    4. Install the Node dependencies for the sample application:

      npm install
      
    5. Modify test_device_twins.js in vi or nano, replacing <device-id> on line 6 with the device connection string copied in Objective 1.

    Connection String

  4. Challenge

    Run the Node.js Application to Apply Desired Properties

    The sample node application acts as a simulated device. It reads the desired properties from the device twin and "sets" the properties as reported. This is how we use device twins to synchronize desired properties (from the back-end application) with the reported properties (from the Iot Devices).

    To run the application, type the following in Cloud Shell:

    node test_device_twins.js
    

    There will be output regarding the progress of the application. Once the application has reported successful changes, it may be exited by pressing CTRL+C.

    Verify That the Reported Properties Were Applied

    Verify that the desired properties are now reported properties in the device twin:

    1. Click on IoT devices in the IoTHub-XXXXX pane, then click on the device created in the first objective.
    2. Click on Device Twin to view the twin. Verify that the reported properties now reside within the twin.

    Device Reported Properties!

The Cloud Content team comprises subject matter experts hyper focused on services offered by the leading cloud vendors (AWS, GCP, and Azure), as well as cloud-related technologies such as Linux and DevOps. The team is thrilled to share their knowledge to help you build modern tech solutions from the ground up, secure and optimize your environments, and so much more!

What's a lab?

Hands-on Labs are real environments created by industry experts to help you learn. These environments help you gain knowledge and experience, practice without compromising your system, test without risk, destroy without fear, and let you learn from your mistakes. Hands-on Labs: practice your skills before delivering in the real world.

Provided environment for hands-on practice

We will provide the credentials and environment necessary for you to practice right within your browser.

Guided walkthrough

Follow along with the author’s guided walkthrough and build something new in your provided environment!

Did you know?

On average, you retain 75% more of your learning if you get time for practice.

Start learning by doing today

View Plans