IMD SolutionComputing LibraryShop main store

How-to

MQTT to Modbus TCP with Node-RED on an edge gateway

Edge computing and IIoT Published 7 min read

Short answer

Yes: Node-RED with the node-red-contrib-modbus package can serve MQTT data to a SCADA client as Modbus TCP. Read the RTU device in Node-RED first, give the Modbus server node a port nothing else owns, and scale each MQTT payload to one integer per holding register before the first end-to-end test.

On this page
  1. Can Node-RED convert MQTT to Modbus TCP for a SCADA client?
  2. Which nodes make up the MQTT to Modbus TCP bridge?
  3. How do you read Modbus RTU device 1 register 5 before wiring anything else?
  4. Why does modbus-flex-server report an IP or port conflict, and which port wins?
  5. How do you map an MQTT payload into a holding register?
  6. How do you verify the whole MQTT to SCADA path end to end?
  7. Questions asked

Yes — an edge gateway running Node-RED can take MQTT data and serve it to a SCADA system as Modbus TCP, and with the node-red-contrib-modbus package the bridge is a handful of nodes. Three decisions decide whether it works: the port the server listens on (502 only when the gateway can bind it and nothing else owns it), the register offset convention (a manual's register 5 can be wire offset 4 or 5), and the scale factor that turns each MQTT payload into a 16-bit integer. Build it as two separately verified halves: read the Modbus RTU device in Node-RED first, then serve values to SCADA, and only then connect MQTT.

Can Node-RED convert MQTT to Modbus TCP for a SCADA client? #

Yes — Node-RED with the node-red-contrib-modbus package runs as the Modbus TCP server: your SCADA polls it as a plain Modbus client, and an MQTT subscription feeds the values it serves, so the SCADA driver never sees MQTT.

The conversion runs one direction per path. The MQTT subscriber writes values into the registers the server exposes; SCADA reads them with function code 3 like any Modbus device, and from the driver's point of view the gateway is just a PLC with holding registers. If your SCADA already offers a native MQTT driver, use it and skip the bridge; build this gateway when Modbus TCP is the only way into that SCADA, which is the common case on older installations. Once that decision is made, the rest is configuration: port, offsets, scaling, and a test order that pins every fault to one half.

Which nodes make up the MQTT to Modbus TCP bridge? #

Four pieces: an MQTT subscribe node on your topic, a function node that scales and stores each payload, a modbus-flex-server node that answers the SCADA, and on the device side a modbus-flex-connection with a modbus-flex-getter for the RTU read.

Each has one job:

  • MQTT subscribe node — connects to the broker with its credentials and delivers every message on the topic, for example plant/line1/oven1/temp.
  • Function node — parses the payload, scales it to an integer, stores it in flow context keyed by register offset, and answers the server's requests from that context.
  • modbus-flex-server — the TCP listener SCADA connects to. When a request arrives it passes the request into the flow and returns to the client whatever values the flow hands back; its configuration carries the listening port and the unit id it answers for.
  • modbus-flex-connection — holds the serial parameters for the RS-485 side.
  • modbus-flex-getter — polls unit id 1 over that connection and outputs the register values.

Keep the device half and the serving half joined only by context, not by wiring: the server node never talks serial itself, so a fault always belongs to exactly one half and can be tested against a known value.

How do you read Modbus RTU device 1 register 5 before wiring anything else? #

Open a modbus-flex-connection in RTU mode with the device's serial settings, point a modbus-flex-getter at unit id 1 with function code 3, and read both start offset 4 and start offset 5 — the one that returns the documented value is the wire address.

  1. Serial settings first. Set the OS name of the RS-485 port (for example /dev/ttyUSB0) and the parameters printed in the device manual — for example 19200 baud, 8 data bits, even parity, 1 stop bit. Wrong parity or baud gives timeouts, and no offset change will fix that.
  2. Read both candidate offsets. The Modbus data model is zero-based on the wire, 0 to 65535, while manuals commonly count registers from 1 or use 4xxxx notation. A manual's register 5 is therefore offset 4 in a one-based manual and offset 5 in a zero-based one; the printed number and the wire offset are different things.
  3. Decide by value, not by convention. The device's display or manual promises a known number — say 250 in this example. The offset that returns 250 is the wire address; confirm it by checking that the neighbouring offset returns the neighbouring documented value.
  4. If neither matches, the fault is serial settings, unit id or wiring — not the offset. When the getter returns nothing at all, trace it hop by hop the way tracing missing Modbus data from Node-RED to an MQTT cloud does: port, unit id, function code, then offset.

Commit the winning offset in the register map, and from then on every test reads that wire offset — never "register 5" — so the convention question is answered once.

Why does modbus-flex-server report an IP or port conflict, and which port wins? #

Because port 502 is the Modbus TCP default and is usually taken or forbidden: on Linux only a privileged process can bind ports below 1024, and a duplicate server node or another service on the same port fails the same way.

Three causes cover nearly every report:

  • Privileged port. The default sits below 1024, and Linux lets only a privileged process bind ports under that line. A Node-RED service running unprivileged fails to bind, and the node reports it like any conflict. Grant the service the low-port binding capability, or move to a higher port.
  • Duplicate listener. Another server node in the same flow, a second runtime or a leftover process already holds the port. Run ss -ltn on the gateway and read the local-address column before blaming the node.
  • Wrong interface address. If the node's configuration carries an address, it must be one the gateway itself owns — for example 192.168.1.50 — never the SCADA PC's address.

Decide it this way: use 502 when ss -ltn shows it free and the runtime can bind low ports, because that is what every Modbus driver assumes by default; otherwise take 5020, write that port into the SCADA device definition, and stop chasing the conflict. Never "fix" a TCP listener conflict by changing an RTU slave address — serial addresses and TCP ports are different layers. And because Modbus TCP carries no authentication, that listener stays on the isolated OT segment, never forwarded beyond it.

How do you map an MQTT payload into a holding register? #

Fix the register map first, then scale every payload to a plain integer: one holding-register offset per value, a documented factor on both ends, a round, and a check that the result stays inside 0 to 65535.

Write the map before any code:

Holding register offset Source Scale Example served value
0 RTU device 1, wire offset 4 none 250
1 MQTT topic plant/line1/oven1/temp ×10 237, read as 23.7 °C

Worked example with the arithmetic. The example payload is {"temp": 23.7} and the map says ×10: 23.7 × 10 = 237, well inside the unsigned register range, and at this scale the ceiling is 6553.5 before overflow — far above any oven. On the wire Modbus sends the high byte first, and 237 = 0 × 256 + 237, so the register travels as the bytes 0x00 and 0xED. SCADA reads it with function code 3 and multiplies by 0.1, and both ends now agree to one decimal place.

Decide the width once: one scaled register whenever the range fits and the value never goes negative; two registers only when you need finer resolution than the ceiling allows or the value crosses zero, and then agree the word order with the SCADA side in writing before the first poll. Keep the data topic separate from any command topic, the way Node-RED IO data MQTT setup: keep publish and control separate does, so a scaling bug can never reach a device write.

How do you verify the whole MQTT to SCADA path end to end? #

Prove three things in order: the RTU read matches the device, a second Modbus client reads the same value through the server on the chosen port, and a controlled MQTT publish changes the served register within one cycle.

  1. Device half. The getter's debug output shows 250 in this example, matching what the device itself reports. Wrong value: fix offset or serial settings before touching anything else.
  2. Server half. From a second Modbus client — a test client or the SCADA's own diagnostic — read the configured unit id at the gateway address, for example 192.168.1.50, on the port you chose, and confirm offset 0 returns the same value as step 1.
  3. MQTT half. Publish a controlled value on the topic — for example 30.0, which must appear as 300 — and confirm offset 1 reads 300 within one publish cycle.
  4. Handover. Only after all three pass, give the SCADA driver the address, the port and the register map. An open TCP port proves only reachability, the same false pass why gRPC ingestion fails when the TCP port test still passes describes on the cloud side; only a value comparison proves the bridge.

For the failures you will actually see: a wrong value means offset, scale or word order; a refused connection means listener or port; a connection that opens but errors means unit id or function code. Fix the half that failed, rerun the three checks, and call the gateway done when SCADA's trend matches the device display and follows a test publish.

Questions asked

Is MQTT really more secure than Modbus TCP for getting data into SCADA?

Not by itself. Plain MQTT on port 1883 sends topics and payloads unencrypted, so on a shared network it is exactly as readable as Modbus TCP. The real gain comes from running MQTT over TLS on port 8883 with per-device credentials. Choose TLS for the long wireless or WAN hop, and treat unencrypted MQTT as no safer than the protocol it replaces.

How does SCADA know a served value is stale?

Serve a heartbeat. Have the function node count received MQTT messages into one register, and let SCADA alarm when that register stops changing. When the broker link drops, the server keeps serving the last stored value indefinitely, so a dead feed looks like a perfectly steady process. The heartbeat turns that silence into a visible alarm.

Can SCADA writes go back out as MQTT commands?

Yes, as a deliberate second path. When the server passes a write request into the flow, function code 6 for one register or 16 for several, publish the new value to a separate command topic in the format the receiving side expects. Keep that path apart from the data topic, and never let a write and a scaled measurement share one register offset.

Does each RTU device need its own Modbus TCP unit id?

It is the cleanest convention. Transparent serial gateways pass the Modbus TCP unit identifier through as the RTU slave address, so SCADA reaches serial device 1 as unit id 1 and device 2 as unit id 2. If your server node answers only one unit id, give each serial device its own block of register offsets instead, and write the map down so nobody stacks two devices into the same offsets.

Keep reading

  • How-to

    Publish one MQTT event when a PLC bit rises

    Publish on the transition, not the level. Retain the previous valid sample of the PLC Boolean and publish one MQTT event only when the pair reads false then true. An acquisition cycle cannot see a pulse shorter than itself, so latch short pulses in the PLC and reset the latch after the event publishes, and make the consumer tolerant of duplicate deliveries.

    Edge computing and IIoT

  • Selection guide

    Selecting a Revolution Pi Connect gateway for S7-1500 and Azure MQTT

    A RevPi Connect covers both jobs: two Ethernet ports separate the PLC network from the Azure uplink, up to five extension modules carry the analog and fieldbus hardware, and Node-RED is preinstalled. Size the analog side by counting 4-20 mA loops against the AIO's current-capable channels, and take Modbus TCP for the read-only feed so the S7-1500 keeps PUT/GET closed.

    Edge computing and IIoT

  • How-to

    Sending PLC data to an IoT hub without custom software

    You do not need custom software to move PLC data to an IoT hub: a configurable edge package with an S7, Modbus TCP or EtherNet/IP driver, a channel mapping and an MQTT publisher replaces hand-written acquisition code. Verify two things before you commit: the driver covers the protocol your controller actually speaks, and the publisher covers the destination's authentication and message format. Software still does the work — you configure it instead of writing it.

    Edge computing and IIoT