- Lab
- A Cloud Guru
Leverage Automatic Device Management with IoT Device Twins in Azure
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).
Path Info
Table of Contents
-
Challenge
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.
- Open the link for this lab in an incognito or InPrivate browser window.
- 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).
- 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 .
- In each device twin, add a
tags
section and alocation
tag to the device twin, just before the desired properties section. Tip: Don't forget the comma after closing bracket of thetags
object.- On one device, assign "Green River" to the location.
- On the second device, assign "Rock Springs" to the location.
- 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.
-
Challenge
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.
- In the Azure IoT Hub that contains your 2 devices, navigate to Configurations + Deployments, and add a new device twin configuration.
- Set the priority to 10.
- Set a desired property setting for
conveyorCabinet
with amaxTemp
of 89 and amaxHumidity
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 theproperties.desired
object for another object calledsettings
, which holds theconveyorCabinet
object and its 2 key-value pairs formaxTemp
andmaxHumidity
. The object names are all lowercase. - Create a query to target any devices with the
location
tag assigned to "Green River." - Review and save the configuration.
-
Challenge
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.
- Navigate to the device twin for each of the 2 devices you created in the first objective.
- Verify that the desired properties are updated for only the device located in Green River and not the device located in Rock Springs.
-
Challenge
Run the Simulated Device in Cloud Shell
-
Launch Cloud Shell from within the Azure Portal, choosing the Bash environment in order to use Azure CLI.
-
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.
-
Install the Azure IoT extension for Azure CLI:
az extension add --name azure-iot
-
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
-
Change to the directory for the Device Twins lab:
cd content-AZ-220/samples/DeviceTwins/
-
Install the Node dependencies for the sample application:
npm install
-
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.
-
Open
test_device_twins.js
and find theconnectionString
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.
- Save your changes.
Tip: Look for the "..." in the upper right of the editor window to save changes. You can also use
CTRL-s
.- 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.
-
-
Challenge
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.
- Navigate to the device twin for each of the 2 devices you created in the first objective.
- Verify that the reported properties are updated for only the device located in Green River and not the device located in Rock Springs.
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.