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

 找回密码
 立即注册
查看: 6146|回复: 22

[人体存在] CEM5855H毫米波雷达使用esphome通过串口连入HA可修改参数

[复制链接]

2

主题

23

帖子

290

积分

中级会员

Rank: 3Rank: 3

积分
290
金钱
267
HASS币
0
发表于 2022-8-13 13:07:30 | 显示全部楼层 |阅读模式
本帖最后由 qqjil 于 2022-8-13 13:07 编辑

前几天写了个CEM5825F的通过串口接入HA,按照一样的思路把5855H的串口也接入了HA,LD1115H有几条串口命令不一样,一样的也是生效的,也能显示运动状态。
https://bbs.hassbian.com/thread-17438-1-1.html CEM5825F的参考链接使用5855H和LD1115H对比了下,发现5855H误报情况比1115H要好,但是5855H有个问题是有时候触发非常不灵敏,就找卖家看能不能改到1115H固件,要了好久卖家给了我2条命令“ind_min_mov=1”和”ind_min_occ=2“,改了下效果立马变好,我也把这2条命令加进去了,有此问题的可以试一下。
yaml配置文件
esphome:
  name: cem5855h
  
  includes:
  - UartReadLineSensor_55h.h

esp32:
  board: esp32dev

# Enable logging
logger:
  #level: VERBOSE #makes uart stream available in esphome logstream
  #baud_rate: 0 #disable logging over uart

# Enable Home Assistant API
api:

ota:
  password: "password"

wifi:
  networks:
  - ssid: "WIFI"
    password: "12345678"
  - ssid: "WIFI2"
    password: "12345678"  
  - ssid: "WIFI3"
    password: "12345678"    
  # Enable fallback hotspot (captive portal) in case wifi connection fails
  ap:
    ssid: "mmWave-Occupancy"
    password: "password"

substitutions:
  device_name: mmwave-sensor
  
captive_portal:

web_server:
  port: 80
  version: 2
  include_internal: true

http_request:
  useragent: esphome/$device_name
  timeout: 2s

# switch:
#   - platform: safe_mode
#     internal: true
#     name: use_safe_mode

binary_sensor:
- platform: gpio
  name: mmwave_in_bedroom
  pin:
    number: GPIO5
    mode: INPUT_PULLDOWN

uart:
  id: uart_bus
  tx_pin: GPIO16
  rx_pin: GPIO17
  baud_rate: 115200
  # debug:
  #   direction: BOTH
  #   dummy_receiver: true
  #   after:
  #     delimiter: "\n"
  #   sequence:
  #     - lambda: UARTDebug::log_string(direction, bytes);

number:
  - platform: template
    id: th1
    name: "th1 移动灵敏度"
    mode: box
    min_value: 130
    max_value: 1000
    initial_value: 130
    optimistic: true
    step: 50
    restore_value: true
    #unit_of_measurement: m
    set_action:
      - uart.write: !lambda
                      int th1 = (int)ceil(x / 1.0);
                      std::string mss = "th1=" + to_string(th1);
                      return std::vector<unsigned char>(mss.begin(), mss.end());
                      
  - platform: template
    id: th2
    name: "th2 静止灵敏度"
    mode: box
    min_value: 200
    max_value: 1000
    initial_value: 250
    optimistic: true
    step: 50
    restore_value: true
    #unit_of_measurement: ms
    set_action:
      - uart.write: !lambda
                      int th2 = (int)ceil(x / 1.0);
                      std::string mss = "th2=" + to_string(th2);
                      return std::vector<unsigned char>(mss.begin(), mss.end());
                      
  - platform: template
    id: th_in
    name: "th_in 触发灵敏度"
    mode: box
    min_value: 130
    max_value: 1000
    initial_value: 360
    optimistic: true
    step: 50
    restore_value: true
    #unit_of_measurement: ms
    set_action:
      - uart.write: !lambda
                      int th_in = (int)ceil(x / 1.0);
                      std::string mss = "th_in=" + to_string(th_in);
                      return std::vector<unsigned char>(mss.begin(), mss.end());
                      
  - platform: template
    id: tons
    name: "GPIO 延时时间"
    mode: box
    min_value: 1
    max_value: 100
    initial_value: 30
    optimistic: true
    step: 1
    restore_value: true
    unit_of_measurement: "秒"
    set_action:
      - uart.write: !lambda
                      int tons = (int)ceil(x / 1.0);
                      std::string mss = "tons=" + to_string(tons);
                      return std::vector<unsigned char>(mss.begin(), mss.end());
                      
  - platform: template
    id: utons
    name: "uart 帧间隔"
    mode: box
    min_value: 50
    max_value: 1000
    initial_value: 100
    optimistic: true
    step: 50
    restore_value: true
    unit_of_measurement: "毫秒"
    set_action:
      - uart.write: !lambda
                      int utons = (int)ceil(x / 1.0);
                      std::string mss = "utons=" + to_string(utons);
                      return std::vector<unsigned char>(mss.begin(), mss.end());
                      
  - platform: template
    id: ind_min_mov
    name: "ind_min_mov 移动幅度"
    mode: box
    min_value: 0
    max_value: 5
    initial_value: 1
    optimistic: true
    step: 1
    restore_value: true
    #unit_of_measurement: m
    set_action:
      - uart.write: !lambda
                      int ind_min_mov = (int)ceil(x / 1.0);
                      std::string mss = "ind_min_mov=" + to_string(ind_min_mov);
                      return std::vector<unsigned char>(mss.begin(), mss.end());
                      
  - platform: template
    id: ind_min_occ
    name: "ind_min_occ 静止幅度"
    mode: box
    min_value: 0
    max_value: 5
    initial_value: 2
    optimistic: true
    step: 1
    restore_value: true
    #unit_of_measurement: m
    set_action:
      - uart.write: !lambda
                      int ind_min_occ = (int)ceil(x / 1.0);
                      std::string mss = "ind_min_occ=" + to_string(ind_min_occ);
                      return std::vector<unsigned char>(mss.begin(), mss.end());
