Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add LD2450 component #5674

Open
wants to merge 23 commits into
base: dev
Choose a base branch
from
Open

Add LD2450 component #5674

wants to merge 23 commits into from

Conversation

hareeshmu
Copy link

@hareeshmu hareeshmu commented Nov 4, 2023

What does this implement/fix?

Support of LD2450 human motion tracking radar sensor

Types of changes

  • Bugfix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • Other

Related issue or feature (if applicable): fixes

Pull request in esphome-docs with documentation (if applicable): esphome/esphome-docs#3327

Test Environment

  • ESP32
  • ESP32 IDF
  • ESP8266
  • RP2040
  • BK72xx
  • RTL87xx

Example entry for config.yaml:

uart:
  id: uart_ld2450
  tx_pin: 17
  rx_pin: 21
  baud_rate: 256000
  parity: NONE
  stop_bits: 1

ld2450:
  id: ld2450_radar
  uart_id: uart_ld2450
  throttle: 1000ms

binary_sensor:
  - platform: ld2450
    ld2450_id: ld2450_radar
    has_target:
      name: Presence
    has_moving_target:
      name: Moving Target
    has_still_target:
      name: Still Target

number:
  - platform: ld2450
    ld2450_id: ld2450_radar
    presence_timeout:
      name: "Timeout"
    zone_1:
      x1:
        name: Zone-1 X1
      y1:
        name: Zone-1 Y1
      x2:
        name: Zone-1 X2
      y2:
        name: Zone-1 Y2
    zone_2:
      x1:
        name: Zone-2 X1
      y1:
        name: Zone-2 Y1
      x2:
        name: Zone-2 X2
      y2:
        name: Zone-2 Y2
    zone_3:
      x1:
        name: Zone-3 X1
      y1:
        name: Zone-3 Y1
      x2:
        name: Zone-3 X2
      y2:
        name: Zone-3 Y2

switch:
  - platform: ld2450
    ld2450_id: ld2450_radar
    bluetooth:
      name: "Bluetooth"
    multi_target:
      name: "Multi Target Tracking"

select:
  - platform: ld2450
    ld2450_id: ld2450_radar
    baud_rate:
      name: "Baud rate"
    zone_type:
      name: "Zone Type"

button:
  - platform: ld2450
    ld2450_id: ld2450_radar
    factory_reset:
      name: "LD2450 Factory Reset"
      entity_category: "config"
    restart:
      name: "LD2450 Restart"
      entity_category: "config"

text_sensor:
  - platform: ld2450
    ld2450_id: ld2450_radar
    version:
      name: "LD2450 Firmware"
    mac_address:
      name: "LD2450 BT MAC"
    target_1:
      direction:
        name: "Target-1 Direction"
    target_2:
      direction:
        name: "Target-2 Direction"
    target_3:
      direction:
        name: "Target-3 Direction"

sensor:
  - platform: ld2450
    ld2450_id: ld2450_radar
    target_count:
      name: Presence Target Count
    still_target_count:
      name: Still Target Count
    moving_target_count:
      name: Moving Target Count
    target_1:
      x:
        name: Target-1 X
      y:
        name: Target-1 Y
      speed:
        name: Target-1 Speed
      angle:
        name: Target-1 Angle
      distance:
        name: Target-1 Distance
      resolution:
        name: Target-1 Resolution
    target_2:
      x:
        name: Target-2 X
      y:
        name: Target-2 Y
      speed:
        name: Target-2 Speed
      angle:
        name: Target-2 Angle
      distance:
        name: Target-2 Distance
      resolution:
        name: Target-2 Resolution
    target_3:
      x:
        name: Target-3 X
      y:
        name: Target-3 Y
      speed:
        name: Target-3 Speed
      angle:
        name: Target-3 Angle
      distance:
        name: Target-3 Distance
      resolution:
        name: Target-3 Resolution
    zone_1:
      target_count:
        name: Zone-1 All Target Count
      still_target_count:
        name: Zone-1 Still Target Count
      moving_target_count:
        name: Zone-1 Moving Target Count
    zone_2:
      target_count:
        name: Zone-2 All Target Count
      still_target_count:
        name: Zone-2 Still Target Count
      moving_target_count:
        name: Zone-2 Moving Target Count
    zone_3:
      target_count:
        name: Zone-3 All Target Count
      still_target_count:
        name: Zone-3 Still Target Count
      moving_target_count:
        name: Zone-3 Moving Target Count

