- A Cloud Guru
Change Device Properties with IoT Hub Direct Methods
The primary purpose for an Azure IoT hub is to be somewhere the devices, or "things," can send telemetry. But did you know it can talk *back* to the things as well? Standard Tiers of Azure IoT Hub allow for cloud-to-device communication. One of the options by which we can communicate from our backend applications to our devices is by using direct methods. Direct methods are used for communications that require immediate confirmation of the result and are often used for interactive control of devices. In this hands-on lab, we will create a device in a preprovisioned Azure IoT hub and then use a Node.js application to simulate the physical device. As our device sends telemetry information back to the IoT hub, we will use a feature in the Azure portal to send a direct method to the device, forcing it to change the interval by which it provides telemetry.
Table of Contents
Register a Device in Azure IoT Hub
- 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 a device. Name the device whatever you choose.
- Copy the Primary Connection String from the device to a text document.
Tip: Make sure you copy the Primary Connection String and not just the Primary Key.
Configure and Run 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 Advanced settings and add the storage account and file share. Add a new storage account. Do not use an existing one. 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, below. This sample application acts as a simulated device. it includes a method that handles the direct method call from the hub or another backend application.
git clone https://github.com/Azure-Samples/azure-iot-samples-node.git
- Change to the directory for the Device Twin lab:
- Install the Node dependencies for the sample application:
- In the top menu of the Cloud Shell terminal window, select the curly brackets icon to open the editor. Navigate to this directory:
Note: Make sure you are in the right directory. There are duplicate files with the same names in other directories — but with different code.
SimulatedDevice.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.
- You now need to find the name of the method name that you can use to send a remote command using a direct method in the Azure IoT Hub.
- The purpose of the method is to change the interval by which telemetry is sent. It currently defaults to 1 second.
- There are no parameters on the method.
- You are looking for a handler of the direct method call from the backend application. The code does not directly expose the method.
- Once you find the method, copy and paste it into a text document for use later.
- Save your changes. > Tip: Look for the "..." in the upper right of the editor window to save changes.
- Back in the command prompt, run the following command to launch the application and run your simulated device:
If the device has successfully connected to the hub, you will see telemetry flowing in the terminal window.
Execute a Direct Method on the Simulated Device
- Leave the terminal window open and the simulated device running in a big enough pane to see the telemetry flowing.
- Return to the device registration you created in the Azure portal, and execute a direct method on the device, using the method name you found in the code. Recall the purpose of the method is to change the telemetry interval from the default of 1 second, so the payload for the method is the digit 5.
- Verify that the messages in the terminal have slowed down substantially.
- Execute the direct method again, using 0.5 for the payload.
- Verify that the messages in the terminal have sped up substantially.
- Feel free to try other values.
- When you are done, you can stop the code from running by returning to the terminal and executing CTRL-C.
Tip: Suppose the direct method name you found in the code was runMe, then you would use runMe for the method name and the digit 5 for the payload — not runMe(5).
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.