Uart - Simple button that writes to the serial bus

I do have “in theory” a simple serial connection between two ESP32’s (Mini D1’s).

One writes a “Hello World” to the serial bus, the other one blinks the onboard LED shortly (200ms) when it reads the “Hello World” string on the serial connection.

I say “in theory” because the two are wirelessly connection using HC-12 modules, but I do not think that is relevant for my question. Just mentioning it for the full context (Prove me wrong).

When programming the transmitter with the Ardrino IDE using the following code the receiver blinks for 200 milliseconds every 2 seconds.

void setup() {
  Serial.begin(9600);
}

void loop() {
  Serial.print("Hello World");
  delay(2000);
}

At this point I think I have proven that the hardware works as intended.

Moving forward I would like to program the sender as a ESPHome device so I can write the “Hello World” string to the serial bus by means of clicking on the button entity in my Home Assistant installation.

I programmed the following yaml, expecting it to work.

esphome:
  name: hc12-hub
  friendly_name: HC12-Hub

esp32:
  board: esp32dev
  framework:
    type: arduino

# Enable logging
logger:

wifi:
  ssid: !secret wifi_ssid
  password: !secret wifi_password

uart:
  tx_pin: 1
  rx_pin: 3
  baud_rate: 9600

captive_portal:

button:
  - platform: uart
    name: "Blink Now"
    data: "Hello World\r\n"
    on_press:
      - uart.write: "Hello World\r\n"

First tried it without the on_press action but after googling I just added it to be sure, I also tried to remove the \r (Only a \n) and tried to remove the \r\n (Just the text).

But the onboard LED on the receiving ESP32 is not responding.

So my basic question is, what is the difference between “Serial.print(“Hello World”);” and “uart.write: “Hello World”” or how can I just print the string to the serial bus with ESPhome.

Sometimes you need to invert the pins on a UART interface.

Anything interesting in the logs if you activate debugging on both devices?

Is it possible to directly wire the devices to test without the HC-12?

My UART port setup is posted here.
Note the inverted parameters, which seems to be a required setting for an RS232 protocol interface.

Thanks a lot for the reply, let’s use your lamda to strip it down to the bare minimum.

button:
  - platform: template
    name: "Blink Now"
    on_press:
      - uart.write: !lambda |-
              std::string str = "Hello World";
              std::vector<uint8_t> vec(str.begin(), str.end());
              return vec;

Unfortunately this did not result to an success, so I tries adding the inverted options setting it to true and to false, but no change here.

Next up trying to see if I can get some logging from the “transmitter” as surgested.

Logging on transmitter ESP log:

[17:22:26][D][button:010]: 'Blink Now' Pressed.
[17:22:26][D][uart_debug:158]: >>> "Hello World"

However I do see this in the logging during startup of ESP:

[17:21:06][W][uart.arduino_esp32:192]: You're using the same serial port for logging and the UART component. Please disable logging over the serial port by setting logger->baud_rate to 0.

To make sure this is not interfaering I followed the instructions and added:
``
# Enable logging (But enable it to output to the local Serial port, as this one is used by HC-12)
logger:
baud_rate: 0


And this did the trick !!!!
1 Like