Checklist:

  • The code change is tested and works locally.
  • Tests have been added to verify that the new code works (under tests/ folder).

If user exposed functionality or configuration variables are added/changed:

@probot-esphome
Copy link

probot-esphome bot commented Nov 4, 2023

Hey there @hareeshmu,
Thanks for submitting this pull request! Can you add yourself as a codeowner for this integration? This way we can notify you if a bug report for this integration is reported.
In __init__.py of the integration, please add:

CODEOWNERS = ["@hareeshmu"]

And run script/build_codeowners.py

(message by NeedsCodeownersLabel)

@kbx81
Copy link
Member

kbx81 commented Nov 4, 2023

Duplicate of #5624...

@hareeshmu
Copy link
Author

Duplicate of #5624...

We are having discussion with PR #5624 owner.
Mentioned you in the discord thread.

@probot-esphome
Copy link

probot-esphome bot commented Nov 4, 2023

Hey there @regevbr, @sebcaps, mind taking a look at this pull request as it has been labeled with an integration (ld2410) you are listed as a code owner for? Thanks!
(message by CodeOwnersMention)

@probot-esphome
Copy link

probot-esphome bot commented Nov 4, 2023

Hey there @niklasweber, mind taking a look at this pull request as it has been labeled with an integration (dfrobot_sen0395) you are listed as a code owner for? Thanks!
(message by CodeOwnersMention)

@probot-esphome
Copy link

probot-esphome bot commented Nov 4, 2023

Hey there @descipher, mind taking a look at this pull request as it has been labeled with an integration (ld2420) you are listed as a code owner for? Thanks!
(message by CodeOwnersMention)

Copy link
Contributor

@sebcaps sebcaps left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok for ld2410, update

@hareeshmu hareeshmu force-pushed the ld2450 branch 4 times, most recently from a648a33 to 3508819 Compare November 8, 2023 14:13
@Fmstrat
Copy link

Fmstrat commented Jan 27, 2024

Progress! But.. weird. The above YAML also works IF:

  • I remove Home Assistant api
  • I don't enable the ld2450 buttons

I.E. without api, I can get sensors. But only 1 target. Oddly enough, if I enable api and reflash, it works, but only once. As soon as I make another change and reflash, it stops.

Also, if I add anything else, like a DHT, it's a no go. Same boot issues.

@M3te0r
Copy link

M3te0r commented Jan 28, 2024

Hi, I took the config from the example and it's working great but I'm facing some issues with some values not working properly

  • Zone-X All Target Count
  • Zone-X Moving Target Count
  • Zone-X Still Target Count

Stay all at zero even though I've correctly setup a zone and presence is detected within the zone

See attached screens

Screenshot 2024-01-29 at 00-09-50 Capteur Presence LD2450 Bureau
Capture12

Board : ESP32 S2 Mini Wemos
Yaml config:

external_components:
  - source:
      type: git
      url: https://github.com/hareeshmu/esphome
      ref: ld2450
    components: [ ld2450 ]

uart:
  id: uart_ld2450
  tx_pin: GPIO16
  rx_pin: GPIO18
  baud_rate: 256000
  parity: NONE
  stop_bits: 1

ld2450:
  id: ld2450_radar
  uart_id: uart_ld2450
  throttle: 1000ms


binary_sensor:
  - platform: ld2450
    ld2450_id: ld2450_radar
    has_target:
      name: Presence
    has_moving_target:
      name: Moving Target
    has_still_target:
      name: Still Target

number:
  - platform: ld2450
    ld2450_id: ld2450_radar
    presence_timeout:
      name: "Timeout"
    zone_1:
      x1:
        name: Zone-1 X1
      y1:
        name: Zone-1 Y1
      x2:
        name: Zone-1 X2
      y2:
        name: Zone-1 Y2
    zone_2:
      x1:
        name: Zone-2 X1
      y1:
        name: Zone-2 Y1
      x2:
        name: Zone-2 X2
      y2:
        name: Zone-2 Y2
    zone_3:
      x1:
        name: Zone-3 X1
      y1:
        name: Zone-3 Y1
      x2:
        name: Zone-3 X2
      y2:
        name: Zone-3 Y2