button:
  - platform: template
    id: save
    name: "save 保存"
    on_press:
      - uart.write: "save"

  - platform: template
    id: get_all
    name: "get_all 获取参数"
    on_press:
      - uart.write: "get_all"

  - platform: template
    id: initial
    name: "initial 恢复出厂设置"
    on_press:
      - uart.write: "initial"
      
text_sensor:
- platform: custom
  lambda: |-
    auto my_custom_sensor = new UartReadLineSensor(id(uart_bus));
    App.register_component(my_custom_sensor);
    return {my_custom_sensor};
  text_sensors:
    id: "uart_readline"
    name: "mmWave状态"
C++文件UartReadLineSensor_55h.h
#include "esphome.h"

class UartReadLineSensor : public Component, public UARTDevice, public TextSensor{
 public:
  UartReadLineSensor(UARTComponent *parent) : UARTDevice(parent) {}

  void setup() override {
    // nothing to do here
  }

  int readline(int readch, char *buffer, int len)
  {
    static int pos = 0;
    int rpos;

    if (readch > 0) {
      switch (readch) {
        case '\n': // Ignore new-lines
          break;
        case '\r': // Return on CR
          rpos = pos;
          pos = 0;  // Reset position index ready for next time
          return rpos;
        default:
          if (pos < len-1) {
            buffer[pos++] = readch;
            buffer[pos] = 0;
          }
      }
    }
    // No end of line has been found, so return -1.
    return -1;
  }

