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

 找回密码
 立即注册
楼主: moe1983

[人体存在] 【更新3】无线调试、监控人体存在感应雷达数据(LD2410H)

  [复制链接]

19

主题

187

帖子

1198

积分

金牌会员

Rank: 6Rank: 6

积分
1198
金钱
1011
HASS币
0
发表于 2023-6-10 22:14:43 | 显示全部楼层
膜拜一下大佬
回复

使用道具 举报

0

主题

2

帖子

24

积分

新手上路

Rank: 1

积分
24
金钱
22
HASS币
0
发表于 2023-6-10 22:50:40 | 显示全部楼层
shafashafa
回复

使用道具 举报

0

主题

2

帖子

30

积分

新手上路

Rank: 1

积分
30
金钱
28
HASS币
0
发表于 2023-6-12 22:35:55 | 显示全部楼层
感谢感谢感谢感谢感谢
回复

使用道具 举报

2

主题

46

帖子

944

积分

高级会员

Rank: 4

积分
944
金钱
898
HASS币
0
发表于 2023-6-13 00:47:28 | 显示全部楼层
无线调试、监控人体存在感应雷达数据(LD
回复

使用道具 举报

0

主题

9

帖子

108

积分

注册会员

Rank: 2

积分
108
金钱
99
HASS币
0
发表于 2023-6-13 17:29:49 | 显示全部楼层
1、支持读取光敏数据
2、增加重启LD2410功能
3、打开
run_RTA
会自动打开DEBUG模式,方便调试

4、tx_pin: GPIO1

   rx_pin: GPIO3
   LD2410输出接: GPIO5  #D1

   SR602输出接: GPIO4  #D2
   总输出接: GPIO0  #D3(红外发现有人开始输出,打开调试模式,LD2410确定没人结束输出,关闭调试模式)

esphome代码
substitutions:
  name: "ld2410zwj"
  friendly_name: "杂物间传感器"

esphome:
  name: ${name}
  friendly_name: ${friendly_name}
  on_boot:
    priority: 700
    then:
      - wait_until: 
          api.connected:
      - lambda: id(config_mode).turn_on();
      - script.execute: get_all_params
      - uart.write: [0xFD,0xFC,0xFB,0xFA,0x02,0x00,0x63,0x00,0x04,0x03,0x02,0x01] #默认关闭工程模式
      - delay: 1s
      - lambda: id(config_mode).turn_off();
  includes:
    - ld2410h.h

esp8266:
  board: d1_mini
  restore_from_flash: true

# Enable logging
logger:
  #level: ERROR 
  baud_rate: 0 #disable logging over uart

# Enable Home Assistant API
api:

ota:

wifi:
  ssid: !secret wifi_ssid
  password: !secret wifi_password

  # Enable fallback hotspot (captive portal) in case wifi connection fails
  ap:
    ssid: ${name}
    password: "huaaiyicheng"
        
web_server:
  port: 80
  include_internal: true
  ota: false
  version: 2

script:
  - id: get_all_params
    then:
      - uart.write: [0xFD,0xFC,0xFB,0xFA,0x02,0x00,0x61,0x00,0x04,0x03,0x02,0x01]
      - delay: 1s

sensor:    
  - platform: custom
    lambda: |-
      auto s = new ld2410(id(uart_bus1));
      App.register_component(s);
      return {s->mov_distance_sensor, s->mov_energy, s->occ_distance_sensor, s->occ_energy, s->detect_distance_sensor, s->en_mov_0, s->en_mov_1, s->en_mov_2, s->en_mov_3, s->en_mov_4, s->en_mov_5, s->en_mov_6, s->en_mov_7, s->en_mov_8, s->en_occ_0, s->en_occ_1, s->en_occ_2, s->en_occ_3, s->en_occ_4, s->en_occ_5, s->en_occ_6, s->en_occ_7, s->en_occ_8, s->brightness};
      
    sensors:
      - name: 2410_mov_distance
        id: mov_distance_sensor
        #internal: true
        unit_of_measurement: m
        accuracy_decimals: 2
        icon: mdi:tape-measure
        filters:
          - lambda: return x/100;

      - name: 2410_mov_energy
        icon: mdi:run-fast
        id: mov_energy

      - name: 2410_occ_distance
        id: occ_distance_sensor
        #internal: true
        unit_of_measurement: m
        accuracy_decimals: 2
        icon: mdi:tape-measure
        filters:
          - lambda: return x/100;

      - name: 2410_occ_energy
        icon: mdi:motion-sensor
        id: occ_energy

      - name: 2410_detect_distance
        id: detect_distance_sensor
        #internal: true
        unit_of_measurement: m
        accuracy_decimals: 2
        icon: mdi:tape-measure
        filters:
          - lambda: return x/100;

      # 各个距离门能量值    
      - name: en_mov_0
        id: en_mov_0
      - name: en_mov_1
        id: en_mov_1
      - name: en_mov_2
        id: en_mov_2
      - name: en_mov_3
        id: en_mov_3
      - name: en_mov_4
        id: en_mov_4
      - name: en_mov_5
        id: en_mov_5
      - name: en_mov_6
        id: en_mov_6
      - name: en_mov_7
        id: en_mov_7
      - name: en_mov_8
        id: en_mov_8
      - name: en_occ_0
        id: en_occ_0
      - name: en_occ_1
        id: en_occ_1
      - name: en_occ_2
        id: en_occ_2
      - name: en_occ_3
        id: en_occ_3
      - name: en_occ_4
        id: en_occ_4
      - name: en_occ_5
        id: en_occ_5
      - name: en_occ_6
        id: en_occ_6
      - name: en_occ_7
        id: en_occ_7
      - name: en_occ_8
        id: en_occ_8
      - name: 2410_brightness
        icon: mdi:solar-power
        id: brightness

text_sensor:
  - platform: template
    name: 2410_detect_state
    id: target_state
        
uart:
  id: uart_bus1
  tx_pin: GPIO1
  rx_pin: GPIO3
  baud_rate: 256000
  parity: NONE
  stop_bits: 1
#  debug:
#    direction: BOTH
#    after:
#      delimiter: [0xF8, 0xF7, 0xF6, 0xF5]

binary_sensor:
  - platform: gpio
    id: human_presence_motion
    pin: GPIO5  #D1
    name: "d1mini_human_presence"
    device_class: presence
    on_release:
      then:
        - switch.turn_off: run_rta
        - light.turn_off: human_presence_light
    
  - platform: gpio
    id: human_presence_PIR
    pin: GPIO4  #D2
    name: "d1mini_human_presence_pir"
    device_class: motion
    on_press:
      then:
        - switch.turn_on: run_rta
        - light.turn_on: human_presence_light

output:
  - platform: gpio
    id: "human_presence"
    pin: GPIO0  #D3
    inverted: true

    
light:
  - platform: binary
    icon: mdi:motion-sensor
    id: human_presence_light
    output: "human_presence"
    name: "human presence light"

switch:
  - platform: template
    name: "Config mode"
    id: config_mode
    optimistic: true
    on_turn_on:
      - uart.write: [0xFD, 0xFC, 0xFB, 0xFA, 0x04, 0x00, 0xFF, 0x00, 0x01, 0x00, 0x04, 0x03, 0x02, 0x01]
      - delay: 1s
    on_turn_off:
      - uart.write: [0xFD, 0xFC, 0xFB, 0xFA, 0x02, 0x00, 0xFE, 0x00, 0x04, 0x03, 0x02, 0x01]
      - delay: 1s
      
  - platform: template
    name: run_RTA    #打开实时监控
    id: run_rta
    optimistic: true
    on_turn_on:
      - lambda: id(debug_mode).turn_on();
    on_turn_off:
      - lambda: id(debug_mode).turn_off();
    
  - platform: template
    name: "Debug mode"
    id: debug_mode
    optimistic: true
    on_turn_on:
      - lambda: id(config_mode).turn_on();
      - uart.write: [0xFD, 0xFC, 0xFB, 0xFA, 0x02, 0x00, 0x62, 0x00, 0x04, 0x03, 0x02, 0x01]
      - delay: 1s
      - lambda: id(config_mode).turn_off();
    on_turn_off:
      - lambda: id(config_mode).turn_on();
      - uart.write: [0xFD, 0xFC, 0xFB, 0xFA, 0x02, 0x00, 0x63, 0x00, 0x04, 0x03, 0x02, 0x01]
      - delay: 1s
      - lambda: id(config_mode).turn_off();
  
#  - platform: template
#    name: "BlueTooth"
#    optimistic: true
#    on_turn_on:
#      - lambda: id(config_mode).turn_on();
#      # 蓝牙开
#      - uart.write: [0xFD, 0xFC, 0xFB, 0xFA, 0x04, 0x00, 0xA4, 0x00, 0x01, 0x00, 0x04, 0x03, 0x02, 0x01]
#      - delay: 0.5s
#      # ld2410b 重启
#      - uart.write: [0xFD, 0xFC, 0xFB, 0xFA, 0x02, 0x00, 0xA3, 0x00, 0x04, 0x03, 0x02, 0x01]
#      - delay: 1s      
#      - lambda: id(config_mode).turn_off();
#    on_turn_off:
#      - lambda: id(config_mode).turn_on();
#      # 蓝牙关
#      - uart.write: [0xFD, 0xFC, 0xFB, 0xFA, 0x04, 0x00, 0xA4, 0x00, 0x00, 0x00, 0x04, 0x03, 0x02, 0x01]
#      - delay: 1s
#      # ld2410b 重启
#      - uart.write: [0xFD, 0xFC, 0xFB, 0xFA, 0x02, 0x00, 0xA3, 0x00, 0x04, 0x03, 0x02, 0x01]
#      - delay: 1s
#      - lambda: id(config_mode).turn_off();
        
button:
  - platform: template
    name: update params
    id: update_params
    on_press:
      - lambda: id(config_mode).turn_on();
      - script.execute: get_all_params
      - lambda: id(config_mode).turn_off();
      
  - platform: restart
    name: Reboot
    id: reboot
  
  - platform: template
    name: "Reboot Ld2410"
    id: Reboot_Ld2410
    on_press:
      - lambda: id(config_mode).turn_on();
      # ld2410b 重启
      - uart.write: [0xFD, 0xFC, 0xFB, 0xFA, 0x02, 0x00, 0xA3, 0x00, 0x04, 0x03, 0x02, 0x01]
      - delay: 1s
      - lambda: id(config_mode).turn_off();
  
  - platform: template
    name: "Set default"
    id: set_default
    on_press:
      - lambda: id(config_mode).turn_on();
      - uart.write: [0xFD, 0xFC, 0xFB, 0xFA, 0x02, 0x00, 0xA2, 0x00, 0x04, 0x03, 0x02, 0x01]
      - delay: 1s
      - script.execute: get_all_params
      - delay: 1s
      - lambda: id(config_mode).turn_off();
      
      
number:
  - platform: template
    name: max_distance  #最大探测距离
    id: max_distance
    min_value: 0.75
    max_value: 6
    initial_value: 6
    step: 0.75
    #restore_value: true
    optimistic: true
    unit_of_measurement: m   #手册没有配置命令??服了
    
  - platform: template
    name: max_mov_distance  #最大移动探测距离
    id: max_mov_distance
    min_value: 0.75
    max_value: 6
    initial_value: 6
    step: 0.75
    #restore_value: true
    optimistic: true
    unit_of_measurement: m
    set_action:
      - uart.write: !lambda
          id(config_mode).turn_on();
          return {0xFD, 0xFC, 0xFB, 0xFA, 0x14, 0x00, 0x60, 0x00, 0x00, 0x00, (unsigned char)(int)(x/0.75), 0x00, 0x00, 0x00, 0x01, 0x00, (unsigned char)(int)(id(max_occ_distance).state/0.75), 0x00, 0x00, 0x00, 0x02, 0x00, (unsigned char)id(none_duration).state, 0x00, 0x00, 0x00, 0x04, 0x03, 0x02, 0x01};

    
  - platform: template
    name: max_occ_distance  #最大静止探测距离
    id: max_occ_distance
    min_value: 0.75
    max_value: 6
    initial_value: 6
    step: 0.75
    #restore_value: true
    optimistic: true
    unit_of_measurement: m
    set_action:
      - uart.write: !lambda
          id(config_mode).turn_on();
          return {0xFD, 0xFC, 0xFB, 0xFA, 0x14, 0x00, 0x60, 0x00, 0x00, 0x00, (unsigned char)(int)(id(max_mov_distance).state/0.75), 0x00, 0x00, 0x00, 0x01, 0x00, (unsigned char)(int)(x/0.75), 0x00, 0x00, 0x00, 0x02, 0x00, (unsigned char)id(none_duration).state, 0x00, 0x00, 0x00, 0x04, 0x03, 0x02, 0x01};

  - platform: template
    name: none_duration  #无人持续时间
    id: none_duration
    min_value: 0
    max_value: 120
    initial_value: 5
    step: 1
    #restore_value: true
    optimistic: true
    unit_of_measurement: s
    set_action:
      - uart.write: !lambda
          id(config_mode).turn_on();
          return {0xFD, 0xFC, 0xFB, 0xFA, 0x14, 0x00, 0x60, 0x00, 0x00, 0x00, (unsigned char)(int)(id(max_mov_distance).state/0.75), 0x00, 0x00, 0x00, 0x01, 0x00, (unsigned char)(int)(id(max_occ_distance).state/0.75), 0x00, 0x00, 0x00, 0x02, 0x00, (unsigned char)x, 0x00, 0x00, 0x00, 0x04, 0x03, 0x02, 0x01};

  - platform: template
    name: mov_sn_distance_0
    id: mov_sn_distance_0
    min_value: 0
    max_value: 100
    initial_value: 40
    step: 1
    #restore_value: true
    optimistic: true
    set_action:
      - uart.write: !lambda
          id(config_mode).turn_on();
          return {0xFD, 0xFC, 0xFB, 0xFA, 0x14, 0x00, 0x64, 0x00, 0x00, 0x00, (unsigned char)0, 0x00, 0x00, 0x00, 0x01, 0x00, (unsigned char)x, 0x00, 0x00, 0x00, 0x02, 0x00, (unsigned char)id(occ_sn_distance_0).state, 0x00, 0x00, 0x00, 0x04, 0x03, 0x02, 0x01};
  - platform: template
    name: mov_sn_distance_1
    id: mov_sn_distance_1
    min_value: 0
    max_value: 100
    initial_value: 40
    step: 1
    #restore_value: true
    optimistic: true
    set_action:
      - uart.write: !lambda
          id(config_mode).turn_on();
          return {0xFD, 0xFC, 0xFB, 0xFA, 0x14, 0x00, 0x64, 0x00, 0x00, 0x00, (unsigned char)1, 0x00, 0x00, 0x00, 0x01, 0x00, (unsigned char)x, 0x00, 0x00, 0x00, 0x02, 0x00, (unsigned char)id(occ_sn_distance_1).state, 0x00, 0x00, 0x00, 0x04, 0x03, 0x02, 0x01};
  - platform: template
    name: mov_sn_distance_2
    id: mov_sn_distance_2
    min_value: 0
    max_value: 100
    initial_value: 40
    step: 1
    #restore_value: true
    optimistic: true
    set_action:
      - uart.write: !lambda
          id(config_mode).turn_on();
          return {0xFD, 0xFC, 0xFB, 0xFA, 0x14, 0x00, 0x64, 0x00, 0x00, 0x00, (unsigned char)2, 0x00, 0x00, 0x00, 0x01, 0x00, (unsigned char)x, 0x00, 0x00, 0x00, 0x02, 0x00, (unsigned char)id(occ_sn_distance_2).state, 0x00, 0x00, 0x00, 0x04, 0x03, 0x02, 0x01};
  - platform: template
    name: mov_sn_distance_3
    id: mov_sn_distance_3
    min_value: 0
    max_value: 100
    initial_value: 40
    step: 1
    #restore_value: true
    optimistic: true
    set_action:
      - uart.write: !lambda
          id(config_mode).turn_on();
          return {0xFD, 0xFC, 0xFB, 0xFA, 0x14, 0x00, 0x64, 0x00, 0x00, 0x00, (unsigned char)3, 0x00, 0x00, 0x00, 0x01, 0x00, (unsigned char)x, 0x00, 0x00, 0x00, 0x02, 0x00, (unsigned char)id(occ_sn_distance_3).state, 0x00, 0x00, 0x00, 0x04, 0x03, 0x02, 0x01};
  - platform: template
    name: mov_sn_distance_4
    id: mov_sn_distance_4
    min_value: 0
    max_value: 100
    initial_value: 40
    step: 1
    #restore_value: true
    optimistic: true
    set_action:
      - uart.write: !lambda
          id(config_mode).turn_on();
          return {0xFD, 0xFC, 0xFB, 0xFA, 0x14, 0x00, 0x64, 0x00, 0x00, 0x00, (unsigned char)4, 0x00, 0x00, 0x00, 0x01, 0x00, (unsigned char)x, 0x00, 0x00, 0x00, 0x02, 0x00, (unsigned char)id(occ_sn_distance_4).state, 0x00, 0x00, 0x00, 0x04, 0x03, 0x02, 0x01};
  - platform: template
    name: mov_sn_distance_5
    id: mov_sn_distance_5
    min_value: 0
    max_value: 100
    initial_value: 40
    step: 1
    #restore_value: true
    optimistic: true
    set_action:
      - uart.write: !lambda
          id(config_mode).turn_on();
          return {0xFD, 0xFC, 0xFB, 0xFA, 0x14, 0x00, 0x64, 0x00, 0x00, 0x00, (unsigned char)5, 0x00, 0x00, 0x00, 0x01, 0x00, (unsigned char)x, 0x00, 0x00, 0x00, 0x02, 0x00, (unsigned char)id(occ_sn_distance_5).state, 0x00, 0x00, 0x00, 0x04, 0x03, 0x02, 0x01};
  - platform: template
    name: mov_sn_distance_6
    id: mov_sn_distance_6
    min_value: 0
    max_value: 100
    initial_value: 40
    step: 1
    #restore_value: true
    optimistic: true
    set_action:
      - uart.write: !lambda
          id(config_mode).turn_on();
          return {0xFD, 0xFC, 0xFB, 0xFA, 0x14, 0x00, 0x64, 0x00, 0x00, 0x00, (unsigned char)6, 0x00, 0x00, 0x00, 0x01, 0x00, (unsigned char)x, 0x00, 0x00, 0x00, 0x02, 0x00, (unsigned char)id(occ_sn_distance_6).state, 0x00, 0x00, 0x00, 0x04, 0x03, 0x02, 0x01};
  - platform: template
    name: mov_sn_distance_7
    id: mov_sn_distance_7
    min_value: 0
    max_value: 100
    initial_value: 40
    step: 1
    #restore_value: true
    optimistic: true
    set_action:
      - uart.write: !lambda
          id(config_mode).turn_on();
          return {0xFD, 0xFC, 0xFB, 0xFA, 0x14, 0x00, 0x64, 0x00, 0x00, 0x00, (unsigned char)7, 0x00, 0x00, 0x00, 0x01, 0x00, (unsigned char)x, 0x00, 0x00, 0x00, 0x02, 0x00, (unsigned char)id(occ_sn_distance_7).state, 0x00, 0x00, 0x00, 0x04, 0x03, 0x02, 0x01};
  - platform: template
    name: mov_sn_distance_8
    id: mov_sn_distance_8
    min_value: 0
    max_value: 100
    initial_value: 40
    step: 1
    #restore_value: true
    optimistic: true
    set_action:
      - uart.write: !lambda
          id(config_mode).turn_on();
          return {0xFD, 0xFC, 0xFB, 0xFA, 0x14, 0x00, 0x64, 0x00, 0x00, 0x00, (unsigned char)8, 0x00, 0x00, 0x00, 0x01, 0x00, (unsigned char)x, 0x00, 0x00, 0x00, 0x02, 0x00, (unsigned char)id(occ_sn_distance_8).state, 0x00, 0x00, 0x00, 0x04, 0x03, 0x02, 0x01};

  - platform: template
    name: occ_sn_distance_0
    id: occ_sn_distance_0
    min_value: 0
    max_value: 100
    initial_value: 40
    step: 1
    #restore_value: true
    optimistic: true
    set_action:
      - uart.write: !lambda
          id(config_mode).turn_on();
          return {0xFD, 0xFC, 0xFB, 0xFA, 0x14, 0x00, 0x64, 0x00, 0x00, 0x00, (unsigned char)0, 0x00, 0x00, 0x00, 0x01, 0x00, (unsigned char)id(mov_sn_distance_0).state, 0x00, 0x00, 0x00, 0x02, 0x00, (unsigned char)x, 0x00, 0x00, 0x00, 0x04, 0x03, 0x02, 0x01};
  - platform: template
    name: occ_sn_distance_1
    id: occ_sn_distance_1
    min_value: 0
    max_value: 100
    initial_value: 40
    step: 1
    #restore_value: true
    optimistic: true
    set_action:
      - uart.write: !lambda
          id(config_mode).turn_on();
          return {0xFD, 0xFC, 0xFB, 0xFA, 0x14, 0x00, 0x64, 0x00, 0x00, 0x00, (unsigned char)1, 0x00, 0x00, 0x00, 0x01, 0x00, (unsigned char)id(mov_sn_distance_1).state, 0x00, 0x00, 0x00, 0x02, 0x00, (unsigned char)x, 0x00, 0x00, 0x00, 0x04, 0x03, 0x02, 0x01};
  - platform: template
    name: occ_sn_distance_2
    id: occ_sn_distance_2
    min_value: 0
    max_value: 100
    initial_value: 40
    step: 1
    #restore_value: true
    optimistic: true
    set_action:
      - uart.write: !lambda
          id(config_mode).turn_on();
          return {0xFD, 0xFC, 0xFB, 0xFA, 0x14, 0x00, 0x64, 0x00, 0x00, 0x00, (unsigned char)2, 0x00, 0x00, 0x00, 0x01, 0x00, (unsigned char)id(mov_sn_distance_2).state, 0x00, 0x00, 0x00, 0x02, 0x00, (unsigned char)x, 0x00, 0x00, 0x00, 0x04, 0x03, 0x02, 0x01};
  - platform: template
    name: occ_sn_distance_3
    id: occ_sn_distance_3
    min_value: 0
    max_value: 100
    initial_value: 40
    step: 1
    #restore_value: true
    optimistic: true
    set_action:
      - uart.write: !lambda
          id(config_mode).turn_on();
          return {0xFD, 0xFC, 0xFB, 0xFA, 0x14, 0x00, 0x64, 0x00, 0x00, 0x00, (unsigned char)3, 0x00, 0x00, 0x00, 0x01, 0x00, (unsigned char)id(mov_sn_distance_3).state, 0x00, 0x00, 0x00, 0x02, 0x00, (unsigned char)x, 0x00, 0x00, 0x00, 0x04, 0x03, 0x02, 0x01};
  - platform: template
    name: occ_sn_distance_4
    id: occ_sn_distance_4
    min_value: 0
    max_value: 100
    initial_value: 40
    step: 1
    #restore_value: true
    optimistic: true
    set_action:
      - uart.write: !lambda
          id(config_mode).turn_on();
          return {0xFD, 0xFC, 0xFB, 0xFA, 0x14, 0x00, 0x64, 0x00, 0x00, 0x00, (unsigned char)4, 0x00, 0x00, 0x00, 0x01, 0x00, (unsigned char)id(mov_sn_distance_4).state, 0x00, 0x00, 0x00, 0x02, 0x00, (unsigned char)x, 0x00, 0x00, 0x00, 0x04, 0x03, 0x02, 0x01};
  - platform: template
    name: occ_sn_distance_5
    id: occ_sn_distance_5
    min_value: 0
    max_value: 100
    initial_value: 40
    step: 1
    #restore_value: true
    optimistic: true
    set_action:
      - uart.write: !lambda
          id(config_mode).turn_on();
          return {0xFD, 0xFC, 0xFB, 0xFA, 0x14, 0x00, 0x64, 0x00, 0x00, 0x00, (unsigned char)5, 0x00, 0x00, 0x00, 0x01, 0x00, (unsigned char)id(mov_sn_distance_5).state, 0x00, 0x00, 0x00, 0x02, 0x00, (unsigned char)x, 0x00, 0x00, 0x00, 0x04, 0x03, 0x02, 0x01};
  - platform: template
    name: occ_sn_distance_6
    id: occ_sn_distance_6
    min_value: 0
    max_value: 100
    initial_value: 40
    step: 1
    #restore_value: true
    optimistic: true
    set_action:
      - uart.write: !lambda
          id(config_mode).turn_on();
          return {0xFD, 0xFC, 0xFB, 0xFA, 0x14, 0x00, 0x64, 0x00, 0x00, 0x00, (unsigned char)6, 0x00, 0x00, 0x00, 0x01, 0x00, (unsigned char)id(mov_sn_distance_6).state, 0x00, 0x00, 0x00, 0x02, 0x00, (unsigned char)x, 0x00, 0x00, 0x00, 0x04, 0x03, 0x02, 0x01};
  - platform: template
    name: occ_sn_distance_7
    id: occ_sn_distance_7
    min_value: 0
    max_value: 100
    initial_value: 40
    step: 1
    #restore_value: true
    optimistic: true
    set_action:
      - uart.write: !lambda
          id(config_mode).turn_on();
          return {0xFD, 0xFC, 0xFB, 0xFA, 0x14, 0x00, 0x64, 0x00, 0x00, 0x00, (unsigned char)7, 0x00, 0x00, 0x00, 0x01, 0x00, (unsigned char)id(mov_sn_distance_7).state, 0x00, 0x00, 0x00, 0x02, 0x00, (unsigned char)x, 0x00, 0x00, 0x00, 0x04, 0x03, 0x02, 0x01};
  - platform: template
    name: occ_sn_distance_8
    id: occ_sn_distance_8
    min_value: 0
    max_value: 100
    initial_value: 40
    step: 1
    #restore_value: true
    optimistic: true
    set_action:
      - uart.write: !lambda
          id(config_mode).turn_on();
          return {0xFD, 0xFC, 0xFB, 0xFA, 0x14, 0x00, 0x64, 0x00, 0x00, 0x00, (unsigned char)8, 0x00, 0x00, 0x00, 0x01, 0x00, (unsigned char)id(mov_sn_distance_8).state, 0x00, 0x00, 0x00, 0x02, 0x00, (unsigned char)x, 0x00, 0x00, 0x00, 0x04, 0x03, 0x02, 0x01};

  - platform: template
    name: update_interval  #实时监控刷新间隔,越大越慢,大概500间隔为1秒
    id: update_interval
    min_value: 100
    max_value: 5000
    initial_value: 500
    step: 100
    restore_value: true
    optimistic: true
    #mode: box
ld2410h.h代码
#include "esphome.h"
#include <sstream>

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

    Sensor *mov_distance_sensor = new Sensor();
    Sensor *mov_energy = new Sensor();
    Sensor *occ_distance_sensor = new Sensor();
    Sensor *occ_energy = new Sensor();
    Sensor *detect_distance_sensor = new Sensor();
    Sensor *en_mov_0 = new Sensor();
    Sensor *en_mov_1 = new Sensor();
    Sensor *en_mov_2 = new Sensor();
    Sensor *en_mov_3 = new Sensor();
    Sensor *en_mov_4 = new Sensor();
    Sensor *en_mov_5 = new Sensor();
    Sensor *en_mov_6 = new Sensor();
    Sensor *en_mov_7 = new Sensor();
    Sensor *en_mov_8 = new Sensor();
    Sensor *en_occ_0 = new Sensor();
    Sensor *en_occ_1 = new Sensor();
    Sensor *en_occ_2 = new Sensor();
    Sensor *en_occ_3 = new Sensor();
    Sensor *en_occ_4 = new Sensor();
    Sensor *en_occ_5 = new Sensor();
    Sensor *en_occ_6 = new Sensor();
    Sensor *en_occ_7 = new Sensor();
    Sensor *en_occ_8 = new Sensor();
    Sensor *brightness = new Sensor();

    std::vector<uint8_t> bytes;
    std::vector<uint8_t> header_parameter = {0xFD, 0xFC, 0xFB, 0xFA, 0x1C, 0x00, 0x61, 0x01, 0x00, 0x00, 0xAA};
    std::vector<uint8_t> header_data = {0xF4, 0xF3, 0xF2, 0xF1, 0x0D, 0x00};
    std::vector<uint8_t> header_data_debug = {0xF4, 0xF3, 0xF2, 0xF1, 0x23, 0x00};
//调试用    std::vector<uint8_t> header_feedback = {0xFD, 0xFC, 0xFB, 0xFA, 0x04, 0x00};


    bool HeaderMatched(std::vector<uint8_t> bytes, std::vector<uint8_t> header)
    {
        if (bytes[0] != header[0] || bytes[1] != header[1] || bytes[2] != header[2] || bytes[3] != header[3])
        {
            return false;
        }
        return bytes[4] == header[4] && bytes[5] == header[5];        
    }

    void setup() override 
    {

    }
    
    void loop() override
    {
        static int data_count = 0;
        static bool presence_state = true;  //无人时不监控,节省运算量

        while (available() > 0)  // && (id(run_rta).state || id(config_mode).state)
        {
            bytes.push_back(read());
            if (bytes.size() < 6 + 3)    //多取3位,用于判断是否有人
            {
                continue;
            }

            if ((HeaderMatched(bytes, header_data) || HeaderMatched(bytes, header_data_debug)) && id(run_rta).state)
            {
//                if (bytes[8])
                if (1==1)
                {
                    if (bytes.size() < 6 + bytes[4])
                    {
                        continue;
                    }

                    data_count++;
                    presence_state = true;
                    if (data_count > id(update_interval).state)
                    {
                        data_count = 0;
                        LDDATAUnion lddataUnion;
                        std::copy(bytes.begin() + 6, (bytes.begin() + 6 + bytes[4]), lddataUnion.bytes);
                        switch (lddataUnion.lddata.target_state)
                        {
                            case 0:
                                id(target_state).publish_state("none");
                                break;
                            case 1:
                                id(target_state).publish_state("mov");
                                break;
                            case 2:
                                id(target_state).publish_state("occ");
                                break;
                            case 3:
                                id(target_state).publish_state("mov & occ");
                                break;
                        }
                        mov_distance_sensor->publish_state(lddataUnion.lddata.mov_distance1 + lddataUnion.lddata.mov_distance2 * 256);
                        mov_energy->publish_state(lddataUnion.lddata.mov_energy);
                        occ_distance_sensor->publish_state(lddataUnion.lddata.occ_distance1 + lddataUnion.lddata.occ_distance2 * 256);
                        occ_energy->publish_state(lddataUnion.lddata.occ_energy);
                        detect_distance_sensor->publish_state(lddataUnion.lddata.detect_distance1 + lddataUnion.lddata.detect_distance2 * 256);
                        if (lddataUnion.lddata.data_mode == 1) //工程模式
                        {
                            en_mov_0->publish_state(lddataUnion.lddata.en_mov_0);
                            en_mov_1->publish_state(lddataUnion.lddata.en_mov_1);
                            en_mov_2->publish_state(lddataUnion.lddata.en_mov_2);
                            en_mov_3->publish_state(lddataUnion.lddata.en_mov_3);
                            en_mov_4->publish_state(lddataUnion.lddata.en_mov_4);
                            en_mov_5->publish_state(lddataUnion.lddata.en_mov_5);
                            en_mov_6->publish_state(lddataUnion.lddata.en_mov_6);
                            en_mov_7->publish_state(lddataUnion.lddata.en_mov_7);
                            en_mov_8->publish_state(lddataUnion.lddata.en_mov_8);
                            en_occ_0->publish_state(lddataUnion.lddata.en_occ_0);
                            en_occ_1->publish_state(lddataUnion.lddata.en_occ_1);
                            en_occ_2->publish_state(lddataUnion.lddata.en_occ_2);
                            en_occ_3->publish_state(lddataUnion.lddata.en_occ_3);
                            en_occ_4->publish_state(lddataUnion.lddata.en_occ_4);
                            en_occ_5->publish_state(lddataUnion.lddata.en_occ_5);
                            en_occ_6->publish_state(lddataUnion.lddata.en_occ_6);
                            en_occ_7->publish_state(lddataUnion.lddata.en_occ_7);
                            en_occ_8->publish_state(lddataUnion.lddata.en_occ_8);
                            brightness->publish_state(lddataUnion.lddata.brightness);
                        }
                        bytes.clear();
                    }
                } 
                else if (presence_state)
                {  
                    id(target_state).publish_state("none");
                    mov_distance_sensor->publish_state(0);
                    mov_energy->publish_state(0);
                    occ_distance_sensor->publish_state(0);
                    occ_energy->publish_state(0);
                    detect_distance_sensor->publish_state(0);
                    en_mov_0->publish_state(0);
                    en_mov_1->publish_state(0);
                    en_mov_2->publish_state(0);
                    en_mov_3->publish_state(0);
                    en_mov_4->publish_state(0);
                    en_mov_5->publish_state(0);
                    en_mov_6->publish_state(0);
                    en_mov_7->publish_state(0);
                    en_mov_8->publish_state(0);
                    en_occ_0->publish_state(0);
                    en_occ_1->publish_state(0);
                    en_occ_2->publish_state(0);
                    en_occ_3->publish_state(0);
                    en_occ_4->publish_state(0);
                    en_occ_5->publish_state(0);
                    en_occ_6->publish_state(0);
                    en_occ_7->publish_state(0);
                    en_occ_8->publish_state(0);
                    brightness->publish_state(0);
                    presence_state = false;
                    bytes.clear();
                } 
                else
                {
                    bytes.clear();
                }
            }
            else if (HeaderMatched(bytes, header_parameter))
            {
                if (bytes.size() < 11 + sizeof(PARAMETER))  
                {
                    continue;
                }
                                
                PARAMETERUnion parameterUnion;
                std::copy(bytes.begin() + 11, (bytes.begin() + 11 + sizeof(PARAMETER)), parameterUnion.bytes);
                // ESP_LOGI("custom", "Sending Parameters");
                                id(max_distance).publish_state(parameterUnion.parameter.max_distance * 0.75 );            // 最大距离门  
                                id(max_mov_distance).publish_state(parameterUnion.parameter.max_mov_distance * 0.75 );    // 配置最大运动距离门
                                id(max_occ_distance).publish_state(parameterUnion.parameter.max_occ_distance * 0.75 );    // 配置最大静止距离门
                                id(mov_sn_distance_0).publish_state(parameterUnion.parameter.mov_sn_distance_0);          // 距离门0运动灵敏度,下同
                                id(mov_sn_distance_1).publish_state(parameterUnion.parameter.mov_sn_distance_1);
                                id(mov_sn_distance_2).publish_state(parameterUnion.parameter.mov_sn_distance_2);
                                id(mov_sn_distance_3).publish_state(parameterUnion.parameter.mov_sn_distance_3);
                                id(mov_sn_distance_4).publish_state(parameterUnion.parameter.mov_sn_distance_4);
                                id(mov_sn_distance_5).publish_state(parameterUnion.parameter.mov_sn_distance_5);
                                id(mov_sn_distance_6).publish_state(parameterUnion.parameter.mov_sn_distance_6);
                                id(mov_sn_distance_7).publish_state(parameterUnion.parameter.mov_sn_distance_7);
                                id(mov_sn_distance_8).publish_state(parameterUnion.parameter.mov_sn_distance_8);
                                id(occ_sn_distance_0).publish_state(parameterUnion.parameter.occ_sn_distance_0);           // 距离门0静止灵敏度,下同
                                id(occ_sn_distance_1).publish_state(parameterUnion.parameter.occ_sn_distance_1);
                                id(occ_sn_distance_2).publish_state(parameterUnion.parameter.occ_sn_distance_2);
                                id(occ_sn_distance_3).publish_state(parameterUnion.parameter.occ_sn_distance_3);
                                id(occ_sn_distance_4).publish_state(parameterUnion.parameter.occ_sn_distance_4);
                                id(occ_sn_distance_5).publish_state(parameterUnion.parameter.occ_sn_distance_5);
                                id(occ_sn_distance_6).publish_state(parameterUnion.parameter.occ_sn_distance_6);
                                id(occ_sn_distance_7).publish_state(parameterUnion.parameter.occ_sn_distance_7);
                                id(occ_sn_distance_8).publish_state(parameterUnion.parameter.occ_sn_distance_8);
                                id(none_duration).publish_state(parameterUnion.parameter.none_duration);     
                
                bytes.clear();
            }
/*       调试反馈用
            else if (HeaderMatched(bytes, header_feedback))
            {
                if (bytes.size() < 6 + sizeof(FEEDBACK))
                {
                    continue;
                }

                FEEDBACKUnion feedbackUnion;
                std::copy(bytes.begin() + 6, (bytes.begin() + 6 + sizeof(FEEDBACK)), feedbackUnion.bytes);
                switch (feedbackUnion.feedback.command1)
                {
                    case 254: //0xFE-配置结束
                        if (!feedbackUnion.feedback.result) ESP_LOGD("custom", "Config successed.");
                        else ESP_LOGI("custom", "!!!Config Failed!!!");
                        break;
                    case 96: //0x60-最大距离门与无人持续时间
                        if (!feedbackUnion.feedback.result) ESP_LOGD("custom", "Max distance setup successed.");
                        else ESP_LOGI("custom", "!!!Max distance setup Failed!!!");
                        break;
                    case 100: //0x64-距离灵敏度配置
                        if (!feedbackUnion.feedback.result) ESP_LOGD("custom", "Sensitivity setup successed.");
                        else ESP_LOGI("custom", "!!!Sensitivity setup Failed!!!");
                        break;
                    case 162: //0xA2-恢复出厂设置  旧版本无用
                        if (!feedbackUnion.feedback.result) ESP_LOGD("custom", "Set default successed.");
                        else ESP_LOGI("custom", "!!!Set default Failed!!!");
                        break;
                }

                bytes.clear();
            }
*/
            else
            {
                bytes.erase(bytes.begin());
                continue;
            }
        }
    }

    typedef struct
    {
        uint8_t max_distance;            // 最大距离门
                uint8_t max_mov_distance;        // 配置最大运动距离门
                uint8_t max_occ_distance;        // 配置最大静止距离门
                uint8_t mov_sn_distance_0;       // 距离门0运动灵敏度,下同
                uint8_t mov_sn_distance_1; 
                uint8_t mov_sn_distance_2;
                uint8_t mov_sn_distance_3;
                uint8_t mov_sn_distance_4;
                uint8_t mov_sn_distance_5;
                uint8_t mov_sn_distance_6;
                uint8_t mov_sn_distance_7;
                uint8_t mov_sn_distance_8;
                uint8_t occ_sn_distance_0;       // 距离门0静止灵敏度,下同
                uint8_t occ_sn_distance_1;
                uint8_t occ_sn_distance_2;
                uint8_t occ_sn_distance_3;
                uint8_t occ_sn_distance_4;
                uint8_t occ_sn_distance_5;
                uint8_t occ_sn_distance_6;
                uint8_t occ_sn_distance_7;
                uint8_t occ_sn_distance_8;
                uint8_t none_duration;            // 无人持续时间
    } PARAMETER        ; // 参数
        
    typedef union
    {
        PARAMETER parameter;
        uint8_t bytes[sizeof(PARAMETER)];
    } PARAMETERUnion;
        
    typedef struct
    {
        uint8_t data_mode;     //数据类型 01-工程模式,02-基本
        uint8_t data_header;     //头部  
        uint8_t target_state;     //目标状态  
        uint8_t mov_distance1;   //运动目标距离
        uint8_t mov_distance2;
        uint8_t mov_energy;      //运动目标能量
        uint8_t occ_distance1;   //静止目标距离
        uint8_t occ_distance2;
        uint8_t occ_energy;      //静止目标能量
        uint8_t detect_distance1;     //探测距离
        uint8_t detect_distance2;
        //能量数据 工程模式下有效
        uint8_t max_mov_distance;        // 配置最大运动距离门
        uint8_t max_occ_distance;        // 配置最大静止距离门
        uint8_t en_mov_0;
        uint8_t en_mov_1;
        uint8_t en_mov_2;
        uint8_t en_mov_3;
        uint8_t en_mov_4;
        uint8_t en_mov_5;
        uint8_t en_mov_6;
        uint8_t en_mov_7;
        uint8_t en_mov_8;
        uint8_t en_occ_0;
        uint8_t en_occ_1;
        uint8_t en_occ_2;
        uint8_t en_occ_3;
        uint8_t en_occ_4;
        uint8_t en_occ_5;
        uint8_t en_occ_6;
        uint8_t en_occ_7;
        uint8_t en_occ_8;
        uint8_t brightness;
    } LDDATA;    //雷达探测数据

    typedef union
    {
        LDDATA lddata;
        uint8_t bytes[sizeof(LDDATA)];
    } LDDATAUnion;
/*    调试用
    typedef struct
    {
        uint8_t command1;     //指令
        uint8_t command2;     //指令  
        uint8_t result;       //反馈  
    } FEEDBACK;

    typedef union
    {
        FEEDBACK feedback;
        uint8_t bytes[sizeof(FEEDBACK)];
    } FEEDBACKUnion;
*/
};


