Using Node-RED to Send Sensor Data to Azure

Getting Started

In this tutorial we will set up a simple Node-RED flow that sends Wireless Sensor telemetry to an Azure IoT Device using device-to-cloud messages. This tutorial assumes you have a basic understanding of Node-RED and Microsoft Azure, and have set up your Node-RED Wireless Sensor Network, and configured an Azure IoT Hub with an IoT Device.

While this same mechanism could be used to send telemetry to multiple IoT Devices using different credentials, we’re going to focus on building a “gateway” solution, whereby the sensor data is sent as messages to a single IoT Device acting as a gateway. The rich ecosystem of Azure tools should allow you to use this data in any way you need to.

Install the packages

This flow will only require 2 packages, ncd-red-wireless, and node-red-contrib-azure-iot-hub. Since you should already have installed the ncd-red-wireless package, make sure Node-RED is running, and click on the hamburger (≡) in the top-right of the flow editor, and click “Manage Palette”. In the pane that appears, click the “Install” tab, type node-red-contrib-azure-iot-hub into the provided search box, then click the “Install” button next to the package. Once this is finished you are ready to start building your flow!

Build your flow!

In the last tutorial, we introduced the Wireless Gateway, and Wireless Sensor nodes. In this tutorial we will just stick with the Wireless Gateway node since it captures and outputs data from all sensors that are currently powered.

Drag a gateway node into the flow, and configure it. This node will send out complete packets with all of the sensor info, including the telemetry data that we will pass to Azure. Next, get an Azure IoT Hub node, and position it to the right of the gateway node, leaving room for us to put another node in between. The IoT Hub Node only has a couple of configuration options, for this tutorial we went with the MQTT protocol, but the setup should be the same for any protocol you’d like to use. Enter your IoT Hub hostname in the provided field and click “Done”.

The IoT Hub node expects the credentials for your IoT Device, namely the deviceId and primary/secondary key, to be sent in as part of the payload. To achieve this we’ll use a function node, grab one and place it in between the gateway node and the IoT Hub node. The content of this node is relatively simple, we’re just building a new payload and assigning the pertinent data from the gateway node to a key called “data”. Copy and paste the below code into the function node, replacing the deviceId and key with the appropriate values for your IoT Device.

msg.payload = {
"deviceId": "node_red_demo",
"key": "7bBA0fsdf4AHP+o53uykyuk2SpwttN1zHx6w=",
"protocol": "mqtt",
"data": msg.payload
return msg;



Test the Data Flow

What we have right now should send data to Azure using the device-to-cloud message paradigm, but out of the box the Azure portal doesn’t provide a way to actually SEE those messages coming through. To ensure that we are actually sending the data correctly, we can use the Azure IoT Hub Receiver node. Drag one out below the Wireless Gateway node. The only configuration option this node has is “connectionString”. 

The connection string it refers to is actually the iothubowner credential. You can get this by going to your IoT Hub on Azure, clicking on “Shared Access Policies”, and then clicking on iothubowner. Copy the connection string in the pop out into your node configuration and click Done. This node will output messages sent to the IoT hub as they are received, the easiest way to view these is to use a Debug node, grab on and add it to the right of the IoT Hub Receiver node and wire them together.

If you have done everything correctly you should have a flow that looks like the one here, and you can deploy it. Depending on the setting in your sensor, it could be a long wait to see telemetry, you can speed this up by simply pressing the physical reset button on the side of the sensor. In the right hand pane on Node-RED, click on the debug tab (depending on your version it may be a bug icon). You should see data flowing! If you don’t check out the note below the article.


That’s all there is to it! We’ve been extremely happy with our experience using Node-RED in conjunction with our Wireless Sensors, and being able to tie into Azure this quickly is a wonderful example of the power and simplicity these tools provide. In the last tutorial we introduced the Node-RED Dashboard as a means to locally monitor sensor data as it is received, we hope that you’ll view these articles as starting points, and continue to build even more amazing utilities and connections with these building blocks!

TLDR; Import this flow

If you already understand Node-RED and Azure and just need the nuts and bolts without the explanations provided above, here is an export of the flow we just built. Remember to configure the nodes (ALL of them require some configuration other than the debug node). 

[{"id":"18041689.e37eb9","type":"ncd-gateway-node","z":"143a8b51.beb3a5","name":"","connection":"","x":250,"y":960,"wires":[["89a29a36.873408"]]},{"id":"c02de5f.1726f18","type":"azureiothub","z":"143a8b51.beb3a5","name":"Azure IoT Hub","protocol":"mqtt","x":740,"y":960,"wires":[[]]},{"id":"89a29a36.873408","type":"function","z":"143a8b51.beb3a5","name":"","func":"msg.payload = {\n  \"deviceId\": \"your_device_id\",\n  \"key\": \"your+primary+or+secondary+device+key\",\n  \"protocol\": \"mqtt\",\n  \"data\": msg.payload\n};\nreturn msg;","outputs":1,"noerr":0,"x":510,"y":960,"wires":[["c02de5f.1726f18"]]},{"id":"a01e83b9.f149b","type":"azureiothubreceiver","z":"143a8b51.beb3a5","name":"Azure IoT Hub Receiver","x":260,"y":1140,"wires":[["6ee471be.70eca"]]},{"id":"6ee471be.70eca","type":"debug","z":"143a8b51.beb3a5","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":730,"y":1140,"wires":[]}]

There is a known issue on some platforms where the Wireless Gateway node loses connectivity to the USB port, it doesn’t happen while the flow is running, it seems to be related to the serial port not being properly managed in certain situations during flow deployment. If you click on the button on the left side of the Wireless Gateway node, you should see a status indicator under it that says “configuring”, clicking it again will return it to “ready”. If this doesn’t work, try stopping and restarting node-red from the shell.