Modbus server

Hi,

I am trying to use ESPHome as a Modbus server (the device that gets queried by the master).

According to the documentation under Example Server, this should be possible with ESPHome.

In my ESPHome config I am using:

uart:
  id: uart_bus
  tx_pin: GPIO7  # Adjust according to your wiring
  rx_pin: GPIO6  # Adjust according to your wiring
  baud_rate: 9600
  parity: NONE
  stop_bits: 1
  data_bits: 8
  flow_control_pin: GPIO4

modbus:
  - uart_id: uart_bus
    id: modbus_server
    role: server

modbus_controller:
  - id: modbus_controller_1
    modbus_id: modbus_server
    address: 2
    server_registers:
      - address: 0x0000
        value_type: S_DWORD_R
        read_lambda: |-
          return 0x00010009;

And within HA I am using the following Modbus command to read the register:

sensors:
    binary_sensors:
      - name: power_relay2
        address: 0x0000
        slave: 2
        scan_interval: 5

I can see in the HA logs that the following is sent out:

>>>>> send: 0x2 0x1 0x0 0x0 0x0 0x1 0xfd 0xf9

And I can see in the ESPHome logs that it is received:

[10:50:22.211][VV][modbus:054]: Modbus received Byte  2 (0X2)
[10:50:22.211][VV][modbus:054]: Modbus received Byte  1 (0X1)
[10:50:22.211][VV][modbus:054]: Modbus received Byte  0 (0X0)
[10:50:22.211][VV][modbus:054]: Modbus received Byte  0 (0X0)
[10:50:22.211][VV][modbus:054]: Modbus received Byte  0 (0X0)
[10:50:22.211][VV][modbus:054]: Modbus received Byte  1 (0X1)
[10:50:22.211][VV][modbus:054]: Modbus received Byte  253 (0Xfd)
[10:50:22.211][VV][modbus:054]: Modbus received Byte  249 (0Xf9)
[10:50:22.211][V][modbus:190]: Clearing buffer of 7 bytes - parse succeeded

However, ESPHome never replies. Why is it not sending back what I have defined in the lamda as return value?

Also, this is my first try to read a register. The goal would be to send a register value via Modbus and have the ESPHome server store it internally via write.

Thanks,
Nils

You are requesting to read coil (0x01) but your register is holding type (0x03).
And your CRC doesn’t match anyway…

Try to request 0x02 0x03 0x00 0x00 0x00 0x01 0x84 0x39