Modbus switches on a 24-relay card

I’m trying to connect a 24-relay Modbus card with EspHome on a Wemos D1 mini.


(this is a picture of a 12-relay model)

I keep getting hundreds of Modbus CRC Check failed!:

[12:01:08][W][modbus:096]: Modbus CRC Check failed! A9C!=1FC
[12:01:08][W][modbus:096]: Modbus CRC Check failed! 7801!=FCC0
[12:01:08][W][modbus:096]: Modbus CRC Check failed! A9C!=1FA
[12:01:10][W][modbus:096]: Modbus CRC Check failed! A9C!=1FC
[12:01:11][W][modbus:096]: Modbus CRC Check failed! A9C!=1FC
[12:01:11][W][modbus:096]: Modbus CRC Check failed! A9C!=1FD
[12:01:12][W][modbus:096]: Modbus CRC Check failed! A9C!=1FC
[12:01:13][W][modbus:096]: Modbus CRC Check failed! A9C!=501
[12:01:13][W][modbus:096]: Modbus CRC Check failed! A9C!=8501
[12:01:14][W][modbus:096]: Modbus CRC Check failed! A9C!=1FC
[12:01:14][W][modbus:096]: Modbus CRC Check failed! A9C!=1FC
[12:01:15][D][switch:017]: 'Switch 24' Turning OFF.
[12:01:15][W][modbus_controller:105]: Duplicate modbus command found
[12:01:15][D][switch:037]: 'Switch 24': Sending state OFF
[12:01:15][W][modbus:096]: Modbus CRC Check failed! A9C!=8501
[12:01:16][W][modbus:096]: Modbus CRC Check failed! A9C!=8501
[12:01:16][W][modbus:096]: Modbus CRC Check failed! A9C!=8501
[12:01:17][W][modbus:096]: Modbus CRC Check failed! A9C!=1FC
[12:01:17][W][modbus:096]: Modbus CRC Check failed! A9C!=8501
[12:01:18][W][modbus:096]: Modbus CRC Check failed! A9C!=1FC
[12:01:19][W][modbus:096]: Modbus CRC Check failed! A9C!=8501
[12:01:19][W][modbus:096]: Modbus CRC Check failed! A9C!=8501
[12:01:20][W][modbus:096]: Modbus CRC Check failed! A9C!=1FC
[12:01:21][W][modbus:096]: Modbus CRC Check failed! A9C!=1FC
[12:01:21][D][switch:013]: 'Switch 24' Turning ON.
[12:01:21][W][modbus_controller:105]: Duplicate modbus command found
[12:01:21][D][switch:037]: 'Switch 24': Sending state ON
[12:01:21][W][modbus:096]: Modbus CRC Check failed! A9C!=1FC
[12:01:22][W][modbus:096]: Modbus CRC Check failed! A9C!=1FC
[12:01:23][W][modbus:096]: Modbus CRC Check failed! A9C!=1FC
[12:01:24][W][modbus:096]: Modbus CRC Check failed! A9C!=8501
[12:01:27][W][modbus:096]: Modbus CRC Check failed! A9C!=FCE0
[12:01:28][W][modbus:096]: Modbus CRC Check failed! A9C!=8501
[12:01:28][W][modbus:096]: Modbus CRC Check failed! A9C!=FC1C
[12:01:29][W][modbus:096]: Modbus CRC Check failed! A6DF!=05
[12:01:29][W][modbus:096]: Modbus CRC Check failed! A9C!=8501
[12:01:30][D][switch:013]: 'Switch 2' Turning ON.
[12:01:30][W][modbus_controller:105]: Duplicate modbus command found
[12:01:30][D][switch:037]: 'Switch 2': Sending state ON
[12:01:30][W][modbus:096]: Modbus CRC Check failed! FADD!=1FF
[12:01:32][W][modbus:096]: Modbus CRC Check failed! FADD!=1FD
[12:01:33][D][switch:017]: 'Switch 24' Turning OFF.
[12:01:33][W][modbus_controller:105]: Duplicate modbus command found
[12:01:33][D][switch:037]: 'Switch 24': Sending state OFF
[12:01:34][W][modbus:096]: Modbus CRC Check failed! FADD!=1FF
[12:01:35][D][switch:013]: 'Switch 1' Turning ON.
[12:01:35][D][switch:037]: 'Switch 1': Sending state ON
[12:01:35][W][modbus:096]: Modbus CRC Check failed! FADD!=1FF
[12:01:35][W][modbus:096]: Modbus CRC Check failed! FADD!=1FF
[12:01:37][W][modbus:096]: Modbus CRC Check failed! FADD!=8501
[12:01:37][W][modbus:096]: Modbus CRC Check failed! FADD!=8501
[12:01:39][D][sensor:113]: 'Wemos Test 1 Uptime': Sending state 3947.31689 s with 0 decimals of accuracy
[12:01:40][W][modbus:096]: Modbus CRC Check failed! FADD!=1FF
[12:01:41][W][modbus_controller:105]: Duplicate modbus command found
[12:01:41][W][modbus_controller:105]: Duplicate modbus command found
[12:01:42][W][modbus:096]: Modbus CRC Check failed! FADD!=1FD
[12:01:43][W][modbus:096]: Modbus CRC Check failed! FADD!=1FD
[12:01:46][W][modbus:096]: Modbus CRC Check failed! FADD!=501
[12:01:47][W][modbus:096]: Modbus CRC Check failed! FADD!=FFFF

