Modbus over RS485 and TCP port 502 next to MQTT through a broker on port 1883 or 8883
On this page

Modbus to MQTT needs something in the middle, because the two protocols work in opposite ways. Modbus is request and response between a master and a device. MQTT is publish and subscribe through a broker. A Modbus to MQTT gateway does both jobs. It polls the registers as Modbus master and publishes the values to a broker as an MQTT client. SCADA, a dashboard or your own script picks them up there.

Whether that data is usable later depends on a handful of settings on the gateway: topic tree, payload, poll interval, QoS, retained messages and the Last Will.

A Modbus to MQTT gateway plays two roles

Towards the Modbus devices, the gateway is the master. It sends requests and the device answers. The Modbus Application Protocol defines a register as a 16-bit word, sent big-endian (sections 4.2 and 4.3). With function code 03 the gateway reads 1 to 125 registers per request (section 6.3). On RS485 that is Modbus RTU; on the network it is Modbus TCP on port 502.

Towards the broker, the same gateway is an ordinary MQTT client. It connects to the broker and publishes messages on topics. Anyone who needs the data subscribes at the broker and never talks to the gateway directly.

RS485 Modbus RTUEnergy meterHeat pumpInverterModbus toMQTT gatewayModbus ⇄ MQTTMQTT brokerSCADA or dashboardRS485TCP 1883 or 8883
On the left the gateway polls as master, on the right it publishes to the broker as a client.

The two sides run independently. If the broker goes down, the gateway keeps polling. If a Modbus device stops answering, the MQTT connection stays up. What the gateway reports in either case is up to you, using the Last Will described further down.

Some gateways also work the other way round: they accept a Modbus request over MQTT and publish the answer. The Teltonika wiki describes this with a request topic and a response topic, plus a cookie in the request that comes back in the reply. For monitoring you rarely need that direction.

The topic tree and payload decide what subscribers can do

A topic is a path of levels separated by forward slashes. A fixed order from broad to narrow works best: site, gateway, Modbus device, value. Each register then becomes one topic.

site-northsite/gw01gateway/hp1Modbus device/flow-tempvalueHolding register 40011, scale 0.1Payload{"value": 42.5, "unit": "°C"}
Raw value 425 from the register, converted to 42.5 °C before it is published.

That fixed order pays off when you subscribe. MQTT has two wildcards, valid only in subscriptions (MQTT 3.1.1, section 4.7): + matches exactly one level and # matches everything below. Subscribing to site-north/+/hp1/# returns every value from heat pump 1 at that site, whichever gateway it came through. Topics starting with $, such as $SYS, do not match a subscription that starts with a wildcard.

Convert the value on the gateway, not at the subscriber. The Modbus specification only knows 16-bit registers. A 32-bit float takes two registers, and the manufacturer decides which word comes first. If the gateway publishes raw register values, every script and dashboard has to get the word order and scale factor right again. Put the unit and a timestamp in the payload.

There are two common payload formats. The table sets them side by side.

JSON per topicSparkplug B
TopicYour own layout, for example site/gateway/device/valueFixed: spBv1.0/group_id/message_type/edge_node_id/[device_id]
PayloadText, readable in any MQTT toolGoogle Protocol Buffers, binary
Online or offlineYour own Last WillNBIRTH and NDEATH, with the Will as death certificate
FitsYour own scripts, dashboards, Node-REDA SCADA or platform that expects Sparkplug

Source: Eclipse Sparkplug Specification 3.0.0, sections 2.4, 4.1 and 6.2.

Choose Sparkplug B only when the subscriber expects it. For your own dashboard, JSON is easier to check by eye, because you can read every message.

Polling and publishing run at their own pace

The gateway polls each register at a fixed interval, but it does not have to publish every reading. Publishing only on change (report by exception) keeps traffic to the broker low. Sparkplug is built on that principle (section 2.2).

ModbusGateway polls every 5 sFC03 Read Holding RegistersMQTTGateway publishes only on changePUBLISH, QoS 10→Elapsed time in seconds
Polling and publishing run independently.

Publishing on change has one weak spot. A value that stays the same for hours produces no message for hours, and then you cannot tell whether the gateway is still working. Combine it with a fixed repeat, say every fifteen minutes, or with the Last Will. Set a minimum change for analogue values. Otherwise a temperature that wanders between 21.4 and 21.5 °C is published on every poll anyway.

Group neighbouring registers into one request. Ten separate requests for one register each cost ten round trips on the bus. One block of ten registers with a single function code 03 costs one.

QoS, retained and Last Will depend on the type of message

MQTT has three delivery levels, and each type of message needs a different one. The table gives the definitions from MQTT 3.1.1 with a matching use.

SettingWhat the specification saysUse with Modbus data
QoS 0At most once, loss possible (4.3)Fast readings where the next poll fills the gap
QoS 1At least once, duplicates possible (4.3)Meter readings, fault codes, status messages
QoS 2Exactly once (4.3)Rarely needed; the subscriber must support it as well
RetainedBroker keeps the last message per topic for new subscribers (3.3.1.3)Status and latest value, so a dashboard shows data straight after a restart
Last WillBroker publishes it when the connection drops without a clean disconnect (3.1.2.5)status set to offline for the gateway

