本帖最后由 coolguy 于 2025-2-21 00:34 编辑
ESP home升级到最新版,Custom Components不能用了,需要改成External Components,问了几个AI,都写不对,只有来求助各位大佬了!
实现的功能就是读取UART RX 并设置到text sensor。
之前参考的帖子:
https://bbs.hassbian.com/thread-9794-1-1.html
https://bbs.hassbian.com/thread-23167-1-1.html
组件:
#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);
}
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();
}
}
};
|