Hi,
I’m trying to pass a button state (0 and 1) from an arduino board to an ESPHome connected to HA (ESP-01s). But I never find an correct configuration for the ESP… Can someone help me to configure the ESP Board?
Just setup gpio binary sensor on esphome and write pin high/low on arduino side. Be aware you need voltage divider or level shifter to connect any pin between esp and 5v arduino.
This is a good solution. Tanks.
But in general I want to know how to use a serial connection between Arduino and ESP… So in future if I need pass some information from Arduino and ESP, I’m abel to do.
Ps. I want to use arduino like an expansion because the ESP are full used in I/O
I’ve often considered using an Arduino as a I/O expansion board for, say, a Linux computer; communicating over serial.
You could employ the Software Serial library on both devices. The downside is that Software Serial is slow and is not interupt driven (requires a lot of handling) But then, you’re only transmitting a small amount of data.
You would create your own protocol. That is, when this byte is sent, perform this action. It’s not plug-and-play; you’re going to have to do some experimenting. (remember RX to TX and TX to RX )
If you’re going to use a lot of Arduinos, have a look at MySensors. One Arduino can act as a gateway to the radio network. I’d consider the RF Nano as it is an Arduino with an integrated radio. HA has an integration. This is probably much more than you’d like to take on right now, but a consideration for future projects.
It’s not so easy with esphome. For some reason unknown to me, esphome doesn’t have any sophisticated component for serial rx. You can use Uart component for tx and uart debug with dummy_receiver option like this:
uart:
baud_rate: 115200
debug:
direction: BOTH
dummy_receiver: false
after:
delimiter: "\n"
sequence:
- lambda: UARTDebug::log_string(direction, bytes);
Another option is external component. Have a look at this post:
Easy UART Parsing ESPHome Component - ESPHome - Home Assistant Community
I try this code… but doesn’t work and I never know how to pass the value from lambda to the sensor…
uart:
baud_rate: 115200
tx_pin: GPIO0
rx_pin: GPIO2
id: UART
debug:
direction: BOTH
dummy_receiver: false
#after:
# delimiter: "\n"
sequence:
- lambda: |-
UARTDebug::log_string(direction, bytes);
int button_state;
std::string str(bytes.begin(), bytes.end());
if (sscanf(str.c_str(), "%i", button_state) != 0) {
id(button1).publish_state(button_state);
}
else {
id(button1).publish_state(0);
}
sensor:
- platform: template
name: Pulsante
id: button1
filters:
- lambda: return x;
It all depends what you want to send.
If you just want binary input, do it with binary sensor like I wrote on post#2.
If you want numeric data, try this (0-255):
UARTDebug::log_string(direction, bytes);
int button_state = bytes[0];
id(button1).publish_state(button_state);
look here for other ideas