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

 找回密码
 立即注册
查看: 11848|回复: 53

[人体存在] (更新)CEM5825F人体存使用esphome通过串口连入HA

  [复制链接]

2

主题

23

帖子

290

积分

中级会员

Rank: 3Rank: 3

积分
290
金钱
267
HASS币
0
发表于 2022-8-10 16:42:51 | 显示全部楼层 |阅读模式
本帖最后由 qqjil 于 2022-11-23 10:17 编辑

增加了测试模式str数据的上传,yaml文件和C++文件都需要替换,发射误报的时候可以查看数据排除误报情况。

更新编译警告问题,看评论有个警告,我看了下mth1设置发命令实际会失败,仔细看了下代码没发现问题,后面把mth2的复制过去就可以,增加了i2c获取bh1750的代码默认被注释需要取消i2c和bh17502处的注释。
esphome:
  name: CEM5825F
  
  includes:
  - UartReadLineSensor.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: "WIFI-2"
    password: "12345678"  
  - ssid: "WIFI-3"
    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

i2c:
  sda: GPIO21
  scl: GPIO22
  scan: true
  id: bus_a
  
# switch:
#   - platform: safe_mode
#     internal: true
#     name: use_safe_mode

# binary_sensor:
# - platform: gpio
#   name: mmwave_in_bedroom
#   icon: mdi:motion-sensor
#   pin:
#     number: GPIO5
#     mode: INPUT_PULLDOWN
    
output:
  - platform: ledc
    pin: GPIO4
    id: gpio_4

# Example usage in a light
light:
  - platform: monochromatic
    output: gpio_4
    name: "led Light"
  
    
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: mth1
    name: "mth1 3.5米内灵敏度设置"
    mode: box
    min_value: 40
    max_value: 400
    initial_value: 60
    optimistic: true
    step: 5
    restore_value: true
    #unit_of_measurement: m
    set_action:
      - uart.write: !lambda
                      int mth1 = (int)ceil(x / 1.0);
                      std::string mss = "mth1=" + to_string(mth1) + "\r\n";
                      return std::vector<unsigned char>(mss.begin(), mss.end());
                      
  - platform: template
    id: mth2
    name: "mth2 3.5~10米灵敏度设置"
    mode: box
    min_value: 40
    max_value: 400
    initial_value: 55
    optimistic: true
    step: 5
    restore_value: true
    #unit_of_measurement: ms
    set_action:
      - uart.write: !lambda
                      int mth2 = (int)ceil(x / 1.0);
                      std::string mss = "mth2=" + to_string(mth2) + "\r\n";
                      return std::vector<unsigned char>(mss.begin(), mss.end());
                      
  - platform: template
    id: mth3
    name: "mth3 10米以上灵敏度设置"
    mode: box
    min_value: 40
    max_value: 400
    initial_value: 40
    optimistic: true
    step: 50
    restore_value: true
    #unit_of_measurement: ms
    set_action:
      - uart.write: !lambda
                      int mth3 = (int)ceil(x / 1.0);
                      std::string mss = "mth3=" + to_string(mth3) + "\r\n";
                      return std::vector<unsigned char>(mss.begin(), mss.end());
  - platform: template
    id: rmax
    name: "rmax 最大检测距离"
    mode: box
    min_value: 10
    max_value: 1400
    initial_value: 600
    optimistic: true
    step: 1
    restore_value: true
    unit_of_measurement: "厘米"
    set_action:
      - uart.write: !lambda
                      float rmax = (float)(x / 100.0);
                      std::string mss = "rmax=" + to_string(rmax) + "\r\n";
                      return std::vector<unsigned char>(mss.begin(), mss.end());                      
  - platform: template
    id: tons
    name: "切换无人时间"
    mode: box
    min_value: 1
    max_value: 100
    initial_value: 15
    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) + "\r\n";
                      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) + "\r\n";
                      return std::vector<unsigned char>(mss.begin(), mss.end());
                      
button:
  - platform: template
    id: save
    name: "save 保存"
    on_press:
      - uart.write: "save\r\n"

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

  - platform: template
    id: testmode
    name: "testmode 测试模式"
    on_press:
      - uart.write: "test_mode=1\r\n"

  - platform: template
    id: normalmode
    name: "normalmode 默认模式"
    on_press:
      - uart.write: "test_mode=0\r\n"

  - platform: template
    id: initial
    name: "initial 恢复出厂设置"
    on_press:
      - uart.write: "initial\r\n"

sensor:
- platform: custom
  lambda: |-
    auto my_custom_sensor = new UartReadLineSensor(id(uart_bus));
    App.register_component(my_custom_sensor);
    return {my_custom_sensor->distance_sensor,my_custom_sensor->movmagsensor,my_custom_sensor->occmagsensor};
  sensors:
    - name: "dis距离"
      id: "uart_readline"
      unit_of_measurement: "厘米"
      icon: mdi:signal-distance-variant
    - name: "mov能量值"
      id: "mov"
      unit_of_measurement: "E"
    - name: "occ能量值"
      id: "occ"
      unit_of_measurement: "E"
      # accuracy_decimals: 1
    # - name: "mag能量值"
    #   id: "mag"
# - platform: hdc1080
#   temperature:
#     name: "Living Room Temperature"
#   humidity:
#     name: "Living Room Humidity"
#   update_interval: 10s
- platform: bh1750
  name: "BH1750 Illuminance"
  #address: 0x5C
  update_interval: 30s
  
text_sensor:
  - platform: template
    name: "mmWave状态"
    id: "mmWave"
c++文件,重命名为UartReadLineSensor.h放置到esphome文件目录下面
#include "esphome.h"

class UartReadLineSensor : public Component, public UARTDevice, public Sensor{
 public:
  UartReadLineSensor(UARTComponent *parent) : UARTDevice(parent) {}
  Sensor *distance_sensor = new Sensor();
  Sensor *movmagsensor = new Sensor();
  Sensor *occmagsensor = new Sensor();

  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 * dis = NULL;
        static char * distemp = NULL;
        static char * mag = NULL;
        static char * magtemp = NULL;
        static char * movflag = NULL;

        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) {
                        
                        if(buffer[0] == 0x6d && buffer[1] == 0x6f && buffer[2] == 0x76)
                        {
                                movflag = strtok(buffer,",");
                                distemp = strtok(NULL,"=");
                                dis = strtok(NULL,",");
                                if(uartdata > 16)
                                {
                                        magtemp = strtok(NULL,"=");
                                        mag = strtok(NULL,"\r");
                                        movmagsensor->publish_state(atof(mag));
                                }
                                distance_sensor->publish_state(atof(dis)*100);
                                
                                if(mov_flag != 0)
                                {
                                        mov_flag = 0;
                                        id(mmWave).publish_state("运动");                
                                }
                        }
                        else if(buffer[0] == 0x6f && buffer[1] == 0x63 && buffer[2] == 0x63)
                        {
                                movflag = strtok(buffer,",");
                                distemp = strtok(NULL,"=");
                                dis = strtok(NULL,",");
                                if(uartdata > 16)
                                {
                                        magtemp = strtok(NULL,"=");
                                        mag = strtok(NULL,"\r");
                                        occmagsensor->publish_state(atof(mag));
                                }
                                distance_sensor->publish_state(atof(dis)*100);
                                
                                if(mov_flag != 1)
                                {
                                        mov_flag = 1;
                                        id(mmWave).publish_state("静止");
                                }                                                
                        }
                        else if(buffer[0] == 0x6e && buffer[1] == 0x75 && buffer[2] == 0x6C)
                        {
                                if(mov_flag != 2)
                                {
                                        mov_flag = 2;
                                        id(mmWave).publish_state("无人");
                                        distance_sensor->publish_state(0);
                                        occmagsensor->publish_state(0);
                                        movmagsensor->publish_state(0);
                                }
                        }
                        else if(buffer[0] == 0x72 && buffer[1] == 0x6d && buffer[2] == 0x61 && buffer[3] == 0x78 && buffer[4] == 0x20 && buffer[5] == 0x69 && buffer[6] == 0x73 && buffer[7] == 0x20 )
                        {
                                j = 0;
                                if(uartdata == 12)
                                {
                                        for(i = 8; i < uartdata; i++)
                                        {
                                                chartemp[j] = buffer[i];
                                                j++;
                                        }
                                        id(rmax).publish_state(atof(chartemp)*100);
                                        
                                }
                                if(uartdata == 13)
                                {
                                        for(i = 8; i < uartdata; i++)
                                        {
                                                chartemp5[j] = buffer[i];
                                                j++;
                                        }
                                        id(rmax).publish_state(atof(chartemp5)*100);
                                }                                
                        }
                        else if(buffer[0] == 0x6d && buffer[1] == 0x74 && buffer[2] == 0x68 && buffer[3] == 0x31 && buffer[4] == 0x20 && buffer[5] == 0x69 && buffer[6] == 0x73 && buffer[7] == 0x20 )
                        {
                                j = 0;
                                if(uartdata == 10)
                                {
                                        for(i = 8; i < uartdata; i++)
                                        {
                                                chartemp2[j] = buffer[i];
                                                j++;
                                        }
                                        id(mth1).publish_state(atoi(chartemp2));                                        
                                }
                                if(uartdata == 11)
                                {
                                        for(i = 8; i < uartdata; i++)
                                        {
                                                chartemp3[j] = buffer[i];
                                                j++;
                                        }
                                        id(mth1).publish_state(atoi(chartemp3));                                        
                                }
                                if(uartdata == 12)
                                {
                                        for(i = 8; i < uartdata; i++)
                                        {
                                                chartemp[j] = buffer[i];
                                                j++;
                                        }
                                        id(mth1).publish_state(atoi(chartemp));                                        
                                }
                        }
                        else if(buffer[0] == 0x6d && buffer[1] == 0x74 && buffer[2] == 0x68 && buffer[3] == 0x32 && buffer[4] == 0x20 && buffer[5] == 0x69 && buffer[6] == 0x73 && buffer[7] == 0x20 )
                        {
                                j = 0;
                                if(uartdata == 10)
                                {
                                        for(i = 8; i < uartdata; i++)
                                        {
                                                chartemp2[j] = buffer[i];
                                                j++;
                                        }
                                        id(mth2).publish_state(atoi(chartemp2));                                        
                                }
                                if(uartdata == 11)
                                {
                                        for(i = 8; i < uartdata; i++)
                                        {
                                                chartemp3[j] = buffer[i];
                                                j++;
                                        }
                                        id(mth2).publish_state(atoi(chartemp3));                                        
                                }
                                if(uartdata == 12)
                                {
                                        for(i = 8; i < uartdata; i++)
                                        {
                                                chartemp[j] = buffer[i];
                                                j++;
                                        }
                                        id(mth2).publish_state(atoi(chartemp));                                        
                                }
                        }
                        else if(buffer[0] == 0x6d && buffer[1] == 0x74 && buffer[2] == 0x68 && buffer[3] == 0x33 && buffer[4] == 0x20 && buffer[5] == 0x69 && buffer[6] == 0x73 && buffer[7] == 0x20 )
                        {
                                j = 0;
                                if(uartdata == 10)
                                {
                                        for(i = 8; i < uartdata; i++)
                                        {
                                                chartemp2[j] = buffer[i];
                                                j++;
                                        }
                                        id(mth3).publish_state(atoi(chartemp2));                                        
                                }
                                if(uartdata == 11)
                                {
                                        for(i = 8; i < uartdata; i++)
                                        {
                                                chartemp3[j] = buffer[i];
                                                j++;
                                        }
                                        id(mth3).publish_state(atoi(chartemp3));                                        
                                }
                                if(uartdata == 12)
                                {
                                        for(i = 8; i < uartdata; i++)
                                        {
                                                chartemp[j] = buffer[i];
                                                j++;
                                        }
                                        id(mth3).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));                                        
                                }                                
                        }
                        memset(chartemp,0,4);
                        memset(chartemp3,0,3);
                        memset(chartemp2,0,2);
                        memset(chartemp5,0,2);
                }
    }
  }
};





显示效果

显示效果

评分

参与人数 4金钱 +45 收起 理由
情非殇 + 10 论坛有你更精彩!
phofun + 5 感谢楼主分享!
jjcs + 10 论坛有你更精彩!
whxciotw + 20 高手,这是高手!

查看全部评分

回复

使用道具 举报

54

主题

317

帖子

1371

积分

金牌会员

Rank: 6Rank: 6

积分
1371
金钱
1054
HASS币
0
发表于 2022-8-10 17:08:19 来自手机 | 显示全部楼层
谢谢分享,太实用了
回复

使用道具 举报

2

主题

711

帖子

5045

积分

论坛元老

Rank: 8Rank: 8

积分
5045
金钱
4334
HASS币
85
发表于 2022-8-10 17:11:45 | 显示全部楼层
感謝分享!!
請問UartReadLineSensor.h是放到esphome/內嗎?
回复

使用道具 举报

2

主题

23

帖子

290

积分

中级会员

Rank: 3Rank: 3

积分
290
金钱
267
HASS币
0
 楼主| 发表于 2022-8-10 17:28:07 | 显示全部楼层
nice2943 发表于 2022-8-10 17:11
感謝分享!!
請問UartReadLineSensor.h是放到esphome/內嗎?

放到esphome/config内就行了
回复

使用道具 举报

35

主题

401

帖子

2565

积分

金牌会员

Rank: 6Rank: 6

积分
2565
金钱
2164
HASS币
10
发表于 2022-8-10 19:39:12 | 显示全部楼层
学习了。请问硬件应该如何配置?
回复

使用道具 举报

14

主题

262

帖子

2223

积分

金牌会员

Rank: 6Rank: 6

积分
2223
金钱
1956
HASS币
50
发表于 2022-8-10 19:47:02 | 显示全部楼层
谢谢分享
回复

使用道具 举报

47

主题

1655

帖子

6684

积分

论坛元老

Rank: 8Rank: 8

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

使用道具 举报

3

主题

78

帖子

765

积分

高级会员

Rank: 4

积分
765
金钱
687
HASS币
0
发表于 2022-8-10 21:13:00 | 显示全部楼层
谢谢分享 问一下接线是
VRX接D17
VTX接D16
V0是接哪里呢
回复

使用道具 举报

54

主题

317

帖子

1371

积分

金牌会员

Rank: 6Rank: 6

积分
1371
金钱
1054
HASS币
0
发表于 2022-8-10 21:19:03 来自手机 | 显示全部楼层
rkevin 发表于 2022-8-10 21:13
谢谢分享 问一下接线是
VRX接D17
VTX接D16

你的雷达模块有V0?
回复

使用道具 举报

3

主题

78

帖子

765

积分

高级会员

Rank: 4

积分
765
金钱
687
HASS币
0
发表于 2022-8-10 21:27:42 | 显示全部楼层
tiani 发表于 2022-8-10 21:19
你的雷达模块有V0?


                               
登录/注册后可看大图

中间那个啊
回复

使用道具 举报

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

本版积分规则

Archiver|手机版|小黑屋|Hassbian

GMT+8, 2024-11-25 08:35 , Processed in 0.092012 second(s), 35 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

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