I have made a small python script to test the communication and use a usb to rs485. all data works ok.
These are the packets I send via rs485
r1on = "FF050000FF00"
r1of = "FF0500000000"
rin = "FF0200000008"
I start and reading inputs, use “FF0200000008”
As I read, the FF is the board modbus address, the 02 is the input read code and next 00 00. is the register address of the inputs and the 00 08 is the number of bits.
for the input number 3 I receive: b'\xff\x02\x01\x04\x91\xa3'
witch I know FFis the modbus address slave, 02 is the reading code, I don’t know what is the 01, but the 04 is the last byte contains the input 3 ON.
That help a lot, the function is not correct, I use “discrete_input”, by docs should have function 2, but instead is using function 1. sometimes it gets right.
left change the last number, the quantity.
the topic “register_count: 8” always gives me error, I try to put near the sensor:
I’l try next the costum command with the bytes I need.
meanwhile the relays are function using lambda
switch:
- platform: modbus_controller
modbus_controller_id: modbus_device
id: R1
register_type: coil
address: 0
name: "Relé 1"
write_lambda: |-
ESP_LOGD("main","Modbus Switch incoming state = %f",x);
// return false ; // use this to just change the value
if(x) {
payload.push_back(0xFF); // device address
payload.push_back(0x05); // force single coil
payload.push_back(0x00); // high byte address of the coil
payload.push_back(0x00); // low byte address of the coil
payload.push_back(0xFF); // ON = 0xFF00 OFF=0000
payload.push_back(0x00);
return true; }
else {
payload.push_back(0xFF); // device address
payload.push_back(0x05); // force single coil
payload.push_back(0x00); // high byte address of the coil
payload.push_back(0x00); // low byte address of the coil
payload.push_back(0x00); // ON = 0xFF00 OFF=0000
payload.push_back(0x00);
return false;
}
Nice that you got solution. I still don’t understand why Esphome was sending invalid code??
Anyway, this is not the worst device I have been “working with”. The most idiotic one I have been scratching my head was responding to slave address x, but with slave address y in response. Invaliding modbus_controller to read response at all, because it was from wrong address.
Nice that you got solution. I still don’t understand why Esphome was sending invalid code??
Because I’m a idiot.
switch:
- platform: modbus_controller
modbus_controller_id: modbus_device
id: R1
register_type: coil
address: 0
name: "Relé 1"
write_lambda: |-
ESP_LOGD("main","Modbus Switch incoming state = %f",x);
// return false ; // use this to just change the value
if(x) {
payload.push_back(0xFF); // device address
payload.push_back(0x05); // force single coil
payload.push_back(0x00); // high byte address of the coil
payload.push_back(0x00); // low byte address of the coil
payload.push_back(0xFF); // ON = 0xFF00 OFF=0000
payload.push_back(0x00);
return true; }
else {
payload.push_back(0xFF); // device address
payload.push_back(0x05); // force single coil
payload.push_back(0x00); // high byte address of the coil
payload.push_back(0x00); // low byte address of the coil
payload.push_back(0x00); // ON = 0xFF00 OFF=0000
payload.push_back(0x00);
return false;
}
This block above, send the regular packet (you see 4 times and no answer, because is not correct, and then send the payload by wirite_lambda code.
So this block send 2 complete packets, one for the switch itself, and the other packet created by me in lambda.
ESPhome works ok. i’m not.
the correct solution is this one, as output. esphoem gets all packets corrected.