本帖最后由 qqjil 于 2022-8-13 13:07 编辑
前几天写了个CEM5825F的通过串口接入HA,按照一样的思路把5855H的串口也接入了HA,LD1115H有几条串口命令不一样,一样的也是生效的,也能显示运动状态。
https://bbs.hassbian.com/thread-17438-1-1.html CEM5825F的参考链接使用5855H和LD1115H对比了下,发现5855H误报情况比1115H要好,但是5855H有个问题是有时候触发非常不灵敏,就找卖家看能不能改到1115H固件,要了好久卖家给了我2条命令“ind_min_mov=1”和”ind_min_occ=2“,改了下效果立马变好,我也把这2条命令加进去了,有此问题的可以试一下。
yaml配置文件
esphome:
name: cem5855h
includes:
- UartReadLineSensor_55h.h
esp32:
board: esp32dev
# Enable logging
logger:
#level: VERBOSE #makes uart stream available in esphome logstream
#baud_rate: 0 #disable logging over uart
# Enable Home Assistant API
api:
ota:
password: "password"
wifi:
networks:
- ssid: "WIFI"
password: "12345678"
- ssid: "WIFI2"
password: "12345678"
- ssid: "WIFI3"
password: "12345678"
# Enable fallback hotspot (captive portal) in case wifi connection fails
ap:
ssid: "mmWave-Occupancy"
password: "password"
substitutions:
device_name: mmwave-sensor
captive_portal:
web_server:
port: 80
version: 2
include_internal: true
http_request:
useragent: esphome/$device_name
timeout: 2s
# switch:
# - platform: safe_mode
# internal: true
# name: use_safe_mode
binary_sensor:
- platform: gpio
name: mmwave_in_bedroom
pin:
number: GPIO5
mode: INPUT_PULLDOWN
uart:
id: uart_bus
tx_pin: GPIO16
rx_pin: GPIO17
baud_rate: 115200
# debug:
# direction: BOTH
# dummy_receiver: true
# after:
# delimiter: "\n"
# sequence:
# - lambda: UARTDebug::log_string(direction, bytes);
number:
- platform: template
id: th1
name: "th1 移动灵敏度"
mode: box
min_value: 130
max_value: 1000
initial_value: 130
optimistic: true
step: 50
restore_value: true
#unit_of_measurement: m
set_action:
- uart.write: !lambda
int th1 = (int)ceil(x / 1.0);
std::string mss = "th1=" + to_string(th1);
return std::vector<unsigned char>(mss.begin(), mss.end());
- platform: template
id: th2
name: "th2 静止灵敏度"
mode: box
min_value: 200
max_value: 1000
initial_value: 250
optimistic: true
step: 50
restore_value: true
#unit_of_measurement: ms
set_action:
- uart.write: !lambda
int th2 = (int)ceil(x / 1.0);
std::string mss = "th2=" + to_string(th2);
return std::vector<unsigned char>(mss.begin(), mss.end());
- platform: template
id: th_in
name: "th_in 触发灵敏度"
mode: box
min_value: 130
max_value: 1000
initial_value: 360
optimistic: true
step: 50
restore_value: true
#unit_of_measurement: ms
set_action:
- uart.write: !lambda
int th_in = (int)ceil(x / 1.0);
std::string mss = "th_in=" + to_string(th_in);
return std::vector<unsigned char>(mss.begin(), mss.end());
- platform: template
id: tons
name: "GPIO 延时时间"
mode: box
min_value: 1
max_value: 100
initial_value: 30
optimistic: true
step: 1
restore_value: true
unit_of_measurement: "秒"
set_action:
- uart.write: !lambda
int tons = (int)ceil(x / 1.0);
std::string mss = "tons=" + to_string(tons);
return std::vector<unsigned char>(mss.begin(), mss.end());
- platform: template
id: utons
name: "uart 帧间隔"
mode: box
min_value: 50
max_value: 1000
initial_value: 100
optimistic: true
step: 50
restore_value: true
unit_of_measurement: "毫秒"
set_action:
- uart.write: !lambda
int utons = (int)ceil(x / 1.0);
std::string mss = "utons=" + to_string(utons);
return std::vector<unsigned char>(mss.begin(), mss.end());
- platform: template
id: ind_min_mov
name: "ind_min_mov 移动幅度"
mode: box
min_value: 0
max_value: 5
initial_value: 1
optimistic: true
step: 1
restore_value: true
#unit_of_measurement: m
set_action:
- uart.write: !lambda
int ind_min_mov = (int)ceil(x / 1.0);
std::string mss = "ind_min_mov=" + to_string(ind_min_mov);
return std::vector<unsigned char>(mss.begin(), mss.end());
- platform: template
id: ind_min_occ
name: "ind_min_occ 静止幅度"
mode: box
min_value: 0
max_value: 5
initial_value: 2
optimistic: true
step: 1
restore_value: true
#unit_of_measurement: m
set_action:
- uart.write: !lambda
int ind_min_occ = (int)ceil(x / 1.0);
std::string mss = "ind_min_occ=" + to_string(ind_min_occ);
return std::vector<unsigned char>(mss.begin(), mss.end());
button:
- platform: template
id: save
name: "save 保存"
on_press:
- uart.write: "save"
- platform: template
id: get_all
name: "get_all 获取参数"
on_press:
- uart.write: "get_all"
- platform: template
id: initial
name: "initial 恢复出厂设置"
on_press:
- uart.write: "initial"
text_sensor:
- platform: custom
lambda: |-
auto my_custom_sensor = new UartReadLineSensor(id(uart_bus));
App.register_component(my_custom_sensor);
return {my_custom_sensor};
text_sensors:
id: "uart_readline"
name: "mmWave状态"
C++文件UartReadLineSensor_55h.h
#include "esphome.h"
class UartReadLineSensor : public Component, public UARTDevice, public TextSensor{
public:
UartReadLineSensor(UARTComponent *parent) : UARTDevice(parent) {}
void setup() override {
// nothing to do here
}
int readline(int readch, char *buffer, int len)
{
static int pos = 0;
int rpos;
if (readch > 0) {
switch (readch) {
case '\n': // Ignore new-lines
break;
case '\r': // Return on CR
rpos = pos;
pos = 0; // Reset position index ready for next time
return rpos;
default:
if (pos < len-1) {
buffer[pos++] = readch;
buffer[pos] = 0;
}
}
}
// No end of line has been found, so return -1.
return -1;
}
void loop() override {
const int max_line_length = 80;
static char buffer[max_line_length];
static uint8_t mov_flag = 0;
static int uartdata = 0;
static char u16_dis[7] = {0};
static char u16_dis_1[6] = {0};
static char u16_dis_2[5] = {0};
static int i,j;
static float intdis = 0;
static float f32rmax;
static char chartemp[4] = {0};
static char chartemp5[5] = {0};
static char chartemp2[2] = {0};
static char chartemp3[3] = {0};
static uint8_t chartemp1 = 0;
while (available()) {
uartdata = readline(read(), buffer, max_line_length);
if(uartdata > 0) {
//publish_state(buffer);
if(buffer[0] == 0x6d && buffer[1] == 0x6f && buffer[2] == 0x76)
{
if(mov_flag != 0)
{
mov_flag = 0;
publish_state("运动");
}
}
else if(buffer[0] == 0x6f && buffer[1] == 0x63 && buffer[2] == 0x63)
{
if(mov_flag != 1)
{
mov_flag = 1;
publish_state("静止");
}
}
else if(buffer[0] == 0x6e && buffer[1] == 0x75 && buffer[2] == 0x6C)
{
if(mov_flag != 2)
{
mov_flag = 2;
publish_state("无人");
}
}
else if(buffer[0] == 0x74 && buffer[1] == 0x68 && buffer[2] == 0x31 && buffer[3] == 0x20 && buffer[4] == 0x69 && buffer[5] == 0x73 && buffer[6] == 0x20 )
{
j = 0;
if(uartdata == 9)
{
for(i = 7; i < uartdata; i++)
{
chartemp2[j] = buffer[i];
j++;
}
id(th1).publish_state(atoi(chartemp2));
}
if(uartdata == 10)
{
for(i = 7; i < uartdata; i++)
{
chartemp3[j] = buffer[i];
j++;
}
id(th1).publish_state(atoi(chartemp3));
}
if(uartdata == 11)
{
for(i = 7; i < uartdata; i++)
{
chartemp[j] = buffer[i];
j++;
}
id(th1).publish_state(atoi(chartemp));
}
}
else if(buffer[0] == 0x74 && buffer[1] == 0x68 && buffer[2] == 0x32 && buffer[3] == 0x20 && buffer[4] == 0x69 && buffer[5] == 0x73 && buffer[6] == 0x20 )
{
j = 0;
if(uartdata == 9)
{
for(i = 7; i < uartdata; i++)
{
chartemp2[j] = buffer[i];
j++;
}
id(th2).publish_state(atoi(chartemp2));
}
if(uartdata == 10)
{
for(i = 7; i < uartdata; i++)
{
chartemp3[j] = buffer[i];
j++;
}
id(th2).publish_state(atoi(chartemp3));
}
if(uartdata == 11)
{
for(i = 7; i < uartdata; i++)
{
chartemp[j] = buffer[i];
j++;
}
id(th2).publish_state(atoi(chartemp));
}
}
else if(buffer[0] == 0x74 && buffer[1] == 0x6f && buffer[2] == 0x6e && buffer[3] == 0x73 && buffer[4] == 0x20 && buffer[5] == 0x69 && buffer[6] == 0x73 && buffer[7] == 0x20 )
{
j = 0;
if(uartdata == 9)
{
chartemp1 = buffer[8]-48;
id(tons).publish_state(chartemp1);
}
if(uartdata == 10)
{
for(i = 8; i < uartdata; i++)
{
chartemp2[j] = buffer[i];
j++;
}
id(tons).publish_state(atoi(chartemp2));
}
if(uartdata == 11)
{
for(i = 8; i < uartdata; i++)
{
chartemp3[j] = buffer[i];
j++;
}
id(tons).publish_state(atoi(chartemp3));
}
}
else if(buffer[0] == 0x75 && buffer[1] == 0x74 && buffer[2] == 0x6f && buffer[3] == 0x6e && buffer[4] == 0x73 && buffer[5] == 0x20 && buffer[6] == 0x69 && buffer[7] == 0x73 && buffer[8] == 0x20 )
{
j = 0;
if(uartdata == 11)
{
for(i = 9; i < uartdata; i++)
{
chartemp2[j] = buffer[i];
j++;
}
id(utons).publish_state(atoi(chartemp2));
}
if(uartdata == 12)
{
for(i = 9; i < uartdata; i++)
{
chartemp3[j] = buffer[i];
j++;
}
id(utons).publish_state(atoi(chartemp3));
}
if(uartdata == 13)
{
for(i = 9; i < uartdata; i++)
{
chartemp[j] = buffer[i];
j++;
}
id(utons).publish_state(atoi(chartemp));
}
}
else if(buffer[0] == 0x74 && buffer[1] == 0x68 && buffer[2] == 0x5f && buffer[3] == 0x69 && buffer[4] == 0x6e && buffer[5] == 0x20 && buffer[6] == 0x69 && buffer[7] == 0x73 && buffer[8] == 0x20 )
{
j = 0;
if(uartdata == 11)
{
for(i = 9; i < uartdata; i++)
{
chartemp2[j] = buffer[i];
j++;
}
id(th_in).publish_state(atoi(chartemp2));
}
if(uartdata == 12)
{
for(i = 9; i < uartdata; i++)
{
chartemp3[j] = buffer[i];
j++;
}
id(th_in).publish_state(atoi(chartemp3));
}
if(uartdata == 13)
{
for(i = 9; i < uartdata; i++)
{
chartemp[j] = buffer[i];
j++;
}
id(th_in).publish_state(atoi(chartemp));
}
}
memset(chartemp,0,4);
memset(chartemp3,0,3);
memset(chartemp2,0,2);
memset(chartemp5,0,2);
}
}
}
};
|