add 2a beta, and fix some type.

This commit is contained in:
sen 2023-08-21 18:13:25 +08:00
parent d4083a26cc
commit 0635c3cb7f
12 changed files with 964 additions and 1004 deletions

View File

@ -1,163 +0,0 @@
esphome:
name: screek-humen-dectet-1w
comment: Screek Human Presence Sensor 24GHz PS-HPS
friendly_name: Screek Human Presence Sensor
name_add_mac_suffix: True
platformio_options:
board_build.flash_mode: dio
project:
name: Screek.Human_Presence_Sensor
version: 1W
external_components:
- source: screek-mod-components
esp32:
board: esp32-c3-devkitm-1
logger:
api:
ota:
password: "YOUR_OTA_PASSWORD"
wifi:
# ssid: !secret wifi_ssid
# password: !secret wifi_password
# fast_connect: True
power_save_mode: NONE
# output_power: 20dB
# Enable fallback hotspot (captive portal) in case wifi connection fails
ap:
ssid: "SCREEK HUMAN-SENSOR"
captive_portal:
web_server:
port: 80
binary_sensor:
- platform: status
name: Online
id: ink_ha_connected
- platform: ld2410
has_target:
name: Presence
has_moving_target:
name: Moving Target
has_still_target:
name: Still Target
sensor:
- platform: template
id: sys_esp_temperature
name: ESP Temperature
lambda: return temperatureRead();
unit_of_measurement: °C
device_class: TEMPERATURE
update_interval: 30s
entity_category: "diagnostic"
- platform: uptime
name: Uptime
id: sys_uptime
update_interval: 10s
- platform: wifi_signal
name: RSSI
id: wifi_signal_db
update_interval: 1s
entity_category: "diagnostic"
- platform: template
id: esp_memory
icon: mdi:memory
name: ESP Free Memory
lambda: return heap_caps_get_free_size(MALLOC_CAP_INTERNAL) / 1024;
unit_of_measurement: 'kB'
state_class: measurement
entity_category: "diagnostic"
- platform: ld2410
moving_distance:
name : Moving Distance
id: moving_distance
still_distance:
name: Still Distance
id: still_distance
moving_energy:
name: Move Energy
still_energy:
name: Still Energy
detection_distance:
name: Detection Distance
light:
name: Sun Light
time:
- platform: sntp
id: time_now
servers:
- ntp.aliyun.com
uart:
id: uart1
tx_pin: GPIO5
rx_pin: GPIO4
baud_rate: 256000
parity: NONE
stop_bits: 1
ld2410:
timeout: 150s
id: ld2410_radar
# max_move_distance : 6m
# max_still_distance: 0.75m
# g0_move_threshold: 10
# g0_still_threshold: 20
# g1_move_threshold: 10
# g1_still_threshold: 20
# g2_move_threshold: 20
# g2_still_threshold: 21
# g3_move_threshold: 30
# g3_still_threshold: 31
# g4_move_threshold: 40
# g4_still_threshold: 41
# g5_move_threshold: 50
# g5_still_threshold: 51
# g6_move_threshold: 60
# g6_still_threshold: 61
# g7_move_threshold: 70
# g7_still_threshold: 71
# g8_move_threshold: 80
# g8_still_threshold: 81
button:
- platform: template
name: "Enable LD2410 BLE"
entity_category: "config"
icon: mdi:bluetooth
on_press:
lambda: |-
id(ld2410_radar) -> ble_control(true);
- platform: template
name: "Disable LD2410 BLE"
entity_category: "config"
icon: mdi:bluetooth-off
on_press:
lambda: |-
id(ld2410_radar) -> ble_control(false);
- platform: template
name: "LD2410 Reboot"
icon: mdi:radar
entity_category: "config"
on_press:
lambda: |-
// auto* radar = LD2410Component::get(ld2410);
// radar -> roboot();
id(ld2410_radar) -> reboot();
- platform: restart
icon: mdi:power-cycle
name: "ESP Reboot"
- platform: factory_reset
disabled_by_default: True
name: Factory Reset
id: factory_reset_all

View File

@ -1,158 +0,0 @@
import esphome.codegen as cg
import esphome.config_validation as cv
from esphome.components import uart
from esphome.const import CONF_ID, CONF_TIMEOUT
from esphome import automation
from esphome.automation import maybe_simple_id
DEPENDENCIES = ["uart"]
CODEOWNERS = ["@sebcaps"]
MULTI_CONF = True
ld2410_ns = cg.esphome_ns.namespace("ld2410")
LD2410Component = ld2410_ns.class_("LD2410Component", cg.Component, uart.UARTDevice)
LD2410Restart = ld2410_ns.class_("LD2410Restart", automation.Action)
CONF_LD2410_ID = "ld2410_id"
CONF_MAX_MOVE_DISTANCE = "max_move_distance"
CONF_MAX_STILL_DISTANCE = "max_still_distance"
CONF_G0_MOVE_THRESHOLD = "g0_move_threshold"
CONF_G0_STILL_THRESHOLD = "g0_still_threshold"
CONF_G1_MOVE_THRESHOLD = "g1_move_threshold"
CONF_G1_STILL_THRESHOLD = "g1_still_threshold"
CONF_G2_MOVE_THRESHOLD = "g2_move_threshold"
CONF_G2_STILL_THRESHOLD = "g2_still_threshold"
CONF_G3_MOVE_THRESHOLD = "g3_move_threshold"
CONF_G3_STILL_THRESHOLD = "g3_still_threshold"
CONF_G4_MOVE_THRESHOLD = "g4_move_threshold"
CONF_G4_STILL_THRESHOLD = "g4_still_threshold"
CONF_G5_MOVE_THRESHOLD = "g5_move_threshold"
CONF_G5_STILL_THRESHOLD = "g5_still_threshold"
CONF_G6_MOVE_THRESHOLD = "g6_move_threshold"
CONF_G6_STILL_THRESHOLD = "g6_still_threshold"
CONF_G7_MOVE_THRESHOLD = "g7_move_threshold"
CONF_G7_STILL_THRESHOLD = "g7_still_threshold"
CONF_G8_MOVE_THRESHOLD = "g8_move_threshold"
CONF_G8_STILL_THRESHOLD = "g8_still_threshold"
DISTANCES = [0.75, 1.5, 2.25, 3, 3.75, 4.5, 5.25, 6]
CONFIG_SCHEMA = cv.All(
cv.Schema(
{
cv.GenerateID(): cv.declare_id(LD2410Component),
cv.Optional(CONF_MAX_MOVE_DISTANCE, default="4.5m"): cv.All(
cv.distance, cv.one_of(*DISTANCES, float=True)
),
cv.Optional(CONF_MAX_STILL_DISTANCE, default="4.5m"): cv.All(
cv.distance, cv.one_of(*DISTANCES, float=True)
),
cv.Optional(CONF_TIMEOUT, default="5s"): cv.All(
cv.positive_time_period_seconds,
cv.Range(max=cv.TimePeriod(seconds=32767)),
),
cv.Optional(CONF_G0_MOVE_THRESHOLD, default=50): cv.int_range(
min=0, max=100
),
cv.Optional(CONF_G0_STILL_THRESHOLD, default=0): cv.int_range(
min=0, max=100
),
cv.Optional(CONF_G1_MOVE_THRESHOLD, default=50): cv.int_range(
min=0, max=100
),
cv.Optional(CONF_G1_STILL_THRESHOLD, default=0): cv.int_range(
min=0, max=100
),
cv.Optional(CONF_G2_MOVE_THRESHOLD, default=40): cv.int_range(
min=0, max=100
),
cv.Optional(CONF_G2_STILL_THRESHOLD, default=40): cv.int_range(
min=0, max=100
),
cv.Optional(CONF_G3_MOVE_THRESHOLD, default=40): cv.int_range(
min=0, max=100
),
cv.Optional(CONF_G3_STILL_THRESHOLD, default=40): cv.int_range(
min=0, max=100
),
cv.Optional(CONF_G4_MOVE_THRESHOLD, default=40): cv.int_range(
min=0, max=100
),
cv.Optional(CONF_G4_STILL_THRESHOLD, default=40): cv.int_range(
min=0, max=100
),
cv.Optional(CONF_G5_MOVE_THRESHOLD, default=40): cv.int_range(
min=0, max=100
),
cv.Optional(CONF_G5_STILL_THRESHOLD, default=40): cv.int_range(
min=0, max=100
),
cv.Optional(CONF_G6_MOVE_THRESHOLD, default=30): cv.int_range(
min=0, max=100
),
cv.Optional(CONF_G6_STILL_THRESHOLD, default=15): cv.int_range(
min=0, max=100
),
cv.Optional(CONF_G7_MOVE_THRESHOLD, default=30): cv.int_range(
min=0, max=100
),
cv.Optional(CONF_G7_STILL_THRESHOLD, default=15): cv.int_range(
min=0, max=100
),
cv.Optional(CONF_G8_MOVE_THRESHOLD, default=30): cv.int_range(
min=0, max=100
),
cv.Optional(CONF_G8_STILL_THRESHOLD, default=15): cv.int_range(
min=0, max=100
),
}
)
.extend(uart.UART_DEVICE_SCHEMA)
.extend(cv.COMPONENT_SCHEMA)
)
FINAL_VALIDATE_SCHEMA = uart.final_validate_device_schema(
"ld2410",
baud_rate=256000,
require_tx=True,
require_rx=True,
parity="NONE",
stop_bits=1,
)
async def to_code(config):
var = cg.new_Pvariable(config[CONF_ID])
await cg.register_component(var, config)
await uart.register_uart_device(var, config)
cg.add(var.set_timeout(config[CONF_TIMEOUT]))
cg.add(var.set_max_move_distance(int(config[CONF_MAX_MOVE_DISTANCE] / 0.75)))
cg.add(var.set_max_still_distance(int(config[CONF_MAX_STILL_DISTANCE] / 0.75)))
cg.add(
var.set_range_config(
config[CONF_G0_MOVE_THRESHOLD],
config[CONF_G0_STILL_THRESHOLD],
config[CONF_G1_MOVE_THRESHOLD],
config[CONF_G1_STILL_THRESHOLD],
config[CONF_G2_MOVE_THRESHOLD],
config[CONF_G2_STILL_THRESHOLD],
config[CONF_G3_MOVE_THRESHOLD],
config[CONF_G3_STILL_THRESHOLD],
config[CONF_G4_MOVE_THRESHOLD],
config[CONF_G4_STILL_THRESHOLD],
config[CONF_G5_MOVE_THRESHOLD],
config[CONF_G5_STILL_THRESHOLD],
config[CONF_G6_MOVE_THRESHOLD],
config[CONF_G6_STILL_THRESHOLD],
config[CONF_G7_MOVE_THRESHOLD],
config[CONF_G7_STILL_THRESHOLD],
config[CONF_G8_MOVE_THRESHOLD],
config[CONF_G8_STILL_THRESHOLD],
)
)
CALIBRATION_ACTION_SCHEMA = maybe_simple_id(
{
cv.Required(CONF_ID): cv.use_id(LD2410Component),
}
)

