请选择 进入手机版 | 继续访问电脑版

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

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

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

[复制链接]

4

主题

182

帖子

871

积分

高级会员

Rank: 4

积分
871
金钱
684
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

帖子

926

积分

高级会员

Rank: 4

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

使用道具 举报

17

主题

163

帖子

758

积分

高级会员

大B头,背背机,舞池里的007。

Rank: 4

积分
758
金钱
595
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-3-29 04:13 , Processed in 0.305142 second(s), 24 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

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