Occasionally this also pops up:

[12:20:33][D][modbus:105]: Modbus error function code: 0x85 exception: 4
[12:20:33][E][modbus_controller:058]: Modbus error function code: 0x5 exception: 4 
[12:20:33][E][modbus_controller:067]: Modbus error - last command: function code=0x5  register adddress = 0x0  registers count=1 payload size=2

Card Modbus address is 1 (configurable by dipswitch), baudrate 9600, no parity.
Coil addresses 0 to 23

The Relays are configured as coils (currently I only configured 3 of them):

logger:
  baud_rate: 0

uart:
  id: mbus
  tx_pin: GPIO1
  rx_pin: GPIO3
  baud_rate: 9600
#  data_bits: 8
#  parity: NONE
#  stop_bits: 2
#  rx_buffer_size: 512

modbus:
#  flow_control_pin: 5
  id: modbus1
  uart_id: mbus
  send_wait_time: 20ms

modbus_controller:
- id: relay24
  address: 1
  modbus_id: modbus1
  setup_priority: -10
#  command_throttle: 20ms

switch:
- platform: restart
  name: ${friendly_name} Restart

- platform: modbus_controller
  modbus_controller_id: relay24
  id: switch_01
  name: "Switch 1"
  register_type: coil
  address: 0
  bitmask: 1

- platform: modbus_controller
  modbus_controller_id: relay24
  id: switch_02
  name: "Switch 2"
  register_type: coil
  address: 1
  bitmask: 1

- platform: modbus_controller
  modbus_controller_id: relay24
  id: switch_24
  name: "Switch 24"
  register_type: coil
  address: 23
  bitmask: 1

Actually relays work in 70% of the cases but not always, and they react slowly. Also relays and switches go out of sync, sometimes after switching off relay still remains on.
The card has an error LED on the board, often that also lights up steadily. There’s a green data transfer LED too, that slightly lights up at every communication, at each CRC fail message too.

I tried with ModbusPOLL in Windows and the card works perfectly with an USB-RS485 adapter (9600baud, 8N1). Must be something with ESPHome…

EDIT:
It works perfectly on an ESP32. Issues above happen with ESP8266.

Hi Robi,

Interesting to read, I don’t have much experience with this board but was already for a long time looking into this board which is similar.

To use as a wired input module, connecting to up to 64 digital input sensors.
If I’m correct this would be directly accessible through a tcp/ip connection (ethernet) skipping the need for an espHome setup here?
If you have made any progress or more insight on this please let me know.
Thank you

Hi Pieter,

Yes I evaluated the relays model but the RJ45 port on the board is nothing more than a serial-to-ethernet converter thus it’s still needed to handle MODBUS protocol before entering HA.

Unfortunately HA’s MODBUS integration is very limited in many aspects, the author can’t really give support. I’ve ran into issues which I reported but couldn’t be fixed, like when IP network connection faults occur between HA and the ModbusTcp adapter (eg adapter not reachable on Ethernet) the whole HA locks up, web interface dies can’t even restart.

Also it’s very slow altogehter and not very reliable. Toggling of the relays works only 95% of the cases, and the worst part is that HA shows the switch is on, while on the board in reality it’s not. This must be due to poor implementation in MODBUS integration, there’s some kind of hard-coded optimistic state assumption.

At least that was the situation back in May 2021. Don’t know how it is now.

The best approach IMHO is to offload MODBUS from HA completely and deploy it in ESPHome, where it’s implemented in a much more proffessional way, in low level C++. Yaml config is very simple. Anpther benefit is that other critical functionality like thermostats can be implemented better in ESPHome than in HA, so that it keeps working even when HA is offline. You can hook up sensors directly to the ESP module and make all decisions there, and use HA just for extras like scheduling states and creating a nice UI for the functionality, but keep the core functionality on ESP. If you’re really paranoid you can always keep a second ESP on the shelf pre-programmed with the same firmware and if there’s any trouble, you can swap it within seconds. You can’t really do that with HA.

What kind of sensors would you use with that board?

I think the board you linked also has R485 connectors present so if it proves to be unreliable with Ethernet for you too, you can still switch to ESPHome. Modbus sensors are very nicely implemented, you can use lambdas to adjust very custom readings to convert them to reality.

1 Like

Hi Robert,

Thanks for this extensive reply!
I’ve read through it and already know a lot more now.
After converting this in my schematics I’ll get back here, maybe you can have a look at them :wink:

I’ve also made a post about DALI as I wanted to implement input device into my DALI lighting.
As it’s also Modbus related you might have an opinion about that as well.
In that thread you can also see the layout I’m after.

Thanks again!