How to control wifi relay by serial port of the ESP8266

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.

Hi, thanks for your answer…

Sorry, but I don’t see why I should reveal my personal data and configuration publicly… Everyone in the world can read what we write here, the internet doesn’t forget… Only users are allowed to write, everyone is allowed to read and copy … This is not a “share your projects” page…

but troubleshooting…

As far as relays are concerned, I sent… There isn’t more of it in my yaml…

Your solution doesn’t work…

But I also read about the same problem on other sites… but ESP8266

here:
ESP8266 Relay (active low): how to prevent triggering on boot or reset (SOLVED) - #4 by wutr

I don’t know if I should also search for this file…

I have many relays myself, but none installed with ESPHome, but Arduino, Tasmota, EasyESP… But they are all the same in one thing:
there are different relays, “high/low” devices, so it has to be defined…

But that doesn’t work in ESPHome…

There are also different settings in Tasmota, I also have different relays, the 8-fold relays can also be connected differently, like the “single relays”… and the function (high/low) is also different…

But I still don’t quite understand why ESPHome is important for Home Assistant, all devices can be found via MQTT, even in Smartthings, which finds Sonoff devices (like Alexa and Google) very quickly, and Home Assistant without Smartthings is quite difficult does, with Sonoff, Ewelink, Tuya and Smarthome cloud devices… (there are few tricks, mostly with HACS)

Arduino also has a platform, “Cloud”, which apparently many don’t like, want to have “own cloud”, but it works…
Even, like Smarttings, is also free…

However, Home Assistant costs 5 EURO per month (Nabu Casa) and it only works partially…

I’m already considering not taking out a subscription to Nabu Casa for the next year, because if something only partially works, it’s of no value… Alexa also turns on the heating on the go, Amazon has almost everything is available and possible…

In Home Assistant I can’t even connect the relay, neither for the flower pump nor for the heating valve…

I bought a pi4 instead of a MiniPC, which would have been even cheaper…

what still annoys me:
at Amazon I pay almost the same for Prime, but I get support in my language and I can always be reached immediately…

I don’t pay anything with Google, but there is also great support in my language via chat immediately…

I don’t pay anything at Smartthings either, you’ve already paid enough for Samsung devices, but the support is also great…

The Arduino IDE is also free, and the support is also very good.

IOBrocker, I got support for NODERed last year, also in my language…

Home Assistant could be a good system, but not in this way…

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

that’s missing:

digitalWrite(Relay, HIGH);
pinMode(Relay, OUTPUT);
there are relays that work in reverse…
ON=LOW
OFF=HIGH

or the C+++ “language” as cmd…

on=1
off=0

I didn’t ask you to provide personal data. I asked you that when you provide your configuration, that you do so in the way the rules require, so that it appears as yaml, not as markup.

You posted this
output:

  • platform: ledc

    pin: GPIO21

    id: buzzer_out

Hard to say exactly what you intended as it is unreadable as yaml codde. I suspect you intended to post it as yaml, like this

output:
  - platform: ledc
    pin: GPIO21
    id: buzzer_out

But who knows as you can’t or won’t do a simple thing like post properly, despite being asked politely to do so.

Home Assistant costs nothng, Nabu Casa is optional and not essential.

Your choice, and nothing to do with choosing home assistant or esphome. Both work on a minipc.

ESP Home is simply a quick way to program an esp without having to write c or c++. If you are comfortable with the arduino ui and programming in c/c++ then all power to you. If you don’t like or cannot understand esphome then do not use it. Use arduino and mqtt.

I assume you mean in esphome. Which brings us back to not posting your code. We don’t want your personal stuff, just the relevant yaml, and the logs showing what doesn’t work.

hi. that was quick…

thanks…

I understand that getting data from others is the only way to improve anything, but I have over 50 devices…

When I click on “Download log data”, everything comes up, not individual devices…

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

how can I install ESPHome only on Pi? Without HA… like NodeRed… it also doesn’t work properly in HA recently… (“bad getawy”)

I don’t have MQTT and NodeRed in HA, so not in Docker…

ESPHome description is really an impertinence… Difficult to install without Linux knowledge…

On the HA pages there are at least “commands” that you should enter in PUTTY… But I can’t find any for ESPHome…

---------------------///////////-------------
So without nabucasa, HA has quite a few problems finding/connecting the devices…

Tuya doesn’t work at all…
Evelink is found, but not integrated… (also with NabuCasa) without Smartthings, HA can’t even find Sonoff, but Alexa, Google can find it without any problems…

Without Nabu Casa I would never have ended up at HA… I was looking for a way where the ESP’s would be integrated… After about 5 years, which have now passed since I started with it, I have However, I also found an SDK at Smartthings, which makes it possible to integrate ESPs… The installation on other platforms, such as Tasmota, EasyESP, Arduino (without line) is easier, like in ESPHome, or in HA. …

Are you doing this in esphome? Looks like you might be doing it in Home Assistant.

Really?

hi nick, thanks for your answer and support.

until a few days ago my system looked like this.

on pi4 = raspi OS 64bit vers.

With all hardware, i.e. WLAN and also LAN…

Mosquito and NodeRed also ran here, normally, on Raspi…

HA as “Core, Docker Verse” with “supervised”

As written: it worked…

I got calls in HA to update “NetworkManager”, I did that and since then HA hasn’t started anymore…

Yes, ESPHome was here in HA because I didn’t manage to install neither in Windows nor in Pi…

I do not understand the description, unfortunately it is described chaotically, but I did not find sufficient help on the Internet either, apparently many users are a bit overwhelmed (especially win users)

It is not understandable where and what to install…

In HA or in pi?

I wanted to run ESPHome other than HA because the Raspi4 has no issues creating/updating files, only the Pi3…

Raspberry is ok…

So I took my other Pi (the Pi3) where HA is installed as the OS to be able to continue using HomeAssistant at all…

I also changed my MQTT back, to HA…

Unfortunately I don’t have ESPHome anymore…

HA (on pi3) found the devices, also added them, but I can’t even “execute update”…

I will keep trying to install ESPHome somehow or do I have to reinstall all in Arduino…

Since this “network manager problem” NodeRed doesn’t work either, and it also has a problem in Pi 3… Hue Magic I had to press the button 5 times and “integrate”, and only then HA doesn’t work anymore asked about it when I deleted the complete NodeRed folder… Only now my automation is gone too… I need to find a solution how to get from one Node to another now without NodeRed and without EspHome, especially the Temperatures from Phillips Hue and the BLE measurements (BLE tracker)

If you have any idea as a solution, I’d appreciate it… (thanks for that)

You seem to have a lot of errors that are probably off topic in this thread. Maybe your network manager problem is sorted here. Supervisor unsupported after latest update 2022.09.1

Hello,

I don’t know what this “network manager” does, but it doesn’t matter either…

HA OS is running on pi3… after update (10.2l without problems, with MQTT+ESPHome-Nodered…

Pi 4 reinstall raspi.

how esphome on raspi os?

How to install esphome on raspberry pi OS - I would use it via docker per this page Getting Started with the ESPHome Command Line — ESPHome - which page I have already referred you to.

You really are off topic. Please start another topic, @ me in so I see it. This topic is bout controlling a wifi relay by serial port of esp8266, not about installing esphome.