Skip to content

Quickstart Node.js

Chaoyi Yuan edited this page Dec 6, 2018 · 5 revisions

Create and control an IoT device connected to an IoT hub (Node.js)

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 will use Visual Studio IoT Hub Toolkit to create a simulated device connected to your IoT hub and control it with a direct method. You can use direct methods to remotely change the behavior of a device connected to your IoT hub.

Prerequisites

  1. Install the Visual Studio Code extension Azure Account and sign in. If you don’t have an Azure subscription, create a free account before you begin.

  2. Install the Visual Studio Code extension Azure IoT Hub Toolkit.

The quickstart also uses a pre-written Node.js applications as 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. For other supported programming languages, you can refer to IoT Hub Documentations.

  1. The application is 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
  1. 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

The first step is to create an IoT hub in your subscription from VS Code. The IoT hub enables you to ingest high volumes of telemetry into the cloud from many devices. The hub then enables one or more back-end services running in the cloud to read and process that telemetry.

  1. Click ... > Create IoT Hub at AZURE IOT HUB DEVICES tab, or type Azure IoT Hub: Create IoT Hub in Command Palette.

create hub

  1. Choose your subscription, resource group, and the closest deploy location to you.
  2. For Pricing and scale tier, select the F1 - Free tier if it's still available on your subscription.
  3. Enter the name of your IoT Hub.
  4. Wait a few minutes until the IoT Hub is created. you can see that your devices status become No device in ....

create hub res

Register a device

A device must be registered with your IoT hub before it can connect. You will also need to make a note of its connection string.

  1. Click ... > Create Device at AZURE IOT HUB DEVICES tab, or type Azure IoT Hub: Create Device in Command Palette.
  2. Enter device ID as MyDevice and press Enter.
  3. Wait a few seconds until the new device is created.
  4. Right-click your device and select Copy Device Connection String, the connection string of the selected device will be copy to your clipboard, which looks like Hostname=...=. You will use this Device Connection String later in the quickstart.

create device

Send simulated telemetry and 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 VS Code integrated 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 and replace the value of the connectionString variable in SimulatedDevice.js with the Device Connection String you made a note of previously. Then save your changes to SimulatedDevice.js file.

  3. In the 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

Monitor IoT Hub D2C message in VS Code

While the device sending telemetry to your IoT hub, it is also possible to monitor those message in VS Code.

  1. Right-click your device and select Start Monitoring D2C Message.

start monitor

  1. The monitored messages will be shown in OUTPUT > Azure IoT Hub Toolkit view.
  2. To stop monitoring, right-click the OUTPUT view and select Stop Monitoring D2C Message.

stop monitor

Invoke the direct method

The VS Code IoT Hub Toolkit extension can also connects to the service-side endpoint on your IoT Hub and makes direct method calls to a device through it.

  1. Right-click your device and select Invoke Direct Method.

invoke

  1. Enter the method name SetTelemetryInterval.
  2. Enter the payload 10 as the number of seconds between each telemetry sent.
  3. Results will be shown in OUTPUT > Azure IoT Hub Toolkit view, if success, you will see that the D2C message interval have changed.

invoke res

set interval

Clone this wiki locally