Skip to content

Latest commit

 

History

History
159 lines (97 loc) · 8.08 KB

quickstart-control-device-node.md

File metadata and controls

159 lines (97 loc) · 8.08 KB
title description author manager ms.service services ms.devlang ms.topic ms.custom ms.date ms.author
Control a device from Azure IoT Hub quickstart (Node.js) | Microsoft Docs
In this quickstart, you run two sample Node.js applications. One application is a back-end application that can remotely control devices connected to your hub. The other application simulates a device connected to your hub that can be controlled remotely.
dominicbetts
timlt
iot-hub
iot-hub
nodejs
quickstart
mvc
06/19/2018
dobett

Quickstart: Control a device connected to an IoT hub (Node.js)

[!INCLUDE iot-hub-quickstarts-2-selector]

IoT Hub is an Azure service that enables you to ingest high volumes of telemetry from your IoT devices into the cloud and manage your devices from the cloud. In this quickstart, you use a direct method to control a simulated device connected to your IoT hub. You can use direct methods to remotely change the behavior of a device connected to your IoT hub.

The quickstart uses two pre-written Node.js applications:

  • A simulated device application that responds to direct methods called from a back-end application. To receive the direct method calls, this application connects to a device-specific endpoint on your IoT hub.

  • A back-end application that calls the direct methods on the simulated device. To call a direct method on a device, this application connects to service-side endpoint on your IoT hub.

[!INCLUDE cloud-shell-try-it.md]

If you don’t have an Azure subscription, create a free account before you begin.

Prerequisites

The two sample applications you run in this quickstart are written using Node.js. You need Node.js v4.x.x or later on your development machine.

You can download Node.js 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 haven't already done so, download the sample Node.js project from https://github.com/Azure-Samples/azure-iot-samples-node/archive/master.zip and extract the ZIP archive.

Create an IoT hub

If you completed the previous Quickstart: Send telemetry from a device to an IoT hub, you can skip this step.

[!INCLUDE iot-hub-include-create-hub]

Register a device

If you completed the previous Quickstart: Send telemetry from a device to an IoT hub, you can skip this step.

A device must be registered with your IoT hub before it can connect. In this quickstart, you use the Azure Cloud Shell to register a simulated device.

  1. Run the following commands in Azure Cloud Shell to add the IoT Hub CLI extension and to create the device identity.

    YourIoTHubName : Replace this placeholder below with the name you chose for your IoT hub.

    MyNodeDevice : This is the name given for the registered device. Use MyNodeDevice as shown. If you choose a different name for your device, you will also need to use that name throughout this article, and update the device name in the sample applications before you run them.

    az extension add --name azure-cli-iot-ext
    az iot hub device-identity create \
      --hub-name YourIoTHubName --device-id MyNodeDevice
    
  2. Run the following commands in Azure Cloud Shell to get the device connection string for the device you just registered:

    YourIoTHubName : Replace this placeholder below with the name you chose for your IoT hub.

    az iot hub device-identity show-connection-string \
      --hub-name YourIoTHubName \
      --device-id MyNodeDevice \
      --output table
    

    Make a note of the device connection string, which looks like:

    HostName={YourIoTHubName}.azure-devices.net;DeviceId=MyNodeDevice;SharedAccessKey={YourSharedAccessKey}

    You use this value later in the quickstart.

  3. You also need a service connection string to enable the back-end application to connect to your IoT hub and retrieve the messages. The following command retrieves the service connection string for your IoT hub:

    YourIoTHubName : Replace this placeholder below with the name you chose for your IoT hub.

    az iot hub show-connection-string \
      --hub-name YourIoTHubName --output table
    

    Make a note of the service connection string, which looks like:

    HostName={YourIoTHubName}.azure-devices.net;SharedAccessKeyName=iothubowner;SharedAccessKey={YourSharedAccessKey}

    You use this value later in the quickstart. The service connection string is different from the device connection string.

Listen for direct method calls

The simulated device application connects to a device-specific endpoint on your IoT hub, sends simulated telemetry, and listens for direct method calls from your hub. In this quickstart, the direct method call from the hub tells the device to change the interval at which it sends telemetry. The simulated device sends an acknowledgement back to your hub after it executes the direct method.

  1. In a local terminal window, navigate to the root folder of the sample Node.js project. Then navigate to the iot-hub\Quickstarts\simulated-device-2 folder.

  2. Open the SimulatedDevice.js file in a text editor of your choice.

    Replace the value of the connectionString variable with the device connection string you made a note of previously. Then save your changes to SimulatedDevice.js file.

  3. In the local terminal window, run the following commands to install the required libraries and run the simulated device application:

    npm install
    node SimulatedDevice.js
    

    The following screenshot shows the output as the simulated device application sends telemetry to your IoT hub:

    Run the simulated device

Call the direct method

The back-end application connects to a service-side endpoint on your IoT Hub. The application makes direct method calls to a device through your IoT hub and listens for acknowledgements. An IoT Hub back-end application typically runs in the cloud.

  1. In another local terminal window, navigate to the root folder of the sample Node.js project. Then navigate to the iot-hub\Quickstarts\back-end-application folder.

  2. Open the BackEndApplication.js file in a text editor of your choice.

    Replace the value of the connectionString variable with the service connection string you made a note of previously. Then save your changes to the BackEndApplication.js file.

  3. In the local terminal window, run the following commands to install the required libraries and run the back-end application:

    npm install
    node BackEndApplication.js
    

    The following screenshot shows the output as the application makes a direct method call to the device and receives an acknowledgement:

    Run the back-end application

    After you run the back-end application, you see a message in the console window running the simulated device, and the rate at which it sends messages changes:

    Change in simulated client

Clean up resources

[!INCLUDE iot-hub-quickstarts-clean-up-resources]

Next steps

In this quickstart, you called a direct method on a device from a back-end application, and responded to the direct method call in a simulated device application.

To learn how to route device-to-cloud messages to different destinations in the cloud, continue to the next tutorial.

[!div class="nextstepaction"] Tutorial: Route telemetry to different endpoints for processing