How to control wifi relay by serial port of the ESP8266

Hi there,
it turned out that my wifi relay cannot be controlled by GPIO but only by the serial port of the ESP8266. But I do not know how to set up an appropriate yaml file in ESPhome.
Please find a picture of the relay here: wifi relay

Although stated in the text the relay cannot be controlled by GPIO pin, only by serial port of the ESP8266. Does anyone know how this can be done with ESPhome?

I found the uart switch platform. Another internet source told about some hex numbers. I used these numbers in the uart switch platform, but it did not work. Please see the part of my yaml-file:

uart:
  baud_rate: 9600
  tx_pin: 2

switch:
  - platform: uart
    name: "WIFI Relais EIN"
    data: [0xA0, 0x01, 0x01, 0xA2]
  - platform: uart
    name: "WIFI Relais AUS"
    data: [0xA0, 0x01, 0x00, 0xA1]

Is there aynone who can help?
Thanks!

You got access to some documentation with your purchase from AZDelivery. What does it say?

The comments on the amazon page (google trans from german)

Works - after intensive research on the net…
Here are a few tips:

  • Flash MicroPython on the ESP8266-01 and set up WiFi (everything is online)
  • Connection: NI- to ground, NI+ to 5V - Important: The relay is controlled internally via the serial interface and not via the ESP8266-01
  • The relay can be programmed in the WebRepl Shell as follows
>>> from machine import UART 
>>> uart = UART(0, 9600) 
>>> uart.init(9600, bits=8, parity=None, stop=1) 
>>> uart.write(b"\xa0\x01\x00\xa1") 
# Turn off the relay (blue LED goes off) 
>>> uart.write(b"\xa0\x01\x01\xa2") 
# Turn on the relay (blue LED is on) 

and it’s tackling… have fun

EDIT: hopefully fixed the quoting.

Thanks, azdelivery did not provide any docs for this product. They sell a similar product with a documentation saying that the relay is triggered by GPIO0, but this is wrong.

Thanks for your help! I am looking for programming in ESPhome.

Look closely in your emails. They send pdf and such.
I surely got those documents for every purchase I made from them…

Sorry I did not finish my post, I just wanted to point out you seem to be on the right track.

However you need a bit more - see ESPHome Switch not respecting state - #2 by tom_l

1 Like

Thanks again! I had the same problem (switch in HA fell back after 1 second) and I could solve it with your advice (optimistic: true). Really, there is more to it…puh!

But frankly speaking, I have an ever bigger problem. The relay does not work! The ESP8266 receives the signal to switch (led lights up in both ways, open and close). But the relay does not move.

I took a closer look to the relay board: Neither GPIO0 nor GPIO2 seem to be connected to the STC chip on the relay board. So - no wonder why there is no relay working. I tried to declare PIN8 (TXD) of the ESP as tx_pin in ESPHome, but this is not acceted by ESPhome. What to do now?

At the moment I feel a little bit lost.

But again: Thanks for the help!

Have you tried the settings I pointed to?

switch:
  - platform: template 
    id: swRelay1
    name: "Relay1"
    turn_on_action:
      - uart.write: [0xA0, 0x01, 0x01, 0xA2]
    turn_off_action:
      - uart.write: [0xA0, 0x01, 0x00, 0xA1]

for init:

button:
  - platform: template
    name: send1
    id: send1
    on_press:
      then:
        - uart.write:
            data: "WIFI CONNECTED WIFI GOT IP"

Please find my new yaml-file out of the ESPhome integration (which is unfortunately not working!)

uart:
  baud_rate: 9600
  tx_pin: 2
  rx_pin: 4

switch:
   - platform: template
     name: "WIFI Relais EIN"
     turn_on_action:
      - uart.write: [0xA0, 0x01, 0x01, 0xA2]
     optimistic: true  
   - platform: template    
     name: "WIFI Relais AUS"
     turn_off_action:
      - uart.write: [0xA0, 0x01, 0x00, 0xA1]
     optimistic: true

(Remarks: The line “rx_pin: 4” is not necessary. Moreover the entity “WIFI Relais AUS” is obsolete either, I will delete it also.)

Meanwhile I found a documentation of the relay-board I am using. It is manufactured by LC Technology (www.chinalctech.com). Product name is “LC 5V WIFI Relay Module, LC-WM-Relay-8266-5V”. You can buy it for example on www.icstation.com. Besides others the manual includes a picture of the printed circuit (see attached an extract with the eight contacts of the ESP8266 on the board):
Ausschnitt pcb print

It seems that both, GPIO0 and GPIO2, are not connected anyway. I wonder how the communication with the serial mcu is done.

The documentation of the relay-board describes the programming by a mobile phone app. But this is not what I want to do as I want to flash it with ESPhome.

Anyway, I have contacted az delivery. They promised to send me a new 1-relay-board which switches the relay by GPIO pin, not by serial port.

Hope it comes true.

You just told us gpio2 is not connected, so why use it?

1 Like

You are right! Makes no sense!
I modified the yaml-file, playing with the tx- and rx-pins according to the ESPhome manual:

“Please note that the naming of these two pins depends on the chosen perspective and can be ambiguous. For example, while the ESP might send (TX ) on pin A and receive (RX ) data on pin B, from the other device’s perspective these two pins are switched (i.e. it sends on pin B and receives on pin A). So you might need to try with the two pins switched if it doesn’t work immediately.”

Now yaml looks like this:

  baud_rate: 9600
  tx_pin: GPIO4

switch:
   - platform: template
     name: "WIFI Relais EIN/AUS"
     turn_on_action:
      - uart.write: [0xA0, 0x01, 0x01, 0xA2]
     turn_off_action:
      - uart.write: [0xA0, 0x01, 0x00, 0xA1]
     optimistic: true

Moreover I added this to my yaml-configuration file:

 includes:
   - my_custom_component.h

GPIO4 is the rx-pin of the ESP8266 and is connected with the rx-pin of the relay-board.
But finally: relay does not work. It’s a pity! I do not have any more ideas.

No it is not. Rx is GPIO3. Anyway, you want the ESP’s Tx pin, which is GPIO1

No the Tx on the ESP gets connected to the Rx on the relay. When one device Transmits, the other Receives.

2 Likes

Hurray, it works!
Setting tx_pin to GPIO1 according to nickrout’s advice makes my wifi relay becoming alive.

This post is especially for nickrout who had infinite patience with my low skills and gave important advices! Thank you very much, nickrout! And thanks to all of you, who have contributed to this topic.

image

In the yaml-file I had to set the logger baud rate to 0, so there is no data collision between logger data and serial port data.

The final yaml looks like this:

 uart:
  baud_rate: 9600
  tx_pin: GPIO1

switch:
   - platform: template
     name: "WIFI Relais EIN/AUS"
     turn_on_action:
      - uart.write: [0xA0, 0x01, 0x01, 0xA2]
     turn_off_action:
      - uart.write: [0xA0, 0x01, 0x00, 0xA1]
     optimistic: true
2 Likes

Hi,
I am writing to you, because I can see that you are busy with ESPHome.

I followed your advice and put HA in Docker on Raspi 4 (8GB).
ESPHome works, but for me it’s a mess, has nothing to do with Arduino and I don’t understand anything about it, I have to copy and read a lot…

My problem:

it doesn’t matter which way I want to connect a “device” (pump), it doesn’t matter if it’s “MOFSET” or “Relay”, it doesn’t work…

As a relay: I can’t turn it off
As a “mofset”: nothing happens…

In EasyESP, however, both work, also in Tasmota on the same pin.

In Arduino I type:
ON pin HIHG
Off pin LOW

but there is nothing in ESPHome, neither of “high” or “low”

I also tried “ALLWAYS OFF”, the relay still works…

Do you have any advice for me, where and what should I change?

output:

  • platform: ledc

    pin: GPIO21

    id: buzzer_out

  • platform: gpio

    pin: GPIO25

    id: blumenpumpe1

switch:

  • platform: template

    name: “buropanel - buzzer”

    turn_on_action:

    then:

    - rtttl.play: "test:d=4,o=5,b=100:16e6,16e6,32p,8e6,16c6,8e6,8g6,8p,8g,8p"
    
  • platform: template

    id: blumenpumpe1_switch

    internal: true

    lambda: |-

    if (id(blumenpumpe1_state).state) {

    return true;
    

    } else {

    return false;
    

    }

    turn_on_action:

    • homeassistant.service:

      service: switch.turn_on

      data:

      entity_id: switch.buropanel_blumenpumpe1
      

    turn_off_action:

    • homeassistant.service:

      service: switch.turn_off

      data:

      entity_id: switch.buropanel_blumenpumpe1
      
  • platform: gpio

    name: “blumenpumpe1”

    restore_mode: ALWAYS_OFF

    pin:

    number: 25

    inverted: True

thank you very mutch

Hi @Betty-G , could you please edit your post and put code in proper tags - place three backticks ``` on a line on its own both before and after the code - see point 11 here How to help us help you - or How to ask a good question

hello, thank you very much for your message…

what does please mean
“insert correct tags”?

surely it is insufficient translation (browser in Win11 PC, automatic translation)

Do you mean?

  1. I should edit my question
  2. I should insert my complete code.

("")

May I please know what you need all my code for? This is a general question, everything else in the code works except the pump connected to ESP (which is also switchable with Nextion Display)

GPI25 works as “out” and the relay seems to be “up” because as soon as it gets power it turns on. I would like to define it, as in the arduino, but in ESPHome this definition “if… high” cannot be found…

Everything else works:
DHT sends the data
Bluetooth tracker sends data
Nextion shows all data…

only the relay output does not work

----------- //// ---------

But what I don’t understand is:

in the description it says:
that relay should be defined as “switch”…

But normally switches are “input” not “output”… at least in arduino, or tasmota, or easyesp…

I assume that it has something to do with it… But I don’t have a direct switch… “Switching” is done in the browser, in HA, or on the display (“touch button”) or in the "Home Assistant App, and actually it should switch automatically, but until it works properly I can’t set it up for automation…

Yes, because you should post it as yaml, which you haven’t. See the other posts in this thread to see the difference.

Defining a relay is done like this

switch:
  - platform: gpio
    name: "blumenpumpe1"
    pin: GPIO25

Simple as that.