Connecting Node-Red to Azure IoT Hub using MQTT nodes.

Nikhil Kinkar
5 min readFeb 12, 2021

--

Recently while working on a project I came across node-red, it is really cool for working on IoT, you don’t need to scratch your head and write every part of code. If you have used it you are well aware of its capabilities, if you are new to node-red give it some time, you will enjoy the journey.

So back to the topic, as we come across node-red and wanted to connect to Azure IoT Hub for sending telemetry data over the cloud, after some research I found out the contrib package for the same, it was node-red-contrib-azure-iot-hub, here we multiple protocols are available for connections.

While working with this package we faced some issues as shown below. One of which was the when we used the AMQP protocol for connection (Later we discovered, it was due to the account tier).

Node-Red debug output.

Another one was using mqtt, one may face the following kind of issue, I saw this one few time was not sure from where it emerged.

TypeError: this.mqttprovider.connect is not a function

So these are few issues that I personally faced and decided to look for another approach to connect to cloud. After taking some good searching efforts I found a way to deal with MQTT nodes along with Azure. Below is a step by step guide that will help anyone who is looking for this kind of alternate solution for connecting Azure IoT Hub with node-red.

Note: Before testing Device to Cloud and Cloud to Device please make sure that you have appropriate account tier, with required capabilities.

Device to Cloud connection using MQTT Out node.

Create the sample flow as shown in below figure, using an inject node and mqtt out node.

Basic flow to setup Azure connection from Mqtt out node.

Edit the MQTT Out node, and create a new server configuration. In server configuration edit window. Add the fields as follows.

Server: — ssl://YOUR-IOT-HUB.azure-devices.net (Hostname with ssl:// as prefix)

Port :- 8883

Client Id: — YOUR-Device (This will be the device name).

Enable secure (SSL/TLS) connection. Leave the other fields of TLS Configuration as blank and add.

Azure server connection setup.

Go to Security Tab, and add Username as YOUR-IOT-HUB.azure-devices.net/YOUR-Device (combination of hostname/device). Password will be generated from Azure Portal’s integrated Console/CLI as follows.

Login to Azure Portal and open the online CLI window from the top navbar. Within the console give the command to generate SAS Token. Enter following command in Azure CLI.

$ az iot hub generate-sas-token -d YOUR-Device -n YOUR-IOT-HUB.azure-devices.net — du 172800

This command will generate the sas token as follows.

{
“sas” : “SharedAccessSigature sr=YOUR-IOT-HUB.azure-devices.net%2Fdevices%2FYOUR-Device&sig=%2BSomeRandomSignatureStringGoesHere&se=161314987245”
}

Copy the “sas” value starting from SharedAccessSignature till end from CLI and paste it in the password field.

Security Setting for Azure Server setup.

Add the server details and get back to edit screen of mqtt out node. Finally add the topic to the topic field in below given format, you can setup QoS and Retian fields as per requirements, I kept it as it is.

devices/YOUR-Device/messages/events/

MQTT out node settings.

Now the flow is ready add sample string to the inject node as follows.

Set sample data in inject node.

Trigger the flow from inject node.

Flow ready for testing Device to Cloud communication.

And the output can be seen in cloud side, for checking the telemetry connection I used the Azure Device Explorer, there are other ways also for it (One alternative was Azure Extention for Visual Studio Code).

Azure Device Explorer Telemetry Data Screen
Azure Device Explorer Output For Telemetry Data.

This is the complete flow for device to cloud. Next we have another flow for cloud to device.

Cloud to Device connection using MQTT In node.

For this we can use the same server setting we did in above section. For connecting from cloud to device, we need to set us the mqtt in node, create a sample flow as follows.

Basic flow to setup Azure connection from Mqtt in node.

Now configure the mqtt in node as follows, by adding the topic, QoS and output fields were kept as it is, default QoS as set to 2 I didn’t changed it.

devices/YOUR-Device/messages/devicebound/#

Azure Mqtt in node setup.

Now the flow is ready to accept the incoming communication.

Flow ready for testing Azure Cloud to Device communication.

From the Cloud side send a message and check in node-red debug console for the incoming message. I have used the same Azure Device Explorer for it.

Azure Device Explorer Cloud-to-device message window.
node-red debug tab output.

These were the basic flows that I have created for communicating with Azure IoT Hub using MQTT, it took some good efforts for finding this solution but it was worth it. These flow I have tested they work fine without any issues (as of now I haven’t faced any issue in this approach). Hope this helps you guys in case you are looking for the simple solution for connection to Azure with MQTT.

References :

Azure IoT Tutorial : https://youtu.be/k3Gvk1cjwhI

Choose the right IoT Hub tier for your solution : https://docs.microsoft.com/en-us/azure/iot-hub/iot-hub-scaling

Communicate with your IoT hub using the MQTT protocol : https://docs.microsoft.com/en-us/azure/iot-hub/iot-hub-mqtt-support#receiving-cloud-to-device-messages

Control access to IoT Hub : https://docs.microsoft.com/en-us/azure/iot-hub/iot-hub-devguide-security#use-sas-tokens-in-a-device-app

Send telemetry from a device to an IoT hub and read it with a back-end application (Node.js) : https://docs.microsoft.com/en-us/azure/iot-hub/quickstart-send-telemetry-node

az iot hub CLI Reference : https://docs.microsoft.com/en-us/cli/azure/ext/azure-cli-iot-ext/iot/hub?view=azure-cli-lates

Visual Studio Code Azure Extensions : https://code.visualstudio.com/docs/azure/extensions

--

--