View File

@ -1,36 +0,0 @@
import esphome.codegen as cg
from esphome.components import binary_sensor
import esphome.config_validation as cv
from esphome.const import DEVICE_CLASS_MOTION, DEVICE_CLASS_OCCUPANCY
from . import CONF_LD2410_ID, LD2410Component
DEPENDENCIES = ["ld2410"]
CONF_HAS_TARGET = "has_target"
CONF_HAS_MOVING_TARGET = "has_moving_target"
CONF_HAS_STILL_TARGET = "has_still_target"
CONFIG_SCHEMA = {
cv.GenerateID(CONF_LD2410_ID): cv.use_id(LD2410Component),
cv.Optional(CONF_HAS_TARGET): binary_sensor.binary_sensor_schema(
device_class=DEVICE_CLASS_OCCUPANCY
),
cv.Optional(CONF_HAS_MOVING_TARGET): binary_sensor.binary_sensor_schema(
device_class=DEVICE_CLASS_MOTION
),
cv.Optional(CONF_HAS_STILL_TARGET): binary_sensor.binary_sensor_schema(
device_class=DEVICE_CLASS_OCCUPANCY
),
}
async def to_code(config):
ld2410_component = await cg.get_variable(config[CONF_LD2410_ID])
if CONF_HAS_TARGET in config:
sens = await binary_sensor.new_binary_sensor(config[CONF_HAS_TARGET])
cg.add(ld2410_component.set_target_sensor(sens))
if CONF_HAS_MOVING_TARGET in config:
sens = await binary_sensor.new_binary_sensor(config[CONF_HAS_MOVING_TARGET])
cg.add(ld2410_component.set_moving_target_sensor(sens))
if CONF_HAS_STILL_TARGET in config:
sens = await binary_sensor.new_binary_sensor(config[CONF_HAS_STILL_TARGET])
cg.add(ld2410_component.set_still_target_sensor(sens))

View File

