『瀚思彼岸』» 智能家居技术论坛

 找回密码
 立即注册
查看: 89|回复: 2

[求助] External Components要怎么写?

[复制链接]

4

主题

41

帖子

1162

积分

金牌会员

Rank: 6Rank: 6

积分
1162
金钱
1121
HASS币
0
发表于 昨天 00:32 | 显示全部楼层 |阅读模式
本帖最后由 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();
    }
  }
};

回复

使用道具 举报

50

主题

1923

帖子

7548

积分

论坛元老

Rank: 8Rank: 8

积分
7548
金钱
5615
HASS币
40
发表于 昨天 09:10 | 显示全部楼层
建议不要升级,回退
折腾精神永存,感恩感谢论坛每一位愿意分享和帮助过我的大佬,论坛有你更精彩
回复

使用道具 举报

1

主题

28

帖子

446

积分

中级会员

Rank: 3Rank: 3

积分
446
金钱
418
HASS币
0
发表于 昨天 11:07 | 显示全部楼层
https://github.com/eigger/espcom ... uartex#state-schema

下载到本地,亲测可用,灰常强大
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Archiver|手机版|小黑屋|Hassbian

GMT+8, 2025-2-22 22:07 , Processed in 0.046487 second(s), 25 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表