I tried to look for some examples but I don't really know how to structure the code for the slaves. Could you guide me or this forum is only support for the Kincony's products?
Do you mean something like this or is there a better way? Let's say I just want to be able to control the GPIO 16 and GPIO 15, would something like this work?
ESPHome master:
Arduino code for slave:
Do you mean something like this or is there a better way? Let's say I just want to be able to control the GPIO 16 and GPIO 15, would something like this work?
ESPHome master:
Code:
uart:
id: rs485_uart
tx_pin: GPIO1
rx_pin: GPIO3
baud_rate: 9600
modbus:
id: modbus_master
uart_id: rs485_uart
modbus_controller:
id: relay_controller
modbus_id: modbus_master
address: 1
switch:
- platform: modbus_controller
name: "Relay_GPIO16"
register_type: holding
address: 0
modbus_controller_id: relay_controller
- platform: modbus_controller
name: "Relay_GPIO15"
register_type: holding
address: 1
modbus_controller_id: relay_controller
Arduino code for slave:
Code:
#include <ModbusMaster.h>
#define TX_PIN 1
#define RX_PIN 3
#define MODBUS_SLAVE_ADDRESS 1
ModbusMaster node;
void setup() {
Serial.begin(9600);
pinMode(16, OUTPUT); // Set GPIO 16 as an output
pinMode(15, OUTPUT); // Set GPIO 15 as an output
node.begin(MODBUS_SLAVE_ADDRESS, Serial);
}
void loop() {
uint8_t result = node.readHoldingRegisters(0, 1); // Request Modbus data
// Check if the request was successful
if (result == node.ku8MBSuccess) {
// Check for Modbus responses
if (node.getResponseBuffer(0) > 1) {
uint16_t regAddress = node.getResponseBuffer(0); // Get the register address
uint16_t regValue = node.getResponseBuffer(1); // Get the value to be written to the register
// Control GPIO 16 and GPIO 15 based on the register address
if (regAddress == 0) {
digitalWrite(16, regValue); // Set GPIO 16 based on the value
} else if (regAddress == 1) {
digitalWrite(15, regValue); // Set GPIO 15 based on the value
}
}
}
}
[color=#dae3e3][font=Consolas, 'Courier New', monospace][/font][/color]