评分

参与人数 1金钱 +12 收起 理由
zxyny1989 + 12 高手,这是高手!

查看全部评分

回复

使用道具 举报

1

主题

8

帖子

50

积分

注册会员

Rank: 2

积分
50
金钱
42
HASS币
0
发表于 2023-6-13 19:55:50 | 显示全部楼层
厉害啊 太强了
回复

使用道具 举报

2

主题

15

帖子

139

积分

注册会员

Rank: 2

积分
139
金钱
124
HASS币
0
发表于 2023-6-15 16:59:05 | 显示全部楼层
学习一下
回复

使用道具 举报

1

主题

20

帖子

194

积分

注册会员

Rank: 2

积分
194
金钱
174
HASS币
0
发表于 2023-6-21 16:48:23 | 显示全部楼层
看看牛批
回复

使用道具 举报

0

主题

20

帖子

178

积分

注册会员

Rank: 2

积分
178
金钱
158
HASS币
0
发表于 2023-6-28 18:01:59 | 显示全部楼层
学习学习!
回复

使用道具 举报

0

主题

13

帖子

88

积分

注册会员

Rank: 2

积分
88
金钱
75
HASS币
0
发表于 2023-7-5 18:49:59 | 显示全部楼层
真好啊 这感觉非常的给力
回复

使用道具 举报

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

本版积分规则

Archiver|手机版|小黑屋|Hassbian

GMT+8, 2024-11-24 10:13 , Processed in 0.108094 second(s), 33 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

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