Modbus TCP with ESPHome?

Hello,

I’m looking into configuring ESPHome to receive Modbus TCP commands to control switches via Modbus TCP. Does anyone have experience setting this up or can provide guidance on how to achieve this with ESPHome? Alternatively, are there any other user-friendly tools or platforms recommended for programming an ESP32 for this task?

Any advice or suggestions would be greatly appreciated!

Thank you!

Hi

Why do you want to use ESPHome for that ? it’s useless as you can do it straight with the MODBUS integration in HA: Modbus - Home Assistant

Vincèn

I want to connect an ESP32 chip directly to another Modbus TCP device without using Home Assistant. However, I find using ESPHome appealing due to its user-friendly programming interface. My goal is to toggle some relays based on the Modbus TCP messages received.

1 Like

Looks like there is a custom component:

Now custom components have been removed but there is an external component to re-add them:

1 Like

Hi there, i’m unable to build a firmware with that at the moment:

src/modbus_tcp_component.h:2:10: fatal error: WiFiClient.h: No such file or directory

Any suggestion. I need to “convert” a modbus tcp device in another network to home assistant sensors.
My home assistant instance can’t reach directly the modbus tcp IP from the cloud to local network.

Hi there, if anyone is still interested I’m trying to convert the modbus h library for esp-idf (that was the problem with WiFiClient.h which is an arduino library).

First steps seem promising as i’m reading correctly data from a modbus TCP device in my network and publishing the value to the sensors using mqtt.

4 Likes

Yes I am very interested by… I was planning to write a native ESPhome component… but I was not aware that WiFiClient was arduino only. Do you have advance since ?

I am very interested as well. Thank you for your efforts!

i still haven’t generalized the library. I have a custom version i’m using to read some modbus data in my local network and send them via mqtt to home assistant.

1 Like

Do you have it on GitHub somewhere? I’m trying to do a similar thing.

Not yet. I still have to push it.

EDIT: here it is
GiuseppeP96/modbustcp_wifi_esphome_espidf: an esp idf compatible version of modbustcp_wifi

i also needed an integer deconding of the value instead of float value so i replaced the call to decode_float with decode_integer

2 Likes

Another way to use modbus tcp

1 Like

I have exactly what you need! :slight_smile:
So far it works very well with OpenTherm with similar purpose as you need.
I was looking for ready solution, but all were bad, so I made one myself.
Cheers and please buy me a coffee if you enjoy it! :ok_hand:

Buy Me A Coffee It took some time to make it and a lot of debug :expressionless:

They should merge it with ESPhome :wink: any errors feel free to contribute.
https://github.com/Gucioo/esphome_modbus_tcp_master

ESPHome Modbus TCP Manager

  • :globe_with_meridians: Modbus TCP Client - Connect to any Modbus TCP server/device
  • :bar_chart: Multiple Data Types - Read 16-bit signed/unsigned integers with scaling
  • :arrows_counterclockwise: Read & Write Support - Single register and multiple register operations
  • :shield: Robust Error Handling - ESP32 stays responsive even when Modbus device is offline
  • :satellite: Connection Monitoring - Real-time connection status reporting
  • :zap: High-Frequency Polling - Support for 1-second update intervals
  • :rotating_light: Optional Watchdog - Device health monitoring with safe mode
  • :wrench: On-Demand Writes - Automatic writes triggered by sensor value changes
5 Likes

Seems great! looking forward to test it!

I have 32-bit registers. How can I read them? I can’t specify a quantity of 2 in your project.

Since Modbus uses 16-bit registers, and you want 10 32-bit values, you’ll need to read them as 20 individual 16-bit registers, for example using Bulk Operations.

After that, combine each pair to form a 32-bit value, but make sure you know which part of the pair is little-endian and which is big-endian. Big-endian is the most common, meaning the high word should go into the first register. Python example:

    # Big-endian (most common)
    value_32bit = (registers[0] << 16) | registers[1]
    
    # Or for little-endian
    # value_32bit = (registers[1] << 16) | registers[0]
    
    # For signed 32-bit integer
    if value_32bit >= 0x80000000:
        value_32bit -= 0x100000000

What does a sensor look like for:
Name = chlorine
Function = FC 3: Read Holding Registers
Unit ID = 1
Address = 100
Quantity = 2
Type = Float
Byte = 4

I rewritten ESPhome Modbus Controller to modbusTCP.
Works same as original Modbus Controller (RTU)
At the Moment works only sensor without error Codes.
Comes in Future. Maybe it helps.
github://creepystefan/esphome_tcp

1 Like

Hi, i need to read data from a TCP device from my ESP32 device, over WiFi. Your TCP Manager is able to do this ?

Hi Vincèn,
What you propose requires a Modbus-TCP connection? I am interested in this as I have a KWB boiler which has no network interface, so I will need to connect my own RS485-TTL board to it, how would you expect that this is visible to home assistant (I had assumed I’d connect to a ESP32 with ESPHome which is then visible over WiFi, looking for options as the Integration looks better but needs a Modbus-TCP unit bridging the modbus device and the network. Thanks