GithubHelp home page GithubHelp logo

marceloofernandes / send-telemetry-node.js Goto Github PK

View Code? Open in Web Editor NEW
0.0 1.0 0.0 5.12 MB

Quickstart: Send telemetry from a device to an IoT hub and read the telemetry from the hub with a back-end application (Node.js)

send-telemetry-node.js's Introduction

Send-telemetry-Node.js-

Quickstart: Send telemetry from a device to an IoT hub and read the telemetry from the hub with a back-end application (Node.js).

This document presents the results of the following hands-on lab about Azure IoT from Microsoft and is available at https://docs.microsoft.com/en-us/azure/iot-hub/quickstart-send-telemetry-node.

Register a device

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

Note: replace YourIoTHubName by barueri-test-hub which is the IoT Hub created for this experiment.

az extension add --name azure-cli-iot-ext az iot hub device-identity create --hub-name YourIoTHubName --device-id MyNodeDevice

az extension add --name azure-cli-iot-ext
az iot hub device-identity create --hub-name barueri-test-hub --device-id MyNodeDevice         

alt text

  1. Run the following commands in Azure Cloud Shell to get the device connection string for the device you just registered:
az iot hub device-identity show-connection-string --hub-name barueri-test-hub --device-id MyNodeDevice --output table     

alt text

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

HostName=barueri-test-hub.azure-devices.net;DeviceId=MyNodeDevice;SharedAccessKey=1DrstUh8tUnp8ndhobjaMYo4mbN4gYhm5pkdtzg2dms=
  1. You also need a service connection string to enable the back-end application to connect to your IoT hub in order to retrieve the messages. The following command retrieves the service connection string for your IoT hub:
az iot hub show-connection-string --hub-name barueri-test-hub --output table       

alt text

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

HostName=barueri-test-hub.azure-devices.net;SharedAccessKeyName=iothubowner;SharedAccessKey=6iwF5p2PNqZrBxejs3FZPNlfPkznrUton9Dlnp0Kz/Y=

Send simulated telemetry

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

alt text

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

alt text

  1. 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

alt text

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

alt text

Read the telemetry from your hub

  1. Open another local terminal window, navigate to the root folder of the sample Node.js project. Then navigate to the iot-hub\Quickstarts\read-d2c-messages folder.

alt text

  1. Open the ReadDeviceToCloudMessages.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 ReadDeviceToCloudMessages.js file.

alt text

  1. In the local terminal window, run the following commands to install the required libraries and run the back-end application:
npm install
node ReadDeviceToCloudMessages.js

alt text

The following screenshot shows the output as the back-end application receives telemetry sent by the simulated device to the hub:

alt text

Summary

The hands-on activity was successfully complete.

send-telemetry-node.js's People

Contributors

marceloofernandes avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.