← How-Tos
electronics 8 days ago ◑ 19 views ◯ 5 min read

Building a Modbus TCP-to-RTU Gateway with an ESP32: Bridging Ethernet/WiFi Clients to a Serial RS-485 Bus

modbus tcpmodbus rtugatewayesp32rs-485home assistantnode-redprotocol bridgeindustrial automation

Most of the Modbus devices a maker actually owns — industrial sensors, energy meters, VFDs — speak Modbus RTU over a serial RS-485 bus, while most of the software that wants to talk to them (Home Assistant, Node-RED, a SCADA-style dashboard, even a simple Python script running on a different machine on the network) expects Modbus TCP over Ethernet or WiFi. A Modbus gateway bridges the two: it sits on the RS-485 bus as the only RTU "master," and exposes every device on that bus to the network as a single Modbus TCP server, translating requests back and forth. Building one with an ESP32 costs a few dollars in hardware and solves a real integration problem that otherwise means running serial cable to wherever your automation software happens to be running.

Why a Gateway, Not Just Direct RTU Polling

This site's RS-485/Modbus RTU basics guide covers wiring a single device directly to a microcontroller that polls it itself — fine when one program on one board needs one sensor's data. A gateway solves a different, more common situation: multiple software clients (Home Assistant and a separate monitoring script and a dashboard) all wanting data from the same physical RS-485 bus, which RTU's single-master design doesn't support directly — only one master can be issuing requests on an RTU bus at a time, or responses collide. A gateway resolves this by being the single RTU master itself, and serving that data out over TCP to as many network clients as want it, each of which can issue requests independently without any of them touching the serial bus directly.

Architecture Overview

The protocols are close cousins — Modbus TCP is essentially Modbus RTU's register/function-code model without the CRC and serial framing, wrapped in a TCP-friendly header (the MBAP header) instead. That similarity is what makes a single small microcontroller capable of translating between them in real time without heavy processing.

Implementation Approach

Rather than hand-rolling the protocol parsing, the practical path on an ESP32 is combining an existing Modbus RTU master library (handling the serial side) with a Modbus TCP server library (handling the network side) in the same sketch, and writing the glue logic that connects them: when a TCP request arrives, extract the target slave address, function code, and register range; issue the equivalent RTU request on the serial bus; wait for the RTU response (with a sensible timeout — RTU devices are often slower to respond than a TCP client might expect); and relay that data back as a TCP response.

  1. Configure the ESP32's UART and DE/RE control pin for the RS-485 transceiver, exactly as in a direct RTU master setup.
  2. Bring up WiFi (or Ethernet, on ESP32 variants with a wired PHY) and start a TCP listener on the Modbus port.
  3. On each incoming TCP connection/request, map the TCP request's unit identifier field to the correct RTU slave address on the bus — this mapping matters because Modbus TCP's unit ID and Modbus RTU's slave address serve the same purpose but are specified separately in each protocol's framing.
  4. Serialize and send the RTU request, handle the response (or timeout) on the serial side, and translate the result back into a TCP response frame.

Handling Multiple Simultaneous Clients

Because the RTU bus still only tolerates one request in flight at a time no matter how many TCP clients are asking, the gateway needs to serialize bus access even while accepting multiple TCP connections — queue incoming requests and process them one at a time against the serial bus, rather than trying to let two TCP clients both issue RTU requests simultaneously. This is usually implemented with a simple mutex/lock around the serial bus access in the gateway's code, and it's the detail most DIY gateway attempts get wrong first, producing garbled responses under any real concurrent load even though single-client testing looked fine.

Integrating with Home Assistant and Node-RED

Both Home Assistant's native Modbus integration and Node-RED's Modbus nodes support connecting to a Modbus TCP server by IP address and port — once the ESP32 gateway is running, configuring either tool is the same as pointing it at any commercial Modbus TCP gateway appliance, just at a fraction of the cost. Define each physical RTU device's registers in the integration's configuration exactly as you would for direct RTU access; the gateway is transparent from the client software's perspective.

Troubleshooting

SymptomLikely Cause
Works with one client, fails with two simultaneous clientsMissing serialization/locking around serial bus access — see above
Intermittent timeouts under loadRTU device response time exceeds the gateway's configured timeout — slow industrial sensors sometimes need a more generous timeout than a fast TCP client expects
Wrong device responds, or no responseUnit ID (TCP) to slave address (RTU) mapping configured incorrectly
Gateway appears to hang entirelyA stalled or disconnected RTU device not timing out cleanly — ensure every serial read has a bounded timeout, never a wait that can block forever

Safety Notes

If the RS-485 bus this gateway serves includes devices tied to mains-powered equipment (VFDs, contactors, energy meters), treat the gateway itself with the same electrical isolation practices as any other node on that bus — an isolated RS-485 transceiver is worth the small extra cost if the bus spans circuits with different electrical origins. Putting a gateway on the network also means Modbus TCP traffic — which has no built-in authentication in its base specification — is now reachable from anywhere on that network; keep this kind of gateway on an isolated VLAN or a private network segment rather than exposing it broadly, especially if any bus device can actually actuate something (a relay, a valve, a VFD).

A homemade Modbus gateway is a genuinely practical piece of shop infrastructure once more than one thing wants to talk to the same serial bus — it turns a single-master limitation into a shared resource any number of dashboards, automations, or scripts can query independently.