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

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

[技术探讨] 求助!!!ESPHOME向串口发送传感器值?

[复制链接]

5

主题

190

帖子

926

积分

高级会员

Rank: 4

积分
926
金钱
731
HASS币
20
发表于 2022-10-4 19:52:26 | 显示全部楼层 |阅读模式
ESP32C3 接陶金驰串口屏,把编码器值通过串口发送给串口屏N0.val,串口屏需要发送字符格式加三个0xFF结束符,怎么把传感器的值转为字符串?
ESPHOME关键代码
sensor:
  - platform: rotary_encoder
    name: "Rotary Encoder"
    pin_a: GPIO05
    pin_b: GPIO08
    publish_initial_value: True
    min_value: 1
    max_value: 100
    id: Rotary1
    on_value:
      - uart.write: "n0.val="
      - uart.write: !lambda |-
         return {id(Rotary1).state};

      - uart.write: [0xFF, 0XFF, 0xFF]
这个有在48-57串口屏会正确接收到1-9
[19:44:42][D][text_sensor:067]: 'harotray': Sending state '48'
[19:44:42][D][uart_debug:158]: >>> "n0.val=0\xFF\xFF\xFF"
[19:44:46][D][sensor:127]: 'Rotary Encoder': Sending state 49.00000 steps with 0 decimals of accuracy
[19:44:46][D][homeassistant.text_sensor:017]: 'sensor.rotary_encoder': Got state '49'
[19:44:46][D][text_sensor:067]: 'harotray': Sending state '49'
[19:44:46][D][uart_debug:158]: >>> "n0.val=1\xFF\xFF\xFF"
[19:44:46][D][sensor:127]: 'Rotary Encoder': Sending state 50.00000 steps with 0 decimals of accuracy
[19:44:46][D][homeassistant.text_sensor:017]: 'sensor.rotary_encoder': Got state '50'
[19:44:46][D][text_sensor:067]: 'harotray': Sending state '50'
[19:44:46][D][uart_debug:158]: >>> "n0.val=2\xFF\xFF\xFF"
[19:44:47][D][sensor:127]: 'Rotary Encoder': Sending state 51.00000 steps with 0 decimals of accuracy
[19:44:47][D][homeassistant.text_sensor:017]: 'sensor.rotary_encoder': Got state '51'
[19:44:47][D][text_sensor:067]: 'harotray': Sending state '51'
[19:44:47][D][uart_debug:158]: >>> "n0.val=3\xFF\xFF\xFF"
[19:44:47][D][sensor:127]: 'Rotary Encoder': Sending state 52.00000 steps with 0 decimals of accuracy
[19:44:47][D][homeassistant.text_sensor:017]: 'sensor.rotary_encoder': Got state '52'
[19:44:47][D][text_sensor:067]: 'harotray': Sending state '52'
[19:44:47][D][uart_debug:158]: >>> "n0.val=4\xFF\xFF\xFF"
用了- uart.write: !lambda |-
          char str[20];
          sprintf(str, "%lf", id(Rotary1).state);
          return str
编译报错:'str' from 'char [20]' to 'std::vector<unsigned cha
回复

使用道具 举报

0

主题

38

帖子

936

积分

高级会员

Rank: 4

积分
936
金钱
898
HASS币
0
发表于 2022-10-5 10:10:54 来自手机 | 显示全部楼层
大佬加油
回复

使用道具 举报

25

主题

224

帖子

1032

积分

金牌会员

007潜水了

Rank: 6Rank: 6

积分
1032
金钱
808
HASS币
0
发表于 2023-10-29 14:47:31 | 显示全部楼层
本帖最后由 demacia 于 2023-10-29 14:48 编辑

https://bbs.hassbian.com/thread-23164-1-1.html
这个帖有lambda的demo,uartwrite返回值是个vector
回复

使用道具 举报

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

本版积分规则

Archiver|手机版|小黑屋|Hassbian

GMT+8, 2024-11-26 14:51 , Processed in 0.090712 second(s), 25 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

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