4 channel Relay ESP8266 Wifi board WORKING with esphome

Hi all,
I’m posting my integrations with a 4 channel relay board which is both cheap and easy to configure.
Board can be found here link to banggood
It hosts a detachable ESP-01 (8266EX) with 1M memory.

issue is that the ESP8266 is not using GPIO pins to trigger the relays, an additional processor is on board to do that called an STC15L101EW
From the datasheet it turns our that this chip uses UART to communicate with the ESP8266.

So steps to get this working with esphome.

  1. get a usb flash programmer (left one)

  2. make sure you solder the wire bridge as shown here. You cannot flash the ESP8266 when this is not present.

  3. create a new device using the esp01_1m profile
    add this to your configuration file:

  • an esphome stanza and logger, wifi ota items should be there already
  • Modify the logger entry to include **baud_rate: 0 ** this will disable local logging messing up the ESP8266 uart, which we need to communicate with the STC15L101EW board
# Enable logging
logger:
  baud_rate: 0 #need this to free up UART pins

uart:
  baud_rate: 115200 # speed to STC15L101EW
  tx_pin: GPIO1
  rx_pin: GPIO3

switch:
  - platform: uart
    name: "A1on"
    data: [0xA0, 0x01, 0x01, 0xA2]

  - platform: uart
    name: "A1off"
    data: [0xA0, 0x01, 0x00, 0xA1]

  - platform: uart
    name: "A2on"
    data: [0xA0, 0x02, 0x01, 0xA3]

  - platform: uart
    name: "A2off"
    data: [0xA0, 0x02, 0x00, 0xA2]

  - platform: uart
    name: "A3on"
    data: [0xA0, 0x03, 0x01, 0xA4]

  - platform: uart
    name: "A3off"
    data: [0xA0, 0x03, 0x00, 0xA3]

  - platform: uart
    name: "A4on"
    data: [0xA0, 0x04, 0x01, 0xA5]

  - platform: uart
    name: "A4off"
    data: [0xA0, 0x04, 0x00, 0xA4]

  - platform: restart
    name: "4switch_a Restart"
  1. save and validate, compile, download binary and

  2. use esphome-flasher to flash the ESP8266 module with the firmware you just compiled.

  3. when done disconnect usb flasher from computer and put esp module into relay board.

  4. wait a minute, esp8266 takes a fair bit of time to start up.

  5. add an integration for this board into home-assistant and you’re good to go.

ps. the beauty of this board is that relay states won’t be changed when upgrading the esphome firmware (as they are controlled by the second chip) and the relays have both a NO and NC connection making this board ideal for hotel (or failsafe) wiring setup.

21 Likes

Why not use esphome to flash? Am I missing something?

IT would be great if you contribute this to the cookbook section of esphome.io docs

4 Likes

True, and then I first need some time to setup a esphome-docs environment and understand how git works :slight_smile:
So will come to that sooner or later, since I have more projects which I want to do with esphome, but probably not in the short run

because the relay board comes with it’s own firmware which is not OTA flashable / esphome compatible.
So as a first (or final) step you at least need one USB flash operation to get the esphome firmware on it. After that you can flash OTA from esphome.

Docs is easy with vs studio code remote and docker.

I guess flashing should work from esphome usb too.

Thanks for the help but I ended with a bit of a nicer solution. You can use a switch template to have this uart thing act like a real single switch in HomeAssistant:

  - platform: template
    name: "Relay2"
    id: "Relay2"
    optimistic: true
    turn_on_action:
      - uart.write: [0xA0, 0x02, 0x01, 0xA3] # turn on Relay2
    turn_off_action:
      - uart.write: [0xA0, 0x02, 0x00, 0xA2]
3 Likes

Yes, figured that out also, and yours looks more compact then mine, so I’ll update.

Tried to share this asap, so hence the barebones setup.

Thanks

I take it by your use of the above statement that there is no state feedback for the relay states on this board?

I thought this could be ideal to control my garden irrigation valves but that would be a deal breaker.

True, there probably is a state feedback from the second IC to the ESP but I’m still figuring it out. For now I use the optimistic option, haven’t had any issues with that.

1 Like

Don’t be confused by the optimistic setting there.
All this kind of devices work the same way at the end, eg. this is very much how the sonoff basic works.
You’re never getting a positive feedback con relay latched

This is very much not the case with Tasmota or ESPhome.

HA sends a command to the ESP module and a command is sent back confirming the ESP output has been turned on.

Well… yes, in the case of this implementation the feedback is sent back confirming the ESP has sent the uart.data requested.

And this is the actual difference, which in practice turns to be no problem, in case the ESP is rebooted the switches will retain their value using the RTC memory and as the underlay board does not lose power the relay will keep their current state too.

So again, the optimistic here is not giving any better feedback than a sonoff like device will give you.

If that were the case it could be used in a value template that could be interrogated at any time by HA (e.g. after a restart) instead of using optimistic mode. As can be done with Tasmota or ESPhome.

Looks like you are saying: “this is different than a sonoff ha side” and I’m saying “this is the same than a sonoff ha side” so I’ll let it there

I don’t see why it could not be used in a value template in ha.

The switch state is preserved in the esp, in fact a gpio switch and a optimistic true (and every other switch) are retained by the same bool variable in memory.

Can confirm the same works for the 2 channel ‘lctech relay x2’.

switch:
  - platform: template
    name: 'Relay 1'
    id: relay1
    turn_on_action:
      - uart.write: [0xA0, 0x01, 0x01, 0xA2]
    turn_off_action:
      - uart.write: [0xA0, 0x01, 0x00, 0xA1]
    optimistic: true
  - platform: template
    name: 'Relay 2'
    id: relay2
    turn_on_action:
      - uart.write: [0xA0, 0x02, 0x01, 0xA3]
    turn_off_action:
      - uart.write: [0xA0, 0x02, 0x00, 0xA2]
    optimistic: true
1 Like

Cool, next step is to see if there is any way to get the current status from the uart back I think :slight_smile:

Sorry for being a bit slow :slight_smile: but I couldn’t fully understand the Tasmota references here…

Has anyone got these 4-channel units to work with Tasmota?

I’ve personally never got the serial-commands to work in Tasmota (with a single relay LCtech unit)

Cheers
Matt

Hi Erwin

Just wanted to know if you did something special to flash it?
I’ve taken the little 8266 daughterboard and I’ve connected the tx/rx/gnd/3.3v to it,but espeasy flasher won’t connect through my FTDI? It does see the COM port, just not a connection when starting the flash.