好吧 你试试我的配置
esphome:
includes:
- my_custom_component_8266.h
# Enable logging
logger:
# Enable Home Assistant API
api:
ota:
captive_portal:
uart:
id: uart_bus
tx_pin: GPIO5
rx_pin: GPIO4
baud_rate: 9600
text_sensor:
- platform: custom
lambda: |-
auto my_custom = new MyCustomComponent(id(uart_bus));
App.register_component(my_custom);
return {my_custom->uart_text_sensors};
text_sensors:
name: "UartSensor"
h文件
#include "esphome.h"
using namespace esphome;
class MyCustomComponent : public Component, public uart::UARTDevice {
protected:
unsigned char telegram[100];
char data[100];
bool read_message() {
int i = 0;
if (available()) {
while (available()) {
telegram[i] = read();
data[i] = (char) telegram[i];
ESP_LOGD("DmsrCustom", "0x%x", telegram[i]);
i++;
}
data[i] = 0;
uart_text_sensors->publish_state(data);
//延迟3秒
// delay(3000);
//发布固定状态为off 配合需要重复触发的情况
// uart_text_sensors->publish_state("off");
}
return false;
}
public:
MyCustomComponent(UARTComponent *parent) : UARTDevice(parent) {}
TextSensor *uart_text_sensors = new TextSensor();
void setup() override {
// nothing to do here
}
void loop() override {
// Use Arduino API to read data, for example
if (available()) {
ESP_LOGD("DmsrCustom", "loop start");
read_message();
}
}
};
|