on
2022-11-02
7:26 AM
- edited on
2025-08-01
2:35 AM
by
Laurids_PETERSE
In this article, you learn how to visualize real-time use data that your IoT hub receives with a Node.js web app running on your local computer.
In this article, we use an IoT Hub, which has the B-U5U5I-IOT20A board connected to it. B-U585I-IOT20A board consists of a low power microcontroller and sensors such as temperature and humidity, a magnetometer, accelerometer, gyroscope, pressure, Time-of-Flight, and gesture-detection. It also provides a Wi-Fi module, which allows us to connect it to Azure IoT Hub.
The board can be purchased here.
Note: Before starting this tutorial, you set up your Azure IoT device and IoT hub, link for which is given in the prerequisites section.
Run az version to find the version and dependent libraries that are installed. To upgrade to the latest version, run az upgrade.
Consumer groups provide independent views into the event stream that enable apps and Azure services to consume independently data from the same Event Hub endpoint. In this section, you add a consumer group to your IoT hub's built-in endpoint that the web app uses to read data from.
Run the following command to add a consumer group to the built-in endpoint of your IoT hub:
Azure CLI-
az iot hub consumer-group create --hub-name YourIoTHubName --name YourConsumerGroupName
Note down the name you choose, you will need it later in this tutorial.
Open a command window, and enter the following commands to download the sample from GitHub and change to the sample directory:
Cmd
git clone https://github.com/Azure-Samples/web-apps-node-iot-hub-data-visualization.git
cd web-apps-node-iot-hub-data-visualization
Examine the web app code
From the web-apps-node-iot-hub-data-visualization directory, open the web app in your favorite editor( I have used Visual Studio Code here for which you need to write code. ). The following shows the file structure viewed in VS Code:
Take a moment to examine the following files:
To read data from your IoT hub, the web app needs your IoT hub's connection string and the name of the consumer group that it should read through. It gets these strings from the process environment in the following lines in server.js:
JavaScript
const iotHubConnectionString = process.env.IotHubConnectionString;
const eventHubConsumerGroup = process.env.EventHubConsumerGroup;
Set the environment variables in your command window with the following commands. Replace the placeholder values with the service connection string for your IoT hub and the name of the consumer group that you created previously. Do not quote the strings.
Cmd
set IotHubConnectionString=YourIoTHubConnectionString
set EventHubConsumerGroup=YourConsumerGroupName
Cmd
npm install
npm start
Open a browser to http://localhost:3000.
In the Select a device list, select your device to see a running plot of the last 50 temperature and humidity data points sent by the device to your IoT hub.