Configuration relay board type R421A08 - RS485 MODBUS RTU

Greetings to the community.

I opened this topic to ask you for help configuring the relay board R421A08
image
My configuration:
raspberry pi2,


and this USB to RS485 adapter
image
(This adapter works well with raspberry + hassio because I use it to read energy consumption data from an EASTRON SDM220-MODBUS.image)

I did some tests and I could not make the R421A08 relay card work.
I used “Modbus Switch” but I did not succeed.

On the web I found this that could help us:

https://forum.logicmachine.net/showthread.php?tid=1403

Some of you much more expert than me could help me, with the code to be written?
Thank you very much.

  1. Install the GitHub code

  2. Control via command line component.

Thanks for the reply but I still can not understand what I have to do. Could you help me?

I follow…

I tried to do a lot of tests.
I changed the register, but I’ve never been successful.

For the register I followed these links:




https://forum.logicmachine.net/showthread.php?tid=1403

Some registers are in the comments.

What am I doing wrong?
I saw that the communication LED sometimes flashes.
This is the last code loaded:

modbus:
  type: serial
  method: rtu
  port: /dev/ttyUSB0
  baudrate: 9600
  stopbits: 1
  bytesize: 8
  parity: N


switch:
  platform: modbus
  slave: 1
  coils:
    - name: R1
      slave: 1
      coil: 40101
    - name: R2
      slave: 1
      coil: 40002
  registers:
    - name: R3
      slave: 1
      register: 40002
      command_on: 1
      command_off: 0
    - name: R4
      slave: 1
      register: 40003
      command_on: 1280
      command_off: 0
    - name: R5
      slave: 1
      register: 40104
      command_on: 01
      command_off: 00
    - name: R6
      slave: 1
      register: 01
      command_on: 1
      command_off: 0

Please can someone help me?
Thank you very much.

I have the same problem.
Has anyone got the solution?
This equipment accect function Preset Multiple Registers (FC=16), but the HA write registers whith de function Preset Single Register (FC=06).
Has anyone know implement this function in HA?

I found a solution that works but I do not get feedback from the relay module.

I wrote a script in python, which uses the mobuspy and is called by a button in the homeassistantt with command line.
My file calls 8O.py, and the id RS485 is 7.

My scripy in python:
#!/usr/bin/python

#---------------------------------------------------------------------------# 
# import the various server implementations
#---------------------------------------------------------------------------# 
from pymodbus.client.sync import ModbusTcpClient as ModbusClient
# from pymodbus.client.sync import ModbusSerialClient as ModbusClient
from pymodbus.transaction import ModbusRtuFramer as ModbusFramer
#---------------------------------------------------------------------------# 
# configure the client logging
#---------------------------------------------------------------------------# 
import logging
import sys

# parametros de entrada
relay = int(sys.argv[1])
value = int(sys.argv[2])

logging.basicConfig()
log = logging.getLogger()
log.setLevel(logging.DEBUG)
#Define Modbus Target Device
client = ModbusClient(host='192.168.1.10',port=502)
connection = client.connect()
print (connection)
# send write command to modbus server
#600= off 300 = on
register_value = client.write_register(relay, value, unit= 7)
client.close()

#---------------------------------------------------------------------------# 

In configuration.yaml:

  • platform: command_line
    switches:
    switch_1:
    command_on: ‘python3 /config/8O.py 1 300’
    command_off: ‘python3 /config/8O.py 1 600’

Please please follow the instructions at he top of the page and format your code properly.

Windows application for STM8S103 & R421A085 module

Use this utility to toggle the relay settings. Ideal for Windows 7.

Written in MS Visual Studio WIN32 C++.

https://sourceforge.net/projects/rs485modbus/

What has this to do with home assistant?

For 4 channel 4CH Modbus RTU relay module 4 ch relay module rs485
In configuration.yaml:

modbus:
  - type: serial
    name: hubrelay
    method: rtu
    port: /dev/ttyUSB1
    baudrate: 9600
    stopbits: 1 
    bytesize: 8
    parity: N
    timeout: 1
    
