From 6f73160d1421bebf2c1bbc9b8017978ff5b16520 Mon Sep 17 00:00:00 2001 From: Florian <1technophile@users.noreply.github.com> Date: Tue, 12 Nov 2019 20:48:23 +0100 Subject: [PATCH] implement minimum rssi filtering (#482) --- docs/use/ble.md | 14 ++ main/ZgatewayBT.ino | 322 ++++++++++++++++++++++++-------------------- main/config_BT.h | 2 + 3 files changed, 190 insertions(+), 148 deletions(-) diff --git a/docs/use/ble.md b/docs/use/ble.md index 261dd8c..cb93741 100644 --- a/docs/use/ble.md +++ b/docs/use/ble.md @@ -72,6 +72,20 @@ you can also force a scan to be done by the following command: Once done the previous value of interval will be recovered. +The default value is set into config_BT.h + +## Setting the minimum RSSI accepted to publish device data + +If you want to change the minimum RSSI value accepted for a device to be published, you can change it by MQTT. For example if you want to set -80 + +`mosquitto_pub -t home/OpenMQTTGateway/commands/MQTTtoBT/set -m '{"minrssi":-80}'` + +you can also accept all the devices by the following command: + +`mosquitto_pub -t home/OpenMQTTGateway/commands/MQTTtoBT/set -m '{"minrssi":0}'` + +The default value is set into config_BT.h + ## Other To check your hm10 firmware version upload a serial sketch to the nodemcu (this will enable communication directly with the hm10) and launch the command: diff --git a/main/ZgatewayBT.ino b/main/ZgatewayBT.ino index 8418871..76fefaf 100644 --- a/main/ZgatewayBT.ino +++ b/main/ZgatewayBT.ino @@ -318,9 +318,6 @@ void MiBandDiscovery(char * mac){ #include #include "soc/timer_group_struct.h" #include "soc/timer_group_reg.h" - - //Time used to wait for an interval before resending BLE infos - unsigned long timeBLE= 0; //core on which the BLE detection task will run static int taskCore = 0; @@ -370,86 +367,94 @@ void MiBandDiscovery(char * mac){ BLEdata.set("servicedata", service_data); #endif BLEdata.set("servicedatauuid", (char *)advertisedDevice.getServiceDataUUID(j).toString().c_str()); - pub((char *)mactopic.c_str(),BLEdata); - if (strstr(BLEdata["servicedatauuid"].as(),"fe95") != NULL){ //Mi FLora, Mi jia, Cleargrass, LYWDS02 - trc("Processing BLE device data"); - int pos = -1; - pos = strpos(service_data,"209800"); - if (pos != -1){ - trc(F("mi flora data reading")); - //example "servicedata":"71209800bc63b6658d7cc40d0910023200" - #ifdef ZmqttDiscovery - if(!isDiscovered(mac)) MiFloraDiscovery(mac); - #endif - process_sensors(pos - 24,service_data,mac); + if(abs((int)BLEdata["rssi"]|0) < abs(Minrssi)) { // publish only the devices close enough + pub((char *)mactopic.c_str(),BLEdata); + if (strstr(BLEdata["servicedatauuid"].as(),"fe95") != NULL){ //Mi FLora, Mi jia, Cleargrass, LYWDS02 + trc("Processing BLE device data"); + int pos = -1; + pos = strpos(service_data,"209800"); + if (pos != -1){ + trc(F("mi flora data reading")); + //example "servicedata":"71209800bc63b6658d7cc40d0910023200" + #ifdef ZmqttDiscovery + if(!isDiscovered(mac)) MiFloraDiscovery(mac); + #endif + process_sensors(pos - 24,service_data,mac); + } + pos = -1; + pos = strpos(service_data,"20aa01"); + if (pos != -1){ + trc(F("mi jia data reading")); + #ifdef ZmqttDiscovery + if(!isDiscovered(mac)) MiJiaDiscovery(mac); + #endif + process_sensors(pos - 26,service_data,mac); + } + pos = -1; + pos = strpos(service_data,"205b04"); + if (pos != -1){ + trc(F("LYWSD02 data reading")); + //example "servicedata":"70205b04b96ab883c8593f09041002e000" + #ifdef ZmqttDiscovery + if(!isDiscovered(mac)) LYWSD02Discovery(mac); + #endif + process_sensors(pos - 24,service_data,mac); + } + pos = -1; + pos = strpos(service_data,"304703"); + if (pos != -1){ + trc(F("ClearGrass T RH data reading")); + //example "servicedata":"5030470340743e10342d58041002d6000a100164" + #ifdef ZmqttDiscovery + if(!isDiscovered(mac)) CLEARGRASSTRHDiscovery(mac); + #endif + process_sensors(pos - 26,service_data,mac); + } + pos = -1; + pos = strpos(service_data,"4030dd"); + if (pos != -1){ + trc(F("Mi Lamp data reading")); + //example "servicedata":4030DD031D0300010100 + #ifdef ZmqttDiscovery + if(!isDiscovered(mac)) MiLampDiscovery(mac); + #endif + process_milamp(service_data,mac); + } } - pos = -1; - pos = strpos(service_data,"20aa01"); - if (pos != -1){ - trc(F("mi jia data reading")); - #ifdef ZmqttDiscovery - if(!isDiscovered(mac)) MiJiaDiscovery(mac); - #endif - process_sensors(pos - 26,service_data,mac); + if (strstr(BLEdata["servicedatauuid"].as(),"181d") != NULL){ // Mi Scale V1 + trc(F("Mi Scale V1 data reading")); + //example "servicedata":"a2ac2be307060207122b" /"a28039e3070602070e28" + #ifdef ZmqttDiscovery + if(!isDiscovered(mac)) MiScaleDiscovery(mac); + #endif + process_scale_v1(service_data,mac); } - pos = -1; - pos = strpos(service_data,"205b04"); - if (pos != -1){ - trc(F("LYWSD02 data reading")); - //example "servicedata":"70205b04b96ab883c8593f09041002e000" - #ifdef ZmqttDiscovery - if(!isDiscovered(mac)) LYWSD02Discovery(mac); - #endif - process_sensors(pos - 24,service_data,mac); + if (strstr(BLEdata["servicedatauuid"].as(),"181b") != NULL){ // Mi Scale V2 + trc(F("Mi Scale V2 data reading")); + //example "servicedata":02c4e1070b1e13050c00002607 / 02a6e20705150a251df401443e /02a6e20705180c0d04d701943e + #ifdef ZmqttDiscovery + if(!isDiscovered(mac)) MiScaleDiscovery(mac); + #endif + process_scale_v2(service_data,mac); } - pos = -1; - pos = strpos(service_data,"304703"); - if (pos != -1){ - trc(F("ClearGrass T RH data reading")); - //example "servicedata":"5030470340743e10342d58041002d6000a100164" - #ifdef ZmqttDiscovery - if(!isDiscovered(mac)) CLEARGRASSTRHDiscovery(mac); - #endif - process_sensors(pos - 26,service_data,mac); + if (strstr(BLEdata["servicedatauuid"].as(),"fee0") != NULL){ // Mi Band //0000fee0-0000-1000-8000-00805f9b34fb // ESP32 only + trc(F("Mi Band data reading")); + //example "servicedata":a21e0000 + #ifdef ZmqttDiscovery + if(!isDiscovered(mac)) MiBandDiscovery(mac); + #endif + process_miband(service_data,mac); } - pos = -1; - pos = strpos(service_data,"4030dd"); - if (pos != -1){ - trc(F("Mi Lamp data reading")); - //example "servicedata":4030DD031D0300010100 - #ifdef ZmqttDiscovery - if(!isDiscovered(mac)) MiLampDiscovery(mac); - #endif - process_milamp(service_data,mac); - } - } - if (strstr(BLEdata["servicedatauuid"].as(),"181d") != NULL){ // Mi Scale V1 - trc(F("Mi Scale V1 data reading")); - //example "servicedata":"a2ac2be307060207122b" /"a28039e3070602070e28" - #ifdef ZmqttDiscovery - if(!isDiscovered(mac)) MiScaleDiscovery(mac); - #endif - process_scale_v1(service_data,mac); - } - if (strstr(BLEdata["servicedatauuid"].as(),"181b") != NULL){ // Mi Scale V2 - trc(F("Mi Scale V2 data reading")); - //example "servicedata":02c4e1070b1e13050c00002607 / 02a6e20705150a251df401443e /02a6e20705180c0d04d701943e - #ifdef ZmqttDiscovery - if(!isDiscovered(mac)) MiScaleDiscovery(mac); - #endif - process_scale_v2(service_data,mac); - } - if (strstr(BLEdata["servicedatauuid"].as(),"fee0") != NULL){ // Mi Band //0000fee0-0000-1000-8000-00805f9b34fb // ESP32 only - trc(F("Mi Band data reading")); - //example "servicedata":a21e0000 - #ifdef ZmqttDiscovery - if(!isDiscovered(mac)) MiBandDiscovery(mac); - #endif - process_miband(service_data,mac); + }else{ + trc("Low rssi, device filtered"); } } }else{ - pub((char *)mactopic.c_str(),BLEdata); // publish device even if there is no service data + if(abs((int)BLEdata["rssi"]|0) < abs(Minrssi)) { // publish only the devices close enough + pub((char *)mactopic.c_str(),BLEdata); // publish device even if there is no service data + }else{ + trc("Low rssi, device filtered"); + } } }else{ trc(F("Filtered mac device")); @@ -486,19 +491,22 @@ void MiBandDiscovery(char * mac){ } void setupBT(){ - BLEinterval = TimeBtw_Read; - trc(F("BLEinterval btw scans")); - trc(BLEinterval); - // we setup a task with priority one to avoid conflict with other gateways - xTaskCreatePinnedToCore( - coreTask, /* Function to implement the task */ - "coreTask", /* Name of the task */ - 10000, /* Stack size in words */ - NULL, /* Task input parameter */ - 1, /* Priority of the task */ - NULL, /* Task handle. */ - taskCore); /* Core where the task should run */ - trc(F("ZgatewayBT multicore ESP32 setup done ")); + BLEinterval = TimeBtw_Read; + Minrssi = MinimumRSSI; + trc(F("BLEinterval btw scans")); + trc(BLEinterval); + trc(F("Minrssi")); + trc(Minrssi); + // we setup a task with priority one to avoid conflict with other gateways + xTaskCreatePinnedToCore( + coreTask, /* Function to implement the task */ + "coreTask", /* Name of the task */ + 10000, /* Stack size in words */ + NULL, /* Task input parameter */ + 1, /* Priority of the task */ + NULL, /* Task handle. */ + taskCore); /* Core where the task should run */ + trc(F("ZgatewayBT multicore ESP32 setup done ")); } bool BTtoMQTT(){ // for on demand BLE scans @@ -524,8 +532,11 @@ void MiBandDiscovery(char * mac){ void setupBT() { BLEinterval = TimeBtw_Read; + Minrssi = MinimumRSSI; trc(F("BLEinterval btw scans")); trc(BLEinterval); + trc(F("Minrssi")); + trc(Minrssi); softserial.begin(9600); softserial.print(F("AT+ROLE1")); delay(100); @@ -603,66 +614,70 @@ void MiBandDiscovery(char * mac){ #ifdef pubBLEServiceData BLEdata.set("servicedata", (char *)service_data.c_str()); #endif - pub((char *)topic.c_str(),BLEdata); - int pos = -1; - pos = strpos(d[5].extract,"209800"); - if (pos != -1) { - trc("mi flora data reading"); - #ifdef ZmqttDiscovery - if(!isDiscovered(d[0].extract)) MiFloraDiscovery(d[0].extract); - #endif - bool result = process_sensors(pos - 38,(char *)service_data.c_str(),d[0].extract); + if(abs((int)BLEdata["rssi"]|0) < abs(Minrssi)) { // publish only the devices close enough + pub((char *)topic.c_str(),BLEdata); + int pos = -1; + pos = strpos(d[5].extract,"209800"); + if (pos != -1) { + trc("mi flora data reading"); + #ifdef ZmqttDiscovery + if(!isDiscovered(d[0].extract)) MiFloraDiscovery(d[0].extract); + #endif + bool result = process_sensors(pos - 38,(char *)service_data.c_str(),d[0].extract); + } + pos = -1; + pos = strpos(d[5].extract,"20aa01"); + //example "servicedata":"5020aa0194dfaa33342d580d1004e3002c02" + if (pos != -1){ + trc("mi jia data reading"); + #ifdef ZmqttDiscovery + if(!isDiscovered(d[0].extract)) MiJiaDiscovery(d[0].extract); + #endif + bool result = process_sensors(pos - 40,(char *)service_data.c_str(),d[0].extract); + } + pos = -1; + pos = strpos(d[5].extract,"205b04"); + //example "servicedata":"141695fe70205b0461298882c8593f09061002d002" + if (pos != -1){ + trc("LYWSD02 data reading"); + #ifdef ZmqttDiscovery + if(!isDiscovered(d[0].extract)) LYWSD02Discovery(d[0].extract); + #endif + bool result = process_sensors(pos - 38,(char *)service_data.c_str(),d[0].extract); + } + pos = -1; + pos = strpos(d[5].extract,"304703"); + if (pos != -1){ + trc("CLEARGRASSTRH data reading"); + #ifdef ZmqttDiscovery + if(!isDiscovered(d[0].extract)) CLEARGRASSTRHDiscovery(d[0].extract); + #endif + bool result = process_sensors(pos - 40,(char *)service_data.c_str(),d[0].extract); + } + pos = -1; + pos = strpos(d[5].extract,"e30706"); + if (pos != -1){ + trc("Mi Scale data reading"); + //example "servicedata":"a2ac2be307060207122b" /"a28039e3070602070e28" + #ifdef ZmqttDiscovery + if(!isDiscovered(d[0].extract)) MiScaleDiscovery(d[0].extract); + #endif + bool result = process_scale_v1((char *)service_data.c_str(),d[0].extract); + } + pos = -1; + pos = strpos(d[5].extract,"4030dd"); + if (pos != -1){ + trc(F("Mi Lamp data reading")); + //example "servicedata":4030dd31d0300010100 + #ifdef ZmqttDiscovery + if(!isDiscovered(d[0].extract)) MiLampDiscovery(d[0].extract); + #endif + process_milamp((char *)service_data.c_str(),d[0].extract); + } + return true; + }else{ + trc("Low rssi, device filtered"); } - pos = -1; - pos = strpos(d[5].extract,"20aa01"); - //example "servicedata":"5020aa0194dfaa33342d580d1004e3002c02" - if (pos != -1){ - trc("mi jia data reading"); - #ifdef ZmqttDiscovery - if(!isDiscovered(d[0].extract)) MiJiaDiscovery(d[0].extract); - #endif - bool result = process_sensors(pos - 40,(char *)service_data.c_str(),d[0].extract); - } - pos = -1; - pos = strpos(d[5].extract,"205b04"); - //example "servicedata":"141695fe70205b0461298882c8593f09061002d002" - if (pos != -1){ - trc("LYWSD02 data reading"); - #ifdef ZmqttDiscovery - if(!isDiscovered(d[0].extract)) LYWSD02Discovery(d[0].extract); - #endif - bool result = process_sensors(pos - 38,(char *)service_data.c_str(),d[0].extract); - } - pos = -1; - pos = strpos(d[5].extract,"304703"); - if (pos != -1){ - trc("CLEARGRASSTRH data reading"); - #ifdef ZmqttDiscovery - if(!isDiscovered(d[0].extract)) CLEARGRASSTRHDiscovery(d[0].extract); - #endif - bool result = process_sensors(pos - 40,(char *)service_data.c_str(),d[0].extract); - } - pos = -1; - pos = strpos(d[5].extract,"e30706"); - if (pos != -1){ - trc("Mi Scale data reading"); - //example "servicedata":"a2ac2be307060207122b" /"a28039e3070602070e28" - #ifdef ZmqttDiscovery - if(!isDiscovered(d[0].extract)) MiScaleDiscovery(d[0].extract); - #endif - bool result = process_scale_v1((char *)service_data.c_str(),d[0].extract); - } - pos = -1; - pos = strpos(d[5].extract,"4030dd"); - if (pos != -1){ - trc(F("Mi Lamp data reading")); - //example "servicedata":4030dd31d0300010100 - #ifdef ZmqttDiscovery - if(!isDiscovered(d[0].extract)) MiLampDiscovery(d[0].extract); - #endif - process_milamp((char *)service_data.c_str(),d[0].extract); - } - return true; } } } @@ -908,6 +923,17 @@ void MQTTtoBT(char * topicOri, JsonObject& BTdata) { // json object decoding BLEinterval = prevBLEinterval; // as 0 was just used as a command we recover previous scan duration } } + // MinRSSI set + if (BTdata.containsKey("minrssi")){ + trc(F("Min RSSI")); + // storing Min RSSI for further use if needed + trc("previous Minrssi"); + trc(Minrssi); + // set Min RSSI if present if not setting default value + Minrssi = (unsigned int)BTdata["minrssi"]; + trc("new Minrssi"); + trc(Minrssi); + } } } diff --git a/main/config_BT.h b/main/config_BT.h index 1828bb2..7f60c96 100644 --- a/main/config_BT.h +++ b/main/config_BT.h @@ -33,6 +33,7 @@ extern void MQTTtoBT(char * topicOri, JsonObject& RFdata); #define subjectBTtoMQTT Base_Topic Gateway_Name "/BTtoMQTT/" #define subjectMQTTtoBTset Base_Topic Gateway_Name "/commands/MQTTtoBT/set" #define TimeBtw_Read 55555 //define default time between 2 scans +#define MinimumRSSI -90 //default minimum rssi value, all the devices below -90 will not be reported #define Scan_duration 10 //define the time for a scan #define HM-10 //#define HM-11 // uncomment this line if you use HM-11 and comment the line above @@ -45,6 +46,7 @@ extern void MQTTtoBT(char * topicOri, JsonObject& RFdata); #define subjectHomePresence Base_Topic "home_presence/" Gateway_Name // will send Home Assistant room presence message to this topic (first part is same for all rooms, second is room name) unsigned int BLEinterval ; //time between 2 scans +int Minrssi ; //minimum rssi value struct BLEdevice{ char macAdr[13];