ModPackQTModPackQT
Node-RED ExamplesResourcesSign up
HomeResourcesNode-RED ExamplesModbus → MQTT Bridge
IntegrationNode-RED Modbus Example

Modbus → MQTT Bridge

Forward Modbus register values to an MQTT broker so other systems (dashboards, cloud platforms, other PLCs) can subscribe. This pattern is common in IIoT gateways. The example polls one register every 10 seconds and publishes it to a topic.

What you need

  • Node-RED v3+
  • node-red-contrib-modbus installed
  • An MQTT broker accessible on the network (e.g. Mosquitto, HiveMQ, or cloud broker)

Flow Overview


Modbus Device                Node-RED                        MQTT Broker
     │                          │                                  │
     │◀── FC03 poll ────────────│                                  │
     │─── 4 registers ─────────▶│                                  │
     │                          │── scale + format ──▶ publish ──▶│
     │                          │                  topic: factory/ │
     │                          │                  line1/temp      │
                                │◀── subscribed clients ───────────│

Node-RED Flow JSON

Node-RED Flow JSON
[
  {"id":"inj11","type":"inject","name":"Poll 10s","repeat":"10","once":true,"wires":[["req11"]],"x":120,"y":1100},
  {"id":"req11","type":"modbus-flex-getter","name":"Read reg 0","dataType":"HoldingRegister","adr":0,"quantity":1,"server":"srv1","wires":[["fn11"],[]],"x":310,"y":1100},
  {"id":"fn11","type":"function","name":"Format for MQTT","func":"msg.topic = 'factory/line1/temperature';
msg.payload = msg.payload.data[0] / 10;  // scale: raw 245 → 24.5°C
return msg;","wires":[["mqtt11"]],"x":510,"y":1100},
  {"id":"mqtt11","type":"mqtt out","name":"Publish","topic":"factory/line1/temperature","qos":"1","retain":false,"broker":"broker11","wires":[],"x":710,"y":1100},
  {"id":"broker11","type":"mqtt-broker","name":"Local Broker","broker":"localhost","port":1883,"clientid":"nodered-modbus"},
  {"id":"srv1","type":"modbus-client","name":"My Slave","clienttype":"tcp","tcpHost":"192.168.1.100","tcpPort":502,"unit_id":1}
]

To import: open Node-RED → Hamburger menu → Import → paste this JSON → Deploy.

Expected Output

// MQTT message on topic 'factory/line1/temperature':
{ topic: "factory/line1/temperature", payload: 24.5 }

Common Gotchas

  • Apply scaling in the function node — raw register values are rarely the engineering unit directly.
  • Use QoS 1 for important process values to ensure delivery even if the broker restarts briefly.
  • For many registers, build one message per register in a loop and return msg array from the function node.
  • If Modbus polling fails, the MQTT retain flag means subscribers keep seeing the last known good value — consider publishing NaN or a status flag on error.
  • Use a change node to suppress publishing when the value has not changed (deadband filtering).

Same in ModPackQT — in 30 seconds

ModPackQT has a built-in MQTT integration that publishes Modbus register values on configurable topics — no Node-RED flow needed. Configure topic, scaling, and deadband in the MQTT tab.

Was this example helpful?

More Integration examples

All Node-RED Modbus Examples (30)

ModPackQT · Node-RED Modbus Examples · Updated 2026