  void loop() override {
    const int max_line_length = 80;
    static char buffer[max_line_length];
        static uint8_t mov_flag = 0;
        static int uartdata = 0;
        static char u16_dis[7] = {0};
        static char u16_dis_1[6] = {0};
        static char u16_dis_2[5] = {0};
        static int i,j;
        static float intdis = 0;
        static float f32rmax;
        static char chartemp[4] = {0};
        static char chartemp5[5] = {0};
        static char chartemp2[2] = {0};
        static char chartemp3[3] = {0};
        static uint8_t chartemp1 = 0;
        
    while (available()) {
                uartdata = readline(read(), buffer, max_line_length);
                if(uartdata > 0) {
                        //publish_state(buffer);
                         if(buffer[0] == 0x6d && buffer[1] == 0x6f && buffer[2] == 0x76)
                        {
                                if(mov_flag != 0)
                                {
                                        mov_flag = 0;
                                        publish_state("运动");
                                }
                        }
                        
                        else if(buffer[0] == 0x6f && buffer[1] == 0x63 && buffer[2] == 0x63)
                        {
                                if(mov_flag != 1)
                                {
                                        mov_flag = 1;
                                        publish_state("静止");
                                }
                                
                        }
                        else if(buffer[0] == 0x6e && buffer[1] == 0x75 && buffer[2] == 0x6C)
                        {
                                if(mov_flag != 2)
                                {
                                        mov_flag = 2;
                                        publish_state("无人");
                                }
                        }
                        else if(buffer[0] == 0x74 && buffer[1] == 0x68 && buffer[2] == 0x31 && buffer[3] == 0x20 && buffer[4] == 0x69 && buffer[5] == 0x73 && buffer[6] == 0x20 )
                        {
                                j = 0;
                                if(uartdata == 9)
                                {
                                        for(i = 7; i < uartdata; i++)
                                        {
                                                chartemp2[j] = buffer[i];
                                                j++;
                                        }
                                        id(th1).publish_state(atoi(chartemp2));                                        
                                }
                                if(uartdata == 10)
                                {
                                        for(i = 7; i < uartdata; i++)
                                        {
                                                chartemp3[j] = buffer[i];
                                                j++;
                                        }
                                        id(th1).publish_state(atoi(chartemp3));                                        
                                }
                                if(uartdata == 11)
                                {
                                        for(i = 7; i < uartdata; i++)
                                        {
                                                chartemp[j] = buffer[i];
                                                j++;
                                        }
                                        id(th1).publish_state(atoi(chartemp));                                        
                                }
                        }
                        else if(buffer[0] == 0x74 && buffer[1] == 0x68 && buffer[2] == 0x32 && buffer[3] == 0x20 && buffer[4] == 0x69 && buffer[5] == 0x73 && buffer[6] == 0x20 )
                        {
                                j = 0;
                                if(uartdata == 9)
                                {
                                        for(i = 7; i < uartdata; i++)
                                        {
                                                chartemp2[j] = buffer[i];
                                                j++;
                                        }
                                        id(th2).publish_state(atoi(chartemp2));                                        
                                }
                                if(uartdata == 10)
                                {
                                        for(i = 7; i < uartdata; i++)
                                        {
                                                chartemp3[j] = buffer[i];
                                                j++;
                                        }
                                        id(th2).publish_state(atoi(chartemp3));                                        
                                }
                                if(uartdata == 11)
                                {
                                        for(i = 7; i < uartdata; i++)
                                        {
                                                chartemp[j] = buffer[i];
                                                j++;
                                        }
                                        id(th2).publish_state(atoi(chartemp));                                        
                                }
                        }
                        else if(buffer[0] == 0x74 && buffer[1] == 0x6f && buffer[2] == 0x6e && buffer[3] == 0x73 && buffer[4] == 0x20 && buffer[5] == 0x69 && buffer[6] == 0x73 && buffer[7] == 0x20 )
                        {
                                j = 0;
                                if(uartdata == 9)
                                {
                                        chartemp1 = buffer[8]-48;
                                        id(tons).publish_state(chartemp1);                                        
                                }
                                if(uartdata == 10)
                                {
                                        for(i = 8; i < uartdata; i++)
                                        {
                                                chartemp2[j] = buffer[i];
                                                j++;
                                        }
                                        id(tons).publish_state(atoi(chartemp2));                                        
                                }
                                if(uartdata == 11)
                                {
                                        for(i = 8; i < uartdata; i++)
                                        {
                                                chartemp3[j] = buffer[i];
                                                j++;
                                        }
                                        id(tons).publish_state(atoi(chartemp3));                                        
                                }                                
                        }
                        else if(buffer[0] == 0x75 && buffer[1] == 0x74 && buffer[2] == 0x6f && buffer[3] == 0x6e && buffer[4] == 0x73 && buffer[5] == 0x20 && buffer[6] == 0x69 && buffer[7] == 0x73 && buffer[8] == 0x20 )
                        {
                                j = 0;
                                if(uartdata == 11)
                                {
                                        for(i = 9; i < uartdata; i++)
                                        {
                                                chartemp2[j] = buffer[i];
                                                j++;
                                        }
                                        id(utons).publish_state(atoi(chartemp2));                                        
                                }
                                if(uartdata == 12)
                                {
                                        for(i = 9; i < uartdata; i++)
                                        {
                                                chartemp3[j] = buffer[i];
                                                j++;
                                        }
                                        id(utons).publish_state(atoi(chartemp3));                                        
                                }        
                                if(uartdata == 13)
                                {
                                        for(i = 9; i < uartdata; i++)
                                        {
                                                chartemp[j] = buffer[i];
                                                j++;
                                        }
                                        id(utons).publish_state(atoi(chartemp));                                        
                                }                                
                        }
                        else if(buffer[0] == 0x74 && buffer[1] == 0x68 && buffer[2] == 0x5f && buffer[3] == 0x69 && buffer[4] == 0x6e && buffer[5] == 0x20 && buffer[6] == 0x69 && buffer[7] == 0x73 && buffer[8] == 0x20 )
                        {
                                j = 0;
                                if(uartdata == 11)
                                {
                                        for(i = 9; i < uartdata; i++)
                                        {
                                                chartemp2[j] = buffer[i];
                                                j++;
                                        }
                                        id(th_in).publish_state(atoi(chartemp2));                                        
                                }
                                if(uartdata == 12)
                                {
                                        for(i = 9; i < uartdata; i++)
                                        {
                                                chartemp3[j] = buffer[i];
                                                j++;
                                        }
                                        id(th_in).publish_state(atoi(chartemp3));                                        
                                }        
                                if(uartdata == 13)
                                {
                                        for(i = 9; i < uartdata; i++)
                                        {
                                                chartemp[j] = buffer[i];
                                                j++;
                                        }
                                        id(th_in).publish_state(atoi(chartemp));                                        
                                }                                
                        }
                        memset(chartemp,0,4);
                        memset(chartemp3,0,3);
                        memset(chartemp2,0,2);
                        memset(chartemp5,0,2);
                }
    }
  }
};



