How to Integrate Modbus TCP Sensors with Grafana Using Node-RED
Learn step by step how to read industrial PLC data using Modbus TCP, publish via MQTT with Node-RED, and display real-time dashboards in Grafana.
Summary
- The Modbus TCP protocol acts as a universal translator enabling computers and PLCs to talk over standard computer networks.
- Node-RED serves as the visual flow tool that collects factory floor data without requiring complex code from scratch.
- The MQTT protocol works like a lightweight courier, ideal for delivering real-time sensor messages to any interested system.
- Grafana transforms raw sensor numbers into intuitive visual dashboards that simplify monitoring industrial machine health.
- Building a modular architecture based on decoupled MQTT topics ensures ease of maintenance and high scalability on the factory floor.
The challenge of connecting the factory floor to the cloud
In modern industrial environments, extracting data from legacy and new machinery is the first step toward optimizing production. Often, these devices speak proprietary languages or traditional protocols that are hard to access directly using modern visualization tools. To solve this, we build efficient data bridges that transform electrical signals into readable web metrics, much like designing a resilient event bus architecture for distributed systems.
The ecosystem uniting Modbus TCP, Node-RED, MQTT, and Grafana solves this pain point with elegance, low cost, and high reliability. The PLC, or Programmable Logic Controller, is the electronic brain controlling motors, conveyor belts, and valves on the factory floor. It stores variables in internal registers that can be read via direct network requests using the Modbus TCP protocol.
Understanding the reading and transport architecture
Modbus TCP is an evolution of classic Serial Modbus, adapted to run over standard TCP/IP networks. It operates on a request-response model, where the computer requests the value of a specific address and the PLC replies with the raw data collected from sensors. In practice, this means we need a system that repeatedly asks the PLC these questions without choking the network.
This is precisely where Node-RED comes in, a flow-based visual programming tool that simplifies the integration of APIs, hardware, and databases. With visual blocks dragged onto the canvas, we can easily configure nodes that request data from the PLC via Modbus at regular intervals. Furthermore, before forwarding this data, we can apply filtering logic similar to the automated routines we use when auditing data flows in infrastructure pipelines.
Configuring Node-RED to interact with the PLC
To start pulling data from the PLC, we need to install the appropriate communication nodes in the Node-RED environment. The process involves connecting a time injection block to a Modbus read block, pointing to the industrial controller's IP address. Each Modbus register has a specific number corresponding to a temperature, pressure, or motor state.
Below is an example helper script in JavaScript that Node-RED can execute to process the raw packet received from the PLC before publishing it to the network:
// Example Node-RED processing script for Modbus TCP data
msg.payload = {
sensor_id: 'boiler_temp_01',
raw_value: msg.payload[0],
temperature_celsius: (msg.payload[0] * 0.1).toFixed(2),
timestamp: new Date().toISOString()
};
return msg;
This initial processing ensures that any electrical fluctuation or out-of-range data is cleaned up before moving forward. In practice, we save bandwidth and prevent polluting the database with unnecessary noise.
Publishing industrial metrics via the MQTT protocol
As soon as Node-RED extracts and cleans the PLC data, we need to send it to a lightweight broker that distributes this information to anyone listening. MQTT fulfills this role perfectly, functioning as a topic-based messaging system where devices publish and subscribe to updates instantly.
We configure Node-RED to send the processed JSON object to a local MQTT broker, such as Mosquitto, using a clean and hierarchical topic structure. For example, publishing to a structure like factory/line1/kiln/temperature allows any application or database to listen to that specific channel without overloading the PLC with hundreds of simultaneous direct connections.
Storing data for history and analysis
Although MQTT is great for real-time transport, it is not built for long-term history storage. To power charts showing factory behavior over days or weeks, we need to feed these MQTT messages into a time-series optimized database like InfluxDB or TimescaleDB.
We create a simple flow in Node-RED that listens to the kiln's MQTT topic and writes the received value directly into the database along with the exact date and time. This step ensures that if the visualization dashboard drops for a moment, no precious historical data is lost in the process.
Visualizing everything in real-time with Grafana
With data stored in a structured manner, it is time to build the ultimate visual dashboard in Grafana. We connect Grafana to our time-series database and create line charts, gauge meters, and visual alerts based on critical temperature or pressure thresholds.
Grafana allows configuring automatic updates every second, giving plant operators a surgical and instantaneous view of factory floor events. If a motor's temperature exceeds the safe limit, the panel changes color immediately, enabling corrective actions before unwanted production downtime occurs.
Final thoughts on industrial monitoring
Integrating Modbus TCP sensors with modern dashboards using Node-RED, MQTT, and Grafana democratizes access to industrial data without requiring millions in proprietary software investments. This modular approach ensures resilience, flexibility, and total control over your company's telemetry infrastructure, modernizing legacy operations with agile open-source tools.