hello ,
i want via ESP32 via ESPhome , recieve serial data .
the hardware is okay
the software is immense and not working
my steps i did :
made 100% testcode on arduino uno to test my serial port and data. > this works fine
second step i did : , followed the tutorial for a custom sensor > this works fine
third step i did : followed the very short info for creating a custom uart
what can i do to let my esp know that the esp32 is recieving serial data ?
arduino code :
void setup() {
Serial.begin(19200 , SERIAL_8N2); // opens serial port, sets data rate to 9600 bps
Serial.setTimeout(100); // set new value to 100 milliseconds
}
void loop() {
// check if data is available
if (Serial.available() > 0) {
// read the incoming string
String incomingString = Serial.readString();
// prints the received data
Serial.print("I received: ");
//Serial.println(incomingString);
char hexArray[incomingString.length() * 2 + 1]; // Allocate memory for the char array
for (int i = 0; i < incomingString.length(); i++) {
sprintf(hexArray + i * 2, "%02x", incomingString[i]); // Convert each character to hexadecimal
}
hexArray[incomingString.length() * 2] = '\0'; // Add null terminator to the char array
Serial.println(hexArray); // Print the converted hexadecimal values
}
}
my yaml in esp code :
esphome:
name: "uarttester"
friendly_name: ESPHome Web 603b5c
includes:
- my_custom_sensor.h
- my_custom_uart.h
esp32:
board: esp32dev
framework:
type: arduino
substitutions: ##vincent peha
device_name : ESPehaVincent
pin_ingebouwdewifiblauweled : GPIO2
pin_ingebouwdestatusled : GPIO16
# Enable logging
logger:
# Enable Home Assistant API
api:
encryption:
key: "2vARB6saWdRJRkt7t4wzdEMX1YEQAWPAaE1x6LM65B8="
ota:
wifi:
ssid: !secret wifi_ssid
password: !secret wifi_password
# Enable fallback hotspot (captive portal) in case wifi connection fails
ap:
ssid: "Esphome-Web-603B5C"
password: "c9tjT38lpsi1"
captive_portal:
web_server:
port : 80
mqtt:
broker: !secret mqtt_broker #192.168.1.64
username: !secret mqtt_user
password: !secret mqtt_password
# Define UART bus connected to the RS-485 adapter with bus parameters
uart:
id: uart_bus
tx_pin: 17
rx_pin: 16
baud_rate: 19200
stop_bits: 2
text_sensor:
- platform: wifi_info
ip_address:
name: "${device_name}_IP"
# Example button configuration
button:
- platform: template
name: Livingroom Lazy Mood
id: my_button
# Optional variables:
icon: "mdi:emoticon-outline"
on_press:
- logger.log: "Button pressed"
- uart.write: "Hello World\r\n"
# Example configuration entry
sensor:
- platform: custom
lambda: |-
auto my_sensor = new MyCustomSensor();
App.register_component(my_sensor);
return {my_sensor};
sensors:
name: "My Custom Sensor"
custom_component:
- lambda: |-
auto my_custom = new MyCustomComponent(id(uart_bus));
return {my_custom};
my custom uart code :
#include "esphome.h"
class MyCustomComponent : public Component, public UARTDevice {
public:
MyCustomComponent(UARTComponent *parent) : UARTDevice(parent) {}
void setup() override {
// nothing to do here
// Serial.begin(19200 , SERIAL_8N2); // opens serial port, sets data rate to 9600 bps
Serial.setTimeout(100); // set new value to 100 milliseconds
}
void loop() override {
// check if data is available
if (Serial.available() > 0) {
// read the incoming string
String incomingString = Serial.readString();
// prints the received data
Serial.print("I received: ");
//Serial.println(incomingString);
char hexArray[incomingString.length() * 2 + 1]; // Allocate memory for the char array
for (int i = 0; i < incomingString.length(); i++) {
sprintf(hexArray + i * 2, "%02x", incomingString[i]); // Convert each character to hexadecimal
}
hexArray[incomingString.length() * 2] = '\0'; // Add null terminator to the char array
Serial.println(hexArray); // Print the converted hexadecimal values
}
}
};