implement minimum rssi filtering (#482)

This commit is contained in:
Florian authored and GitHub committed 2019-11-12 20:48:23 +01:00
1 parent 34d31600ee
commit 6f73160d14
3 files changed
+190 -148

No files matched your search

+14
View File
@@ -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. 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 ## 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: 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:
+174 -148
View File
@@ -319,9 +319,6 @@ void MiBandDiscovery(char * mac){
#include "soc/timer_group_struct.h" #include "soc/timer_group_struct.h"
#include "soc/timer_group_reg.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 //core on which the BLE detection task will run
static int taskCore = 0; static int taskCore = 0;
@@ -370,86 +367,94 @@ void MiBandDiscovery(char * mac){
BLEdata.set("servicedata", service_data); BLEdata.set("servicedata", service_data);
#endif #endif
BLEdata.set("servicedatauuid", (char *)advertisedDevice.getServiceDataUUID(j).toString().c_str()); BLEdata.set("servicedatauuid", (char *)advertisedDevice.getServiceDataUUID(j).toString().c_str());
pub((char *)mactopic.c_str(),BLEdata); if(abs((int)BLEdata["rssi"]|0) < abs(Minrssi)) { // publish only the devices close enough
if (strstr(BLEdata["servicedatauuid"].as<char*>(),"fe95") != NULL){ //Mi FLora, Mi jia, Cleargrass, LYWDS02 pub((char *)mactopic.c_str(),BLEdata);
trc("Processing BLE device data"); if (strstr(BLEdata["servicedatauuid"].as<char*>(),"fe95") != NULL){ //Mi FLora, Mi jia, Cleargrass, LYWDS02
int pos = -1; trc("Processing BLE device data");
pos = strpos(service_data,"209800"); int pos = -1;
if (pos != -1){ pos = strpos(service_data,"209800");
trc(F("mi flora data reading")); if (pos != -1){
//example "servicedata":"71209800bc63b6658d7cc40d0910023200" trc(F("mi flora data reading"));
#ifdef ZmqttDiscovery //example "servicedata":"71209800bc63b6658d7cc40d0910023200"
if(!isDiscovered(mac)) MiFloraDiscovery(mac); #ifdef ZmqttDiscovery
#endif if(!isDiscovered(mac)) MiFloraDiscovery(mac);
process_sensors(pos - 24,service_data,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; if (strstr(BLEdata["servicedatauuid"].as<char*>(),"181d") != NULL){ // Mi Scale V1
pos = strpos(service_data,"20aa01"); trc(F("Mi Scale V1 data reading"));
if (pos != -1){ //example "servicedata":"a2ac2be307060207122b" /"a28039e3070602070e28"
trc(F("mi jia data reading")); #ifdef ZmqttDiscovery
#ifdef ZmqttDiscovery if(!isDiscovered(mac)) MiScaleDiscovery(mac);
if(!isDiscovered(mac)) MiJiaDiscovery(mac); #endif
#endif process_scale_v1(service_data,mac);
process_sensors(pos - 26,service_data,mac);
} }
pos = -1; if (strstr(BLEdata["servicedatauuid"].as<char*>(),"181b") != NULL){ // Mi Scale V2
pos = strpos(service_data,"205b04"); trc(F("Mi Scale V2 data reading"));
if (pos != -1){ //example "servicedata":02c4e1070b1e13050c00002607 / 02a6e20705150a251df401443e /02a6e20705180c0d04d701943e
trc(F("LYWSD02 data reading")); #ifdef ZmqttDiscovery
//example "servicedata":"70205b04b96ab883c8593f09041002e000" if(!isDiscovered(mac)) MiScaleDiscovery(mac);
#ifdef ZmqttDiscovery #endif
if(!isDiscovered(mac)) LYWSD02Discovery(mac); process_scale_v2(service_data,mac);
#endif
process_sensors(pos - 24,service_data,mac);
} }
pos = -1; if (strstr(BLEdata["servicedatauuid"].as<char*>(),"fee0") != NULL){ // Mi Band //0000fee0-0000-1000-8000-00805f9b34fb // ESP32 only
pos = strpos(service_data,"304703"); trc(F("Mi Band data reading"));
if (pos != -1){ //example "servicedata":a21e0000
trc(F("ClearGrass T RH data reading")); #ifdef ZmqttDiscovery
//example "servicedata":"5030470340743e10342d58041002d6000a100164" if(!isDiscovered(mac)) MiBandDiscovery(mac);
#ifdef ZmqttDiscovery #endif
if(!isDiscovered(mac)) CLEARGRASSTRHDiscovery(mac); process_miband(service_data,mac);
#endif
process_sensors(pos - 26,service_data,mac);
} }
pos = -1; }else{
pos = strpos(service_data,"4030dd"); trc("Low rssi, device filtered");
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<char*>(),"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<char*>(),"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<char*>(),"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{ }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{ }else{
trc(F("Filtered mac device")); trc(F("Filtered mac device"));
@@ -486,19 +491,22 @@ void MiBandDiscovery(char * mac){
} }
void setupBT(){ void setupBT(){
BLEinterval = TimeBtw_Read; BLEinterval = TimeBtw_Read;
trc(F("BLEinterval btw scans")); Minrssi = MinimumRSSI;
trc(BLEinterval); trc(F("BLEinterval btw scans"));
// we setup a task with priority one to avoid conflict with other gateways trc(BLEinterval);
xTaskCreatePinnedToCore( trc(F("Minrssi"));
coreTask, /* Function to implement the task */ trc(Minrssi);
"coreTask", /* Name of the task */ // we setup a task with priority one to avoid conflict with other gateways
10000, /* Stack size in words */ xTaskCreatePinnedToCore(
NULL, /* Task input parameter */ coreTask, /* Function to implement the task */
1, /* Priority of the task */ "coreTask", /* Name of the task */
NULL, /* Task handle. */ 10000, /* Stack size in words */
taskCore); /* Core where the task should run */ NULL, /* Task input parameter */
trc(F("ZgatewayBT multicore ESP32 setup done ")); 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 bool BTtoMQTT(){ // for on demand BLE scans
@@ -524,8 +532,11 @@ void MiBandDiscovery(char * mac){
void setupBT() { void setupBT() {
BLEinterval = TimeBtw_Read; BLEinterval = TimeBtw_Read;
Minrssi = MinimumRSSI;
trc(F("BLEinterval btw scans")); trc(F("BLEinterval btw scans"));
trc(BLEinterval); trc(BLEinterval);
trc(F("Minrssi"));
trc(Minrssi);
softserial.begin(9600); softserial.begin(9600);
softserial.print(F("AT+ROLE1")); softserial.print(F("AT+ROLE1"));
delay(100); delay(100);
@@ -603,66 +614,70 @@ void MiBandDiscovery(char * mac){
#ifdef pubBLEServiceData #ifdef pubBLEServiceData
BLEdata.set("servicedata", (char *)service_data.c_str()); BLEdata.set("servicedata", (char *)service_data.c_str());
#endif #endif
pub((char *)topic.c_str(),BLEdata); if(abs((int)BLEdata["rssi"]|0) < abs(Minrssi)) { // publish only the devices close enough
int pos = -1; pub((char *)topic.c_str(),BLEdata);
pos = strpos(d[5].extract,"209800"); int pos = -1;
if (pos != -1) { pos = strpos(d[5].extract,"209800");
trc("mi flora data reading"); if (pos != -1) {
#ifdef ZmqttDiscovery trc("mi flora data reading");
if(!isDiscovered(d[0].extract)) MiFloraDiscovery(d[0].extract); #ifdef ZmqttDiscovery
#endif if(!isDiscovered(d[0].extract)) MiFloraDiscovery(d[0].extract);
bool result = process_sensors(pos - 38,(char *)service_data.c_str(),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 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);
}
} }
} }
+2
View File
@@ -33,6 +33,7 @@ extern void MQTTtoBT(char * topicOri, JsonObject& RFdata);
#define subjectBTtoMQTT Base_Topic Gateway_Name "/BTtoMQTT/" #define subjectBTtoMQTT Base_Topic Gateway_Name "/BTtoMQTT/"
#define subjectMQTTtoBTset Base_Topic Gateway_Name "/commands/MQTTtoBT/set" #define subjectMQTTtoBTset Base_Topic Gateway_Name "/commands/MQTTtoBT/set"
#define TimeBtw_Read 55555 //define default time between 2 scans #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 Scan_duration 10 //define the time for a scan
#define HM-10 #define HM-10
//#define HM-11 // uncomment this line if you use HM-11 and comment the line above //#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) #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 unsigned int BLEinterval ; //time between 2 scans
int Minrssi ; //minimum rssi value
struct BLEdevice{ struct BLEdevice{
char macAdr[13]; char macAdr[13];