Smart Home Automation Forum
Communicate between multiple ESP's with RS485 Modbus - Printable Version

+- Smart Home Automation Forum (https://www.kincony.com/forum)
+-- Forum: Technical Support (https://www.kincony.com/forum/forumdisplay.php?fid=1)
+--- Forum: Getting Started with ESPHome and Home Assistant (https://www.kincony.com/forum/forumdisplay.php?fid=54)
+--- Thread: Communicate between multiple ESP's with RS485 Modbus (/showthread.php?tid=4084)

Pages: 1 2


Communicate between multiple ESP's with RS485 Modbus - k0cka - 01-04-2024

Hello, I was searching on the internet but I could not find answers to my questions. So I would like to try my luck here and see if someone can help me.

I would like to ask if it’s possible to communicate between multiple ESP’s using modbus in ESPHome. So for example - I press a button on esp1 and turn on a relay on esp2.

I have some max485 modules to convert UART to RS485, but I just don’t understand how should the yaml files been written since I’m a total newbie in this field.

Thank you in advance!


RE: Communicate between multiple ESP's with RS485 Modbus - admin - 01-05-2024

just use RS485 as a serial port in ESPHome. you need to confirm your second ESP relay board's protocol. then you can config ESPHome as this example:

# Example configuration entry
uart:
baud_rate: 9600
tx_pin: D0

switch:
- platform: uart
name: "UART String Output"
data: 'DataToSend'
- platform: uart
name: "UART Bytes Output"
data: [0xDE, 0xAD, 0xBE, 0xEF]
- platform: uart
name: "UART Recurring Output"
data: [0xDE, 0xAD, 0xBE, 0xEF]
send_every: 1s
- platform: uart
name: "UART On/Off"
data:
turn_on: "TurnOn\r\n"
turn_off: "TurnOff\r\n"

here is details about ESPHome UART switch for yaml: https://esphome.io/components/switch/uart.html


RE: Communicate between multiple ESP's with RS485 Modbus - k0cka - 01-05-2024

Thanks for your quick reply!

I'm not really sure if I understand this so I will try to explain a little more specifically.
Currently I have connected 2 ESP boards as shown in this video - https://youtu.be/arUVjKPX9Aw?si=sB3zYPEhohtweyiW&t=935

My plan is to place ESP boards all around the house but I would like to avoid WiFi connection and that's why I was thinking about communicate between them with Modbus trough cable. So let's say I want to trigger the ESP number 2 gpioX with ESP number 1 gpioY, how should the yaml file look for both ESP's? Sorry if my questions are not clear, I just need someone to guide me towards the right direction.


RE: Communicate between multiple ESP's with RS485 Modbus - admin - 01-05-2024

1. You need write arduino modbus code for every "SLAVE" ESP board firstly.
2. Then config ESPHome for "MASTER" ESP board. every "SALVE" ESP board will have different RS485 address.


RE: Communicate between multiple ESP's with RS485 Modbus - k0cka - 01-05-2024

Like this? - https://youtu.be/xmBWzVGpggo?si=JCTABQzvegYUNMeQ

And if yes, how should the arduino code for the slaves look like? Is it available somewhere or do I need to write it myself?


RE: Communicate between multiple ESP's with RS485 Modbus - admin - 01-05-2024

yes, you need to write arduino code by yourself. KinCony's relay board just download KCS firmware to use for "SLAVE" board.


RE: Communicate between multiple ESP's with RS485 Modbus - k0cka - 01-07-2024

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:
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]



RE: Communicate between multiple ESP's with RS485 Modbus - k0cka - 01-07-2024

I tested it but I couldn't get the arduino code for the slave to work...


RE: Communicate between multiple ESP's with RS485 Modbus - admin - 01-07-2024

sorry, this need test by yourself. all our KinCony product's arduino demo source code on the forum.


RE: Communicate between multiple ESP's with RS485 Modbus - k0cka - 01-08-2024

So the code that you use is available on the forum? Could I use it or I misunderstood you?