switch:
  - platform: modbus
    registers:
      - name: Relay1 
        hub: hubrelay
        slave: 1
        register: 1
        command_on: 256
        command_off: 512
        state_on: 1
        state_off: 0
      - name: Relay2 
        hub: hubrelay
        slave: 1
        register: 2
        command_on: 256
        command_off: 512
        state_on: 1
        state_off: 0
      - name: Relay3 
        hub: hubrelay
        slave: 1
        register: 3
        command_on: 256
        command_off: 512
        state_on: 1
        state_off: 0
      - name: Relay4 
        hub: hubrelay
        slave: 1
        register: 4
        command_on: 256
        command_off: 512
        state_on: 1
        state_off: 0  

Thanks alleonname for sharing your configuration with us, it worked right away for me with the R4D3B16:

modbus:
  - type: serial
    name: hubrelay
    method: rtu
    port: /dev/ttyUSB0
    baudrate: 9600
    stopbits: 1 
    bytesize: 8
    parity: N
    timeout: 1
    
switch:
  - platform: modbus
    registers:
      - name: Relay1 
        hub: hubrelay
        slave: 1
        register: 1
        command_on: 256
        command_off: 512
        state_on: 1
        state_off: 0
      - name: Relay2 
        hub: hubrelay
        slave: 1
        register: 2
        command_on: 256
        command_off: 512
        state_on: 1
        state_off: 0
      - name: Relay3 
        hub: hubrelay
        slave: 1
        register: 3
        command_on: 256
        command_off: 512
        state_on: 1
        state_off: 0
      - name: Relay4 
        hub: hubrelay
        slave: 1
        register: 4
        command_on: 256
        command_off: 512
        state_on: 1
        state_off: 0
      - name: Relay5
        hub: hubrelay
        slave: 1
        register: 5
        command_on: 256
        command_off: 512
        state_on: 1
        state_off: 0
      - name: Relay6
        hub: hubrelay
        slave: 1
        register: 6
        command_on: 256
        command_off: 512
        state_on: 1
        state_off: 0
      - name: Relay7
        hub: hubrelay
        slave: 1
        register: 7
        command_on: 256
        command_off: 512
        state_on: 1
        state_off: 0
      - name: Relay8
        hub: hubrelay
        slave: 1
        register: 8
        command_on: 256
        command_off: 512
        state_on: 1
        state_off: 0 
      - name: Relay9
        hub: hubrelay
        slave: 1
        register: 9
        command_on: 256
        command_off: 512
        state_on: 1
        state_off: 0
      - name: Relay10
        hub: hubrelay
        slave: 1
        register: 10
        command_on: 256
        command_off: 512
        state_on: 1
        state_off: 0
      - name: Relay11
        hub: hubrelay
        slave: 1
        register: 11
        command_on: 256
        command_off: 512
        state_on: 1
        state_off: 0
      - name: Relay12
        hub: hubrelay
        slave: 1
        register: 12
        command_on: 256
        command_off: 512
        state_on: 1
        state_off: 0
      - name: Relay13
        hub: hubrelay
        slave: 1
        register: 13
        command_on: 256
        command_off: 512
        state_on: 1
        state_off: 0
      - name: Relay14
        hub: hubrelay
        slave: 1
        register: 14
        command_on: 256
        command_off: 512
        state_on: 1
        state_off: 0
      - name: Relay15
        hub: hubrelay
        slave: 1
        register: 15
        command_on: 256
        command_off: 512
        state_on: 1
        state_off: 0
      - name: Relay16
        hub: hubrelay
        slave: 1
        register: 16
        command_on: 256
        command_off: 512
        state_on: 1
        state_off: 0

hi i bought a modbus RTU relay (WAVESHARE).
i installed usb adapter in a raspb (it should be recognized directly as i read in posts.
to activate realay 1 I have to send a hex: 01 05 00 06 FF 00 6C 3B.
How can I do?
thanks

I tried to read.
But how i send a hex 01 05 00 06 FF 00 6c 3b to activate the relay?

Are you sure this is modbus?

Yes.
I use “register” and “command on” but don’t work with 01 05 00 ecc.

:kissing_smiling_eyes:I managed to activate the relays using the -call service- function:
modbus.write_coil
unit: 1
hub: hubrelay
address: 2
state: 1

but in my case -switch- and -binary sensor- of -modbus- don’t work
so I can’t read the status of the relays

What is your binary_sensor: yaml?

In configuratio.yalm…

binary_sensor:

  • platform: modbus
    inputs:
    • name: Sensor1
      hub: hubrelay
      slave: 1
      address: 3

:thinking: a function as well as read coil?