Automatic Device Management with Device Twins in Azure

1 hour
  • 4 Learning Objectives

About this Hands-on Lab

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!

Learning Objectives

Successfully complete this lab by achieving the following learning objectives:

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"}}'
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!

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

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!

Additional Resources

You have recently been hired by Parktronics, a monitoring company that works with local, state, and national park services. They have decided to use Azure IoT to develop Gatekeeper, a solution that will monitor entry and exit gates to ensure they are closed and locked at closing time.

You've been tasked with a proof-of-concept of the Gatekeeper solution. Prior to shipping out a device for use in your proof-of-concept, you are using Azure’s Automated Device Management feature to set properties on your simulated IoT device.

You will need to:

  • Register a device with Azure IoT Hub
  • Create an IoT device deployment
  • View the device twin
  • Download sample Node app
  • Modify and execute Node app
  • Verify results

If you get stuck, feel free to check out the solution video or the detailed instructions under each objective. Good luck!

Tools

You will be using the Azure CLI to perform most of the tasks in this lab. You can access the CLI by using Azure Cloud Shell. When accessing the shell, select Show advanced settings, and use the existing storage account that is in the location where the storage group resides.

Node.js Application

We will be using the sample IoT Node.js application to simulate the IoT device application. This app can be downloaded from GitHub.

Lab Suffix

Predeployed lab resources will all end with a unique five-character suffix. Make note of this suffix, as you may need it when naming resources during the lab.

WARNING: Be Prepared for UI Changes

Given the fluid nature of Microsoft cloud tools, you may experience user interface (UI) changes that were made following the development of this hands-on lab that do not match up with lab instructions. When any such changes are brought to our attention, we will attempt to update the content accordingly. However, if changes occur, students will have to adapt to the changes and work through them in the hands-on labs as needed.

What are Hands-on Labs

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.

Sign In
Welcome Back!

Psst…this one if you’ve been moved to ACG!

Get Started
Who’s going to be learning?