@ -1,416 +0,0 @@
#include "ld2410.h"
#define highbyte(val) (uint8_t)((val) >> 8)
#define lowbyte(val) (uint8_t)((val) &0xff)
namespace esphome {
namespace ld2410 {
static const char *const TAG = "ld2410";
void LD2410Component::dump_config() {
ESP_LOGCONFIG(TAG, "LD2410:");
#ifdef USE_BINARY_SENSOR
LOG_BINARY_SENSOR(" ", "HasTargetSensor", this->target_binary_sensor_);
LOG_BINARY_SENSOR(" ", "MovingSensor", this->moving_binary_sensor_);
LOG_BINARY_SENSOR(" ", "StillSensor", this->still_binary_sensor_);
#endif
#ifdef USE_SENSOR
LOG_SENSOR(" ", "Moving Distance", this->moving_target_distance_sensor_);
LOG_SENSOR(" ", "Still Distance", this->still_target_distance_sensor_);
LOG_SENSOR(" ", "Moving Energy", this->moving_target_energy_sensor_);
LOG_SENSOR(" ", "Still Energy", this->still_target_energy_sensor_);
LOG_SENSOR(" ", "Detection Distance", this->detection_distance_sensor_);
LOG_SENSOR(" ", "Light", this->light_sensor_);
#endif
this->set_config_mode_(true);
this->get_version_();
this->set_config_mode_(false);
ESP_LOGCONFIG(TAG, " Firmware Version : %u.%u.%u%u%u%u", this->version_[0], this->version_[1], this->version_[2],
this->version_[3], this->version_[4], this->version_[5]);
}
void LD2410Component::setup() {
ESP_LOGCONFIG(TAG, "Setting up LD2410...");
// ESP_LOGCONFIG(TAG, "Apply screek patch...");
this->set_config_mode_(true);
/*
this->set_max_distances_timeout_(this->max_move_distance_, this->max_still_distance_, this->timeout_);
// Configure Gates sensitivity
this->set_gate_threshold_(0, this->rg0_move_threshold_, this->rg0_still_threshold_);
this->set_gate_threshold_(1, this->rg1_move_threshold_, this->rg1_still_threshold_);
this->set_gate_threshold_(2, this->rg2_move_threshold_, this->rg2_still_threshold_);
this->set_gate_threshold_(3, this->rg3_move_threshold_, this->rg3_still_threshold_);
this->set_gate_threshold_(4, this->rg4_move_threshold_, this->rg4_still_threshold_);
this->set_gate_threshold_(5, this->rg5_move_threshold_, this->rg5_still_threshold_);
this->set_gate_threshold_(6, this->rg6_move_threshold_, this->rg6_still_threshold_);
this->set_gate_threshold_(7, this->rg7_move_threshold_, this->rg7_still_threshold_);
this->set_gate_threshold_(8, this->rg8_move_threshold_, this->rg8_still_threshold_);
*/
this->get_version_();
this->set_config_mode_(false);
// this->factory_mode(true);
ESP_LOGCONFIG(TAG, "Firmware Version : %u.%u.%u%u%u%u", this->version_[0], this->version_[1], this->version_[2],
this->version_[3], this->version_[4], this->version_[5]);
ESP_LOGCONFIG(TAG, "LD2410 setup complete.");
}
void LD2410Component::loop() {
const int max_line_length = 80;
static uint8_t buffer[max_line_length];
while (available()) {
this->readline_(read(), buffer, max_line_length);
}
}
void LD2410Component::send_command_(uint8_t command, uint8_t *command_value, int command_value_len) {
// lastCommandSuccess->publish_state(false);
// frame start bytes
this->write_array(CMD_FRAME_HEADER, 4);
// length bytes
int len = 2;
if (command_value != nullptr)
len += command_value_len;
this->write_byte(lowbyte(len));
this->write_byte(highbyte(len));
// command
this->write_byte(lowbyte(command));
this->write_byte(highbyte(command));
// command value bytes
if (command_value != nullptr) {
for (int i = 0; i < command_value_len; i++) {
this->write_byte(command_value[i]);
}
}
// frame end bytes
this->write_array(CMD_FRAME_END, 4);
// FIXME to remove
delay(50); // NOLINT
}
long map(long x, long in_min, long in_max, long out_min, long out_max) {
return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
}
void LD2410Component::handle_periodic_data_(uint8_t *buffer, int len) {
if (len < 12)
return; // 4 frame start bytes + 2 length bytes + 1 data end byte + 1 crc byte + 4 frame end bytes
if (buffer[0] != 0xF4 || buffer[1] != 0xF3 || buffer[2] != 0xF2 || buffer[3] != 0xF1) // check 4 frame start bytes
return;
if (buffer[7] != HEAD || buffer[len - 6] != END || buffer[len - 5] != CHECK) // Check constant values
return; // data head=0xAA, data end=0x55, crc=0x00
/*
Data Type: 6th
0x01: Engineering mode
0x02: Normal mode
*/
// char data_type = buffer[DATA_TYPES];
/*
Target states: 9th
0x00 = No target
0x01 = Moving targets
0x02 = Still targets
0x03 = Moving+Still targets
*/
#ifdef USE_BINARY_SENSOR
char target_state = buffer[TARGET_STATES];
if (this->target_binary_sensor_ != nullptr) {
this->target_binary_sensor_->publish_state(target_state != 0x00);
}
#endif
/*
Reduce data update rate to prevent home assistant database size grow fast
*/
int32_t current_millis = millis();
if (current_millis - last_periodic_millis < 1000)
return;
last_periodic_millis = current_millis;
#ifdef USE_BINARY_SENSOR
if (this->moving_binary_sensor_ != nullptr) {
this->moving_binary_sensor_->publish_state(CHECK_BIT(target_state, 0));
}
if (this->still_binary_sensor_ != nullptr) {
this->still_binary_sensor_->publish_state(CHECK_BIT(target_state, 1));
}
#endif
/*
Moving target distance: 10~11th bytes
Moving target energy: 12th byte
Still target distance: 13~14th bytes
Still target energy: 15th byte
Detect distance: 16~17th bytes
*/
#ifdef USE_SENSOR
if (this->moving_target_distance_sensor_ != nullptr) {
int new_moving_target_distance = this->two_byte_to_int_(buffer[MOVING_TARGET_LOW], buffer[MOVING_TARGET_HIGH]);
if (this->moving_target_distance_sensor_->get_state() != new_moving_target_distance)
this->moving_target_distance_sensor_->publish_state(new_moving_target_distance);
}
if (this->moving_target_energy_sensor_ != nullptr) {
int new_moving_target_energy = buffer[MOVING_ENERGY];
if (this->moving_target_energy_sensor_->get_state() != new_moving_target_energy)
this->moving_target_energy_sensor_->publish_state(new_moving_target_energy);
}
if (this->still_target_distance_sensor_ != nullptr) {
int new_still_target_distance = this->two_byte_to_int_(buffer[STILL_TARGET_LOW], buffer[STILL_TARGET_HIGH]);
if (this->still_target_distance_sensor_->get_state() != new_still_target_distance)
this->still_target_distance_sensor_->publish_state(new_still_target_distance);
}
if (this->still_target_energy_sensor_ != nullptr) {
int new_still_target_energy = buffer[STILL_ENERGY];
if (this->still_target_energy_sensor_->get_state() != new_still_target_energy)
this->still_target_energy_sensor_->publish_state(new_still_target_energy);
}
if (this->detection_distance_sensor_ != nullptr) {
int new_detect_distance = this->two_byte_to_int_(buffer[DETECT_DISTANCE_LOW], buffer[DETECT_DISTANCE_HIGH]);
if (this->detection_distance_sensor_->get_state() != new_detect_distance)
this->detection_distance_sensor_->publish_state(new_detect_distance);
}
if (this->light_sensor_ != nullptr) {
int data_type = buffer[6];
int new_light = -1;
if (data_type == 0x01){ // 0x01 = 工程模式!
new_light = buffer[37];
new_light = map(new_light, 85, 255, 0, 100);
if (new_light < 0){
new_light = 0;
}
if (new_light > 100){
new_light = 100;
}
ESP_LOGD(TAG,"LD2410 Sun Light: %d%%", new_light);
}else{
int32_t now_millis = millis();
if (now_millis - last_change_fatory_mode_millis > 2000){
ESP_LOGD(TAG,"Normal mode no light, change to factory mode");
this->factory_mode(true);
last_change_fatory_mode_millis = now_millis;
}
}
if (this->light_sensor_->get_state() != new_light){
this->light_sensor_->publish_state(new_light);
}
}
#endif
}
void LD2410Component::handle_ack_data_(uint8_t *buffer, int len) {
ESP_LOGV(TAG, "Handling ACK DATA for COMMAND");
if (len < 10) {
ESP_LOGE(TAG, "Error with last command : incorrect length");
return;
}
if (buffer[0] != 0xFD || buffer[1] != 0xFC || buffer[2] != 0xFB || buffer[3] != 0xFA) { // check 4 frame start bytes
ESP_LOGE(TAG, "Error with last command : incorrect Header");
return;
}
if (buffer[COMMAND_STATUS] != 0x01) {
ESP_LOGE(TAG, "Error with last command : status != 0x01");
return;
}
if (this->two_byte_to_int_(buffer[8], buffer[9]) != 0x00) {
ESP_LOGE(TAG, "Error with last command , last buffer was: %u , %u", buffer[8], buffer[9]);
return;
}
switch (buffer[COMMAND]) {
case lowbyte(CMD_ENABLE_CONF):
ESP_LOGV(TAG, "Handled Enable conf command");
break;
case lowbyte(CMD_DISABLE_CONF):
ESP_LOGV(TAG, "Handled Disabled conf command");
break;
case lowbyte(CMD_VERSION):
ESP_LOGV(TAG, "FW Version is: %u.%u.%u%u%u%u", buffer[13], buffer[12], buffer[17], buffer[16], buffer[15],
buffer[14]);
this->version_[0] = buffer[13];
this->version_[1] = buffer[12];
this->version_[2] = buffer[17];
this->version_[3] = buffer[16];
this->version_[4] = buffer[15];
this->version_[5] = buffer[14];
break;
case lowbyte(CMD_GATE_SENS):
ESP_LOGV(TAG, "Handled sensitivity command");
break;
case lowbyte(CMD_QUERY): // Query parameters response
{
if (buffer[10] != 0xAA)
return; // value head=0xAA
/*
Moving distance range: 13th byte
Still distance range: 14th byte
*/
// TODO
// maxMovingDistanceRange->publish_state(buffer[12]);
// maxStillDistanceRange->publish_state(buffer[13]);
/*
Moving Sensitivities: 15~23th bytes
Still Sensitivities: 24~32th bytes
*/
for (int i = 0; i < 9; i++) {
moving_sensitivities[i] = buffer[14 + i];
}
for (int i = 0; i < 9; i++) {
still_sensitivities[i] = buffer[23 + i];
}
/*
None Duration: 33~34th bytes
*/
// noneDuration->publish_state(this->two_byte_to_int_(buffer[32], buffer[33]));
} break;
default:
break;
}
}
void LD2410Component::readline_(int readch, uint8_t *buffer, int len) {
static int pos = 0;
if (readch >= 0) {
if (pos < len - 1) {
buffer[pos++] = readch;
buffer[pos] = 0;
} else {
pos = 0;
}
if (pos >= 4) {
if (buffer[pos - 4] == 0xF8 && buffer[pos - 3] == 0xF7 && buffer[pos - 2] == 0xF6 && buffer[pos - 1] == 0xF5) {
ESP_LOGV(TAG, "Will handle Periodic Data");
this->handle_periodic_data_(buffer, pos);
pos = 0; // Reset position index ready for next time
} else if (buffer[pos - 4] == 0x04 && buffer[pos - 3] == 0x03 && buffer[pos - 2] == 0x02 &&
buffer[pos - 1] == 0x01) {
ESP_LOGV(TAG, "Will handle ACK Data");
this->handle_ack_data_(buffer, pos);
pos = 0; // Reset position index ready for next time
}
}
}
}
void LD2410Component::set_config_mode_(bool enable) {
uint8_t cmd = enable ? CMD_ENABLE_CONF : CMD_DISABLE_CONF;
uint8_t cmd_value[2] = {0x01, 0x00};
this->send_command_(cmd, enable ? cmd_value : nullptr, 2);
}
void LD2410Component::query_parameters_() { this->send_command_(CMD_QUERY, nullptr, 0); }
void LD2410Component::get_version_() { this->send_command_(CMD_VERSION, nullptr, 0); }
void LD2410Component::set_max_distances_timeout_(uint8_t max_moving_distance_range, uint8_t max_still_distance_range,
uint16_t timeout) {
uint8_t value[18] = {0x00,
0x00,
lowbyte(max_moving_distance_range),
highbyte(max_moving_distance_range),
0x00,
0x00,
0x01,
0x00,
lowbyte(max_still_distance_range),
highbyte(max_still_distance_range),
0x00,
0x00,
0x02,
0x00,
lowbyte(timeout),
highbyte(timeout),
0x00,
0x00};
this->send_command_(CMD_MAXDIST_DURATION, value, 18);
this->query_parameters_();
}
void LD2410Component::set_gate_threshold_(uint8_t gate, uint8_t motionsens, uint8_t stillsens) {
// reference
// https://drive.google.com/drive/folders/1p4dhbEJA3YubyIjIIC7wwVsSo8x29Fq-?spm=a2g0o.detail.1000023.17.93465697yFwVxH
// Send data: configure the motion sensitivity of distance gate 3 to 40, and the static sensitivity of 40
// 00 00 (gate)
// 03 00 00 00 (gate number)
// 01 00 (motion sensitivity)
// 28 00 00 00 (value)
// 02 00 (still sensitivtiy)
// 28 00 00 00 (value)
uint8_t value[18] = {0x00, 0x00, lowbyte(gate), highbyte(gate), 0x00, 0x00,
0x01, 0x00, lowbyte(motionsens), highbyte(motionsens), 0x00, 0x00,
0x02, 0x00, lowbyte(stillsens), highbyte(stillsens), 0x00, 0x00};
this->send_command_(CMD_GATE_SENS, value, 18);
}
void LD2410Component::factoryReset()
{
this->set_config_mode_(true);
uint8_t cmd = 0x00A2;
this -> send_command_(cmd, nullptr, 0);
}
void LD2410Component::reboot()
{
ESP_LOGD(TAG, "reboot ld2410b...");
this->set_config_mode_(true);
uint8_t cmd = 0x00A3;
this -> send_command_(cmd, nullptr, 0);
// not need to exit config mode because the ld2410 will reboot automatically
}
void LD2410Component::ble_control(bool enable)
{
this->set_config_mode_(true);
uint8_t CMD_BLE_CONF = 0x00A4;
uint8_t CMD_REBOOT = 0x00A3;
uint8_t cmd_value[2] = {0x00, 0x00};
if (enable){
cmd_value[0] = 0x01;
ESP_LOGD(TAG, "Enable BLE...");
}else{
ESP_LOGD(TAG, "Disable BLE...");
}
this -> send_command_(CMD_BLE_CONF, cmd_value, 2);
//this -> send_command_(CMD_REBOOT, nullptr, 0);
this->set_config_mode_(false);
this -> reboot();
}
void LD2410Component::factory_mode(bool enable){
uint8_t CMD_FACTORY_ON = 0x0062;
uint8_t CMD_FACTORY_OFF = 0x0063;
this->set_config_mode_(true);
uint8_t cmd = enable ? CMD_FACTORY_ON: CMD_FACTORY_OFF;
this->send_command_(cmd, nullptr, 0);
this->set_config_mode_(false);
}
} // namespace ld2410
} // namespace esphome