switch:
  - platform: ld2450
    ld2450_id: ld2450_radar
    bluetooth:
      name: "Bluetooth"
    multi_target:
      name: "Multi Target Tracking"

select:
  - platform: ld2450
    ld2450_id: ld2450_radar
    baud_rate:
      name: "Baud rate"
    zone_type:
      name: "Zone Type"

text_sensor:
  - platform: ld2450
    ld2450_id: ld2450_radar
    version:
      name: "LD2450 Firmware"
    mac_address:
      name: "LD2450 BT MAC"
    target_1:
      direction:
        name: "Target-1 Direction"
    target_2:
      direction:
        name: "Target-2 Direction"
    target_3:
      direction:
        name: "Target-3 Direction"

sensor:
  - platform: ld2450
    ld2450_id: ld2450_radar
    target_count:
      name: Presence Target Count
    still_target_count:
      name: Still Target Count
    moving_target_count:
      name: Moving Target Count
    target_1:
      x:
        name: Target-1 X
      y:
        name: Target-1 Y
      speed:
        name: Target-1 Speed
      angle:
        name: Target-1 Angle
      distance:
        name: Target-1 Distance
      resolution:
        name: Target-1 Resolution
    target_2:
      x:
        name: Target-2 X
      y:
        name: Target-2 Y
      speed:
        name: Target-2 Speed
      angle:
        name: Target-2 Angle
      distance:
        name: Target-2 Distance
      resolution:
        name: Target-2 Resolution
    target_3:
      x:
        name: Target-3 X
      y:
        name: Target-3 Y
      speed:
        name: Target-3 Speed
      angle:
        name: Target-3 Angle
      distance:
        name: Target-3 Distance
      resolution:
        name: Target-3 Resolution
    zone_1:
      target_count:
        name: Zone-1 All Target Count
      still_target_count:
        name: Zone-1 Still Target Count
      moving_target_count:
        name: Zone-1 Moving Target Count
    zone_2:
      target_count:
        name: Zone-2 All Target Count
      still_target_count:
        name: Zone-2 Still Target Count
      moving_target_count:
        name: Zone-2 Moving Target Count
    zone_3:
      target_count:
        name: Zone-3 All Target Count
      still_target_count:
        name: Zone-3 Still Target Count
      moving_target_count:
        name: Zone-3 Moving Target Count

EDIT : Tried with Everything Smart Home code for ld2450 at https://github.com/EverythingSmartHome/everything-presence-lite/blob/main/common/ld2450-base.yaml

Works with per zone count occupancy if that's any help

@wittimagic
Copy link

Tested without Factory Reboot and Restart Button on current ESPHome release.
Used with an ESP32-C3 "super mini" board (esp32-c3-devkitm-1).

Result: Works perfect !

Only one minor thing:
The text_sensor "direction" only shows "moving away" although I am moving towards the LD2450. Maybe it would be more useful if the direction would show a relative heading (vector) in degrees in relation to the direction the LD2450 is facing .

Good job nevertheless !!!

:-)

@lboue
Copy link
Contributor

lboue commented Mar 7, 2024

Is it possible to validate this PR?

@Fmstrat
Copy link

Fmstrat commented Mar 16, 2024

@hareeshmu After using this a while with two different batches of LD2450s, I've discovered something you may wish to add to the documentation.

Multiple sensors from both batches have required that I use the white JST plug on the side at least once after flashing firmware for ESPHome on an ESP32 to recognize the Mac address. Once the white plug was used, the Mac address showed up and the device started working. I could then switch to the pins on the back with no issues.

I tested and reproduced this on:

  • Devices
    • Multiple ESP32s
    • Multiple LD2450s
  • Process (repeated many times to confirm)
    • Flash using pins, didn't work until connecting via JST, then pins worked
    • Flash using JST, worked using JST, then pins worked

I did not test to validate this was happening on:

  • ESP8266 (Though it most likely was my issue above)
  • Use of the solder points on the LD2450

Examining the traces on the board, my guess is that the longer distance from the pins to the main processing chip is the reason for this need to "jump start". I would also guess that the solder points would have the same problems, as they seem to join with the traces from the pins.

My suggestion for the doc:

