Leverage Automatic Device Management with IoT Device Twins in Azure

45 minutes
  • 5 Learning Objectives

About this Hands-on Lab

Managing 1 or 2 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 (ADM) in Azure IoT Hub. We can use this handy feature to manage properties and tags on hundreds of IoT devices at the same time!

In this lab you will:
– Register 2 devices with Azure IoT Hub and edit the JSON device twins.
– Set up an ADM configuration to target 1 or more devices, based on device twin properties.
– Download, modify, and run a Node.js app that simulates an IoT device.
– Verify that the ADM configuration properly updated the targeted device(s).

Learning Objectives

Successfully complete this lab by achieving the following learning objectives:

Register and Configure Devices in Azure IoT Hub

NOTE: As an additional challenge, the precise JSON you should add to each twin is not provided here. Check the lab guide to see what the JSON should look like in the device twins once you have completed step 4, below.

  1. Open the link for this lab in an incognito or InPrivate browser window.
  2. Enter the credentials assigned to this lab (they will be different from the credentials you see in the solution video; each student gets a unique lab session and credentials).
  3. Navigate to the only IoT Hub already deployed, and register 2 devices. You can name the devices whatever you choose, but in the video and lab guide, we refer to them as myTargetDevice and myOtherDevice to make their purposes more clear .
  4. In each device twin, add a tags section and a location tag to the device twin, just before the desired properties section. Tip: Don’t forget the comma after closing bracket of the tags object.
    • On one device, assign "Green River" to the location.
    • On the second device, assign "Rock Springs" to the location.
  5. Copy the Primary Connection String from the "Green River" device to a text document. Tip: Make sure you copy the Primary Connection String and not just the primary key.
Create an Automatic Device Management Configuration

NOTE: As an additional challenge, the exact syntax for the desired twin settings and target device query are not provided, here. Check the lab guide for the proper syntax on both steps 2 and 3, below. Also, the order of steps in the wizard may change, so just ensure you cover all the settings, regardless of the order in which they are requested.

  1. In the Azure IoT Hub that contains your 2 devices, navigate to Configurations + Deployments, and add a new device twin configuration.
  2. Set the priority to 10.
  3. Set a desired property setting for conveyorCabinet with a maxTemp of 89 and a maxHumidity reading of 40. Hint: There is no set schema for device twins; it all depends on the application. In this case, the simulated device app will be looking under the properties.desired object for another object called settings, which holds the conveyorCabinet object and its 2 key-value pairs for maxTemp and maxHumidity. The object names are all lowercase.
  4. Create a query to target any devices with the location tag assigned to "Green River."
  5. Review and save the configuration.
Verify ADM Configuration Targeted the Correct Device

NOTE: As an additional challenge, the JSON you should see in the twin is not provided, here. Check the lab guide to see what the JSON should look like in the targeted device.

  1. Navigate to the device twin for each of the 2 devices you created in the first objective.
  2. Verify that the desired properties are updated for only the device located in Green River and not the device located in Rock Springs.
Run the Simulated Device in Cloud Shell
  1. Launch Cloud Shell from within the Azure Portal, choosing the Bash environment in order to use Azure CLI.

  2. When prompted to initialize storage for Cloud Shell, select Show advanced settings, and add the storage account and file share. Note that the storage account name must be universally unique; you might want to use the random characters at the beginning of the resource group name as a part of your storage account name.

  3. Install the Azure IoT extension for Azure CLI:

     az extension add --name azure-iot
  4. Download the sample Node.js application by cloning the Git repository for this course, below. This sample application acts as a simulated device. Once connected to the hub, it reads the desired properties from the device twin and echos back the desired properties as reported properties on the twin. This is how we use device twins to synchronize desired properties (from the backend application) with the reported properties (maintained by the IoT devices).

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

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

    npm install
  7. In the top menu of the Cloud Shell terminal window, select the curly brackets icon to open the editor. Navigate to this directory: content-AZ-220/samples/DeviceTwins.

  8. Open test_device_twins.js and find the connectionString variable, near the top of the code. Replace the value assigned to that variable with the device connection string you copied in the first objective.

    Tip: Make sure the value is wrapped in single quotes.

  9. Save your changes.

    Tip: Look for the "…" in the upper right of the editor window to save changes. You can also use CTRL-s.

  10. Back at the command prompt, run the following command to launch the application and run your simulated device:

    node test_device_twins.js

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

Verify the Targeted Device Updated the Twin’s Reported Properties

NOTE: As an additional challenge, the JSON you should see in the twin is not provided, here. Check the lab guide to see what the JSON should look like in the targeted device.

  1. Navigate to the device twin for each of the 2 devices you created in the first objective.
  2. Verify that the reported properties are updated for only the device located in Green River and not the device located in Rock Springs.

Additional Resources

You have recently been hired by Railtronics, a monitoring company that works with rail carriers to automate the monitoring and management of station equipment and facilities. They have decided to use Azure IoT to develop a solution that manages environmental temperature and humidity on sensors in both public areas as well as enclosed equipment housing.

You've been tasked with a proof-of-concept of the solution. Prior to shipping out a device for use in your proof-of-concept, you are using Azure’s automatic device management feature to set properties on your simulated IoT devices. You need to show that the solution can target specific devices based on configurable properties.

If you get stuck, feel free to check out the solution video or the lab guide. Have fun and 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 via the Azure portal.

Node.js Application

We will be using the sample IoT Node.js application to simulate the IoT device application. You do not need prior experience with Node.js. Follow the instructions in the lab objectives.

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.

Syntax Notes

Note that the device twin editor in the Azure portal will "beautify" JSON with proper indenting. However, unlike some tagged languages, JSON doesn't care about leading spaces and indenting. You can ignore any indentation differences. The key features are the object and value names and proper nesting and punctuation. Technically, JSON is case-sensitive, but some consuming code is more tolerant than others. It is usually best to try to match capitalization based on other similar objects in the JSON.

The end-state for each of the JSON objects you create is documented in the lab guide.

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?