Skip to content

Latest commit

 

History

History
86 lines (61 loc) · 3.33 KB

node-run-sample.md

File metadata and controls

86 lines (61 loc) · 3.33 KB
platform device language
debian, fedora, Linux, opensuse, raspbian, Ubuntu, windows, yocto
any
javascript

Run a simple Node.js sample on device


Table of Contents

Introduction

About this document

This document describes how to build and run the simple_sample_http.js Node.js sample application. This multi-step process includes:

  • Configuring Azure IoT Hub
  • Registering your IoT device
  • Build and deploy Azure IoT SDK on device

Step 1: Prerequisites

You should have the following items ready before beginning the process:

Step 2: Prepare your Device

Step 3: Build and Run the sample

  • Get the following sample files from https://github.com/Azure/azure-iot-sdks/tree/master/node/device/samples

    • package.json
    • simple_sample_device.js
  • Place the files in the folder of your choice on the target machine/device

  • Open the file simple_sample_device.js in a text editor.

  • Locate the following code in the file:

    var connectionString = '[IoT Device Connection String]';
    
  • Replace [IoT Device Connection String] with the connection string for your device. Save the changes.

  • From the shell or command prompt you used earlier to run the iothub-explorer utility, use the following command to receive device-to-cloud messages from the sample application (replace with the ID you assigned your device earlier):

    node iothub-explorer.js "<iothub-connection-string>" monitor-events <device-id>
    
  • Open a new shell or Node.js command prompt and navigate to the folder where you placed the sample files. Run the sample application using the following commands:

    npm install
    node simple_sample_device.js
    
  • The sample application will send messages to your IoT hub, and the iothub-explorer utility will display the messages as your IoT hub receives them.

Experimenting with various transport protocols

The same sample can be used to test AMQP, AMQP over Websockets, HTTP and MQTT. In order to change the transport, uncomment whichever you want to evaluate in the require calls on top of the sample code and pass it to the call to Client.fromConnectionString() when creating the client.

Debugging the samples (and/or your code)

Visual Studio Code provides an excellent environment to write and debug Node.js code: