platform | device | language |
---|---|---|
debian, fedora, Linux, opensuse, raspbian, Ubuntu, windows, yocto |
any |
javascript |
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
You should have the following items ready before beginning the process:
- Computer with Git client installed and access to the azure-iot-sdks GitHub public repository.
- Prepare your development environment.
- Setup your IoT hub
- Provision your device and get its credentials
- Make sure desktop is ready as per instructions given on Prepare your development environment.
-
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.
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.
Visual Studio Code provides an excellent environment to write and debug Node.js code: