Modbus to MQTT
PLCGateway polls Modbus TCP and RTU devices – energy meters, drives, PLCs, sensors – and publishes their registers and coils to an MQTT broker as named, scaled values.
- Modbus TCP / RTU
modbus_tcpmodbus_rtu_tcpmodbus_rtu- MQTT
Modbus devices answer only when asked, and what they return is raw register content. An IoT platform wants the opposite: named values that arrive by themselves. PLCGateway polls the registers, combines 32- and 64-bit values with the right word order, applies scaling and publishes each tag when it changes.
Modbus TCP, RTU over TCP and RTU on a serial line (RS-485 / RS-232) are supported in the same installation, so a serial energy meter and an Ethernet PLC end up on the same broker. Consecutive registers are read in one request.
- Source
- Modbus TCP / RTU
modbus_tcpmodbus_rtu_tcpmodbus_rtu - Connection
- TCP 502 · RTU over TCP · RS-485 / RS-232
- Target
- MQTT
- Writing
- yes
Set it up in five steps
Add a device, choose Modbus TCP, RTU over TCP or RTU serial and enter the IP address or the serial port with baud rate and parity.
Set the unit id and, for 32-bit values, the word order of the device.
Create the tags with their addresses and data types, or import a register list from CSV; test the connection to see the first values.
Open Gateway → MQTT, enter the broker address, port and login, switch on TLS if the broker requires it, and choose the base topic.
Choose publishing on change or at an interval and the JSON or plain-value format, then check the topics with mosquitto_sub or in your platform.
Example: topics and payloads
A device with the id meter1 and these tags is published as shown below; the lines are what mosquitto_sub -v prints. Names and values are examples.
| Tag | Address | Example value |
|---|---|---|
ActivePower | HR100 | 42.75 kW |
Energy | HR200 | 128450.5 kWh |
PumpRunning | C12 | true |
MQTT topics and payloads
plcgateway/status online
plcgateway/meter1/ActivePower {"q":"GOOD","t":1794465246839,"u":"kW","v":42.75}
plcgateway/meter1/Energy {"q":"GOOD","t":1794465246839,"u":"kWh","v":128450.5}
plcgateway/meter1/PumpRunning {"q":"GOOD","t":1794465246839,"v":true}
Topic: base topic / device id / tag name. The JSON payload carries the value (v), the quality (q), the timestamp in milliseconds (t) and the unit (u).
The MQTT publisher in PLCGateway
PLCGateway publishes every tag to your MQTT broker – Mosquitto, EMQX, HiveMQ, AWS IoT Core or a cloud platform behind a bridge – so IT systems subscribe to topics instead of polling the controller. It speaks MQTT 3.1.1 over TCP or TLS, with user name and password.
Values are sent when they change or at a fixed interval, as JSON with quality and timestamp or as the bare value. A retained status topic with a last will tells subscribers whether the gateway is online.
- One topic per tag: base topic / device / tag, with a base topic of your choice
- QoS 0 or 1, retained messages, birth message and last will
- Optional writing: a value published to the tag's /set topic is written to the controller
- Messages are queued while the broker is unreachable and sent after the reconnect
Controllers and devices
Equipment that is typically connected with this driver.
Modbus TCP / RTU
Registers, coils and discrete inputs over TCP, RTU over TCP or serial RS-485.
Learn more →Schneider Electric Modicon
Modicon M580, M340, Quantum, Premium, M221, M241 / M251 / M262
Learn more →CODESYS-based controllers
WAGO PFC200, ABB AC500, Modicon M262, Eaton XC300, Festo CPX-E, soft PLCs
Learn more →Building automation and energy meters
Desigo, Metasys, Honeywell, EcoStruxure, chillers, AHUs, energy meters
Learn more →Questions and answers
Does it work with Modbus RTU over RS-485?
Yes. Use a serial port or USB adapter of the computer, or an Ethernet-to-serial converter with RTU over TCP. Baud rate, parity and unit id are set per device.
How are 32-bit floats handled?
The data type of the tag defines the width, and the word order (ABCD, CDAB, BADC, DCBA) is chosen per device or per tag, so the published value is the real number, not two raw registers.
Can I write a register through MQTT?
Yes, when writing is enabled: a value sent to the tag's /set topic is written to the holding register or coil.
Connect your machines
Tell us which controllers you run; we show you PLCGateway with your own addresses.
Product and model names are trademarks of their owners. They are named only to show which equipment the drivers communicate with; PLCGateway is not affiliated with these companies.
