November 11, 2025

How to Send Data to Azure IoT Hub Using Node-RED

Blog Details Image

Connecting industrial equipment to cloud platforms enables real-time monitoring, data analysis, and remote management of factory floor operations. This tutorial demonstrates how to send sensor data from industrial devices to Azure IoT Hub using Node-RED, a visual programming tool that simplifies data flow configuration.

We'll build a complete data pipeline that reads digital input switches from an industrial controller and transmits their status to Azure IoT Hub for cloud-based processing and storage.

System Architecture Overview

This implementation uses several components working together:

Industrial IoT Gateway: An edge device that runs Node-RED natively. This gateway connects to industrial controllers and acts as a bridge between factory equipment and cloud services.

Industrial Controller: An Opto 22 PLC with digital input switches that represent equipment status or sensor conditions.

Azure IoT Hub: Microsoft's cloud service that receives and manages data from IoT devices at scale.

Node-RED: A flow-based programming tool running on the IoT gateway that handles data collection, formatting, and transmission.

Registering Your Device in Azure IoT Hub

Before sending data to Azure, you must register your IoT gateway as a device in Azure IoT Hub. This establishes the device identity and generates security credentials.

Log into the Azure portal and navigate to your IoT Hub resource. Scroll down to find "IoT devices" in the left menu and click it. Click the "New" button to register a new device.

Enter a name for your device that identifies the gateway or its location—for example, "IIoT_Gateway_01" or "Factory_Edge_Device."

For authentication type, leave it set to "Symmetric Key." This method uses shared secret keys for authentication. Enable the option to let Azure auto-generate the primary and secondary keys rather than creating them manually.

Verify that "Connect this device to an IoT Hub" is enabled, then save the configuration. Azure creates your device registration and generates the necessary connection credentials.

Getting Device Connection Information

After creating the device, click on its name to view the connection details. You'll see several pieces of information, but the most important is the primary connection string.

The connection string contains everything needed to connect to Azure IoT Hub:

  • The IoT Hub hostname
  • The device ID
  • The shared access key

Copy the entire primary connection string—you'll need it when configuring Node-RED. Store it temporarily in a text editor where you can easily reference it.

Accessing the Node-RED Editor

To configure your data flow, access the Node-RED editor running on your IoT gateway. Open a web browser and enter your gateway's IP address followed by port 1880. For example: 192.168.1.100:1880

This opens the Node-RED visual programming interface where you'll create the data flow.

Installing the Industrial Controller Package

Node-RED uses packages (also called nodes) to interact with different devices and services. For Opto 22 controllers, you need to install the specific Node-RED package.

Click the menu icon (three horizontal lines) in the upper right corner, select "Manage palette," then click the "Install" tab. Search for the Opto 22 package name and click Install.

After installation completes, the Opto 22 nodes appear in the Node-RED palette on the left side of the editor.

Creating the Data Collection Flow

Build your Node-RED flow by dragging nodes onto the canvas and connecting them together.

Adding a Timer Trigger

Start by dragging an inject node onto the canvas. Double-click it to configure the timing interval. Set it to trigger every 5 seconds to read data regularly without overwhelming the controller or network.

Configuring Industrial Controller Reads

Scroll through the node palette to find the Opto 22 read node you just installed. Drag it onto the canvas.

Double-click the node to configure it. Click "Add new pack device" to create a connection to your controller. Enter your industrial controller's IP address.

The Opto 22 controller uses API keys for authentication. Enter the API key name and value that were pre-configured on your controller.

After adding the device, select "Digital Input" as the input type you want to read. Enter the exact tag name as it appears in your controller configuration.

Give the node a descriptive display name like "Switch 1" to identify which input it reads.

If you have multiple digital inputs to monitor, copy this node and modify the tag name and display name for each additional input. For example, create a second node for "Switch 2" with its corresponding tag name.

Connect all your read nodes to the inject node so they all trigger simultaneously every 5 seconds.

Formatting Data for Azure IoT Hub

The data from your controller needs to be formatted as JSON messages that Azure IoT Hub can process. Use function nodes to perform this conversion.

Drag a function node onto the canvas and double-click it to edit the code. Create a JSON object with the following structure:

javascript

{
   "deviceId": "[your device ID]",
   "key": "[your shared access key]",
   "protocol": "mqtt",
   "data": msg.payload
}

To populate the deviceId and key fields, reference the primary connection string you copied earlier:

The device ID is the part after "DeviceId=" in the connection string. Copy this portion and paste it into the deviceId field.

The key is the long string after "SharedAccessKey=" in the connection string. Copy this portion and paste it into the key field.

Leave the protocol as "mqtt" (the standard messaging protocol for IoT devices) and set the data field to msg.payload (which contains the input switch status).

Name this function node something descriptive like "Prepare Azure Message." Create a copy of this function node for each digital input you're reading.

Connect each controller read node to its corresponding function node.

Installing the Azure IoT Hub Package

Node-RED needs an Azure-specific package to communicate with IoT Hub. Go to the menu again, select "Manage palette," navigate to the Install tab, and search for "node-red-contrib-azure-iot-hub."

Click Install to add this package to your Node-RED installation. Once installed, you'll find Azure IoT Hub nodes in your palette.

Configuring the Azure IoT Hub Connection

Drag the Azure IoT Hub node onto the canvas. Double-click it to configure the connection settings.

Select "MQTT" as the protocol—this matches what you specified in the function nodes.

For the hostname, return to your primary connection string. The hostname is the part after "HostName=" and before the semicolon. It looks like: your-hub-name.azure-devices.net

Copy this hostname and paste it into the hostname field in the Azure IoT Hub node configuration. Click Done.

Connect both (or all) of your function nodes to this single Azure IoT Hub node. Multiple data sources can share the same Azure connection.

Deploying the Data Flow

Your complete flow now reads digital inputs from the industrial controller, formats them as JSON messages with authentication credentials, and sends them to Azure IoT Hub.

Click the "Deploy" button in the upper right corner to activate your flow. Node-RED starts executing the flow, reading inputs every 5 seconds and transmitting them to Azure.

You should see "sent message" status indicators on the Azure IoT Hub node, confirming data is being transmitted.

Verifying Data Transmission

To confirm messages actually reach Azure IoT Hub, use the Device Explorer utility provided by Microsoft. This tool monitors data flowing into your IoT Hub.

Open Device Explorer and enter your IoT Hub connection string—not the device connection string, but the hub-level connection string. Find this in the Azure portal under your IoT Hub resource. Click "Shared access policies," select the "iothubowner" policy (which has read and write permissions), and copy the primary connection string.

Paste this into Device Explorer and click Update. The tool connects to your IoT Hub.

Navigate to the Management tab to see all registered devices. Switch to the Data tab, select your device from the list, and click the Monitor button.

Device Explorer displays messages as they arrive at Azure IoT Hub. You should see your digital input states appearing every 5 seconds. If both switches are closed, the messages show "true" for both inputs.

Test the system by opening or closing your physical switches on the controller and watching the values change in Device Explorer.

Data Flow Summary

The complete data flow works as follows:

  1. The inject node triggers every 5 seconds
  2. Read nodes query the industrial controller for digital input states
  3. Function nodes convert the raw data into Azure-compatible JSON messages with authentication
  4. The Azure IoT Hub node transmits messages using MQTT protocol
  5. Azure IoT Hub receives and stores the data for further processing

Applications for Industrial IoT Data Transmission

This architecture enables various industrial use cases:

  • Monitoring equipment status and operational state
  • Tracking production line conditions and downtime events
  • Collecting alarm and safety system status
  • Aggregating data from multiple machines for analytics
  • Enabling remote equipment monitoring and diagnostics

Summary

Connecting industrial controllers to Azure IoT Hub with Node-RED provides a flexible, visual approach to building IoT data pipelines. The process involves registering devices in Azure, installing the necessary Node-RED packages, configuring data read operations, formatting messages with proper authentication, and establishing the cloud connection.

Node-RED's visual programming interface makes it easy to see the complete data flow, troubleshoot issues, and make changes without writing extensive code. Once deployed, the system continuously streams industrial data to the cloud where it can be analyzed, stored, and integrated with other enterprise applications.