#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;
ESP_LOGD("DmsrCustom", data);
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();
}
}
};
解决了 加两行代码就ok了 |