View File

@ -1,158 +0,0 @@
#pragma once
#include "esphome/core/defines.h"
#include "esphome/core/component.h"
#ifdef USE_BINARY_SENSOR
#include "esphome/components/binary_sensor/binary_sensor.h"
#endif
#ifdef USE_SENSOR
#include "esphome/components/sensor/sensor.h"
#endif
#include "esphome/components/uart/uart.h"
#include "esphome/core/automation.h"
#include "esphome/core/helpers.h"
namespace esphome {
namespace ld2410 {
#define CHECK_BIT(var, pos) (((var) >> (pos)) & 1)
// Commands
static const uint8_t CMD_ENABLE_CONF = 0x00FF;
static const uint8_t CMD_DISABLE_CONF = 0x00FE;
static const uint8_t CMD_MAXDIST_DURATION = 0x0060;
static const uint8_t CMD_QUERY = 0x0061;
static const uint8_t CMD_GATE_SENS = 0x0064;
static const uint8_t CMD_VERSION = 0x00A0;
// Commands values
static const uint8_t CMD_MAX_MOVE_VALUE = 0x0000;
static const uint8_t CMD_MAX_STILL_VALUE = 0x0001;
static const uint8_t CMD_DURATION_VALUE = 0x0002;
// Command Header & Footer
static const uint8_t CMD_FRAME_HEADER[4] = {0xFD, 0xFC, 0xFB, 0xFA};
static const uint8_t CMD_FRAME_END[4] = {0x04, 0x03, 0x02, 0x01};
// Data Header & Footer
static const uint8_t DATA_FRAME_HEADER[4] = {0xF4, 0xF3, 0xF2, 0xF1};
static const uint8_t DATA_FRAME_END[4] = {0xF8, 0xF7, 0xF6, 0xF5};
/*
Data Type: 6th byte
Target states: 9th byte
Moving target distance: 10~11th bytes
Moving target energy: 12th byte
Still target distance: 13~14th bytes
Still target energy: 15th byte
Detect distance: 16~17th bytes
*/
enum PeriodicDataStructure : uint8_t {
DATA_TYPES = 5,
TARGET_STATES = 8,
MOVING_TARGET_LOW = 9,
MOVING_TARGET_HIGH = 10,
MOVING_ENERGY = 11,
STILL_TARGET_LOW = 12,
STILL_TARGET_HIGH = 13,
STILL_ENERGY = 14,
DETECT_DISTANCE_LOW = 15,
DETECT_DISTANCE_HIGH = 16,
};
// 上报数据的固定结构。(23年3月13日_16时54分_)
enum PeriodicDataValue : uint8_t { HEAD = 0XAA, END = 0x55, CHECK = 0x00 };
enum AckDataStructure : uint8_t { COMMAND = 6, COMMAND_STATUS = 7 };
// char cmd[2] = {enable ? 0xFF : 0xFE, 0x00};
class LD2410Component : public Component, public uart::UARTDevice {
#ifdef USE_SENSOR
SUB_SENSOR(moving_target_distance)
SUB_SENSOR(still_target_distance)
SUB_SENSOR(moving_target_energy)
SUB_SENSOR(still_target_energy)
SUB_SENSOR(detection_distance)
SUB_SENSOR(light)
#endif
public:
void setup() override;
void dump_config() override;
void loop() override;
#ifdef USE_BINARY_SENSOR
void set_target_sensor(binary_sensor::BinarySensor *sens) { this->target_binary_sensor_ = sens; };
void set_moving_target_sensor(binary_sensor::BinarySensor *sens) { this->moving_binary_sensor_ = sens; };
void set_still_target_sensor(binary_sensor::BinarySensor *sens) { this->still_binary_sensor_ = sens; };
#endif
void set_timeout(uint16_t value) { this->timeout_ = value; };
void set_max_move_distance(uint8_t value) { this->max_move_distance_ = value; };
void set_max_still_distance(uint8_t value) { this->max_still_distance_ = value; };
void set_range_config(int rg0_move, int rg0_still, int rg1_move, int rg1_still, int rg2_move, int rg2_still,
int rg3_move, int rg3_still, int rg4_move, int rg4_still, int rg5_move, int rg5_still,
int rg6_move, int rg6_still, int rg7_move, int rg7_still, int rg8_move, int rg8_still) {
this->rg0_move_threshold_ = rg0_move;
this->rg0_still_threshold_ = rg0_still;
this->rg1_move_threshold_ = rg1_move;
this->rg1_still_threshold_ = rg1_still;
this->rg2_move_threshold_ = rg2_move;
this->rg2_still_threshold_ = rg2_still;
this->rg3_move_threshold_ = rg3_move;
this->rg3_still_threshold_ = rg3_still;
this->rg4_move_threshold_ = rg4_move;
this->rg4_still_threshold_ = rg4_still;
this->rg5_move_threshold_ = rg5_move;
this->rg5_still_threshold_ = rg5_still;
this->rg6_move_threshold_ = rg6_move;
this->rg6_still_threshold_ = rg6_still;
this->rg7_move_threshold_ = rg7_move;
this->rg7_still_threshold_ = rg7_still;
this->rg8_move_threshold_ = rg8_move;
this->rg8_still_threshold_ = rg8_still;
};
int moving_sensitivities[9] = {0};
int still_sensitivities[9] = {0};
int32_t last_periodic_millis = millis();
int32_t last_change_fatory_mode_millis = 0;
void factoryReset();
void reboot();
void ble_control(bool enable);
void factory_mode(bool enable);
protected:
#ifdef USE_BINARY_SENSOR
binary_sensor::BinarySensor *target_binary_sensor_{nullptr};
binary_sensor::BinarySensor *moving_binary_sensor_{nullptr};
binary_sensor::BinarySensor *still_binary_sensor_{nullptr};
#endif
std::vector<uint8_t> rx_buffer_;
int two_byte_to_int_(char firstbyte, char secondbyte) { return (int16_t)(secondbyte << 8) + firstbyte; }
void send_command_(uint8_t command_str, uint8_t *command_value, int command_value_len);
void set_max_distances_timeout_(uint8_t max_moving_distance_range, uint8_t max_still_distance_range,
uint16_t timeout);
void set_gate_threshold_(uint8_t gate, uint8_t motionsens, uint8_t stillsens);
void set_config_mode_(bool enable);
void handle_periodic_data_(uint8_t *buffer, int len);
void handle_ack_data_(uint8_t *buffer, int len);
void readline_(int readch, uint8_t *buffer, int len);
void query_parameters_();
void get_version_();
uint16_t timeout_;
uint8_t max_move_distance_;
uint8_t max_still_distance_;
uint8_t version_[6];
uint8_t rg0_move_threshold_, rg0_still_threshold_, rg1_move_threshold_, rg1_still_threshold_, rg2_move_threshold_,
rg2_still_threshold_, rg3_move_threshold_, rg3_still_threshold_, rg4_move_threshold_, rg4_still_threshold_,
rg5_move_threshold_, rg5_still_threshold_, rg6_move_threshold_, rg6_still_threshold_, rg7_move_threshold_,
rg7_still_threshold_, rg8_move_threshold_, rg8_still_threshold_;
};
} // namespace ld2410
} // namespace esphome