Source: OASIS MQTT Version 3.1.1, 29 October 2014.

QoS 1 can deliver a message twice. A meter reading that arrives twice does no harm. A consumption delta that the subscriber adds up itself does. The specification keeps order per topic, but after a retransmission an older message can arrive after a newer one (section 4.6). A timestamp in the payload lets the subscriber drop the older one.

A retained message stays until you overwrite it. When you remove a Modbus device, publish an empty retained message on its topics. That clears the stored value (section 3.3.1.3). If you skip it, a new subscriber keeps seeing an old temperature for years.

The Last Will works together with the Keep Alive. If the broker hears nothing from the gateway within one and a half times the Keep Alive, it drops the connection (section 3.1.2.10) and publishes the Will. With a Keep Alive of 60 s, subscribers know within 90 s. Have the gateway publish online itself on every new connection, retained, on the same topic.

Broker, port and security

MQTT uses port 1883 without encryption and port 8883 with TLS; both are registered with IANA (MQTT 3.1.1, section 4.2). When the connection runs over the internet or 4G, use 8883 with a username and password or a client certificate per gateway.

Mosquitto, the open-source broker from the Eclipse Foundation, changed its defaults in version 2.0 of 3 December 2020. Without a listener in the configuration it only listens on 127.0.0.1, and anonymous connections are off by default. A gateway on the network cannot connect until you configure a listener and users. Check that first when a new broker does not respond.

MQTT 3.1.1 is also published as ISO/IEC 20922:2016. MQTT 5.0 has been an OASIS Standard since 7 March 2019. Nearly every broker supports both; the gateway manual tells you which one the gateway uses.

Three faults that keep coming back

Two gateways with the same client ID. When a second client connects with an ID that is already connected, the broker must disconnect the first one (MQTT 3.1.1, section 3.1.4). Two gateways with the same ID knock each other off the broker in turn. Users on the Eclipse, Home Assistant and openHAB forums describe exactly this loop; the broker log keeps showing "already connected, closing old connection". It often happens after a gateway configuration was copied. Give every gateway its own ID. Brokers must accept IDs of 1 to 23 bytes.

Request topic and response topic set to the same name. A gateway that accepts Modbus requests over MQTT publishes the answer on the response topic. A user with a Teltonika RUT956 and ThingsBoard gave both topics the same name (Teltonika forum, 4 June 2025). The router read its own answer as a new request and ran in a loop.

Publishing raw registers. A float32 sent as two separate numbers, or a temperature of 425 that should read 42.5 °C, passes the problem on to every subscriber. Check word order and scale once on the gateway, against a value you can read on the Modbus device's own display. How registers and addresses are counted is covered in Modbus register map explained. For timeouts and exception codes, see Modbus exception codes and timeouts.

Which gateway suits which job is covered in the Modbus gateway buyer guide. For sites without a network connection there is the 4G Modbus gateway.

Frequently asked questions

What is the difference between Modbus and MQTT?

Modbus is request and response: a master asks a device for registers, and the device only answers when asked. MQTT is publish and subscribe through a broker: a client publishes on a topic and every subscriber receives the message. A gateway translates between the two, polling on one side and publishing on the other.

Do I need a gateway to send Modbus data over MQTT?

Yes, or software that plays the same role. A Modbus device has no MQTT and sends nothing on its own. Something has to poll the registers as master and publish the values as an MQTT client. That can be an industrial gateway, or Node-RED or a script on a computer that can reach the bus.

Which port does MQTT use?

Port 1883 without encryption and port 8883 with TLS. Both are registered with IANA, as MQTT 3.1.1 notes in section 4.2. Use 8883 whenever the connection runs over the internet or 4G. Modbus TCP on the other side of the gateway uses port 502, which is a separate connection.

Which QoS level should I use for Modbus readings?

QoS 1 for meter readings, fault codes and status, QoS 0 for fast readings where the next poll replaces a lost message. QoS 1 can deliver a message twice, so include a timestamp in the payload. QoS 2 is rarely needed and the subscriber has to support it too.

What is a retained message in MQTT?

A message the broker stores per topic and hands straight to every new subscriber. A dashboard then shows the latest value immediately after a restart. A retained message with an empty payload clears the stored one, which is what you send when you remove a Modbus device.

What is Sparkplug B?

A specification from the Eclipse Foundation that fixes topics and payload for industrial MQTT. Topics follow spBv1.0/group_id/message_type/edge_node_id/device_id, the payload is binary Protocol Buffers, and birth and death messages report whether a gateway is online. Use it when your SCADA or platform expects Sparkplug.

Why does the broker keep disconnecting my gateway?

Usually a duplicate client ID. MQTT 3.1.1 requires the broker to drop the old connection when a second client connects with the same ID. Two gateways with a copied configuration then throw each other off in turn. Give each gateway its own ID and check the broker log.

The ModbusCloud Gateway sends its own readings over MQTT with TLS (port 8883) to a broker in Frankfurt. It opens that connection outward itself, so the site needs no port forwarding and no fixed IP address. If the connection drops, the Gateway buffers the readings and sends them once it is back. For your own systems, the values come out of the portal through the REST API and webhooks.