author | ms.author | ms.service | ms.topic | ms.date |
---|---|---|---|---|
dominicbetts |
dobett |
iot-pnp |
include |
11/20/2020 |
This tutorial shows you how to build a sample IoT Plug and Play device application with components, connect it to your IoT hub, and use the Azure IoT explorer tool to view the information it sends to the hub. The sample application is written for Node.js and is included in the Azure IoT Hub Device SDK for Node.js. A solution builder can use the Azure IoT explorer tool to understand the capabilities of an IoT Plug and Play device without the need to view any device code.
[!INCLUDE iot-pnp-prerequisites]
To complete this tutorial, you need Node.js on your development machine. You can download the latest recommended version for multiple platforms from nodejs.org.
You can verify the current version of Node.js on your development machine using the following command:
node --version
If you completed Quickstart: Connect a sample IoT Plug and Play device application running on Windows to IoT Hub (Node), you've already cloned the repository.
Open a command prompt in the directory of your choice. Execute the following command to clone the Microsoft Azure IoT SDK for Node.js GitHub repository into this location:
git clone https://github.com/Azure/azure-iot-sdk-node
You use the device SDK to build the included sample code. The application you build simulates a Plug and Play device with multiple components that connects to an IoT hub. The application sends telemetry and properties and receives commands.
- In a local terminal window, go to the folder of your cloned repository and navigate to the /azure-iot-sdk-node/device/samples/pnp folder. Then run the following command to install the required libraries:
npm install
This will install the relevant npm files required to run the samples in the folder.
Navigate to the azure-iot-sdk-node\device\samples\pnp folder.
The azure-iot-sdk-node\device\samples\pnp folder contains the sample code for the IoT Plug and Play temperature controller device.
The code in the pnpTemperatureController.js file implements an IoT Plug and Play temperature controller device. The model this sample implements uses multiple components. The Digital Twins definition language (DTDL) model file for the temperature device defines the telemetry, properties, and commands the device implements.
Open the pnpTemperatureController.js file in a code editor of your choice. The sample code shows how to:
-
Define the
modelId
which is the DTMI for the device that's being implemented. This DTMI is user-defined and must match the DTMI of the temperature controller DTDL model. -
Implement the components defined in the temperature controller DTDL model. The components in a real temperature controller should implement these two interfaces. These two interfaces are already published in a central repository. In this sample, the two interfaces are:
- Thermostat
- Device information developed by Azure
-
Define component names. This sample has two thermostats and one device information component.
-
Define command name. These are the commands the device responds to.
-
Define the
serialNumber
constant. TheserialNumber
is fixed any given device. -
Define the command handlers.
-
Define the functions to send command responses.
-
Define helper functions to log command requests.
-
Define a helper function to create the properties.
-
Define a listener for property updates.
-
Define a function to send telemetry from this device. Both thermostats and the default component send telemetry. This function receives the component name as parameter.
-
Define a
main
function that:-
Uses the device SDK to create a device client and connect to your IoT hub. The device supplies the
modelId
so that IoT Hub can identify the device as an IoT Plug and Play device. -
Starts listening for command requests using the
onDeviceMethod
function. The function sets up a listener for command requests from the service:- The device DTDL defines the
reboot
andgetMaxMinReport
commands. - The
commandHandler
function defines how the device responds to a command.
- The device DTDL defines the
-
Starts sending telemetry by using
setInterval
andsendTelemetry
. -
Uses the
helperCreateReportedPropertiesPatch
function to create the properties and theupdateComponentReportedProperties
to update the properties. -
Uses
desiredPropertyPatchListener
to listen for property updates. -
Disables all the listeners and tasks, and exits the loop when you press Q or q.
-
[!INCLUDE iot-pnp-environment]
To learn more about the sample configuration, see the sample readme.
Now that you've seen the code, use the following command to run the sample:
node pnpTemperatureController.js
You see the following output, indicating the device has begun sending telemetry data to the hub, and is now ready to receive commands and property updates.
Keep the sample running as you complete the next steps.
After the device client sample starts, use the Azure IoT explorer tool to verify it's working.
[!INCLUDE iot-pnp-iot-explorer.md]
[!INCLUDE iot-pnp-clean-resources.md]