If, after flashing the appropriate firmware, the LD2450 does not respond with a mac address or firmware revision in ESPHome, try connecting the LD2450 to the ESP device via the white JST plug. With many devices, this will cause the LD2450 to begin responding, at which point you can switch back to utilizing the pin or solder-point connections.

@jerome6994
Copy link

Good morning

I wanted to know what was blocking this development?

@53l3cu5
Copy link

53l3cu5 commented Apr 7, 2024

@kbx81 can you review this request?
And if there is an issue can you explain what it is?
Thanks in advance

@TrevorSchirmer
Copy link
Contributor

Hello, thanks for the work on this integration. We've noticed the same as mentioned in another comment, the "direction" only shows "moving away".

Thanks for the contribution to the community!

@jcbshw
Copy link

jcbshw commented Apr 30, 2024

Works well if I use the full config.

Though if I use a stripped down config my ESP32 will never boot with the LD2450 connected. Once disconnected it will boot but the LD2450 never connects.

This is the error I get:

Guru Meditation Error: Core 1 panic'ed (StoreProhibited). Exception was unhandled.

This is the stripped down config I am attempting to use:

uart:
  id: uart_ld2450
  tx_pin: GPIO17
  rx_pin: GPIO16
  baud_rate: 256000
  parity: NONE
  stop_bits: 1

ld2450:
  id: ld2450_radar
  uart_id: uart_ld2450
  throttle: 1000ms

binary_sensor:
  - platform: ld2450
    ld2450_id: ld2450_radar
    has_target:
      name: Presence

number:
  - platform: ld2450
    ld2450_id: ld2450_radar
    presence_timeout:
      name: "Timeout"

switch:
  - platform: ld2450
    ld2450_id: ld2450_radar
    bluetooth:
      name: "Bluetooth"
    multi_target:
      name: "Multi Target Tracking"

text_sensor:
  - platform: ld2450
    ld2450_id: ld2450_radar
    version:
      name: "LD2450 Firmware"
    mac_address:
      name: "LD2450 BT MAC"

sensor:
  - platform: ld2450
    ld2450_id: ld2450_radar
    target_count:
      name: Presence Target Count

@codecov-commenter
Copy link

codecov-commenter commented May 2, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 53.80%. Comparing base (4d8b5ed) to head (18c9ebf).
Report is 1696 commits behind head on dev.

Additional details and impacted files
@@            Coverage Diff             @@
##              dev    #5674      +/-   ##
==========================================
+ Coverage   53.70%   53.80%   +0.09%     
==========================================
  Files          50       50              
  Lines        9408     9812     +404     
  Branches     1654     1353     -301     
==========================================
+ Hits         5053     5279     +226     
- Misses       4056     4207     +151     
- Partials      299      326      +27     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@vladm
Copy link

vladm commented May 17, 2024

any updates to get this merged ?

@WiebKastanje
Copy link

WiebKastanje commented Aug 17, 2024

Not sure where to put this request, but here it is.
Kindly add support for multiple LD2450's in one ESPHome device. Could look like this:

ld2450:
- id: ld2450_radar_1
uart_id: uart_ld2450_1
throttle: 1s
- id: ld2450_radar_2
uart_id: uart_ld2450_2
throttle: 1s

If this is not the right place, please inform me. Thank you.

@GoodnessJSON
Copy link

Can this please be looked at merging soon? This is a very capable sensor and it would be an amazing addition for DIY Home Occupancy Sensors.

marcusb and others added 3 commits September 4, 2024 08:57
Movement should be registered if speed is non-zero, in both
directions.
Detect movement also in the negative direction
@KSumwalt
Copy link

Any movement on merging this? I recently used this:

# External Components
external_components:
  - source:
      type: git
      url: https://github.com/hareeshmu/esphome
      ref: ld2450
    components: [ ld2450 ]

and outside of the target direction reporting as Moving Away for any movement towards or away from the sensor (Stationary reports as Stationary) it is working well.

float angle_degrees = angle_radians * (180.0 / M_PI);
return angle_degrees;
}
std::string get_direction_(int16_t speed) {
Copy link
Contributor

@amcfague amcfague Nov 26, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor, but you can improve performance by creating static strings to return here. I.e.,

  const std::string& get_direction_(int16_t speed) {
    static const std::string MOVING_AWAY = "Moving away";
...

Fixes target direction always being moving away

Co-authored-by: Trevor Schirmer <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.