5855H.JPG

评分

参与人数 5金钱 +41 收起 理由
zxyny1989 + 12 感谢楼主分享!
jonceli + 8
jjcs + 10 在下对你的景仰犹如滔滔长江之水,连绵不绝.
情非殇 + 10 感谢楼主分享!
sxrcm + 1 高手,这是高手!

查看全部评分

回复

使用道具 举报

0

主题

215

帖子

2553

积分

金牌会员

Rank: 6Rank: 6

积分
2553
金钱
2338
HASS币
0
发表于 2022-8-13 13:41:16 | 显示全部楼层
本帖最后由 sxrcm 于 2022-10-20 11:45 编辑

给雷达大王点赞
回复

使用道具 举报

47

主题

1655

帖子

6684

积分

论坛元老

Rank: 8Rank: 8

积分
6684
金钱
5019
HASS币
40
发表于 2022-8-13 15:35:44 | 显示全部楼层
大佬牛逼
折腾精神永存,感恩感谢论坛每一位愿意分享和帮助过我的大佬,论坛有你更精彩
回复

使用道具 举报

0

主题

5

帖子

28

积分

新手上路

Rank: 1

积分
28
金钱
23
HASS币
0
发表于 2022-8-15 09:50:50 | 显示全部楼层
这个牛的,大佬牛逼!
回复

使用道具 举报

0

主题

28

帖子

788

积分

高级会员

Rank: 4

积分
788
金钱
760
HASS币
0
发表于 2022-8-24 17:25:31 | 显示全部楼层
哎,问个问题,无论5855还是1115, 调整的参数每次断电重启都恢复到出厂设置,我不清楚,ESPhome的固件,是否每次断电重启后,把存储的参数写入5855/1115?

一直困扰着我啊。
回复

使用道具 举报

2

主题

23

帖子

290

积分

中级会员

Rank: 3Rank: 3

积分
290
金钱
267
HASS币
0
 楼主| 发表于 2022-8-24 21:15:54 | 显示全部楼层
fjn1999 发表于 2022-8-24 17:25
哎,问个问题,无论5855还是1115, 调整的参数每次断电重启都恢复到出厂设置,我不清楚,ESPhome的固件,是 ...

你应该是设置完参数后没有发save命令或者没有点我固件上面的保存按钮,否则断电参数会重置的
回复

使用道具 举报

0

主题

53

帖子

463

积分

中级会员

Rank: 3Rank: 3

积分
463
金钱
410
HASS币
0
发表于 2022-8-31 18:37:48 | 显示全部楼层
大佬牛逼   
回复

使用道具 举报

2

主题

35

帖子

516

积分

高级会员

Rank: 4

积分
516
金钱
481
HASS币
0
发表于 2022-9-28 22:07:52 | 显示全部楼层
干货太干了,难以下咽,99%像我这种小白,看完跟看天书一样
回复

使用道具 举报

0

主题

53

帖子

463

积分

中级会员

Rank: 3Rank: 3

积分
463
金钱
410
HASS币
0
发表于 2022-9-28 22:57:53 | 显示全部楼层
这个牛的,大佬牛逼!
回复

使用道具 举报

39

主题

203

帖子

1625

积分

金牌会员

Rank: 6Rank: 6

积分
1625
金钱
1422
HASS币
0
发表于 2022-10-2 13:11:54 来自手机 | 显示全部楼层
不会切换无人
回复

使用道具 举报

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

本版积分规则

Archiver|手机版|小黑屋|Hassbian

GMT+8, 2024-11-25 06:47 , Processed in 0.391662 second(s), 37 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

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