feature-requests icon indicating copy to clipboard operation
feature-requests copied to clipboard

Add Modbus slave support (RTU and TCP)

Open tuxmartin opened this issue 4 years ago • 13 comments

Describe the problem you have/What new integration you would like

It would be great to be able to use ESPhome on ESP32 as a Modbus slave.

I would like to use Modbus RTU via RS-485, but Modbus TCP would also suffice.

Please describe your use case for this integration and alternatives you've tried:

I asked at Discord and ESPhome can only use Modbus master.

Additional context

/

tuxmartin avatar Apr 28 '20 20:04 tuxmartin

actually I'm interested in modbus slave RTU too. Any hints how it can be implemented?

bademux avatar May 05 '20 15:05 bademux

I have also been wanting Modbus RTU on esphome. Y have many devices that are far away and y don't want wire a long cable to a PLC to monitor these devices. Is Modbus RTU master already possible? I couldn't find that anywhere.

Nicomagno avatar Oct 16 '20 21:10 Nicomagno

Hello:

I'm interested in this question, does anyone already have the solution?

Thank you.

vtb77 avatar Dec 26 '21 12:12 vtb77

If anybody does start this, i suggest you base yourself on a real library instead of doing some partial solution in esphome. Using for example m-bus or libmodbus.

elupus avatar Dec 26 '21 14:12 elupus

It has recently been integrated into the main ESPhome repository. https://esphome.io/components/modbus_controller.html We have to thank https://github.com/martgras. He did the integration, many people tested it and now it works with a lot of devices.

Nicomagno avatar Dec 26 '21 14:12 Nicomagno

Version 2021.10.0 of ESPHome (see https://github.com/esphome/esphome/pull/1779) introduced Modbus client support (props to @martgras) for talking to Modbus servers (previously known as slaves) but support for making ESPHome itself act as a Modbus slave/server is still not available.

What are the complexities with creating a generic server device support? From the outside it seems like mapping sensors/state to coil/registry values might be the most complicated part due to the different ways these values could be represented in the Modbus payload. Are there other things I'm missing?

kasparsd avatar Apr 23 '22 19:04 kasparsd

Just to reinforce the request for modbus slave functionality.

jgracio avatar Aug 25 '22 10:08 jgracio

+1 for this...

grob6000 avatar Aug 29 '22 10:08 grob6000

@kasparsd: It depends. I would say some people want to just use any commercially available modbus controller and hook an esp32 in slave mode to it. But in some cases it would be nice to have some auto-mapping, if you want to use modbus instead of api/mqtt (still dreaming that it would be possible to use Zigbee, LoRa,ESP-Now,... instead of WiFi). All esp32's could be wired, only in case of OTA you will need to enable WiFi.

randybb avatar Sep 05 '22 17:09 randybb

Hello all,

I just wrote a component that allows you to expose your ESPHome as a RTU modbus slave. The documentation is pending, but it is straightforward to use with the example provided.

https://github.com/epiclabs-io/esphome-modbus-server

Please let me know your feedback.

jpeletier avatar Sep 20 '22 12:09 jpeletier

@jpeletier sorry for stupid question, is it possible to expose your modbus slave implementation with RS485 (serial).

bademux avatar Sep 20 '22 13:09 bademux

Hello @bademux.

This implementation already and actually only works over RS485.

In the example you will see that first a serial uart has to be defined by specifying gpio pins where your RS485 serial adapter is connected:

uart:
  - id: my_uart
    tx_pin: 13
    rx_pin: 16
    baud_rate: 9600
    stop_bits: 1
    data_bits: 8
    parity: NONE
    debug:
      direction: BOTH

Then, you instantiate modbus_server linking to the above defined uart:

modbus_server:
  - id: modbuserver
    uart_id: my_uart
   # [...]

Let me know if this clarifies. Cheers

jpeletier avatar Sep 20 '22 13:09 jpeletier

Excellent!!! We need thorough documentation, please. Let's not just point people towards MODBus standards literature, let's explain in a few words how it works and how registers build up, and provide examples for each type of registers (Coil, Discrete Input, Input Register, Holding Register).

Moreover it would be nice to show an example where two ESPHome nodes talk to each other via MODBus.

nagyrobi avatar Sep 20 '22 18:09 nagyrobi

Excellent!!! We need thorough documentation, please. Let's not just point people towards MODBus standards literature, let's explain in a few words how it works and how registers build up, and provide examples for each type of registers (Coil, Discrete Input, Input Register, Holding Register).

Moreover it would be nice to show an example where two ESPHome nodes talk to each other via MODBus.

Nagyrobi, i actually have an implementation of 2 esp32 talking in modbus with @jpeletier component, basically works but master and slave codes need some tweak in order to avoid crc errors ( i think that the esphome master sends too many requests to the slve), i will buy some rs485-ttl adapters to rule out hardware malfunctioning

btw, thank you @jpeletier :)

fdepalo avatar Nov 06 '22 11:11 fdepalo