Add publish only BLE sensors function (#741)

don't publish the non identified BLE devices when requested (at build or by MQTT setting)
This commit is contained in:
Florian authored and GitHub committed 2020-08-26 17:24:23 -05:00
1 parent 8e0c1e5e51
commit 5e2d86e963
3 files changed
+35 -10

No files matched your search

+22 -9
View File
@@ -434,8 +434,9 @@ class MyAdvertisedDeviceCallbacks : public BLEAdvertisedDeviceCallbacks {
if (advertisedDevice->haveTXPower())
BLEdata.set("txpower", (int8_t)advertisedDevice->getTXPower());
# ifdef subjectHomePresence
if (advertisedDevice->haveRSSI())
haRoomPresence(BLEdata); // this device has an rssi in consequence we can use it for home assistant room presence component
if (advertisedDevice->haveRSSI() && !publishOnlySensors) {
haRoomPresence(BLEdata); // this device has an rssi and we don't want only sensors so in consequence we can use it for home assistant room presence component
}
# endif
if (advertisedDevice->haveServiceData()) {
int serviceDataCount = advertisedDevice->getServiceDataCount();
@@ -677,6 +678,7 @@ void changelow_power_mode(int newLowPowerMode) {
void setupBT() {
Log.notice(F("BLE scans interval: %d" CR), BLEinterval);
Log.notice(F("BLE scans number before connect: %d" CR), BLEscanBeforeConnect);
Log.notice(F("Publishing only BLE sensors: %T" CR), publishOnlySensors);
Log.notice(F("minrssi: %d" CR), minRssi);
Log.notice(F("Low Power Mode: %d" CR), low_power_mode);
@@ -712,6 +714,7 @@ struct decompose d[6] = {{0, 12, true}, {12, 2, false}, {14, 2, false}, {16, 2,
void setupBT() {
Log.notice(F("BLE interval: %d" CR), BLEinterval);
Log.notice(F("BLE scans number before connect: %d" CR), BLEscanBeforeConnect);
Log.notice(F("Publishing only BLE sensors: %T" CR), publishOnlySensors);
Log.notice(F("minrssi: %d" CR), minRssi);
softserial.begin(HMSerialSpeed);
softserial.print(F("AT+ROLE1" CR));
@@ -802,7 +805,8 @@ bool BTtoMQTT() {
BLEdata.set("rssi", (int)rssi);
# ifdef subjectHomePresence
haRoomPresence(BLEdata); // this device has an rssi in consequence we can use it for home assistant room presence component
if (!publishOnlySensors)
haRoomPresence(BLEdata); // this device has an rssi and we don't want only sensors so in consequence we can use it for home assistant room presence component
# endif
Log.trace(F("Service data: %s" CR), restData.c_str());
BLEdata.set("servicedata", restData.c_str());
@@ -876,16 +880,18 @@ void launchDiscovery() {
void PublishDeviceData(JsonObject& BLEdata) {
if (abs((int)BLEdata["rssi"] | 0) < minRssi) { // process only the devices close enough
JsonObject& BLEdataOut = process_bledata(BLEdata);
if (!publishOnlySensors || BLEdataOut.containsKey("model")) {
# if !pubBLEServiceUUID
RemoveJsonPropertyIf(BLEdataOut, "servicedatauuid", BLEdataOut.containsKey("servicedatauuid"));
RemoveJsonPropertyIf(BLEdataOut, "servicedatauuid", BLEdataOut.containsKey("servicedatauuid"));
# endif
# if !pubKnownBLEServiceData
RemoveJsonPropertyIf(BLEdataOut, "servicedata", BLEdataOut.containsKey("model") && BLEdataOut.containsKey("servicedata"));
RemoveJsonPropertyIf(BLEdataOut, "servicedata", BLEdataOut.containsKey("model") && BLEdataOut.containsKey("servicedata"));
# endif
String mactopic = BLEdataOut["id"].as<const char*>();
mactopic.replace(":", "");
mactopic = subjectBTtoMQTT + String("/") + mactopic;
pub((char*)mactopic.c_str(), BLEdataOut);
String mactopic = BLEdataOut["id"].as<const char*>();
mactopic.replace(":", "");
mactopic = subjectBTtoMQTT + String("/") + mactopic;
pub((char*)mactopic.c_str(), BLEdataOut);
}
} else {
Log.trace(F("Low rssi, device filtered" CR));
}
@@ -1265,6 +1271,13 @@ void MQTTtoBT(char* topicOri, JsonObject& BTdata) { // json object decoding
BLEscanBeforeConnect = (unsigned int)BTdata["scanbcnct"];
Log.notice(F("New scan number before connect: %d" CR), BLEscanBeforeConnect);
}
// publish all BLE devices discovered or only the identified sensors (like temperature sensors)
if (BTdata.containsKey("onlysensors")) {
Log.trace(F("Do we publish only sensors" CR));
Log.trace(F("Previous value: %T" CR), publishOnlySensors);
publishOnlySensors = (bool)BTdata["onlysensors"];
Log.notice(F("New value onlysensors: %T" CR), publishOnlySensors);
}
// MinRSSI set
if (BTdata.containsKey("minrssi")) {
// storing Min RSSI for further use if needed
+4
View File
@@ -44,6 +44,9 @@ extern void MQTTtoBT(char* topicOri, JsonObject& RFdata);
#ifndef TimeBtwRead
# define TimeBtwRead 55555 //define default time between 2 scans
#endif
#ifndef PublishOnlySensors
# define PublishOnlySensors false //true if we publish all BLE devices discovered or false only the identified sensors (like temperature sensors)
#endif
#define HMSerialSpeed 9600 // Communication speed with the HM module, softwareserial doesn't support 115200
//#define HM_BLUE_LED_STOP true //uncomment to stop the blue led light of HM1X
@@ -58,6 +61,7 @@ extern void MQTTtoBT(char* topicOri, JsonObject& RFdata);
unsigned int BLEinterval = TimeBtwRead; //time between 2 scans
unsigned int BLEscanBeforeConnect = ScanBeforeConnect; //Number of BLE scans between connection cycles
bool publishOnlySensors = PublishOnlySensors;
#ifndef pubKnownBLEServiceData
# define pubKnownBLEServiceData false // define true if you want to publish service data belonging to recognised sensors