ferbulous

mmwave

Sep 28th, 2023
355
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 37.53 KB | None | 0 0
  1. # This is a custom firmware for Human Presence Sensor 2A
  2. # https://docs.screek.io/2a
  3. # Current version synchronized with public version: V230907_1_beta
  4.  
  5. esphome:
  6. name: ld2450-sensor-esp32c3
  7. comment: LD2450 Presence Sensor
  8. friendly_name: LD2450 Presence Sensor esp32c3
  9. name_add_mac_suffix: false
  10. platformio_options:
  11. board_build.flash_mode: dio
  12. # board_build.f_cpu: 80000000L
  13. project:
  14. name: Screek.Human_Presence_Sensor
  15. version: 2A
  16. on_boot:
  17. - priority: 100
  18. then:
  19. lambda: |-
  20. id(cpu_speed) = ESP.getCpuFreqMHz();
  21. - priority: -200
  22. then:
  23. lambda: |-
  24. id(zone1_target_exsits).publish_state(false);
  25. id(zone2_target_exsits).publish_state(false);
  26. id(zone3_target_exsits).publish_state(false);
  27. id(zone_ex1_target_exsits).publish_state(false);
  28.  
  29. preferences:
  30. flash_write_interval: 5s
  31.  
  32. external_components:
  33. - source:
  34. type: git
  35. url: https://github.com/screekworkshop/custom_components_support_for_screek_2a
  36. ref: main
  37. components: [esp32, uart]
  38.  
  39. esp32:
  40. board: esp32-c3-devkitm-1
  41. framework:
  42. type: arduino
  43.  
  44. globals:
  45. - id: cpu_speed
  46. type: int
  47. restore_value: no
  48. initial_value: '0'
  49. - id: last_update_ld2450
  50. type: unsigned long
  51. restore_value: no
  52. initial_value: '0'
  53. - id: init_zone_publish
  54. type: bool
  55. restore_value: no
  56. initial_value: "false"
  57. - id: last_illuminance
  58. type: float
  59. restore_value: no
  60. initial_value: "-1"
  61.  
  62. logger:
  63.  
  64. debug:
  65. update_interval: 30s
  66.  
  67. api:
  68.  
  69. ota:
  70.  
  71. wifi:
  72. ssid: !secret wifi_ssid
  73. password: !secret wifi_password
  74.  
  75. ap:
  76. ssid: "HUMAN-SENSOR 2A Store"
  77.  
  78. captive_portal:
  79.  
  80. web_server:
  81. port: 80
  82.  
  83.  
  84. text_sensor:
  85. - platform: debug
  86. reset_reason:
  87. name: "ESP Reset Reason"
  88. icon: mdi:anchor
  89. disabled_by_default: True
  90. - platform: wifi_info
  91. ip_address:
  92. name: ESP IP Address
  93. entity_category: "diagnostic"
  94. disabled_by_default: True
  95. icon: mdi:ip-network
  96. mac_address:
  97. name: ESP MAC
  98. entity_category: "diagnostic"
  99. icon: mdi:ip-network
  100. disabled_by_default: True
  101. - platform: template
  102. name: "Zone1 Info"
  103. id: tips_zone1_conf
  104. icon: mdi:information-outline
  105. entity_category: config
  106. lambda: |-
  107. return {"Configure below" };
  108. update_interval: 1000s
  109. - platform: template
  110. name: "Zone2 Info"
  111. id: tips_zone2_conf
  112. icon: mdi:information-outline
  113. entity_category: config
  114. lambda: |-
  115. return {"Configure below" };
  116. update_interval: 1000s
  117. - platform: template
  118. name: "Zone3 Info"
  119. id: tips_zone3_conf
  120. icon: mdi:information-outline
  121. entity_category: config
  122. lambda: |-
  123. return {"Configure below" };
  124. update_interval: 1000s
  125. - platform: template
  126. name: "Zout1 Info"
  127. id: tips_zone_ex1_conf
  128. icon: mdi:information-outline
  129. entity_category: config
  130. lambda: |-
  131. return {"Zone Exclusion 1" };
  132. update_interval: 1000s
  133. - platform: template
  134. name: "Any-Presence Info"
  135. id: tips_any_presence_conf
  136. icon: mdi:information-outline
  137. entity_category: config
  138. lambda: |-
  139. return {"Any Presence Config" };
  140. update_interval: 1000s
  141. - platform: template
  142. name: "Target1 Direction"
  143. id: target1_direction
  144. icon: mdi:directions
  145. - platform: template
  146. name: "Target2 Direction"
  147. id: target2_direction
  148. icon: mdi:directions
  149. - platform: template
  150. name: "Target3 Direction"
  151. id: target3_direction
  152. icon: mdi:directions
  153. - platform: template
  154. name: "Target1 Position"
  155. id: target1_position
  156. icon: mdi:directions
  157. - platform: template
  158. name: "Target2 Position"
  159. id: target2_position
  160. icon: mdi:directions
  161. - platform: template
  162. name: "Target3 Position"
  163. id: target3_position
  164. icon: mdi:directions
  165.  
  166. number:
  167. - platform: template
  168. name: "Any Presence Timeout"
  169. id: any_presence_timeout
  170. min_value: 0
  171. max_value: 600
  172. mode: box
  173. device_class: duration
  174. entity_category: config
  175. unit_of_measurement: s
  176. icon: mdi:timer-off
  177. step: 1
  178. optimistic: True
  179. initial_value: 0
  180. restore_value: True
  181. - platform: template
  182. name: "Zone1 Timeout"
  183. id: zone1_x_timeout
  184. min_value: 0
  185. max_value: 600
  186. mode: box
  187. device_class: duration
  188. entity_category: config
  189. unit_of_measurement: s
  190. icon: mdi:timer-off
  191. step: 1
  192. optimistic: True
  193. initial_value: 0
  194. restore_value: True
  195. - platform: template
  196. name: "Zone2 Timeout"
  197. id: zone2_x_timeout
  198. min_value: 0
  199. max_value: 600
  200. mode: box
  201. device_class: duration
  202. entity_category: config
  203. unit_of_measurement: s
  204. icon: mdi:timer-off
  205. step: 1
  206. optimistic: True
  207. initial_value: 0
  208. restore_value: True
  209. - platform: template
  210. name: "Zone3 Timeout"
  211. id: zone3_x_timeout
  212. min_value: 0
  213. max_value: 600
  214. mode: box
  215. device_class: duration
  216. entity_category: config
  217. unit_of_measurement: s
  218. icon: mdi:timer-off
  219. step: 1
  220. optimistic: True
  221. initial_value: 0
  222. restore_value: True
  223. # Zone 1
  224. - platform: template
  225. name: Zone1 X-Begin
  226. id: zone1_x_begin
  227. min_value: -4000
  228. max_value: 4000
  229. mode: box
  230. device_class: distance
  231. entity_category: config
  232. unit_of_measurement: mm
  233. icon: mdi:arrow-left-bold
  234. step: 10
  235. optimistic: True
  236. initial_value: 0
  237. restore_value: True
  238. on_value:
  239. then:
  240. - script.execute: check_zone1_vaild
  241. - platform: template
  242. name: Zone1 X-End
  243. id: zone1_x_end
  244. mode: box
  245. min_value: -4000
  246. max_value: 4000
  247. device_class: distance
  248. unit_of_measurement: mm
  249. entity_category: config
  250. icon: mdi:arrow-right-bold
  251. step: 10
  252. initial_value: 0
  253. optimistic: True
  254. restore_value: True
  255. on_value:
  256. then:
  257. - script.execute: check_zone1_vaild
  258. - platform: template
  259. name: Zone1 Y-Begin
  260. id: zone1_y_begin
  261. mode: box
  262. min_value: 0
  263. max_value: 6000
  264. device_class: distance
  265. entity_category: config
  266. icon: mdi:arrow-up-bold
  267. unit_of_measurement: mm
  268. step: 10
  269. initial_value: 0
  270. optimistic: True
  271. restore_value: True
  272. on_value:
  273. then:
  274. - script.execute: check_zone1_vaild
  275. - platform: template
  276. name: Zone1 Y-End
  277. id: zone1_y_end
  278. icon: mdi:arrow-down-bold
  279. mode: box
  280. min_value: 0
  281. max_value: 6000
  282. initial_value: 0
  283. entity_category: config
  284. device_class: distance
  285. unit_of_measurement: mm
  286. step: 10
  287. optimistic: True
  288. restore_value: True
  289. on_value:
  290. then:
  291. - script.execute: check_zone1_vaild
  292.  
  293. # Zone 2
  294. - platform: template
  295. name: Zone2 X-Begin
  296. id: zone2_x_begin
  297. min_value: -4000
  298. max_value: 4000
  299. mode: box
  300. device_class: distance
  301. entity_category: config
  302. unit_of_measurement: mm
  303. icon: mdi:arrow-left-bold
  304. step: 10
  305. optimistic: True
  306. initial_value: 0
  307. restore_value: True
  308. on_value:
  309. then:
  310. - script.execute: check_zone2_vaild
  311. - platform: template
  312. name: Zone2 X-End
  313. id: zone2_x_end
  314. mode: box
  315. min_value: -4000
  316. max_value: 4000
  317. device_class: distance
  318. unit_of_measurement: mm
  319. entity_category: config
  320. icon: mdi:arrow-right-bold
  321. step: 10
  322. initial_value: 0
  323. optimistic: True
  324. restore_value: True
  325. on_value:
  326. then:
  327. - script.execute: check_zone2_vaild
  328. - platform: template
  329. name: Zone2 Y-Begin
  330. id: zone2_y_begin
  331. mode: box
  332. min_value: 0
  333. max_value: 6000
  334. device_class: distance
  335. entity_category: config
  336. icon: mdi:arrow-up-bold
  337. unit_of_measurement: mm
  338. step: 10
  339. initial_value: 0
  340. optimistic: True
  341. restore_value: True
  342. on_value:
  343. then:
  344. - script.execute: check_zone2_vaild
  345. - platform: template
  346. name: Zone2 Y-End
  347. id: zone2_y_end
  348. icon: mdi:arrow-down-bold
  349. mode: box
  350. min_value: 0
  351. max_value: 6000
  352. initial_value: 0
  353. entity_category: config
  354. device_class: distance
  355. unit_of_measurement: mm
  356. step: 10
  357. optimistic: True
  358. restore_value: True
  359. on_value:
  360. then:
  361. - script.execute: check_zone2_vaild
  362.  
  363. # Zone 3
  364. - platform: template
  365. name: Zone3 X-Begin
  366. id: zone3_x_begin
  367. min_value: -4000
  368. max_value: 4000
  369. mode: box
  370. device_class: distance
  371. entity_category: config
  372. unit_of_measurement: mm
  373. icon: mdi:arrow-left-bold
  374. step: 10
  375. optimistic: True
  376. initial_value: 0
  377. restore_value: True
  378. on_value:
  379. then:
  380. - script.execute: check_zone3_vaild
  381. - platform: template
  382. name: Zone3 X-End
  383. id: zone3_x_end
  384. mode: box
  385. min_value: -4000
  386. max_value: 4000
  387. device_class: distance
  388. unit_of_measurement: mm
  389. entity_category: config
  390. icon: mdi:arrow-right-bold
  391. step: 10
  392. initial_value: 0
  393. optimistic: True
  394. restore_value: True
  395. on_value:
  396. then:
  397. - script.execute: check_zone3_vaild
  398. - platform: template
  399. name: Zone3 Y-Begin
  400. id: zone3_y_begin
  401. mode: box
  402. min_value: 0
  403. max_value: 6000
  404. device_class: distance
  405. entity_category: config
  406. icon: mdi:arrow-up-bold
  407. unit_of_measurement: mm
  408. step: 10
  409. initial_value: 0
  410. optimistic: True
  411. restore_value: True
  412. on_value:
  413. then:
  414. - script.execute: check_zone3_vaild
  415. - platform: template
  416. name: Zone3 Y-End
  417. id: zone3_y_end
  418. icon: mdi:arrow-down-bold
  419. mode: box
  420. min_value: 0
  421. max_value: 6000
  422. initial_value: 0
  423. entity_category: config
  424. device_class: distance
  425. unit_of_measurement: mm
  426. step: 10
  427. optimistic: True
  428. restore_value: True
  429. on_value:
  430. then:
  431. - script.execute: check_zone3_vaild
  432.  
  433. # Zout1
  434. - platform: template
  435. name: Zout1 X-Begin
  436. id: zone_ex1_x_begin
  437. min_value: -4000
  438. max_value: 4000
  439. mode: box
  440. device_class: distance
  441. entity_category: config
  442. unit_of_measurement: mm
  443. icon: mdi:arrow-left-bold
  444. step: 10
  445. optimistic: True
  446. initial_value: 0
  447. restore_value: True
  448. on_value:
  449. then:
  450. - script.execute: check_zout1_vaild
  451. - platform: template
  452. name: Zout1 X-End
  453. id: zone_ex1_x_end
  454. mode: box
  455. min_value: -4000
  456. max_value: 4000
  457. device_class: distance
  458. unit_of_measurement: mm
  459. entity_category: config
  460. icon: mdi:arrow-right-bold
  461. step: 10
  462. initial_value: 0
  463. optimistic: True
  464. restore_value: True
  465. on_value:
  466. then:
  467. - script.execute: check_zout1_vaild
  468. - platform: template
  469. name: Zout1 Y-Begin
  470. id: zone_ex1_y_begin
  471. mode: box
  472. min_value: 0
  473. max_value: 6000
  474. device_class: distance
  475. entity_category: config
  476. icon: mdi:arrow-up-bold
  477. unit_of_measurement: mm
  478. step: 10
  479. initial_value: 0
  480. optimistic: True
  481. restore_value: True
  482. on_value:
  483. then:
  484. - script.execute: check_zout1_vaild
  485. - platform: template
  486. name: Zout1 Y-End
  487. id: zone_ex1_y_end
  488. icon: mdi:arrow-down-bold
  489. mode: box
  490. min_value: 0
  491. max_value: 6000
  492. initial_value: 0
  493. entity_category: config
  494. device_class: distance
  495. unit_of_measurement: mm
  496. step: 10
  497. optimistic: True
  498. restore_value: True
  499. on_value:
  500. then:
  501. - script.execute: check_zout1_vaild
  502.  
  503. binary_sensor:
  504. - platform: status
  505. name: Online
  506. id: ink_ha_connected
  507. - platform: template
  508. name: "Any Presence"
  509. id: any_target_exsits
  510. device_class: occupancy
  511. filters:
  512. - delayed_off: !lambda |-
  513. if (!id(init_zone_publish) || !id(zone_fn_enable).state) {
  514. return 0;
  515. };
  516. return id(any_presence_timeout).state * 1000.0;
  517. - platform: template
  518. name: "Zone1 Presence"
  519. id: zone1_target_exsits
  520. device_class: occupancy
  521. filters:
  522. - delayed_off: !lambda |-
  523. if (!id(init_zone_publish) || !id(zone_fn_enable).state) {
  524. return 0;
  525. }
  526. return id(zone1_x_timeout).state * 1000.0;
  527. - platform: template
  528. name: "Zone2 Presence"
  529. id: zone2_target_exsits
  530. device_class: occupancy
  531. filters:
  532. - delayed_off: !lambda |-
  533. if (!id(init_zone_publish) || !id(zone_fn_enable).state) {
  534. return 0;
  535. }
  536. return id(zone2_x_timeout).state * 1000.0;
  537. - platform: template
  538. name: "Zone3 Presence"
  539. id: zone3_target_exsits
  540. device_class: occupancy
  541. filters:
  542. - delayed_off: !lambda |-
  543. if (!id(init_zone_publish) || !id(zone_fn_enable).state) {
  544. return 0;
  545. }
  546. return id(zone3_x_timeout).state * 1000.0;
  547. - platform: template
  548. name: "Zout1 Presence"
  549. id: zone_ex1_target_exsits
  550. icon: mdi:account-multiple-remove
  551. device_class: occupancy
  552.  
  553. i2c:
  554. sda: GPIO19
  555. scl: GPIO18
  556. scan: true
  557. id: bus_a
  558.  
  559. script:
  560. - id: check_zone1_vaild
  561. then:
  562. - lambda: |-
  563. if (id(zone1_x_begin).state > id(zone1_x_end).state){
  564. id(tips_zone1_conf).publish_state("Err: X-Begin > X-End");
  565. return;
  566. }
  567. if (id(zone1_y_begin).state > id(zone1_y_end).state){
  568. id(tips_zone1_conf).publish_state("Err: Y-Begin > Y-End");
  569. return;
  570. }
  571. if (id(zone1_x_begin).state == 0, id(zone1_x_end).state == 0, id(zone1_y_begin).state == 0, id(zone1_y_end).state == 0){
  572. id(tips_zone1_conf).publish_state("Configure below");
  573. return;
  574. }
  575.  
  576. int x_size = id(zone1_x_end).state - id(zone1_x_begin).state;
  577. int y_size = id(zone1_y_end).state - id(zone1_y_begin).state;
  578.  
  579. char combined[80];
  580. sprintf(combined, "Curr Size: %d x %d", x_size, y_size);
  581. id(tips_zone1_conf).publish_state(combined);
  582. - id: check_zone2_vaild
  583. then:
  584. - lambda: |-
  585. if (id(zone2_x_begin).state > id(zone2_x_end).state){
  586. id(tips_zone2_conf).publish_state("Err: X-Begin > X-End");
  587. return;
  588. }
  589. if (id(zone2_y_begin).state > id(zone2_y_end).state){
  590. id(tips_zone2_conf).publish_state("Err: Y-Begin > Y-End");
  591. return;
  592. }
  593. if (id(zone2_x_begin).state == 0, id(zone2_x_end).state == 0, id(zone2_y_begin).state == 0, id(zone2_y_end).state == 0){
  594. id(tips_zone2_conf).publish_state("Configure below");
  595. return;
  596. }
  597.  
  598. int x_size = id(zone2_x_end).state - id(zone2_x_begin).state;
  599. int y_size = id(zone2_y_end).state - id(zone2_y_begin).state;
  600.  
  601. char combined[80];
  602. sprintf(combined, "Curr Size: %d x %d", x_size, y_size);
  603. id(tips_zone2_conf).publish_state(combined);
  604. - id: check_zone3_vaild
  605. then:
  606. - lambda: |-
  607. if (id(zone3_x_begin).state > id(zone3_x_end).state){
  608. id(tips_zone3_conf).publish_state("Err: X-Begin > X-End");
  609. return;
  610. }
  611. if (id(zone3_y_begin).state > id(zone3_y_end).state){
  612. id(tips_zone3_conf).publish_state("Err: Y-Begin > Y-End");
  613. return;
  614. }
  615. if (id(zone3_x_begin).state == 0, id(zone3_x_end).state == 0, id(zone3_y_begin).state == 0, id(zone3_y_end).state == 0){
  616. id(tips_zone3_conf).publish_state("Configure below");
  617. return;
  618. }
  619.  
  620. int x_size = id(zone3_x_end).state - id(zone3_x_begin).state;
  621. int y_size = id(zone3_y_end).state - id(zone3_y_begin).state;
  622.  
  623. char combined[80];
  624. sprintf(combined, "Curr Size: %d x %d", x_size, y_size);
  625. id(tips_zone3_conf).publish_state(combined);
  626. - id: check_zout1_vaild
  627. then:
  628. - lambda: |-
  629. if (id(zone_ex1_x_begin).state > id(zone_ex1_x_end).state){
  630. id(tips_zone_ex1_conf).publish_state("Err: X-Begin > X-End");
  631. return;
  632. }
  633. if (id(zone_ex1_y_begin).state > id(zone_ex1_y_end).state){
  634. id(tips_zone_ex1_conf).publish_state("Err: Y-Begin > Y-End");
  635. return;
  636. }
  637. id(tips_zone_ex1_conf).publish_state("Zone Exclusion 1");
  638.  
  639. sensor:
  640. - platform: template
  641. name: "ESP CPU Speed"
  642. accuracy_decimals: 0
  643. icon: mdi:cpu-32-bit
  644. unit_of_measurement: Mhz
  645. disabled_by_default: True
  646. lambda: |-
  647. return (id(cpu_speed));
  648. entity_category: "diagnostic"
  649. update_interval: 600s
  650. - platform: template
  651. id: sys_esp_temperature
  652. name: ESP Temperature
  653. lambda: return temperatureRead();
  654. unit_of_measurement: °C
  655. device_class: TEMPERATURE
  656. update_interval: 45s
  657. entity_category: "diagnostic"
  658. - platform: uptime
  659. name: ESP Uptime
  660. id: sys_uptime
  661. update_interval: 60s
  662. - platform: wifi_signal
  663. name: RSSI
  664. id: wifi_signal_db
  665. update_interval: 60s
  666. entity_category: "diagnostic"
  667. - platform: template
  668. id: esp_memory
  669. icon: mdi:memory
  670. name: ESP Free Memory
  671. lambda: return heap_caps_get_free_size(MALLOC_CAP_INTERNAL) / 1024;
  672. unit_of_measurement: 'kB'
  673. state_class: measurement
  674. entity_category: "diagnostic"
  675. update_interval: 60s
  676. - platform: bh1750
  677. name: "Illuminance"
  678. accuracy_decimals: 1
  679. id: bh1750_light
  680. update_interval: 1s
  681. filters:
  682. - lambda: !lambda |-
  683. if (id(last_illuminance) == x){
  684. return {};
  685. }
  686. if (id(bh1750_fast_update).state){
  687. id(last_illuminance) = x;
  688. // ESP_LOGD("custom", "Fast Update BH1850");
  689. return x;
  690. }
  691. if (abs(id(last_illuminance) - x) > 1){
  692.  
  693. id(last_illuminance) = x;
  694. return x;
  695. }
  696. return {};
  697.  
  698. #-------------------------------------#
  699. # 高级雷达数据
  700. - platform: template
  701. name: "All Target Counts"
  702. id: all_target_count
  703. accuracy_decimals: 0
  704. icon: "mdi:counter"
  705. unit_of_measurement: "targets"
  706. - platform: template
  707. name: "Zone1 Target Counts"
  708. id: zone1_target_count
  709. accuracy_decimals: 0
  710. icon: "mdi:counter"
  711. unit_of_measurement: "targets"
  712. - platform: template
  713. name: "Zone2 Target Counts"
  714. id: zone2_target_count
  715. accuracy_decimals: 0
  716. icon: "mdi:counter"
  717. unit_of_measurement: "targets"
  718. - platform: template
  719. name: "Zone3 Target Counts"
  720. id: zone3_target_count
  721. accuracy_decimals: 0
  722. icon: "mdi:counter"
  723. unit_of_measurement: "targets"
  724. - platform: template
  725. name: "Zout1 Target Counts"
  726. id: zone_ex1_target_count
  727. accuracy_decimals: 0
  728. icon: mdi:account-multiple-minus-outline
  729. unit_of_measurement: "targets"
  730.  
  731. # Target 1
  732. - platform: template
  733. name: "Target1 X"
  734. id: target1_x
  735. accuracy_decimals: 0
  736. unit_of_measurement: 'mm'
  737. state_class: measurement
  738. icon: mdi:focus-field-horizontal
  739. device_class: distance
  740. - platform: template
  741. name: "Target1 Y"
  742. id: target1_y
  743. accuracy_decimals: 0
  744. unit_of_measurement: 'mm'
  745. state_class: measurement
  746. device_class: distance
  747. icon: mdi:focus-field-vertical
  748. - platform: template
  749. name: "Target1 Speed"
  750. id: target1_speed
  751. accuracy_decimals: 2
  752. unit_of_measurement: 'm/s'
  753. state_class: measurement
  754. device_class: speed
  755. - platform: template
  756. name: "Target1 Resolution"
  757. id: target1_resolution
  758. accuracy_decimals: 0
  759. unit_of_measurement: 'mm'
  760. state_class: measurement
  761. device_class: distance
  762.  
  763. # Target 2
  764. - platform: template
  765. name: "Target2 X"
  766. id: target2_x
  767. accuracy_decimals: 0
  768. unit_of_measurement: 'mm'
  769. state_class: measurement
  770. device_class: distance
  771. icon: mdi:focus-field-horizontal
  772. - platform: template
  773. name: "Target2 Y"
  774. id: target2_y
  775. accuracy_decimals: 0
  776. unit_of_measurement: 'mm'
  777. state_class: measurement
  778. device_class: distance
  779. icon: mdi:focus-field-vertical
  780. - platform: template
  781. name: "Target2 Speed"
  782. id: target2_speed
  783. accuracy_decimals: 0
  784. unit_of_measurement: 'm/s'
  785. state_class: measurement
  786. device_class: speed
  787. - platform: template
  788. name: "Target2 Resolution"
  789. id: target2_resolution
  790. accuracy_decimals: 0
  791. unit_of_measurement: 'mm'
  792. state_class: measurement
  793. device_class: distance
  794.  
  795. # Target 3
  796. - platform: template
  797. name: "Target3 X"
  798. id: target3_x
  799. accuracy_decimals: 0
  800. unit_of_measurement: 'mm'
  801. state_class: measurement
  802. device_class: distance
  803. icon: mdi:focus-field-horizontal
  804. - platform: template
  805. name: "Target3 Y"
  806. id: target3_y
  807. accuracy_decimals: 0
  808. unit_of_measurement: 'mm'
  809. state_class: measurement
  810. device_class: distance
  811. icon: mdi:focus-field-vertical
  812. - platform: template
  813. name: "Target3 Speed"
  814. id: target3_speed
  815. accuracy_decimals: 0
  816. unit_of_measurement: 'm/s'
  817. state_class: measurement
  818. device_class: speed
  819. - platform: template
  820. name: "Target3 Resolution"
  821. id: target3_resolution
  822. accuracy_decimals: 0
  823. unit_of_measurement: 'mm'
  824. state_class: measurement
  825. device_class: distance
  826. - platform: template
  827. name: "Target1 Angle"
  828. id: target1_angle
  829. unit_of_measurement: 'º'
  830. accuracy_decimals: 1
  831. icon: mdi:angle-acute
  832. - platform: template
  833. name: "Target2 Angle"
  834. id: target2_angle
  835. accuracy_decimals: 1
  836. unit_of_measurement: 'º'
  837. icon: mdi:angle-acute
  838. - platform: template
  839. name: "Target3 Angle"
  840. id: target3_angle
  841. accuracy_decimals: 1
  842. unit_of_measurement: 'º'
  843. icon: mdi:angle-acute
  844.  
  845. light:
  846. - platform: status_led
  847. name: sys_status
  848. pin: GPIO10
  849. internal: True
  850. restore_mode: ALWAYS_OFF
  851. - platform: binary
  852. name: "Red Info Light"
  853. output: board_info_ed
  854. entity_category: diagnostic
  855. restore_mode: ALWAYS_OFF
  856.  
  857. time:
  858. - platform: sntp
  859. id: time_now
  860.  
  861. output:
  862. - platform: gpio
  863. id: board_info_ed
  864. pin: GPIO3
  865.  
  866. switch:
  867. - platform: factory_reset
  868. name: Factory Reset
  869. disabled_by_default: True
  870. icon: mdi:heart-broken
  871. entity_category: diagnostic
  872. - platform: template
  873. name: Zout1 Enable
  874. id: zone_ex1_enable
  875. optimistic: True
  876. icon: mdi:account-cancel
  877. entity_category: config
  878. restore_mode: RESTORE_DEFAULT_OFF
  879. - platform: template
  880. name: Zone Enable
  881. id: zone_fn_enable
  882. optimistic: True
  883. icon: mdi:target-variant
  884. entity_category: config
  885. restore_mode: RESTORE_DEFAULT_ON
  886. - platform: template
  887. name: Illuminance Fast-Update
  888. id: bh1750_fast_update
  889. optimistic: True
  890. entity_category: diagnostic
  891. restore_mode: RESTORE_DEFAULT_OFF
  892. icon: mdi:run-fast
  893.  
  894. button:
  895. - platform: restart
  896. icon: mdi:power-cycle
  897. name: "ESP Reboot"
  898. entity_category: diagnostic
  899.  
  900. uart:
  901. id: uart_bus
  902. tx_pin:
  903. number: GPIO21
  904. mode:
  905. input: true
  906. pullup: true
  907. rx_pin:
  908. number: GPIO20
  909. mode:
  910. input: true
  911. pullup: true
  912. baud_rate: 256000
  913. parity: NONE
  914. stop_bits: 1
  915. data_bits: 8
  916. debug:
  917. direction: BOTH
  918. dummy_receiver: True
  919. after:
  920. delimiter: [0X55, 0XCC]
  921. sequence:
  922. - lambda: |-
  923. if ((millis() - id(last_update_ld2450)) <= 500) {
  924. return;
  925. };
  926. id(last_update_ld2450) = millis();
  927.  
  928. // p1
  929. int16_t p1_x = (uint16_t((bytes[5] << 8) | bytes[4] ));
  930. if ((bytes[5] & 0x80) >> 7){
  931. p1_x -= pow(2, 15);
  932. }else{
  933. p1_x = 0 - p1_x;
  934. }
  935.  
  936. int16_t p1_y = (uint16_t((bytes[7] << 8) | bytes[6] ));
  937. if ((bytes[7] & 0x80) >> 7){
  938. p1_y -= pow(2, 15);
  939. }else{
  940. p1_y = 0 - p1_y;
  941. }
  942.  
  943. int p1_speed = (bytes[9] << 8 | bytes[8] );
  944. if ((bytes[9] & 0x80) >> 7){
  945. p1_speed -= pow(2, 15);
  946. }else{
  947. p1_speed = 0 - p1_speed;
  948. }
  949. int16_t p1_distance_resolution = (uint16_t((bytes[11] << 8) | bytes[10] ));
  950.  
  951. // p2
  952. int16_t p2_x = (uint16_t((bytes[13] << 8) | bytes[12] ));
  953. if ((bytes[13] & 0x80) >> 7){
  954. p2_x -= pow(2, 15);
  955. }else{
  956. p2_x = 0 - p2_x;
  957. }
  958.  
  959. int16_t p2_y = (uint16_t((bytes[15] << 8) | bytes[14] ));
  960. if ((bytes[15] & 0x80) >> 7){
  961. p2_y -= pow(2, 15);
  962. }else{
  963. p2_y = 0 - p2_y;
  964. }
  965.  
  966. int p2_speed = (bytes[17] << 8 | bytes[16] );
  967. if ((bytes[17] & 0x80) >> 7){
  968. p2_speed -= pow(2, 15);
  969. }else{
  970. p2_speed = 0 - p2_speed;
  971. }
  972. int16_t p2_distance_resolution = (uint16_t((bytes[19] << 8) | bytes[18] ));
  973.  
  974. // p3
  975. int16_t p3_x = (uint16_t((bytes[21] << 8) | bytes[20] ));
  976. if ((bytes[21] & 0x80) >> 7){
  977. p3_x -= pow(2, 15);
  978. }else{
  979. p3_x = 0 - p3_x;
  980. }
  981.  
  982. int16_t p3_y = (uint16_t((bytes[23] << 8) | bytes[22] ));
  983. if ((bytes[23] & 0x80) >> 7){
  984. p3_y -= pow(2, 15);
  985. }else{
  986. p3_y = 0 - p3_y;
  987. }
  988.  
  989. int p3_speed = (bytes[25] << 8 | bytes[24] );
  990. if ((bytes[25] & 0x80) >> 7){
  991. p3_speed -= pow(2, 15);
  992. }else{
  993. p3_speed = 0 - p3_speed;
  994. }
  995.  
  996. int16_t p3_distance_resolution = (uint16_t((bytes[27] << 8) | bytes[26] ));
  997.  
  998. bool p1_vaild = (p1_x != 0 || p1_y > 0);
  999. bool p2_vaild = (p2_x != 0 || p2_y > 0);
  1000. bool p3_vaild = (p3_x != 0 || p3_y > 0);
  1001.  
  1002. // zone exlude 1
  1003.  
  1004. int16_t target_count_in_zone_ex1 = 0;
  1005.  
  1006. int16_t zone_ex1_x_min = id(zone_ex1_x_begin).state;
  1007. int16_t zone_ex1_x_max = id(zone_ex1_x_end).state;
  1008. int16_t zone_ex1_y_min = id(zone_ex1_y_begin).state;
  1009. int16_t zone_ex1_y_max = id(zone_ex1_y_end).state;
  1010.  
  1011. bool p1_zone_ex_enter = false;
  1012. bool p2_zone_ex_enter = false;
  1013. bool p3_zone_ex_enter = false;
  1014.  
  1015. if (id(zone_ex1_enable).state){
  1016. if (p1_vaild){
  1017. if (p1_x >= zone_ex1_x_min && p1_x <= zone_ex1_x_max && p1_y >= zone_ex1_y_min && p1_y <= zone_ex1_y_max){
  1018. p1_zone_ex_enter = true;
  1019. target_count_in_zone_ex1 ++;
  1020. }
  1021. }
  1022. if (p2_vaild){
  1023. if (p2_x >= zone_ex1_x_min && p2_x <= zone_ex1_x_max && p2_y >= zone_ex1_y_min && p2_y <= zone_ex1_y_max){
  1024. p2_zone_ex_enter = true;
  1025. target_count_in_zone_ex1 ++;
  1026. }
  1027. }
  1028. if (p3_vaild){
  1029. if (p3_x >= zone_ex1_x_min && p3_x <= zone_ex1_x_max && p3_y >= zone_ex1_y_min && p3_y <= zone_ex1_y_max){
  1030. p3_zone_ex_enter = true;
  1031. target_count_in_zone_ex1 ++;
  1032. }
  1033. }
  1034. }
  1035.  
  1036. bool has_target_in_zone_ex1 = (target_count_in_zone_ex1 > 0);
  1037.  
  1038. int16_t all_target_counts = 0;
  1039. if (p1_vaild && !p1_zone_ex_enter){
  1040. all_target_counts ++;
  1041. }
  1042. if (p2_vaild && !p2_zone_ex_enter){
  1043. all_target_counts ++;
  1044. }
  1045. if (p3_vaild && !p3_zone_ex_enter){
  1046. all_target_counts ++;
  1047. }
  1048.  
  1049. bool has_target_in_zone_all = (all_target_counts > 0);
  1050.  
  1051. int16_t target_count_in_zone1 = 0;
  1052. bool has_target_in_zone1 = false;
  1053.  
  1054. int16_t target_count_in_zone2 = 0;
  1055. bool has_target_in_zone2 = false;
  1056.  
  1057. int16_t target_count_in_zone3 = 0;
  1058. bool has_target_in_zone3 = false;
  1059.  
  1060. if (id(zone_fn_enable).state){
  1061.  
  1062. // zone 1 check
  1063.  
  1064. int16_t zone1_x_min = id(zone1_x_begin).state;
  1065. int16_t zone1_x_max = id(zone1_x_end).state;
  1066. int16_t zone1_y_min = id(zone1_y_begin).state;
  1067. int16_t zone1_y_max = id(zone1_y_end).state;
  1068.  
  1069. if (p1_vaild && !p1_zone_ex_enter){
  1070. if (p1_x >= zone1_x_min && p1_x <= zone1_x_max && p1_y >= zone1_y_min && p1_y <= zone1_y_max){
  1071. target_count_in_zone1 ++;
  1072. }
  1073. }
  1074. if (p2_vaild && !p2_zone_ex_enter){
  1075. if (p2_x >= zone1_x_min && p2_x <= zone1_x_max && p2_y >= zone1_y_min && p2_y <= zone1_y_max){
  1076. target_count_in_zone1 ++;
  1077. }
  1078. }
  1079. if (p3_vaild && !p3_zone_ex_enter){
  1080. if (p3_x >= zone1_x_min && p3_x <= zone1_x_max && p3_y >= zone1_y_min && p3_y <= zone1_y_max){
  1081. target_count_in_zone1 ++;
  1082. }
  1083. }
  1084. has_target_in_zone1 = (target_count_in_zone1 > 0);
  1085.  
  1086. // zone 2 check
  1087.  
  1088. int16_t zone2_x_min = id(zone2_x_begin).state;
  1089. int16_t zone2_x_max = id(zone2_x_end).state;
  1090. int16_t zone2_y_min = id(zone2_y_begin).state;
  1091. int16_t zone2_y_max = id(zone2_y_end).state;
  1092.  
  1093. if (p1_vaild && !p1_zone_ex_enter){
  1094. if (p1_x >= zone2_x_min && p1_x <= zone2_x_max && p1_y >= zone2_y_min && p1_y <= zone2_y_max){
  1095. target_count_in_zone2 ++;
  1096. }
  1097. }
  1098. if (p2_vaild && !p2_zone_ex_enter){
  1099. if (p2_x >= zone2_x_min && p2_x <= zone2_x_max && p2_y >= zone2_y_min && p2_y <= zone2_y_max){
  1100. target_count_in_zone2 ++;
  1101. }
  1102. }
  1103. if (p3_vaild && !p3_zone_ex_enter){
  1104. if (p3_x >= zone2_x_min && p3_x <= zone2_x_max && p3_y >= zone2_y_min && p3_y <= zone2_y_max){
  1105. target_count_in_zone2 ++;
  1106. }
  1107. }
  1108.  
  1109. has_target_in_zone2 = (target_count_in_zone2 > 0);
  1110.  
  1111. // zone 3 check
  1112.  
  1113. int16_t zone3_x_min = id(zone3_x_begin).state;
  1114. int16_t zone3_x_max = id(zone3_x_end).state;
  1115. int16_t zone3_y_min = id(zone3_y_begin).state;
  1116. int16_t zone3_y_max = id(zone3_y_end).state;
  1117.  
  1118. if (p1_vaild && !p1_zone_ex_enter){
  1119. if (p1_x >= zone3_x_min && p1_x <= zone3_x_max && p1_y >= zone3_y_min && p1_y <= zone3_y_max){
  1120. target_count_in_zone3 ++;
  1121. }
  1122. }
  1123. if (p2_vaild && !p2_zone_ex_enter){
  1124. if (p2_x >= zone3_x_min && p2_x <= zone3_x_max && p2_y >= zone3_y_min && p2_y <= zone3_y_max){
  1125. target_count_in_zone3 ++;
  1126. }
  1127. }
  1128. if (p3_vaild && !p3_zone_ex_enter){
  1129. if (p3_x >= zone3_x_min && p3_x <= zone3_x_max && p3_y >= zone3_y_min && p3_y <= zone3_y_max){
  1130. target_count_in_zone3 ++;
  1131. }
  1132. }
  1133. has_target_in_zone3 = (target_count_in_zone3 > 0);
  1134.  
  1135. }
  1136.  
  1137. // Angle, Position and Direction, idea from walberjunior.
  1138.  
  1139. float p1_angle = 0;
  1140. if (p1_vaild){
  1141. p1_angle = ((float)p1_x / (float)p1_y) * 180 / M_PI;;
  1142. }
  1143.  
  1144. std::basic_string<char> p1_position = "Static";
  1145. if (p1_speed > 0) {
  1146. p1_position = "Moving away";
  1147. } else if (p1_speed < 0) {
  1148. p1_position = "Approaching";
  1149. }
  1150.  
  1151. std::basic_string<char> p1_direction = "None";
  1152. if (p1_x > 0) {
  1153. p1_direction = "Right";
  1154. } else if (p1_x < 0) {
  1155. p1_direction = "Left";
  1156. } else if (p1_y > 0){
  1157. p1_direction = "Middle";
  1158. }
  1159.  
  1160. float p2_angle = 0;
  1161. if (p2_vaild){
  1162. p2_angle = ((float)p2_x / (float)p2_y) * 180 / M_PI;;
  1163. }
  1164.  
  1165. std::basic_string<char> p2_position = "Static";;
  1166. if (p2_speed > 0) {
  1167. p2_position = "Moving away";
  1168. } else if (p2_speed < 0) {
  1169. p2_position = "Approaching";
  1170. }
  1171.  
  1172. std::basic_string<char> p2_direction = "None";
  1173. if (p2_x > 0) {
  1174. p2_direction = "Right";
  1175. } else if (p2_x < 0) {
  1176. p2_direction = "Left";
  1177. } else if (p2_y > 0){
  1178. p2_direction = "Middle";
  1179. }
  1180.  
  1181. float p3_angle = 0;
  1182. if (p3_vaild){
  1183. p3_angle = ((float)p3_x / (float)p3_y) * 180 / M_PI;;
  1184. }
  1185.  
  1186. std::basic_string<char> p3_position = "Static";;
  1187. if (p3_speed > 0) {
  1188. p3_position = "Moving away";
  1189. } else if (p3_speed < 0) {
  1190. p3_position = "Approaching";
  1191. }
  1192.  
  1193. std::basic_string<char> p3_direction = "None";
  1194. if (p3_x > 0) {
  1195. p3_direction = "Right";
  1196. } else if (p3_x < 0) {
  1197. p3_direction = "Left";
  1198. } else if (p3_y > 0){
  1199. p3_direction = "Middle";
  1200. }
  1201.  
  1202. if (id(target1_angle).state != p1_angle){
  1203. id(target1_angle).publish_state(p1_angle);
  1204. }
  1205. if (id(target2_angle).state != p2_angle){
  1206. id(target2_angle).publish_state(p2_angle);
  1207. }
  1208. if (id(target3_angle).state != p3_angle){
  1209. id(target3_angle).publish_state(p3_angle);
  1210. }
  1211.  
  1212. if (p1_position != id(target1_position).state){
  1213. id(target1_position).publish_state(p1_position);
  1214. }
  1215. if (p2_position != id(target2_position).state){
  1216. id(target2_position).publish_state(p2_position);
  1217. }
  1218. if (p3_position != id(target3_position).state){
  1219. id(target3_position).publish_state(p3_position);
  1220. }
  1221.  
  1222. if (p1_direction != id(target1_direction).state){
  1223. id(target1_direction).publish_state(p1_direction);
  1224. }
  1225. if (p2_direction != id(target2_direction).state){
  1226. id(target2_direction).publish_state(p2_direction);
  1227. }
  1228. if (p3_direction != id(target3_direction).state){
  1229. id(target3_direction).publish_state(p3_direction);
  1230. }
  1231.  
  1232. // public all info
  1233.  
  1234. if (id(target1_x).state != p1_x){
  1235. id(target1_x).publish_state(p1_x);
  1236. }
  1237. if (id(target1_y).state != p1_y){
  1238. id(target1_y).publish_state(p1_y);
  1239. }
  1240.  
  1241. float p1_m_speed = float(p1_speed) / 100.0;
  1242. if (id(target1_speed).state != p1_m_speed){
  1243. id(target1_speed).publish_state(p1_m_speed);
  1244. }
  1245. if (id(target1_resolution).state != p1_distance_resolution){
  1246. id(target1_resolution).publish_state(p1_distance_resolution);
  1247. }
  1248.  
  1249. if (id(target2_x).state != p2_x){
  1250. id(target2_x).publish_state(p2_x);
  1251. }
  1252. if (id(target2_y).state != p2_y){
  1253. id(target2_y).publish_state(p2_y);
  1254. }
  1255. if (id(target2_speed).state != p2_speed){
  1256. id(target2_speed).publish_state(p2_speed);
  1257. }
  1258. if (id(target2_resolution).state != p2_distance_resolution){
  1259. id(target2_resolution).publish_state(p2_distance_resolution);
  1260. }
  1261.  
  1262. if (id(target3_x).state != p3_x){
  1263. id(target3_x).publish_state(p3_x);
  1264. }
  1265. if (id(target3_y).state != p3_y){
  1266. id(target3_y).publish_state(p3_y);
  1267. }
  1268. if (id(target3_speed).state != p3_speed){
  1269. id(target3_speed).publish_state(p3_speed);
  1270. }
  1271. if (id(target3_resolution).state != p3_distance_resolution){
  1272. id(target3_resolution).publish_state(p3_distance_resolution);
  1273. }
  1274.  
  1275. // publish target info
  1276.  
  1277. if (id(all_target_count).state != all_target_counts){
  1278. id(all_target_count).publish_state(all_target_counts);
  1279. id(any_target_exsits).publish_state(has_target_in_zone_all);
  1280. }else if (id(any_target_exsits).state != has_target_in_zone_all){
  1281. id(any_target_exsits).publish_state(has_target_in_zone_all);
  1282. }
  1283.  
  1284. if (id(zone1_target_count).state != target_count_in_zone1){
  1285. id(zone1_target_count).publish_state(target_count_in_zone1);
  1286. id(zone1_target_exsits).publish_state(has_target_in_zone1);
  1287. }else if (id(zone1_target_exsits).state != has_target_in_zone1){
  1288. id(zone1_target_exsits).publish_state(has_target_in_zone1);
  1289. }
  1290.  
  1291. if (id(zone2_target_count).state != target_count_in_zone2){
  1292. id(zone2_target_count).publish_state(target_count_in_zone2);
  1293. id(zone2_target_exsits).publish_state(has_target_in_zone2);
  1294. }else if (id(zone2_target_exsits).state != has_target_in_zone2){
  1295. id(zone2_target_exsits).publish_state(has_target_in_zone2);
  1296. }
  1297.  
  1298. if (id(zone3_target_count).state != target_count_in_zone3){
  1299. id(zone3_target_count).publish_state(target_count_in_zone3);
  1300. id(zone3_target_exsits).publish_state(has_target_in_zone3);
  1301. }else if (id(zone3_target_exsits).state != has_target_in_zone3){
  1302. id(zone3_target_exsits).publish_state(has_target_in_zone3);
  1303. }
  1304.  
  1305. // zout
  1306. if (id(zone_ex1_target_count).state != target_count_in_zone_ex1){
  1307. id(zone_ex1_target_count).publish_state(target_count_in_zone_ex1);
  1308. }
  1309.  
  1310. if (id(zone_ex1_target_exsits).state != has_target_in_zone_ex1){
  1311. id(zone_ex1_target_exsits).publish_state(has_target_in_zone_ex1);
  1312. }
  1313.  
  1314. if (!id(init_zone_publish)){
  1315. id(init_zone_publish) = true;
  1316. }
Advertisement
Add Comment
Please, Sign In to add comment