Free for commercial use! A low-code visual development platform for industrial web with over 1500 components.
Essential knowledge!!! Understand Modbus time synchronization in 3 minutes.
Where does HTTP fall short? Why is IoT relying on MQTT!!!
In the Industrial Internet of Things (IIoT), Modbus and MQTT are two common communication protocols, each with its own characteristics: Modbus is used for data communication between devices, while MQTT is a lightweight messaging protocol designed for IoT. So, how can we seamlessly integrate Modbus data into MQTT? Here is a detailed technical implementation guide.
What are Modbus and MQTT?

- Modbus is a simple and easy-to-implement communication protocol widely used in industrial hardware such as PLCs and sensors. It typically transmits data via RS-485, RS-232, or TCP/IP.
- MQTT (Message Queuing Telemetry Transport) is a lightweight publish/subscribe protocol particularly suited for transmitting data over networks with limited bandwidth and resources, such as IoT systems.
By integrating these two protocols, data from industrial devices can be sent to modern IoT platforms, enabling more efficient data monitoring and management.
Technical Implementation Steps

Sending Modbus data to MQTT requires several key steps. Below is a detailed breakdown:
1. Prepare Hardware and Software
Before starting, ensure the following devices and tools are available:
- Devices supporting Modbus protocol (such as PLCs or sensors).
- MQTT Broker (such as Mosquitto, EMQX, etc., for receiving MQTT messages).
- Gateway device or integrated software that supports dual protocols.
Additionally, you may need a single-board computer (like Raspberry Pi) or an industrial PC with a network interface as a gateway.
2. Collect Modbus Data
Typically, you can use a Modbus library or tool to collect data. Here are a few popular options:
- Python + pymodbus: The
<span>pymodbus</span>library in Python can easily communicate with Modbus devices. - Node-RED: Node-RED supports the Modbus protocol and can collect data through simple nodes.
Example code (using Python):
from pymodbus.client.sync import ModbusTcpClient
client = ModbusTcpClient('192.168.0.10') # Modbus device IP
result = client.read_holding_registers(1, 10) # Read registers
print(result.registers)
client.close()
With this code, you can read register data from a Modbus device.
3. Convert and Package Data
Before publishing Modbus data to MQTT, it needs to be processed. The steps include:
- 1.Data format conversion: Convert the binary or register data returned by Modbus into JSON format.
- 2.Package the message: Ensure to include key information such as device ID and timestamp.
For example:
data = {
"device_id": "PLC001",
"timestamp": "2025-04-29T10:00:00",
"temperature": result.registers[0]
}
4. Send Data to MQTT
Use an MQTT client to send the data to the corresponding Topic. You can choose from the following tools:
- paho-mqtt (Python library): Supports easy message publishing.
- Node-RED: Built-in MQTT nodes, suitable for beginners.
Example publish code:
import paho.mqtt.client as mqtt
import json
client = mqtt.Client()
client.connect("MQTT_BROKER_IP", 1883, 60)
mqtt_topic = "modbus/data"
client.publish(mqtt_topic, json.dumps(data))
client.disconnect()
This code publishes the
<span>data</span>object to the MQTT topic<span>modbus/data</span>.
5. Verification and Debugging
After successfully sending data, you can verify it in the following ways:
- Use an MQTT client (such as MQTT Explorer): Confirm whether the data has been successfully transmitted to the specified Topic.
- Set up logging: Add logging in both Modbus and MQTT clients to facilitate troubleshooting.
Sending Modbus data to MQTT is not complicated. By following the steps of “collecting, converting, and transmitting” and choosing the right tools and solutions, you can bridge traditional industrial devices with modern IoT platforms. This not only enhances data utilization but also makes operations smarter and more efficient.
Recent hot articles:
| Essential knowledge! Finally, someone has clarified configuration!!!Hardcore knowledge! How SCADA can control PLCs remotely?Essential knowledge!!! Understand Modbus time synchronization in 3 minutes.Essential knowledge! Finally, someone has clarified the OPC UA address space.Ten years in industrial control!!! Still don’t know the three topologies of RS-485? |