本帖最后由 qqjil 于 2022-11-23 10:17 编辑
增加了测试模式str数据的上传,yaml文件和C++文件都需要替换,发射误报的时候可以查看数据排除误报情况。
更新编译警告问题,看评论有个警告,我看了下mth1设置发命令实际会失败,仔细看了下代码没发现问题,后面把mth2的复制过去就可以,增加了i2c获取bh1750的代码默认被注释需要取消i2c和bh17502处的注释。
esphome:
name: CEM5825F
includes:
- UartReadLineSensor.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: "WIFI-2"
password: "12345678"
- ssid: "WIFI-3"
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
i2c:
sda: GPIO21
scl: GPIO22
scan: true
id: bus_a
# switch:
# - platform: safe_mode
# internal: true
# name: use_safe_mode
# binary_sensor:
# - platform: gpio
# name: mmwave_in_bedroom
# icon: mdi:motion-sensor
# pin:
# number: GPIO5
# mode: INPUT_PULLDOWN
output:
- platform: ledc
pin: GPIO4
id: gpio_4
# Example usage in a light
light:
- platform: monochromatic
output: gpio_4
name: "led Light"
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: mth1
name: "mth1 3.5米内灵敏度设置"
mode: box
min_value: 40
max_value: 400
initial_value: 60
optimistic: true
step: 5
restore_value: true
#unit_of_measurement: m
set_action:
- uart.write: !lambda
int mth1 = (int)ceil(x / 1.0);
std::string mss = "mth1=" + to_string(mth1) + "\r\n";
return std::vector<unsigned char>(mss.begin(), mss.end());
- platform: template
id: mth2
name: "mth2 3.5~10米灵敏度设置"
mode: box
min_value: 40
max_value: 400
initial_value: 55
optimistic: true
step: 5
restore_value: true
#unit_of_measurement: ms
set_action:
- uart.write: !lambda
int mth2 = (int)ceil(x / 1.0);
std::string mss = "mth2=" + to_string(mth2) + "\r\n";
return std::vector<unsigned char>(mss.begin(), mss.end());
- platform: template
id: mth3
name: "mth3 10米以上灵敏度设置"
mode: box
min_value: 40
max_value: 400
initial_value: 40
optimistic: true
step: 50
restore_value: true
#unit_of_measurement: ms
set_action:
- uart.write: !lambda
int mth3 = (int)ceil(x / 1.0);
std::string mss = "mth3=" + to_string(mth3) + "\r\n";
return std::vector<unsigned char>(mss.begin(), mss.end());
- platform: template
id: rmax
name: "rmax 最大检测距离"
mode: box
min_value: 10
max_value: 1400
initial_value: 600
optimistic: true
step: 1
restore_value: true
unit_of_measurement: "厘米"
set_action:
- uart.write: !lambda
float rmax = (float)(x / 100.0);
std::string mss = "rmax=" + to_string(rmax) + "\r\n";
return std::vector<unsigned char>(mss.begin(), mss.end());
- platform: template
id: tons
name: "切换无人时间"
mode: box
min_value: 1
max_value: 100
initial_value: 15
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) + "\r\n";
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) + "\r\n";
return std::vector<unsigned char>(mss.begin(), mss.end());
button:
- platform: template
id: save
name: "save 保存"
on_press:
- uart.write: "save\r\n"
- platform: template
id: get_all
name: "get_all 获取参数"
on_press:
- uart.write: "get_all\r\n"
- platform: template
id: testmode
name: "testmode 测试模式"
on_press:
- uart.write: "test_mode=1\r\n"
- platform: template
id: normalmode
name: "normalmode 默认模式"
on_press:
- uart.write: "test_mode=0\r\n"
- platform: template
id: initial
name: "initial 恢复出厂设置"
on_press:
- uart.write: "initial\r\n"
sensor:
- platform: custom
lambda: |-
auto my_custom_sensor = new UartReadLineSensor(id(uart_bus));
App.register_component(my_custom_sensor);
return {my_custom_sensor->distance_sensor,my_custom_sensor->movmagsensor,my_custom_sensor->occmagsensor};
sensors:
- name: "dis距离"
id: "uart_readline"
unit_of_measurement: "厘米"
icon: mdi:signal-distance-variant
- name: "mov能量值"
id: "mov"
unit_of_measurement: "E"
- name: "occ能量值"
id: "occ"
unit_of_measurement: "E"
# accuracy_decimals: 1
# - name: "mag能量值"
# id: "mag"
# - platform: hdc1080
# temperature:
# name: "Living Room Temperature"
# humidity:
# name: "Living Room Humidity"
# update_interval: 10s
- platform: bh1750
name: "BH1750 Illuminance"
#address: 0x5C
update_interval: 30s
text_sensor:
- platform: template
name: "mmWave状态"
id: "mmWave"
c++文件,重命名为UartReadLineSensor.h放置到esphome文件目录下面
#include "esphome.h"
class UartReadLineSensor : public Component, public UARTDevice, public Sensor{
public:
UartReadLineSensor(UARTComponent *parent) : UARTDevice(parent) {}
Sensor *distance_sensor = new Sensor();
Sensor *movmagsensor = new Sensor();
Sensor *occmagsensor = new Sensor();
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 * dis = NULL;
static char * distemp = NULL;
static char * mag = NULL;
static char * magtemp = NULL;
static char * movflag = NULL;
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) {
if(buffer[0] == 0x6d && buffer[1] == 0x6f && buffer[2] == 0x76)
{
movflag = strtok(buffer,",");
distemp = strtok(NULL,"=");
dis = strtok(NULL,",");
if(uartdata > 16)
{
magtemp = strtok(NULL,"=");
mag = strtok(NULL,"\r");
movmagsensor->publish_state(atof(mag));
}
distance_sensor->publish_state(atof(dis)*100);
if(mov_flag != 0)
{
mov_flag = 0;
id(mmWave).publish_state("运动");
}
}
else if(buffer[0] == 0x6f && buffer[1] == 0x63 && buffer[2] == 0x63)
{
movflag = strtok(buffer,",");
distemp = strtok(NULL,"=");
dis = strtok(NULL,",");
if(uartdata > 16)
{
magtemp = strtok(NULL,"=");
mag = strtok(NULL,"\r");
occmagsensor->publish_state(atof(mag));
}
distance_sensor->publish_state(atof(dis)*100);
if(mov_flag != 1)
{
mov_flag = 1;
id(mmWave).publish_state("静止");
}
}
else if(buffer[0] == 0x6e && buffer[1] == 0x75 && buffer[2] == 0x6C)
{
if(mov_flag != 2)
{
mov_flag = 2;
id(mmWave).publish_state("无人");
distance_sensor->publish_state(0);
occmagsensor->publish_state(0);
movmagsensor->publish_state(0);
}
}
else if(buffer[0] == 0x72 && buffer[1] == 0x6d && buffer[2] == 0x61 && buffer[3] == 0x78 && buffer[4] == 0x20 && buffer[5] == 0x69 && buffer[6] == 0x73 && buffer[7] == 0x20 )
{
j = 0;
if(uartdata == 12)
{
for(i = 8; i < uartdata; i++)
{
chartemp[j] = buffer[i];
j++;
}
id(rmax).publish_state(atof(chartemp)*100);
}
if(uartdata == 13)
{
for(i = 8; i < uartdata; i++)
{
chartemp5[j] = buffer[i];
j++;
}
id(rmax).publish_state(atof(chartemp5)*100);
}
}
else if(buffer[0] == 0x6d && buffer[1] == 0x74 && buffer[2] == 0x68 && buffer[3] == 0x31 && buffer[4] == 0x20 && buffer[5] == 0x69 && buffer[6] == 0x73 && buffer[7] == 0x20 )
{
j = 0;
if(uartdata == 10)
{
for(i = 8; i < uartdata; i++)
{
chartemp2[j] = buffer[i];
j++;
}
id(mth1).publish_state(atoi(chartemp2));
}
if(uartdata == 11)
{
for(i = 8; i < uartdata; i++)
{
chartemp3[j] = buffer[i];
j++;
}
id(mth1).publish_state(atoi(chartemp3));
}
if(uartdata == 12)
{
for(i = 8; i < uartdata; i++)
{
chartemp[j] = buffer[i];
j++;
}
id(mth1).publish_state(atoi(chartemp));
}
}
else if(buffer[0] == 0x6d && buffer[1] == 0x74 && buffer[2] == 0x68 && buffer[3] == 0x32 && buffer[4] == 0x20 && buffer[5] == 0x69 && buffer[6] == 0x73 && buffer[7] == 0x20 )
{
j = 0;
if(uartdata == 10)
{
for(i = 8; i < uartdata; i++)
{
chartemp2[j] = buffer[i];
j++;
}
id(mth2).publish_state(atoi(chartemp2));
}
if(uartdata == 11)
{
for(i = 8; i < uartdata; i++)
{
chartemp3[j] = buffer[i];
j++;
}
id(mth2).publish_state(atoi(chartemp3));
}
if(uartdata == 12)
{
for(i = 8; i < uartdata; i++)
{
chartemp[j] = buffer[i];
j++;
}
id(mth2).publish_state(atoi(chartemp));
}
}
else if(buffer[0] == 0x6d && buffer[1] == 0x74 && buffer[2] == 0x68 && buffer[3] == 0x33 && buffer[4] == 0x20 && buffer[5] == 0x69 && buffer[6] == 0x73 && buffer[7] == 0x20 )
{
j = 0;
if(uartdata == 10)
{
for(i = 8; i < uartdata; i++)
{
chartemp2[j] = buffer[i];
j++;
}
id(mth3).publish_state(atoi(chartemp2));
}
if(uartdata == 11)
{
for(i = 8; i < uartdata; i++)
{
chartemp3[j] = buffer[i];
j++;
}
id(mth3).publish_state(atoi(chartemp3));
}
if(uartdata == 12)
{
for(i = 8; i < uartdata; i++)
{
chartemp[j] = buffer[i];
j++;
}
id(mth3).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));
}
}
memset(chartemp,0,4);
memset(chartemp3,0,3);
memset(chartemp2,0,2);
memset(chartemp5,0,2);
}
}
}
};
|