找回密码
 立即注册
查看: 259|回复: 1

[硬件DIY] 新玩具cem5881雷达

[复制链接]

44

主题

177

回帖

1803

积分

金牌会员

积分
1803
金钱
1582
HASS币
0
发表于 前天 08:13 | 显示全部楼层 |阅读模式

substitutions:
  devicename: cem5881-m11

esphome:
  name: $devicename
  on_boot:
    - switch.turn_off: switch1
    - delay: 5s
    - switch.turn_on: switch1
esp32:
  board: esp32-c3-devkitm-1
  framework:
    type: arduino

logger:

api:
ota:
  platform: esphome

wifi:
  networks:
  - ssid: "Xiaomi-4343"
    password: "3uf8583n"
  - ssid: "ChinaNet-3CEEVV"
    password: "12345678"
  use_address: 192.168.2.92
  ap:
    ssid: "Esp32C3-Cem5826 Hotspot"
    password: "12345678"

captive_portal:

web_server:
  port: 80
switch:
  - platform: gpio
    pin: 20
    name: 雷达power
    id: switch1
    on_turn_on:
      - delay: 5s
      - uart.write: "get_all\r\n"
  # - platform: template
  #   id: radar_mode_switch
  #   name: 雷达模块模式切换toggle
  #   icon: mdi:toggle-switch
  #   optimistic: True
  #   turn_on_action:
  #     - uart.write: "usart2led=0\r\n"
  #   turn_off_action:
  #     - uart.write: "usart2led=1\r\n"

#通过串口设置雷达检测阈值

#通过串口设置雷达检测阈值
number:
  - platform: template
    id: th1
    name: TH1运动灵敏度(缺省85)
    icon: mdi:radar
    mode: box
    min_value: 1
    max_value: 200
    initial_value: 15
    optimistic: true
    step: 1
    restore_value: true
    set_action:
      - uart.write: !lambda
                      int th1 = (int)ceil(x / 1.0);
                      std::string mss = "ThIn=" + to_string(th1) + "\r\n";
                      return std::vector<unsigned char>(mss.begin(), mss.end());
  - platform: template
    id: ThNormal1
    name: THN1微动灵敏度(缺省85)
    icon: mdi:radar
    mode: box
    min_value: 1
    max_value: 200
    initial_value: 15
    optimistic: true
    step: 1
    restore_value: true
    set_action:
      - uart.write: !lambda
                      int ThNormal1 = (int)ceil(x / 1.0);
                      std::string mss = "ThNormal=" + to_string(ThNormal1) + "\r\n";
                      return std::vector<unsigned char>(mss.begin(), mss.end());
  - platform: template
    id: EffNormal1
    name: EFFN微动检查概率(缺省0)
    icon: mdi:radar
    mode: box
    min_value: 0
    max_value: 100
    initial_value: 0
    optimistic: true
    step: 1
    restore_value: true
    set_action:
      - uart.write: !lambda
                      int EffNormal1 = (int)ceil(x / 1.0);
                      std::string mss = "EffNormal=" + to_string(EffNormal1) + "\r\n";
                      return std::vector<unsigned char>(mss.begin(), mss.end());
  - platform: template
    id: EffIn
    name: EFFI运动检查概率(缺省0)
    icon: mdi:radar
    mode: box
    min_value: 0
    max_value: 100
    initial_value: 10
    optimistic: true
    step: 1
    restore_value: true
    set_action:
      - uart.write: !lambda
                      int EffIn1 = (int)ceil(x / 1.0);
                      std::string mss = "EffIn=" + to_string(EffIn1) + "\r\n";
                      return std::vector<unsigned char>(mss.begin(), mss.end());
  # - platform: template
  #   id: led_ton1
  #   name: OUT维持时间(单位ms)
  #   icon: mdi:motion-sensor-off
  #   mode: box
  #   min_value: 100
  #   max_value: 100000
  #   initial_value: 2000
  #   optimistic: true
  #   step: 100
  #   restore_value: true
  #   unit_of_measurement: "ms"
  #   set_action:
  #     - uart.write: !lambda
  #                     int led_ton1 = (int)ceil(x / 100);
  #                     std::string mss = "led_ton=" + to_string(led_ton1) + "\r\n";
  #                     return std::vector<unsigned char>(mss.begin(), mss.end());                                          
button:
  - platform: template
    id: save
    name: 固化配置到Flash
    icon: mdi:check-circle-outline
    on_press:
      - uart.write: "save\r\n"
  - platform: template
    id: get_all
    name: 读取Flash配置
    icon: mdi:information-outline
    on_press:
      - uart.write: "get_all\r\n"
  # - platform: template
  #   id: get_version_button
  #   name: 获取雷达模块版本号
  #   icon: mdi:information
  #   on_press:
  #     - uart.write: "VER\r\n"
uart:
  - id: uart_bus
    tx_pin: GPIO4
    rx_pin: GPIO2
    baud_rate: 115200
    parity: NONE
    stop_bits: 1
    rx_buffer_size: 1024

external_components:
  - source:
      type: git
      url: https://github.com/ssieb/esphome_components
    components: [ serial ]

text_sensor:
  - platform: serial
    uart_id: uart_bus
    id: uart_readline
    name: UART元数据
    on_value:
      then:
        - lambda: |-
            auto raw = id(uart_readline).state;

            float speed_value = 0.0f;
            int signal_value = 0;
            std::string direction = "静止";
            
            size_t v_start = raw.find("v=");
            if (v_start != std::string::npos) {
                size_t num_start = v_start + 2;
               
                size_t num_end = raw.find_first_of(" ,", num_start);
                if (num_end == std::string::npos) {
                    num_end = raw.length();
                }
               
                std::string speed_str = raw.substr(num_start, num_end - num_start);
               
                char* endptr;
                speed_value = strtof(speed_str.c_str(), &endptr);
                if (endptr == speed_str.c_str()) {
                    speed_value = 0.0f;
                }
               
                if (speed_value > 0) {
                    direction = "靠近";
                } else if (speed_value < 0) {
                    direction = "远离";
                } else {
                    direction = "静止";
                }
            }
            
            size_t str_start = raw.find("str=");
            if (str_start != std::string::npos) {
                size_t num_start = str_start + 4;
               
                size_t num_end = raw.find_first_not_of("0123456789", num_start);
                if (num_end == std::string::npos) {
                    num_end = raw.length();
                }
               
                std::string signal_str = raw.substr(num_start, num_end - num_start);
               
                char* endptr;
                signal_value = strtol(signal_str.c_str(), &endptr, 10);
                if (endptr == signal_str.c_str()) {
                    signal_value = 0;
                }
            }
            
            id(target_speed).publish_state(speed_value);
            id(target_signal).publish_state(signal_value);
            id(target_direction).publish_state(direction);
  # - platform: template
  #   id: usart2led_status_sensor
  #   name: 工作模式toggle
  #   icon: mdi:toggle-switch

  - platform: template
    id: target_direction
    name: 目标方向
    icon: mdi:motion-sensor
  
  - platform: wifi_info
    ssid:
      name: Connected SSID
      icon: mdi:signal-variant
    ip_address:
      name: IP Address
      icon: mdi:ip-network
    mac_address:
      name: Mac Address
      icon: mdi:wifi-marker

sensor:
  - platform: template
    id: target_speed
    name: 目标速度km/h
    unit_of_measurement: "km/h"
    accuracy_decimals: 1
    icon: mdi:motion-sensor

  - platform: template
    id: target_signal
    name: 目标强度
    accuracy_decimals: 0
    icon: mdi:arrow-expand-horizontal


回复

使用道具 举报

innx 手机认证

25

主题

189

回帖

2810

积分

论坛DIY达人

积分
2810
金钱
2591
HASS币
30
发表于 前天 15:29 | 显示全部楼层
真便宜  8块钱就能买毫米波雷达
回复

使用道具 举报

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

本版积分规则

Archiver|手机版|小黑屋|Hassbian ( 晋ICP备17001384号-1 )

GMT+8, 2025-6-12 20:04 , Processed in 0.123225 second(s), 22 queries .

Powered by Discuz! X3.5

© 2001-2025 Discuz! Team.

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