View File

@ -1,70 +0,0 @@
import esphome.codegen as cg
from esphome.components import sensor
import esphome.config_validation as cv
from esphome.const import (
DEVICE_CLASS_DISTANCE,
DEVICE_CLASS_ENERGY,
UNIT_CENTIMETER,
UNIT_PERCENT,
STATE_CLASS_NONE,
ICON_BRIGHTNESS_5,
UNIT_EMPTY,
UNIT_LUX,
)
from . import CONF_LD2410_ID, LD2410Component
DEPENDENCIES = ["ld2410"]
CONF_MOVING_DISTANCE = "moving_distance"
CONF_STILL_DISTANCE = "still_distance"
CONF_MOVING_ENERGY = "moving_energy"
CONF_STILL_ENERGY = "still_energy"
CONF_DETECTION_DISTANCE = "detection_distance"
CONF_LIGHT = "light"
CONFIG_SCHEMA = {
cv.GenerateID(CONF_LD2410_ID): cv.use_id(LD2410Component),
cv.Optional(CONF_MOVING_DISTANCE): sensor.sensor_schema(
device_class=DEVICE_CLASS_DISTANCE, unit_of_measurement=UNIT_CENTIMETER
),
cv.Optional(CONF_STILL_DISTANCE): sensor.sensor_schema(
device_class=DEVICE_CLASS_DISTANCE, unit_of_measurement=UNIT_CENTIMETER
),
cv.Optional(CONF_MOVING_ENERGY): sensor.sensor_schema(
device_class=DEVICE_CLASS_ENERGY, unit_of_measurement=UNIT_PERCENT
),
cv.Optional(CONF_STILL_ENERGY): sensor.sensor_schema(
device_class=DEVICE_CLASS_ENERGY, unit_of_measurement=UNIT_PERCENT
),
cv.Optional(CONF_DETECTION_DISTANCE): sensor.sensor_schema(
device_class=DEVICE_CLASS_DISTANCE, unit_of_measurement=UNIT_CENTIMETER
),
cv.Optional(CONF_LIGHT): sensor.sensor_schema(
device_class=STATE_CLASS_NONE,
icon=ICON_BRIGHTNESS_5,
unit_of_measurement=UNIT_PERCENT
),
}
async def to_code(config):
ld2410_component = await cg.get_variable(config[CONF_LD2410_ID])
if CONF_MOVING_DISTANCE in config:
sens = await sensor.new_sensor(config[CONF_MOVING_DISTANCE])
cg.add(ld2410_component.set_moving_target_distance_sensor(sens))
if CONF_STILL_DISTANCE in config:
sens = await sensor.new_sensor(config[CONF_STILL_DISTANCE])
cg.add(ld2410_component.set_still_target_distance_sensor(sens))
if CONF_MOVING_ENERGY in config:
sens = await sensor.new_sensor(config[CONF_MOVING_ENERGY])
cg.add(ld2410_component.set_moving_target_energy_sensor(sens))
if CONF_STILL_ENERGY in config:
sens = await sensor.new_sensor(config[CONF_STILL_ENERGY])
cg.add(ld2410_component.set_still_target_energy_sensor(sens))
if CONF_DETECTION_DISTANCE in config:
sens = await sensor.new_sensor(config[CONF_DETECTION_DISTANCE])
cg.add(ld2410_component.set_detection_distance_sensor(sens))
if CONF_LIGHT in config:
sens = await sensor.new_sensor(config[CONF_LIGHT])
cg.add(ld2410_component.set_light_sensor(sens))

5
2a/yaml/beta/README.md Normal file
View File

@ -0,0 +1,5 @@
Current version synchronized with public version: V230818_2_beta
More Doc: https://docs.screek.io/2a
Thank you for supporting Screek.

View File

