Marcio Cunha

How to build an industrial IoT gateway with Node-RED and MQTT

Learn how to build an edge IoT gateway using Node-RED and MQTT to turn raw sensor data into clean, structured payloads.

Marcio Cunha•5 min
Also available in:PortuguêsEspañol
Summary
  • Edge gateways reduce network traffic by processing raw sensor data locally.
  • Node-RED accelerates automation flow creation without requiring complex coding.
  • The MQTT protocol ensures lightweight and efficient communication for constrained devices.
  • Standardizing payloads improves integration with enterprise systems and databases.
  • Ensuring local redundancy and security prevents catastrophic plant failures.

Why use an industrial IoT gateway at the edge?

In modern engineering, connecting the factory floor to corporate systems requires tools that process information before sending it to the cloud. This is where edge gateways come in—small computers or microcontrollers installed physically close to machines to gather data without clogging the main network. In practice, this means that instead of sending thousands of raw readings per second, the gateway filters out the noise and makes quick decisions locally. Furthermore, if the internet connection drops, the equipment keeps running and temporarily stores data, ensuring no critical history is lost.

To build this bridge quickly, visual flow tools have become indispensable in the automation ecosystem. If you have ever worked with legacy hardware integration, you know dealing with old protocols is a constant headache. However, modern software architecture concepts make communication much smoother, much like we discussed when exploring how to read industrial sensors with ESP32 via Modbus TCP to unify different equipment brands into a single dashboard.

Setting up the development environment in Node-RED

Node-RED is a visual programming tool originally developed by IBM to wire together hardware devices, APIs, and online services using block-based flows. In practice, it works like a giant Lego board where you drag input, processing, and output nodes, wiring them together to create logic without writing hundreds of lines of code. To get started, you can install it on a mini PC, a factory Raspberry Pi, or even run it inside Docker containers on your local infrastructure. The first practical step is to prepare the server and make sure the essential communication packages are installed and ready for use.

After the basic installation, the next step involves adding the libraries needed to interact with industrial protocols and the MQTT broker that will handle message delivery. To perform this preparation cleanly and neatly in your Linux terminal, run the commands below:

sudo apt update && sudo apt upgrade -y
npm install -g --unsafe-perm node-red
node-red-admin install node-red-contrib-modbus
node-red-admin install node-red-node-mqtt

With the nodes properly installed, the Node-RED web interface will be accessible in your browser, allowing you to start drawing data flows visually and intuitively.

Transforming raw sensor data into structured payloads

Traditional industrial sensors send raw numbers, such as a raw voltage or a hex temperature that means very little on its own. For these numbers to be useful in a database or monitoring dashboard, the gateway needs to turn them into a structured payload—usually in JSON format—containing the sensor ID, current reading, timestamp, and unit of measurement. In practice, this prevents downstream systems from having to guess the context of each received number, standardizing all plant telemetry consistently.

Inside Node-RED, this transformation happens in a function node that executes a small block of JavaScript code to map the variables. The code snippet below demonstrates exactly how to receive raw data and convert it into a JSON object ready for transmission:

let rawValue = msg.payload;
let temperatureCelsius = (rawValue * 0.1) - 15;

msg.payload = {
    deviceId: "compressor_01",
    metric: "temperature",
    value: parseFloat(temperatureCelsius.toFixed(2)),
    unit: "C",
    timestamp: new Date().toISOString()
};

return msg;

This clean, standardized format ensures any consuming system can interpret the information immediately, facilitating subsequent analysis and automated alarm triggers.

Transmitting messages with the MQTT protocol

With data already structured into clean JSON packages, it is time to send it across the rest of the network using the MQTT protocol. MQTT is an extremely lightweight messaging protocol designed specifically for unstable networks and resource-constrained devices, operating under a publish-subscribe model. In practice, sensors publish information to specific topics—like 'factory/line1/temperature'—and any system interested in that data simply subscribes to the topic to receive it instantly. This decoupled architecture eliminates the need for complex point-to-point connections, dramatically reducing bandwidth consumption.

Choosing a solid network infrastructure is crucial to ensure these messages arrive without noticeable delays, especially when dozens of machines are sending data simultaneously. If you notice instability or delays in your factory network readings, it is worth checking advanced traffic strategies, such as the guide explaining how to configure QoS and fq_codel to prioritize critical automation packets over ordinary traffic.

Security and edge resiliency best practices

Building an industrial IoT gateway requires close attention to physical and cybersecurity, as the factory floor is a hostile environment full of electromagnetic interference and potential access breaches. The first golden rule is never to expose the MQTT broker directly to the public internet without TLS encryption and robust authentication based on certificates or strong passwords. In practice, this prevents intruders from intercepting commands sent to machines or injecting false readings capable of disrupting entire industrial processes.

Beyond network security, hardware resiliency is a decisive factor in avoiding constant technician visits to the production line. Using read-only file systems when possible, configuring automatic restart policies for containers, and keeping frequent backups of Node-RED flows ensure the system recovers on its own in case of sudden power outages.

Final considerations

Building an industrial IoT gateway using Node-RED and MQTT represents a highly efficient approach to modernizing manufacturing plants without requiring astronomical investments in closed proprietary platforms. By processing data at the edge and structuring consistent payloads, engineers and operators gain real-time visibility, agility in predictive maintenance, and full control over the company's data infrastructure. The secret to success lies in planning network topology well, ensuring communication security, and keeping flows clean and modular.

With the ongoing expansion of Industry 4.0, mastering open-source tools and lightweight protocols is no longer just a technical differentiator—it is a basic requirement for anyone looking to build robust, scalable, and future-proof systems. Start by deploying the gateway on a single pilot machine, validate the stability of the MQTT flow, and gradually expand to the rest of the plant as you gain confidence in your chosen architecture.