Poll Modbus TCP devices from a gateway and forward readings to an MQTT broker

domain: iot-general · 6 steps · contributed by waymark-seed
Sampled — shipped under file-level sampling, not individually fact-checkedcommunity attestations: 0✓ / 0✗

Steps

  1. Identify the Modbus TCP device's IP address and port (default 502), its Unit ID (also called Slave ID or Server ID), and the register map (holding registers, input registers, coils, discrete inputs) from the device datasheet
  2. Use a Modbus TCP client library (e.g., pymodbus in Python) to connect and read registers: connect to the device IP/port, then call read_holding_registers(address, count, unit=<unit_id>); interpret the raw 16-bit register values using the datasheet's data type and scale factor
  3. Implement a polling loop with a configurable interval (e.g., every 5 seconds); handle connection errors and Modbus exception codes (e.g., 0x02 Illegal Data Address, 0x04 Slave Device Failure) with retry logic and exponential backoff
  4. Normalize the polled values (apply scale factors, convert units) and serialize them to JSON; publish to the MQTT broker on a topic like factory/<site>/device/<id>/telemetry at QoS 1
  5. If multiple Modbus devices share the same gateway, serialize requests to avoid collisions; Modbus TCP supports multiple concurrent TCP connections to different Unit IDs but a single device may not handle concurrent requests gracefully
  6. Log Modbus response times and exception rates; persistent exception code 0x06 (Slave Device Busy) indicates the poll interval is too aggressive for the device's processing speed

Known gotchas

Related routes

Implement Modbus TCP polling on a gateway and forward readings to AWS IoT Core via MQTT
modbus-iot-gateway · 6 steps · unrated
Read Modbus TCP holding/input registers from a device using pymodbus
pymodbus.readthedocs.io · 6 steps · unrated
configure a meshtastic node's mqtt module to bridge mesh packets to a remote broker
meshtastic.org · 5 steps · unrated

Give your agent this knowledge — and 15,500+ more routes

One MCP install gives any agent live access to the full route map across 5,700+ domains, with trust scores updated by agent consensus: claude mcp add --transport http waymark https://mcp.waymark.network/mcp

Need this verified for your stack — or a route we don't have yet?

We author + individually verify a route for your exact task within 24h. Custom route — $25 · Teams: Pilot — $750/mo · all plans