@ -0,0 +1,959 @@
# This is a custom firmware for Human Presence Sensor 2A
# https://screek.io/2a
# Current version synchronized with public version: V230818_2_beta
esphome:
name: screek-humen-sensor-2a
comment: Screek Human Sensor 2A
friendly_name: Human Sensor 2A
name_add_mac_suffix: True
platformio_options:
board_build.flash_mode: dio
# board_build.f_cpu: 80000000L
project:
name: Screek.Human_Presence_Sensor
version: 2A
on_boot:
- priority: 900
then:
lambda: |-
id(cpu_speed) = ESP.getCpuFreqMHz();
- priority: 2000
then:
lambda: |-
id(zone1_target_exsits).publish_state(false);
id(zone2_target_exsits).publish_state(false);
id(zone3_target_exsits).publish_state(false);
id(zone_ex1_target_exsits).publish_state(false);
preferences:
flash_write_interval: 5s
external_components:
- source: screek-components
esp32:
board: lolin_c3_mini
framework:
type: arduino
version: 2.0.9
platform_version: 6.3.0
globals:
- id: cpu_speed
type: int
restore_value: no
initial_value: '0'
- id: last_update_ld2450
type: unsigned long
restore_value: no
initial_value: '0'
- id: init_zone_publish
type: bool
restore_value: no
initial_value: "false"
- id: last_illuminance
type: float
restore_value: no
initial_value: "0"
improv_serial:
logger:
debug:
update_interval: 30s
text_sensor:
- platform: debug
reset_reason:
name: "ESP Reset Reason"
icon: mdi:anchor
disabled_by_default: True
- platform: wifi_info
ip_address:
name: ESP IP Address
entity_category: "diagnostic"
disabled_by_default: True
icon: mdi:ip-network
mac_address:
name: ESP MAC
entity_category: "diagnostic"
icon: mdi:ip-network
disabled_by_default: True
- platform: template
name: "Zone1 Info"
id: tips_zone1_conf
icon: mdi:information-outline
entity_category: config
lambda: |-
return {"Configure below" };
update_interval: 1000s
- platform: template
name: "Zone2 Info"
id: tips_zone2_conf
icon: mdi:information-outline
entity_category: config
lambda: |-
return {"Configure below" };
update_interval: 1000s
- platform: template
name: "Zone3 Info"
id: tips_zone3_conf
icon: mdi:information-outline
entity_category: config
lambda: |-
return {"Configure below" };
update_interval: 1000s
- platform: template
name: "Zout1 Info"
id: tips_zone_ex1_conf
icon: mdi:information-outline
entity_category: config
lambda: |-
return {"Zone Exclusion 1" };
update_interval: 1000s
api:
ota:
# use your own ota password plz. this is a words by Socrates.
password: "all-things-in-their-being-are-good-for-something"
safe_mode: False
wifi:
power_save_mode: LIGHT
reboot_timeout: 10min
ap:
ssid: "HUMAN-SENSOR 2A"
captive_portal:
web_server:
port: 80
number:
- platform: template
name: Zone1 X-Begin
id: zone1_x_begin
min_value: -6000
max_value: 6000
mode: box
device_class: distance
entity_category: config
unit_of_measurement: mm
icon: mdi:arrow-left-bold
step: 10
optimistic: True
initial_value: 0
restore_value: True
- platform: template
name: Zone1 X-End
id: zone1_x_end
mode: box
min_value: -6000
max_value: 6000
device_class: distance
unit_of_measurement: mm
entity_category: config
icon: mdi:arrow-right-bold
step: 10
initial_value: 0
optimistic: True
restore_value: True
- platform: template
name: Zone1 Y-Begin
id: zone1_y_begin
mode: box
min_value: 0
max_value: 6000
device_class: distance
entity_category: config
icon: mdi:arrow-up-bold
unit_of_measurement: mm
step: 10
initial_value: 0
optimistic: True
restore_value: True
- platform: template
name: Zone1 Y-End
id: zone1_y_end
icon: mdi:arrow-down-bold
mode: box
min_value: 0
max_value: 6000
initial_value: 0
entity_category: config
device_class: distance
unit_of_measurement: mm
step: 10
optimistic: True
restore_value: True
- platform: template
name: Zone2 X-Begin
id: zone2_x_begin
min_value: -6000
max_value: 6000
mode: box
device_class: distance
entity_category: config
unit_of_measurement: mm
icon: mdi:arrow-left-bold
step: 10
optimistic: True
initial_value: -6000
restore_value: True
- platform: template
name: Zone2 X-End
id: zone2_x_end
mode: box
min_value: -6000
max_value: 6000
device_class: distance
unit_of_measurement: mm
entity_category: config
icon: mdi:arrow-right-bold
step: 10
initial_value: 0
optimistic: True
restore_value: True
- platform: template
name: Zone2 Y-Begin
id: zone2_y_begin
mode: box
min_value: 0
max_value: 6000
device_class: distance
entity_category: config
icon: mdi:arrow-up-bold
unit_of_measurement: mm
step: 10
initial_value: 0
optimistic: True
restore_value: True
- platform: template
name: Zone2 Y-End
id: zone2_y_end
icon: mdi:arrow-down-bold
mode: box
min_value: 0
max_value: 6000
initial_value: 0
entity_category: config
device_class: distance
unit_of_measurement: mm
step: 10
optimistic: True
restore_value: True
- platform: template
name: Zone3 X-Begin
id: zone3_x_begin
min_value: -6000
max_value: 6000
mode: box
device_class: distance
entity_category: config
unit_of_measurement: mm
icon: mdi:arrow-left-bold
step: 10
optimistic: True
initial_value: 0
restore_value: True
- platform: template
name: Zone3 X-End
id: zone3_x_end
mode: box
min_value: -6000
max_value: 6000
device_class: distance
unit_of_measurement: mm
entity_category: config
icon: mdi:arrow-right-bold
step: 10
initial_value: 0
optimistic: True
restore_value: True
- platform: template
name: Zone3 Y-Begin
id: zone3_y_begin
mode: box
min_value: 0
max_value: 6000
device_class: distance
entity_category: config
icon: mdi:arrow-up-bold
unit_of_measurement: mm
step: 10
initial_value: 0
optimistic: True
restore_value: True
- platform: template
name: Zone3 Y-End
id: zone3_y_end
icon: mdi:arrow-down-bold
mode: box
min_value: 0
max_value: 6000
initial_value: 0
entity_category: config
device_class: distance
unit_of_measurement: mm
step: 10
optimistic: True
restore_value: True
- platform: template
name: Zout1 X-Begin
id: zone_ex1_x_begin
min_value: -6000
max_value: 6000
mode: box
device_class: distance
entity_category: config
unit_of_measurement: mm
icon: mdi:arrow-left-bold
step: 10
optimistic: True
initial_value: 0
restore_value: True
- platform: template
name: Zout1 X-End
id: zone_ex1_x_end
mode: box
min_value: -6000
max_value: 6000
device_class: distance
unit_of_measurement: mm
entity_category: config
icon: mdi:arrow-right-bold
step: 10
initial_value: 0
optimistic: True
restore_value: True
- platform: template
name: Zout1 Y-Begin
id: zone_ex1_y_begin
mode: box
min_value: 0
max_value: 6000
device_class: distance
entity_category: config
icon: mdi:arrow-up-bold
unit_of_measurement: mm
step: 10
initial_value: 0
optimistic: True
restore_value: True
- platform: template
name: Zout1 Y-End
id: zone_ex1_y_end
icon: mdi:arrow-down-bold
mode: box
min_value: 0
max_value: 6000
initial_value: 0
entity_category: config
device_class: distance
unit_of_measurement: mm
step: 10
optimistic: True
restore_value: True
binary_sensor:
- platform: status
name: Online
id: ink_ha_connected
- platform: template
name: "Any Presence"
id: any_target_exsits
device_class: occupancy
- platform: template
name: "Zone1 Presence"
id: zone1_target_exsits
device_class: occupancy
- platform: template
name: "Zone2 Presence"
id: zone2_target_exsits
device_class: occupancy
- platform: template
name: "Zone3 Presence"
id: zone3_target_exsits
device_class: occupancy
- platform: template
name: "Zout1 Presence"
id: zone_ex1_target_exsits
icon: mdi:account-multiple-remove
device_class: occupancy
# - platform: gpio
# pin: 9
# name: "Boot Btn"
# device_class: plug
# on_press:
# then:
# - uart.write:
# id: uart_bus
# data: 'date'
# - logger.log: "按下Boot"
i2c:
sda: GPIO7
scl: GPIO11
scan: true
id: bus_a
sensor:
- platform: template
name: "ESP CPU Speed"
accuracy_decimals: 0
icon: mdi:cpu-32-bit
unit_of_measurement: Mhz
disabled_by_default: True
lambda: |-
return (id(cpu_speed));
entity_category: "diagnostic"
update_interval: 600s
- platform: template
id: sys_esp_temperature
name: ESP Temperature
lambda: return temperatureRead();
# 这里不要引号也可以的,没事。(23年1月10日_15时44分_)
unit_of_measurement: °C
device_class: TEMPERATURE
update_interval: 45s
entity_category: "diagnostic"
- platform: uptime
# 显示名称
name: ESP Uptime
id: sys_uptime
# 更新间隔日期(23年2月26日_17时46分_)
update_interval: 60s
- platform: wifi_signal
name: RSSI
id: wifi_signal_db
update_interval: 60s
entity_category: "diagnostic"
- platform: template
id: esp_memory
icon: mdi:memory
name: ESP Free Memory
lambda: return heap_caps_get_free_size(MALLOC_CAP_INTERNAL) / 1024;
unit_of_measurement: 'kB'
state_class: measurement
entity_category: "diagnostic"
update_interval: 60s
# - platform: template
# name: "Illuminance"
# id: illuminance
# device_class: illuminance
# accuracy_decimals: 1
# state_class: measurement
- platform: bh1750
name: "Illuminance"
accuracy_decimals: 1
id: bh1750_light
# internal: True
update_interval: 1s
# 这个方法不再使用,因为过滤器好用啊!(23年7月30日_11时18分_)
# on_value:
# then:
# lambda: |-
# float curr_value = id(bh1750_light).state;
# float last_light = std::isnan(id(illuminance).state) ? 0 : id(illuminance).state;
# float diff_light = abs(last_light - curr_value);
# // ESP_LOGD("custom", "diff light: %f", diff_light);
# if (abs(last_light - curr_value) > 0.5){
# // ESP_LOGD("custom", "update light");
# id(illuminance).publish_state(curr_value);
# }
# return;
# filters:
# - throttle: 1s
# - delta: 1.0
# # 加入心率1分钟总是上传一次数据CSZ的没有光线的情况可能有助于改善(23年8月3日_22时07分_)
# # - heartbeat: 5min
# 第三版,增加开关(23年8月18日_10时29分_)
filters:
# - throttle: 1s
- lambda: !lambda |-
// 重复值没必要推送。(23年8月18日_11时48分_)
if (id(last_illuminance) == x){
return {};
}
if (id(bh1750_fast_update).state){
// ESP_LOGD("custom", "Fast Update BH1850");
return x;
}
if (abs(id(last_illuminance) - x) > 1){
// 更新上次的值,一次性推送。(23年8月18日_10时33分_)
id(last_illuminance) = x;
return x;
}
return {};
#-------------------------------------#
# 高级雷达数据
- platform: template
name: "All Target Counts"
id: all_target_count
accuracy_decimals: 0
icon: "mdi:counter"
unit_of_measurement: "targets"
- platform: template
name: "Zone1 Target Counts"
id: zone1_target_count
accuracy_decimals: 0
icon: "mdi:counter"
unit_of_measurement: "targets"
- platform: template
name: "Zone2 Target Counts"
id: zone2_target_count
accuracy_decimals: 0
icon: "mdi:counter"
unit_of_measurement: "targets"
- platform: template
name: "Zone3 Target Counts"
id: zone3_target_count
accuracy_decimals: 0
icon: "mdi:counter"
unit_of_measurement: "targets"
- platform: template
name: "Zout1 Target Counts"
id: zone_ex1_target_count
accuracy_decimals: 0
icon: mdi:account-multiple-minus-outline
unit_of_measurement: "targets"
- platform: template
name: "Target1 X"
id: target1_x
accuracy_decimals: 0
unit_of_measurement: 'mm'
state_class: measurement
device_class: distance
- platform: template
name: "Target1 Y"
id: target1_y
accuracy_decimals: 0
unit_of_measurement: 'mm'
state_class: measurement
device_class: distance
- platform: template
name: "Target1 Speed"
id: target1_speed
accuracy_decimals: 2
unit_of_measurement: 'm/s'
state_class: measurement
device_class: speed
- platform: template
name: "Target1 Resolution"
id: target1_resolution
accuracy_decimals: 0
unit_of_measurement: 'mm'
state_class: measurement
device_class: distance
- platform: template
name: "Target2 X"
id: target2_x
accuracy_decimals: 0
unit_of_measurement: 'mm'
state_class: measurement
device_class: distance
# update_interval: 1s
- platform: template
name: "Target2 Y"
id: target2_y
accuracy_decimals: 0
unit_of_measurement: 'mm'
state_class: measurement
device_class: distance
- platform: template
name: "Target2 Speed"
id: target2_speed
accuracy_decimals: 0
unit_of_measurement: 'm/s'
state_class: measurement
device_class: speed
- platform: template
name: "Target2 Resolution"
id: target2_resolution
accuracy_decimals: 0
unit_of_measurement: 'mm'
state_class: measurement
device_class: distance
- platform: template
name: "Target3 X"
id: target3_x
accuracy_decimals: 0
unit_of_measurement: 'mm'
state_class: measurement
device_class: distance
- platform: template
name: "Target3 Y"
id: target3_y
accuracy_decimals: 0
unit_of_measurement: 'mm'
state_class: measurement
device_class: distance
- platform: template
name: "Target3 Speed"
id: target3_speed
accuracy_decimals: 0
unit_of_measurement: 'm/s'
state_class: measurement
device_class: speed
# update_interval: 1s
- platform: template
name: "Target3 Resolution"
id: target3_resolution
accuracy_decimals: 0
unit_of_measurement: 'mm'
state_class: measurement
device_class: distance
light:
- platform: status_led
name: sys_status
pin: GPIO13
internal: True
restore_mode: ALWAYS_OFF
- platform: binary
name: "Red Info Light"
output: board_info_ed
entity_category: "config"
restore_mode: ALWAYS_OFF
time:
- platform: sntp
id: time_now
output:
- platform: gpio
id: board_info_ed
pin: GPIO12
switch:
- platform: factory_reset
name: Factory Reset
disabled_by_default: True
icon: mdi:heart-broken
- platform: template
name: Zout1 Enable
id: zone_ex1_enable
optimistic: True
icon: mdi:account-cancel
entity_category: config
restore_mode: RESTORE_DEFAULT_OFF
- platform: template
name: Illuminance Fast-Update
id: bh1750_fast_update
optimistic: True
entity_category: diagnostic
restore_mode: RESTORE_DEFAULT_OFF
icon: mdi:run-fast
button:
- platform: restart
icon: mdi:power-cycle
name: "ESP Reboot"
entity_category: diagnostic
uart:
id: uart_bus
tx_pin:
number: GPIO5
mode:
input: true
pullup: true
rx_pin:
number: GPIO4
mode:
input: true
pullup: true
baud_rate: 256000
parity: NONE
stop_bits: 1
data_bits: 8
debug:
direction: BOTH
dummy_receiver: True
after:
delimiter: [0X55, 0XCC]
# timeout: 20ms
# delimiter: ""
# delimiter: [0xF8,0xF7,0xF6,0xF5]
sequence:
# - lambda: UARTDebug::log_hex(direction, bytes, ' ');
# 计算第一个坐标目标1的x坐标
- lambda: |-
if ((millis() - id(last_update_ld2450)) <= 500) {
return;
};
id(last_update_ld2450) = millis();
int16_t p1_x = (uint16_t((bytes[5] << 8) | bytes[4] ));
if ((bytes[5] & 0x80) >> 7){
p1_x -= pow(2, 15);
}else{
p1_x = 0 - p1_x;
}
int16_t p1_y = (uint16_t((bytes[7] << 8) | bytes[6] ));
if ((bytes[7] & 0x80) >> 7){
p1_y -= pow(2, 15);
}else{
p1_y = 0 - p1_y;
}
int p1_speed = (bytes[9] << 8 | bytes[8] );
if ((bytes[9] & 0x80) >> 7){
p1_speed -= pow(2, 15);
}else{
p1_speed = 0 - p1_speed;
}
int16_t p1_distance_resolution = (uint16_t((bytes[11] << 8) | bytes[10] ));
// p2信息
int16_t p2_x = (uint16_t((bytes[13] << 8) | bytes[12] ));
if ((bytes[13] & 0x80) >> 7){ // 最高位是1
p2_x -= pow(2, 15);
}else{
p2_x = 0 - p2_x;
}
int16_t p2_y = (uint16_t((bytes[15] << 8) | bytes[14] ));
if ((bytes[15] & 0x80) >> 7){
p2_y -= pow(2, 15);
}else{
p2_y = 0 - p2_y;
}
int p2_speed = (bytes[17] << 8 | bytes[16] );
if ((bytes[17] & 0x80) >> 7){
p2_speed -= pow(2, 15);
}else{
p2_speed = 0 - p2_speed;
}
int16_t p2_distance_resolution = (uint16_t((bytes[19] << 8) | bytes[18] ));
int16_t p3_x = (uint16_t((bytes[21] << 8) | bytes[20] ));
if ((bytes[21] & 0x80) >> 7){
p3_x -= pow(2, 15);
}else{
p3_x = 0 - p3_x;
}
int16_t p3_y = (uint16_t((bytes[23] << 8) | bytes[22] ));
if ((bytes[23] & 0x80) >> 7){
p3_y -= pow(2, 15);
}else{
p3_y = 0 - p3_y;
}
int p3_speed = (bytes[25] << 8 | bytes[24] );
if ((bytes[25] & 0x80) >> 7){
p3_speed -= pow(2, 15);
}else{
p3_speed = 0 - p3_speed;
}
int16_t p3_distance_resolution = (uint16_t((bytes[27] << 8) | bytes[26] ));
bool p1_vaild = (p1_x > 0 || p1_y > 0);
bool p2_vaild = (p2_x > 0 || p2_y > 0);
bool p3_vaild = (p3_x > 0 || p3_y > 0);
int16_t target_count_in_zone_ex1 = 0;
int16_t zone_ex1_x_min = id(zone_ex1_x_begin).state; // - x_ex_tolerance;
int16_t zone_ex1_x_max = id(zone_ex1_x_end).state; // + x_ex_tolerance;
int16_t zone_ex1_y_min = id(zone_ex1_y_begin).state; // - y_ex_tolerance;
int16_t zone_ex1_y_max = id(zone_ex1_y_end).state; // + y_ex_tolerance;
bool p1_zone_ex_enter = false;
bool p2_zone_ex_enter = false;
bool p3_zone_ex_enter = false;
if (id(zone_ex1_enable).state){
if (p1_vaild){
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){
p1_zone_ex_enter = true;
target_count_in_zone_ex1 ++;
}
}
if (p2_vaild){
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){
p2_zone_ex_enter = true;
target_count_in_zone_ex1 ++;
}
}
if (p3_vaild){
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){
p3_zone_ex_enter = true;
target_count_in_zone_ex1 ++;
}
}
}
bool has_target_in_zone_ex1 = (target_count_in_zone_ex1 > 0);
int16_t all_target_counts = 0;
if (p1_vaild && !p1_zone_ex_enter){
all_target_counts ++;
}
if (p2_vaild && !p2_zone_ex_enter){
all_target_counts ++;
}
if (p3_vaild && !p3_zone_ex_enter){
all_target_counts ++;
}
bool has_target_in_zone_all = (all_target_counts > 0);
int16_t target_count_in_zone1 = 0;
int16_t zone1_x_min = id(zone1_x_begin).state; // - x_tolerance;
int16_t zone1_x_max = id(zone1_x_end).state; // + x_tolerance;
int16_t zone1_y_min = id(zone1_y_begin).state; // - y_tolerance;
int16_t zone1_y_max = id(zone1_y_end).state; // + y_tolerance;
if (p1_vaild && !p1_zone_ex_enter){
if (p1_x >= zone1_x_min && p1_x <= zone1_x_max && p1_y >= zone1_y_min && p1_y <= zone1_y_max){
target_count_in_zone1 ++;
}
}
if (p2_vaild && !p2_zone_ex_enter){
if (p2_x >= zone1_x_min && p2_x <= zone1_x_max && p2_y >= zone1_y_min && p2_y <= zone1_y_max){
target_count_in_zone1 ++;
}
}
if (p3_vaild && !p3_zone_ex_enter){
if (p3_x >= zone1_x_min && p3_x <= zone1_x_max && p3_y >= zone1_y_min && p3_y <= zone1_y_max){
target_count_in_zone1 ++;
}
}
bool has_target_in_zone1 = (target_count_in_zone1 > 0);
int16_t target_count_in_zone2 = 0;
int16_t zone2_x_min = id(zone2_x_begin).state; // - x_tolerance;
int16_t zone2_x_max = id(zone2_x_end).state; // + x_tolerance;
int16_t zone2_y_min = id(zone2_y_begin).state; // - y_tolerance;
int16_t zone2_y_max = id(zone2_y_end).state; // + y_tolerance;
if (p1_vaild && !p1_zone_ex_enter){
if (p1_x >= zone2_x_min && p1_x <= zone2_x_max && p1_y >= zone2_y_min && p1_y <= zone2_y_max){
target_count_in_zone2 ++;
}
}
if (p2_vaild && !p2_zone_ex_enter){
if (p2_x >= zone2_x_min && p2_x <= zone2_x_max && p2_y >= zone2_y_min && p2_y <= zone2_y_max){
target_count_in_zone2 ++;
}
}
if (p3_vaild && !p3_zone_ex_enter){
if (p3_x >= zone2_x_min && p3_x <= zone2_x_max && p3_y >= zone2_y_min && p3_y <= zone2_y_max){
target_count_in_zone2 ++;
}
}
bool has_target_in_zone2 = (target_count_in_zone2 > 0);
int16_t target_count_in_zone3 = 0;
int16_t zone3_x_min = id(zone3_x_begin).state; // - x_tolerance;
int16_t zone3_x_max = id(zone3_x_end).state; // + x_tolerance;
int16_t zone3_y_min = id(zone3_y_begin).state; // - y_tolerance;
int16_t zone3_y_max = id(zone3_y_end).state; // + y_tolerance;
if (p1_vaild && !p1_zone_ex_enter){
if (p1_x >= zone3_x_min && p1_x <= zone3_x_max && p1_y >= zone3_y_min && p1_y <= zone3_y_max){
target_count_in_zone3 ++;
}
}
if (p2_vaild && !p2_zone_ex_enter){
if (p2_x >= zone3_x_min && p2_x <= zone3_x_max && p2_y >= zone3_y_min && p2_y <= zone3_y_max){
target_count_in_zone3 ++;
}
}
if (p3_vaild && !p3_zone_ex_enter){
if (p3_x >= zone3_x_min && p3_x <= zone3_x_max && p3_y >= zone3_y_min && p3_y <= zone3_y_max){
target_count_in_zone3 ++;
}
}
bool has_target_in_zone3 = (target_count_in_zone3 > 0);
if (id(target1_x).state != p1_x){
id(target1_x).publish_state(p1_x);
}
if (id(target1_y).state != p1_y){
id(target1_y).publish_state(p1_y);
}
float p1_m_speed = float(p1_speed) / 100.0;
if (id(target1_speed).state != p1_m_speed){
id(target1_speed).publish_state(p1_m_speed);
}
if (id(target1_resolution).state != p1_distance_resolution){
id(target1_resolution).publish_state(p1_distance_resolution);
}
if (id(target2_x).state != p2_x){
id(target2_x).publish_state(p2_x);
}
if (id(target2_y).state != p2_y){
id(target2_y).publish_state(p2_y);
}
if (id(target2_speed).state != p2_speed){
id(target2_speed).publish_state(p2_speed);
}
if (id(target2_resolution).state != p2_distance_resolution){
id(target2_resolution).publish_state(p2_distance_resolution);
}
if (id(target3_x).state != p3_x){
id(target3_x).publish_state(p3_x);
}
if (id(target3_y).state != p3_y){
id(target3_y).publish_state(p3_y);
}
if (id(target3_speed).state != p3_speed){
id(target3_speed).publish_state(p3_speed);
}
if (id(target3_resolution).state != p3_distance_resolution){
id(target3_resolution).publish_state(p3_distance_resolution);
}
if (id(all_target_count).state != all_target_counts){
id(all_target_count).publish_state(all_target_counts);
}
if (id(any_target_exsits).state != has_target_in_zone_all){
id(any_target_exsits).publish_state(has_target_in_zone_all);
}
if (id(zone1_target_count).state != target_count_in_zone1){
id(zone1_target_count).publish_state(target_count_in_zone1);
}
if (id(zone1_target_exsits).state != has_target_in_zone1){
id(zone1_target_exsits).publish_state(has_target_in_zone1);
}
if (id(zone2_target_count).state != target_count_in_zone2){
id(zone2_target_count).publish_state(target_count_in_zone2);
}
if (id(zone2_target_exsits).state != has_target_in_zone2 ){
id(zone2_target_exsits).publish_state(has_target_in_zone2);
}
if (id(zone3_target_count).state != target_count_in_zone3){
id(zone3_target_count).publish_state(target_count_in_zone3);
}
if (id(zone3_target_exsits).state != has_target_in_zone3){
id(zone3_target_exsits).publish_state(has_target_in_zone3);
}
if (id(zone_ex1_target_count).state != target_count_in_zone_ex1){
id(zone_ex1_target_count).publish_state(target_count_in_zone_ex1);
}
if (id(zone_ex1_target_exsits).state != has_target_in_zone_ex1){
id(zone_ex1_target_exsits).publish_state(has_target_in_zone_ex1);
}
id(init_zone_publish) = true;

View File

@ -61,10 +61,7 @@ text_sensor:
entity_category: "diagnostic"
disabled_by_default: True
# Enable Home Assistant API
api:
# encryption:
# key: "b1b1F1ArnSZmIvk7WLL9oG19gjPTCmBP1irQlSlSDGY="
ota:
password: "all-things-in-their-being-are-good-for-something" # words by Socrates