From 8996dd8f60a84bef3cbc380c7499a90469dccf89 Mon Sep 17 00:00:00 2001 From: Bad Date: Sun, 14 Aug 2022 10:03:47 +0200 Subject: [PATCH 01/18] Move minRssi in config_BT.h Move var definitions next to compilation var definitions --- main/ZgatewayBT.ino | 2 -- main/config_BT.h | 16 ++++++++++------ 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/main/ZgatewayBT.ino b/main/ZgatewayBT.ino index 14b6d8feca..c8a8ed7a97 100644 --- a/main/ZgatewayBT.ino +++ b/main/ZgatewayBT.ino @@ -92,8 +92,6 @@ static BLEdevice NO_DEVICE_FOUND = {{0}, TheengsDecoder::BLE_ID_NUM::UNKNOWN_MODEL}; static bool oneWhite = false; -int minRssi = abs(MinimumRSSI); //minimum rssi value - void pubBTMainCore(JsonObject& data, bool haPresenceEnabled = true) { if (abs((int)data["rssi"] | 0) < minRssi && data.containsKey("id")) { String topic = data["id"].as(); diff --git a/main/config_BT.h b/main/config_BT.h index 38ff2db96a..4c155399a8 100644 --- a/main/config_BT.h +++ b/main/config_BT.h @@ -49,13 +49,14 @@ bool bleConnect = AttemptBLECOnnect; # include "NimBLEDevice.h" #endif -/*----------------------BT topics & parameters-------------------------*/ +/*-----------BT TOPICS & COMPILATION PARAMETERS-----------*/ #define subjectBTtoMQTT "/BTtoMQTT" #define subjectMQTTtoBTset "/commands/MQTTtoBT/config" // Uncomment to send undecoded device data to another gateway device for decoding // #define MQTTDecodeTopic "undecoded" #define MinimumRSSI -100 //default minimum rssi value, all the devices below -90 will not be reported +int minRssi = abs(MinimumRSSI); //minimum rssi value #ifndef Scan_duration # define Scan_duration 10000 //define the time for a scan @@ -72,18 +73,25 @@ bool bleConnect = AttemptBLECOnnect; #ifndef ScanBeforeConnect # define ScanBeforeConnect 10 //define number of scans before connecting to BLE devices (ESP32 only, minimum 1) #endif +unsigned int BLEscanBeforeConnect = ScanBeforeConnect; //Number of BLE scans between connection cycles + #ifndef BLEScanDuplicateCacheSize # define BLEScanDuplicateCacheSize 200 #endif #ifndef TimeBtwRead # define TimeBtwRead 55555 //define default time between 2 scans #endif +unsigned int BLEinterval = TimeBtwRead; //time between 2 scans + #ifndef PublishOnlySensors # define PublishOnlySensors false //false if we publish all BLE devices discovered or true only the identified sensors (like temperature sensors) #endif +bool publishOnlySensors = PublishOnlySensors; + #ifndef HassPresence # define HassPresence false //false if we publish into Home Assistant presence topic #endif +bool hassPresence = HassPresence; #ifndef BTQueueSize # define BTQueueSize 4 // lockless queue size for multi core cases (ESP32 currently) @@ -101,11 +109,7 @@ bool bleConnect = AttemptBLECOnnect; #define ServicedataMinLength 27 -unsigned int BLEinterval = TimeBtwRead; //time between 2 scans -unsigned int BLEscanBeforeConnect = ScanBeforeConnect; //Number of BLE scans between connection cycles unsigned long scanCount = 0; -bool publishOnlySensors = PublishOnlySensors; -bool hassPresence = HassPresence; #ifndef pubKnownBLEServiceData # define pubKnownBLEServiceData false // define true if you want to publish service data belonging to recognised sensors @@ -131,7 +135,7 @@ bool hassPresence = HassPresence; # define useBeaconUuidForTopic false // define true to use iBeacon UUID as topic, instead of sender (random) MAC address #endif -/*-------------------HOME ASSISTANT ROOM PRESENCE ----------------------*/ +/*--------------HOME ASSISTANT ROOM PRESENCE--------------*/ #define subjectHomePresence "presence/" // will send Home Assistant room presence message to this topic (first part is same for all rooms, second is room name) #ifndef useBeaconUuidForPresence From cd34e81f017169d94616dd4178272d969d56e7da Mon Sep 17 00:00:00 2001 From: Bad Date: Sun, 14 Aug 2022 11:34:42 +0200 Subject: [PATCH 02/18] Introduce UseExtDecoder when is def MQTTDecodeTopic Replace #ifdef MQTTDecodeTopic by #if UseExtDecoder --- main/ZgatewayBT.ino | 10 +++++----- main/config_BT.h | 8 ++++++++ 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/main/ZgatewayBT.ino b/main/ZgatewayBT.ino index c8a8ed7a97..2b6353d9d3 100644 --- a/main/ZgatewayBT.ino +++ b/main/ZgatewayBT.ino @@ -66,7 +66,7 @@ using namespace std; # define device_flags_isBlackL 1 << 2 # define device_flags_connect 1 << 3 -# ifndef MQTTDecodeTopic +# if !UseExtDecoder TheengsDecoder decoder; # endif @@ -100,7 +100,7 @@ void pubBTMainCore(JsonObject& data, bool haPresenceEnabled = true) { if (data.containsKey("model_id") && data["model_id"].as() == "IBEACON") topic = data["uuid"].as(); // If model_id is IBEACON, use uuid as topic # endif -# ifdef MQTTDecodeTopic +# if UseExtDecoder if (!data.containsKey("model")) topic = MQTTDecodeTopic; // If external decoder, topic is MQTTDecodeTopic # endif @@ -885,7 +885,7 @@ void launchBTDiscovery() { p->sensorModel_id != TheengsDecoder::BLE_ID_NUM::GAEN) { String macWOdots = String(p->macAdr); macWOdots.replace(":", ""); -# ifndef MQTTDecodeTopic +# if !UseExtDecoder if (p->sensorModel_id > TheengsDecoder::BLE_ID_NUM::UNKNOWN_MODEL && p->sensorModel_id < TheengsDecoder::BLE_ID_NUM::BLE_ID_MAX && p->sensorModel_id != TheengsDecoder::BLE_ID_NUM::HHCCJCY01HHCC) { // Exception on HHCCJCY01HHCC as this one is discoverable and connectable for battery retrieving @@ -999,7 +999,7 @@ void process_bledata(JsonObject& BLEdata) { const char* mac = BLEdata["id"].as(); int model_id = -1; int mac_type = BLEdata["mac_type"].as(); -# ifndef MQTTDecodeTopic +# if !UseExtDecoder model_id = decoder.decodeBLEJson(BLEdata); if (model_id >= 0) { // Broadcaster devices Log.trace(F("Decoder found device: %s" CR), BLEdata["model_id"].as()); @@ -1027,7 +1027,7 @@ void process_bledata(JsonObject& BLEdata) { createOrUpdateDevice(mac, device_flags_connect, model_id, mac_type); } } -# ifdef MQTTDecodeTopic +# if UseExtDecoder else if (model_id < 0 && BLEdata.containsKey("servicedata")) { const char* service_data = (const char*)(BLEdata["servicedata"] | ""); if (strstr(service_data, "209800") != NULL) { diff --git a/main/config_BT.h b/main/config_BT.h index 4c155399a8..1ff38cec0e 100644 --- a/main/config_BT.h +++ b/main/config_BT.h @@ -54,6 +54,14 @@ bool bleConnect = AttemptBLECOnnect; #define subjectMQTTtoBTset "/commands/MQTTtoBT/config" // Uncomment to send undecoded device data to another gateway device for decoding // #define MQTTDecodeTopic "undecoded" +#ifndef UseExtDecoder +# ifdef MQTTDecodeTopic +# define UseExtDecoder true +# else +# define UseExtDecoder false +# endif +#endif + #define MinimumRSSI -100 //default minimum rssi value, all the devices below -90 will not be reported int minRssi = abs(MinimumRSSI); //minimum rssi value From fe3c836b2f1791a71791f4150c5c44b879a04273 Mon Sep 17 00:00:00 2001 From: Bad Date: Sun, 14 Aug 2022 12:18:56 +0200 Subject: [PATCH 03/18] Rename AttemptBLECOnnect to AttemptBLEConnect --- main/config_BT.h | 6 +++--- platformio.ini | 4 ++-- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/main/config_BT.h b/main/config_BT.h index 1ff38cec0e..aff7dc113a 100644 --- a/main/config_BT.h +++ b/main/config_BT.h @@ -36,10 +36,10 @@ extern void launchBTDiscovery(); extern int btQueueBlocked; extern int btQueueLengthSum; extern int btQueueLengthCount; -# ifndef AttemptBLECOnnect -# define AttemptBLECOnnect true //do we by default attempt a BLE connection to sensors with ESP32 +# ifndef AttemptBLEConnect +# define AttemptBLEConnect true //do we by default attempt a BLE connection to sensors with ESP32 # endif -bool bleConnect = AttemptBLECOnnect; +bool bleConnect = AttemptBLEConnect; // Sets whether to filter publishing of scanned devices that require a connection. // Setting this to 1 prevents overwriting the publication of the device connection data with the advertised data (Recommended for use with OpenHAB). diff --git a/platformio.ini b/platformio.ini index cc0f58060c..980a1b6eef 100644 --- a/platformio.ini +++ b/platformio.ini @@ -482,7 +482,7 @@ build_flags = '-DGateway_Name="OpenMQTTGateway_ESP32_BLE_C"' '-DTimeBtwRead=0' '-DScan_duration=1000' - '-DAttemptBLECOnnect=false' + '-DAttemptBLEConnect=false' [env:esp32feather-ble] platform = ${com.esp32_platform} @@ -519,7 +519,7 @@ build_flags = ; '-DDEFAULT_LOW_POWER_MODE=2' ; '-DTimeBtwRead=155000' ; '-DScan_duration=2000' -; '-DAttemptBLECOnnect=false' +; '-DAttemptBLEConnect=false' ; '-DActiveBLEScan=false' ; '-DESPWifiManualSetup=true' ; '-DMQTT_USER="lolin-esp32"' From 5462e3cd203849efabdbb8c4e944c810763a3a0f Mon Sep 17 00:00:00 2001 From: Bad Date: Sun, 14 Aug 2022 12:26:28 +0200 Subject: [PATCH 04/18] Create a struct (BTConfig_s) for BT configuration data Create a default config for BTConfig_s (BTConfig_default) based on defines Create a global variable (BTConfig) to hold live BT configuration data and initialize it Move bleConnect to BTConfig --- main/ZgatewayBT.ino | 15 +++++++++------ main/config_BT.h | 11 ++++++++++- 2 files changed, 19 insertions(+), 7 deletions(-) diff --git a/main/ZgatewayBT.ino b/main/ZgatewayBT.ino index 2b6353d9d3..20b54c5010 100644 --- a/main/ZgatewayBT.ino +++ b/main/ZgatewayBT.ino @@ -60,6 +60,9 @@ QueueHandle_t BLEQueue; using namespace std; +// Global struct to store live BT configuration data +BTConfig_s BTConfig = BTConfig_default; + # define device_flags_init 0 << 0 # define device_flags_isDisc 1 << 0 # define device_flags_isWhiteL 1 << 1 @@ -606,7 +609,7 @@ void coreTask(void* pvParameters) { if (xSemaphoreTake(semaphoreBLEOperation, pdMS_TO_TICKS(30000)) == pdTRUE) { BLEscan(); // Launching a connect every BLEscanBeforeConnect - if ((!(scanCount % BLEscanBeforeConnect) || scanCount == 1) && bleConnect) + if ((!(scanCount % BLEscanBeforeConnect) || scanCount == 1) && BTConfig.bleConnect) BLEconnect(); dumpDevices(); xSemaphoreGive(semaphoreBLEOperation); @@ -1066,7 +1069,7 @@ void BTforceScan() { BTtoMQTT(); Log.trace(F("Scan done" CR)); # ifdef ESP32 - if (bleConnect) + if (BTConfig.bleConnect) BLEconnect(); # endif } else { @@ -1103,7 +1106,7 @@ void immediateBTAction(void* pvParameters) { std::swap(BLEactions, act_swap); // If we stopped the scheduled connect for this action, do the scheduled now - if ((!(scanCount % BLEscanBeforeConnect) || scanCount == 1) && bleConnect) + if ((!(scanCount % BLEscanBeforeConnect) || scanCount == 1) && BTConfig.bleConnect) BLEconnect(); xSemaphoreGive(semaphoreBLEOperation); } else { @@ -1255,9 +1258,9 @@ void MQTTtoBT(char* topicOri, JsonObject& BTdata) { // json object decoding // Attempts to connect to elligible devices or not if (BTdata.containsKey("bleconnect")) { Log.trace(F("Do we initiate a connection to retrieve data" CR)); - Log.trace(F("Previous value: %T" CR), bleConnect); - bleConnect = (bool)BTdata["bleconnect"]; - Log.notice(F("New value bleConnect: %T" CR), bleConnect); + Log.trace(F("Previous value: %T" CR), BTConfig.bleConnect); + BTConfig.bleConnect = (bool)BTdata["bleconnect"]; + Log.notice(F("New value bleConnect: %T" CR), BTConfig.bleConnect); } if (BTdata.containsKey("lowpowermode")) { changelowpowermode((int)BTdata["lowpowermode"]); diff --git a/main/config_BT.h b/main/config_BT.h index aff7dc113a..6885860023 100644 --- a/main/config_BT.h +++ b/main/config_BT.h @@ -39,7 +39,6 @@ extern int btQueueLengthCount; # ifndef AttemptBLEConnect # define AttemptBLEConnect true //do we by default attempt a BLE connection to sensors with ESP32 # endif -bool bleConnect = AttemptBLEConnect; // Sets whether to filter publishing of scanned devices that require a connection. // Setting this to 1 prevents overwriting the publication of the device connection data with the advertised data (Recommended for use with OpenHAB). @@ -150,6 +149,16 @@ unsigned long scanCount = 0; # define useBeaconUuidForPresence false // //define true to use iBeacon UUID as for presence, instead of sender MAC (random) address #endif +/*----------------CONFIGURABLE PARAMETERS-----------------*/ +struct BTConfig_s { + bool bleConnect; // Attempt a BLE connection to sensors with ESP32 +} BTConfig_default = { + .bleConnect = AttemptBLEConnect +}; + +// Global struct to store live BT configuration data +extern BTConfig_s BTConfig; + /*-------------------PIN DEFINITIONS----------------------*/ #if !defined(BT_RX) || !defined(BT_TX) # ifdef ESP8266 From af0761d07dcbdc668e09437a538cc42b8aea0ae6 Mon Sep 17 00:00:00 2001 From: Bad Date: Sun, 14 Aug 2022 13:17:36 +0200 Subject: [PATCH 05/18] Move BLEscanBeforeConnect in BTConfig --- main/ZgatewayBT.ino | 14 +++++++------- main/config_BT.h | 5 +++-- main/main.ino | 2 +- 3 files changed, 11 insertions(+), 10 deletions(-) diff --git a/main/ZgatewayBT.ino b/main/ZgatewayBT.ino index 20b54c5010..ab2dbbee28 100644 --- a/main/ZgatewayBT.ino +++ b/main/ZgatewayBT.ino @@ -609,7 +609,7 @@ void coreTask(void* pvParameters) { if (xSemaphoreTake(semaphoreBLEOperation, pdMS_TO_TICKS(30000)) == pdTRUE) { BLEscan(); // Launching a connect every BLEscanBeforeConnect - if ((!(scanCount % BLEscanBeforeConnect) || scanCount == 1) && BTConfig.bleConnect) + if ((!(scanCount % BTConfig.BLEscanBeforeConnect) || scanCount == 1) && BTConfig.bleConnect) BLEconnect(); dumpDevices(); xSemaphoreGive(semaphoreBLEOperation); @@ -683,7 +683,7 @@ void changelowpowermode(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("BLE scans number before connect: %d" CR), BTConfig.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), lowpowermode); @@ -744,7 +744,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("BLE scans number before connect: %d" CR), BTConfig.BLEscanBeforeConnect); Log.notice(F("Publishing only BLE sensors: %T" CR), publishOnlySensors); Log.notice(F("minrssi: %d" CR), minRssi); softserial.begin(HMSerialSpeed); @@ -1106,7 +1106,7 @@ void immediateBTAction(void* pvParameters) { std::swap(BLEactions, act_swap); // If we stopped the scheduled connect for this action, do the scheduled now - if ((!(scanCount % BLEscanBeforeConnect) || scanCount == 1) && BTConfig.bleConnect) + if ((!(scanCount % BTConfig.BLEscanBeforeConnect) || scanCount == 1) && BTConfig.bleConnect) BLEconnect(); xSemaphoreGive(semaphoreBLEOperation); } else { @@ -1243,9 +1243,9 @@ void MQTTtoBT(char* topicOri, JsonObject& BTdata) { // json object decoding // Number of scan before a connect set if (BTdata.containsKey("scanbcnct")) { Log.trace(F("BLE scans number before a connect setup" CR)); - Log.trace(F("Previous number: %d" CR), BLEscanBeforeConnect); - BLEscanBeforeConnect = (unsigned int)BTdata["scanbcnct"]; - Log.notice(F("New scan number before connect: %d" CR), BLEscanBeforeConnect); + Log.trace(F("Previous number: %d" CR), BTConfig.BLEscanBeforeConnect); + BTConfig.BLEscanBeforeConnect = (unsigned int)BTdata["scanbcnct"]; + Log.notice(F("New scan number before connect: %d" CR), BTConfig.BLEscanBeforeConnect); } // publish all BLE devices discovered or only the identified sensors (like temperature sensors) if (BTdata.containsKey("onlysensors")) { diff --git a/main/config_BT.h b/main/config_BT.h index 6885860023..2191e46047 100644 --- a/main/config_BT.h +++ b/main/config_BT.h @@ -80,7 +80,6 @@ int minRssi = abs(MinimumRSSI); //minimum rssi value #ifndef ScanBeforeConnect # define ScanBeforeConnect 10 //define number of scans before connecting to BLE devices (ESP32 only, minimum 1) #endif -unsigned int BLEscanBeforeConnect = ScanBeforeConnect; //Number of BLE scans between connection cycles #ifndef BLEScanDuplicateCacheSize # define BLEScanDuplicateCacheSize 200 @@ -152,8 +151,10 @@ unsigned long scanCount = 0; /*----------------CONFIGURABLE PARAMETERS-----------------*/ struct BTConfig_s { bool bleConnect; // Attempt a BLE connection to sensors with ESP32 + unsigned int BLEscanBeforeConnect; // Number of BLE scans between connection cycles } BTConfig_default = { - .bleConnect = AttemptBLEConnect + .bleConnect = AttemptBLEConnect, + .BLEscanBeforeConnect = ScanBeforeConnect, }; // Global struct to store live BT configuration data diff --git a/main/main.ino b/main/main.ino index 3f5efb08fd..8ef9befad8 100644 --- a/main/main.ino +++ b/main/main.ino @@ -1576,7 +1576,7 @@ void stateMeasures() { SYSdata["btqavg"] = (btQueueLengthCount > 0 ? btQueueLengthSum / (float)btQueueLengthCount : 0); # endif SYSdata["interval"] = BLEinterval; - SYSdata["scanbcnct"] = BLEscanBeforeConnect; + SYSdata["scanbcnct"] = BTConfig.BLEscanBeforeConnect; SYSdata["scnct"] = scanCount; # endif # ifdef ZboardM5STACK From 4c57dff22b24c11915d434368ed63a18d38b6764 Mon Sep 17 00:00:00 2001 From: Bad Date: Sun, 14 Aug 2022 13:55:19 +0200 Subject: [PATCH 06/18] Move BLEinterval in BTConfig --- main/ZgatewayBT.ino | 18 +++++++++--------- main/config_BT.h | 3 ++- main/main.ino | 2 +- 3 files changed, 12 insertions(+), 11 deletions(-) diff --git a/main/ZgatewayBT.ino b/main/ZgatewayBT.ino index ab2dbbee28..540beb9baa 100644 --- a/main/ZgatewayBT.ino +++ b/main/ZgatewayBT.ino @@ -622,7 +622,7 @@ void coreTask(void* pvParameters) { int scan = atomic_exchange_explicit(&forceBTScan, 0, ::memory_order_seq_cst); // is this enough, it will wait the full deepsleep... if (scan == 1) BTforceScan(); } else { - for (int interval = BLEinterval, waitms; interval > 0; interval -= waitms) { + for (int interval = BTConfig.BLEinterval, waitms; interval > 0; interval -= waitms) { int scan = atomic_exchange_explicit(&forceBTScan, 0, ::memory_order_seq_cst); if (scan == 1) BTforceScan(); // should we break after this? delay(waitms = interval > 100 ? 100 : interval); // 100ms @@ -637,8 +637,8 @@ void coreTask(void* pvParameters) { void lowPowerESP32() // low power mode { - Log.trace(F("Going to deep sleep for: %l s" CR), (BLEinterval / 1000)); - deepSleep(BLEinterval * 1000); + Log.trace(F("Going to deep sleep for: %l s" CR), (BTConfig.BLEinterval / 1000)); + deepSleep(BTConfig.BLEinterval * 1000); } void deepSleep(uint64_t time_in_us) { @@ -682,7 +682,7 @@ void changelowpowermode(int newLowPowerMode) { } void setupBT() { - Log.notice(F("BLE scans interval: %d" CR), BLEinterval); + Log.notice(F("BLE scans interval: %d" CR), BTConfig.BLEinterval); Log.notice(F("BLE scans number before connect: %d" CR), BTConfig.BLEscanBeforeConnect); Log.notice(F("Publishing only BLE sensors: %T" CR), publishOnlySensors); Log.notice(F("minrssi: %d" CR), minRssi); @@ -743,7 +743,7 @@ unsigned long timebt = 0; struct decompose d[6] = {{0, 12, true}, {12, 2, false}, {14, 2, false}, {16, 2, false}, {28, 4, true}, {32, 60, false}}; void setupBT() { - Log.notice(F("BLE interval: %d" CR), BLEinterval); + Log.notice(F("BLE interval: %d" CR), BTConfig.BLEinterval); Log.notice(F("BLE scans number before connect: %d" CR), BTConfig.BLEscanBeforeConnect); Log.notice(F("Publishing only BLE sensors: %T" CR), publishOnlySensors); Log.notice(F("minrssi: %d" CR), minRssi); @@ -774,7 +774,7 @@ bool BTtoMQTT() { returnedString += String(a, HEX); } - if (millis() > (timebt + BLEinterval)) { //retrieving data + if (millis() > (timebt + BTConfig.BLEinterval)) { //retrieving data timebt = millis(); returnedString.remove(0); //init data string softserial.print(F(QUESTION_MSG)); //start new discovery @@ -1235,9 +1235,9 @@ void MQTTtoBT(char* topicOri, JsonObject& BTdata) { // json object decoding BTforceScan(); # endif } else { - Log.trace(F("Previous interval: %d ms" CR), BLEinterval); - BLEinterval = interval; - Log.notice(F("New interval: %d ms" CR), BLEinterval); + Log.trace(F("Previous interval: %d ms" CR), BTConfig.BLEinterval); + BTConfig.BLEinterval = interval; + Log.notice(F("New interval: %d ms" CR), BTConfig.BLEinterval); } } // Number of scan before a connect set diff --git a/main/config_BT.h b/main/config_BT.h index 2191e46047..995d5825cd 100644 --- a/main/config_BT.h +++ b/main/config_BT.h @@ -87,7 +87,6 @@ int minRssi = abs(MinimumRSSI); //minimum rssi value #ifndef TimeBtwRead # define TimeBtwRead 55555 //define default time between 2 scans #endif -unsigned int BLEinterval = TimeBtwRead; //time between 2 scans #ifndef PublishOnlySensors # define PublishOnlySensors false //false if we publish all BLE devices discovered or true only the identified sensors (like temperature sensors) @@ -151,9 +150,11 @@ unsigned long scanCount = 0; /*----------------CONFIGURABLE PARAMETERS-----------------*/ struct BTConfig_s { bool bleConnect; // Attempt a BLE connection to sensors with ESP32 + unsigned int BLEinterval; // Time between 2 scans unsigned int BLEscanBeforeConnect; // Number of BLE scans between connection cycles } BTConfig_default = { .bleConnect = AttemptBLEConnect, + .BLEinterval = TimeBtwRead, .BLEscanBeforeConnect = ScanBeforeConnect, }; diff --git a/main/main.ino b/main/main.ino index 8ef9befad8..86fa23edf5 100644 --- a/main/main.ino +++ b/main/main.ino @@ -1575,7 +1575,7 @@ void stateMeasures() { SYSdata["btqsnd"] = btQueueLengthCount; SYSdata["btqavg"] = (btQueueLengthCount > 0 ? btQueueLengthSum / (float)btQueueLengthCount : 0); # endif - SYSdata["interval"] = BLEinterval; + SYSdata["interval"] = BTConfig.BLEinterval; SYSdata["scanbcnct"] = BTConfig.BLEscanBeforeConnect; SYSdata["scnct"] = scanCount; # endif From 5dfe818e1848a6ae6dc55ebd366112cc029ff25e Mon Sep 17 00:00:00 2001 From: Bad Date: Sun, 14 Aug 2022 13:59:37 +0200 Subject: [PATCH 07/18] Move PublishOnlySensors in BTConfig --- main/ZgatewayBT.ino | 16 ++++++++-------- main/config_BT.h | 3 ++- 2 files changed, 10 insertions(+), 9 deletions(-) diff --git a/main/ZgatewayBT.ino b/main/ZgatewayBT.ino index 540beb9baa..62786cc124 100644 --- a/main/ZgatewayBT.ino +++ b/main/ZgatewayBT.ino @@ -468,7 +468,7 @@ void procBLETask(void* pvParameters) { BLEdata["rssi"] = (int)advertisedDevice->getRSSI(); if (advertisedDevice->haveTXPower()) BLEdata["txpower"] = (int8_t)advertisedDevice->getTXPower(); - if (advertisedDevice->haveRSSI() && !publishOnlySensors && hassPresence) { + if (advertisedDevice->haveRSSI() && !BTConfig.pubOnlySensors && hassPresence) { hass_presence(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 } if (advertisedDevice->haveServiceData()) { @@ -684,7 +684,7 @@ void changelowpowermode(int newLowPowerMode) { void setupBT() { Log.notice(F("BLE scans interval: %d" CR), BTConfig.BLEinterval); Log.notice(F("BLE scans number before connect: %d" CR), BTConfig.BLEscanBeforeConnect); - Log.notice(F("Publishing only BLE sensors: %T" CR), publishOnlySensors); + Log.notice(F("Publishing only BLE sensors: %T" CR), BTConfig.pubOnlySensors); Log.notice(F("minrssi: %d" CR), minRssi); Log.notice(F("Low Power Mode: %d" CR), lowpowermode); @@ -745,7 +745,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), BTConfig.BLEinterval); Log.notice(F("BLE scans number before connect: %d" CR), BTConfig.BLEscanBeforeConnect); - Log.notice(F("Publishing only BLE sensors: %T" CR), publishOnlySensors); + Log.notice(F("Publishing only BLE sensors: %T" CR), BTConfig.pubOnlySensors); Log.notice(F("minrssi: %d" CR), minRssi); softserial.begin(HMSerialSpeed); softserial.print(F("AT+ROLE1" CR)); @@ -834,7 +834,7 @@ bool BTtoMQTT() { return false; //if we have at least one white MAC and this MAC is not white we go out BLEdata["rssi"] = (int)rssi; - if (!publishOnlySensors && hassPresence) + if (!BTConfig.pubOnlySensors && hassPresence) hass_presence(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 Log.trace(F("Service data: %s" CR), restData.c_str()); BLEdata["servicedata"] = restData.c_str(); @@ -971,7 +971,7 @@ void launchBTDiscovery() { void PublishDeviceData(JsonObject& BLEdata, bool processBLEData) { if (abs((int)BLEdata["rssi"] | 0) < minRssi) { // process only the devices close enough if (processBLEData) process_bledata(BLEdata); - if (!publishOnlySensors || BLEdata.containsKey("model") || BLEdata.containsKey("distance")) { + if (!BTConfig.pubOnlySensors || BLEdata.containsKey("model") || BLEdata.containsKey("distance")) { # if !pubBLEServiceUUID RemoveJsonPropertyIf(BLEdata, "servicedatauuid", BLEdata.containsKey("model") && BLEdata.containsKey("servicedatauuid")); # endif @@ -1250,9 +1250,9 @@ void MQTTtoBT(char* topicOri, JsonObject& BTdata) { // json object decoding // 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); + Log.trace(F("Previous value: %T" CR), BTConfig.pubOnlySensors); + BTConfig.pubOnlySensors = (bool)BTdata["onlysensors"]; + Log.notice(F("New value onlysensors: %T" CR), BTConfig.pubOnlySensors); } # ifdef ESP32 // Attempts to connect to elligible devices or not diff --git a/main/config_BT.h b/main/config_BT.h index 995d5825cd..fa923e1da0 100644 --- a/main/config_BT.h +++ b/main/config_BT.h @@ -91,7 +91,6 @@ int minRssi = abs(MinimumRSSI); //minimum rssi value #ifndef PublishOnlySensors # define PublishOnlySensors false //false if we publish all BLE devices discovered or true only the identified sensors (like temperature sensors) #endif -bool publishOnlySensors = PublishOnlySensors; #ifndef HassPresence # define HassPresence false //false if we publish into Home Assistant presence topic @@ -152,10 +151,12 @@ struct BTConfig_s { bool bleConnect; // Attempt a BLE connection to sensors with ESP32 unsigned int BLEinterval; // Time between 2 scans unsigned int BLEscanBeforeConnect; // Number of BLE scans between connection cycles + bool pubOnlySensors; // Publish only the identified sensors (like temperature sensors) } BTConfig_default = { .bleConnect = AttemptBLEConnect, .BLEinterval = TimeBtwRead, .BLEscanBeforeConnect = ScanBeforeConnect, + .pubOnlySensors = PublishOnlySensors, }; // Global struct to store live BT configuration data From 4dc7c7f182028a2cdcc748c0b447fbbd2183aaa6 Mon Sep 17 00:00:00 2001 From: Bad Date: Sun, 14 Aug 2022 14:03:44 +0200 Subject: [PATCH 08/18] Move hassPresence in BTConfig --- main/ZgatewayBT.ino | 10 +++++----- main/config_BT.h | 3 ++- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/main/ZgatewayBT.ino b/main/ZgatewayBT.ino index 62786cc124..ccfb328b23 100644 --- a/main/ZgatewayBT.ino +++ b/main/ZgatewayBT.ino @@ -468,7 +468,7 @@ void procBLETask(void* pvParameters) { BLEdata["rssi"] = (int)advertisedDevice->getRSSI(); if (advertisedDevice->haveTXPower()) BLEdata["txpower"] = (int8_t)advertisedDevice->getTXPower(); - if (advertisedDevice->haveRSSI() && !BTConfig.pubOnlySensors && hassPresence) { + if (advertisedDevice->haveRSSI() && !BTConfig.pubOnlySensors && BTConfig.presenceEnable) { hass_presence(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 } if (advertisedDevice->haveServiceData()) { @@ -834,7 +834,7 @@ bool BTtoMQTT() { return false; //if we have at least one white MAC and this MAC is not white we go out BLEdata["rssi"] = (int)rssi; - if (!BTConfig.pubOnlySensors && hassPresence) + if (!BTConfig.pubOnlySensors && BTConfig.presenceEnable) hass_presence(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 Log.trace(F("Service data: %s" CR), restData.c_str()); BLEdata["servicedata"] = restData.c_str(); @@ -1279,10 +1279,10 @@ void MQTTtoBT(char* topicOri, JsonObject& BTdata) { // json object decoding // Home Assistant presence message if (BTdata.containsKey("hasspresence")) { // storing Min RSSI for further use if needed - Log.trace(F("Previous hasspresence: %T" CR), hassPresence); + Log.trace(F("Previous hasspresence: %T" CR), BTConfig.presenceEnable); // set Min RSSI if present if not setting default value - hassPresence = (bool)BTdata["hasspresence"]; - Log.notice(F("New hasspresence: %T" CR), hassPresence); + BTConfig.presenceEnable = (bool)BTdata["hasspresence"]; + Log.notice(F("New hasspresence: %T" CR), BTConfig.presenceEnable); } } } diff --git a/main/config_BT.h b/main/config_BT.h index fa923e1da0..2f1e69b94c 100644 --- a/main/config_BT.h +++ b/main/config_BT.h @@ -95,7 +95,6 @@ int minRssi = abs(MinimumRSSI); //minimum rssi value #ifndef HassPresence # define HassPresence false //false if we publish into Home Assistant presence topic #endif -bool hassPresence = HassPresence; #ifndef BTQueueSize # define BTQueueSize 4 // lockless queue size for multi core cases (ESP32 currently) @@ -152,11 +151,13 @@ struct BTConfig_s { unsigned int BLEinterval; // Time between 2 scans unsigned int BLEscanBeforeConnect; // Number of BLE scans between connection cycles bool pubOnlySensors; // Publish only the identified sensors (like temperature sensors) + bool presenceEnable; // Publish into Home Assistant presence topic } BTConfig_default = { .bleConnect = AttemptBLEConnect, .BLEinterval = TimeBtwRead, .BLEscanBeforeConnect = ScanBeforeConnect, .pubOnlySensors = PublishOnlySensors, + .presenceEnable = HassPresence, }; // Global struct to store live BT configuration data From 9b568ae362ad63b8d2da801dab8c9a3b59a27b13 Mon Sep 17 00:00:00 2001 From: Bad Date: Sun, 14 Aug 2022 14:06:22 +0200 Subject: [PATCH 09/18] Move minRssi into BTConfig --- main/ZgatewayBT.ino | 14 +++++++------- main/config_BT.h | 3 ++- 2 files changed, 9 insertions(+), 8 deletions(-) diff --git a/main/ZgatewayBT.ino b/main/ZgatewayBT.ino index ccfb328b23..76ecd59930 100644 --- a/main/ZgatewayBT.ino +++ b/main/ZgatewayBT.ino @@ -96,7 +96,7 @@ static BLEdevice NO_DEVICE_FOUND = {{0}, static bool oneWhite = false; void pubBTMainCore(JsonObject& data, bool haPresenceEnabled = true) { - if (abs((int)data["rssi"] | 0) < minRssi && data.containsKey("id")) { + if (abs((int)data["rssi"] | 0) < BTConfig.minRssi && data.containsKey("id")) { String topic = data["id"].as(); topic.replace(":", ""); // Initially publish topic ends with MAC address # if useBeaconUuidForTopic @@ -685,7 +685,7 @@ void setupBT() { Log.notice(F("BLE scans interval: %d" CR), BTConfig.BLEinterval); Log.notice(F("BLE scans number before connect: %d" CR), BTConfig.BLEscanBeforeConnect); Log.notice(F("Publishing only BLE sensors: %T" CR), BTConfig.pubOnlySensors); - Log.notice(F("minrssi: %d" CR), minRssi); + Log.notice(F("minrssi: %d" CR), BTConfig.minRssi); Log.notice(F("Low Power Mode: %d" CR), lowpowermode); atomic_init(&forceBTScan, 0); // in theory, we don't need this @@ -746,7 +746,7 @@ void setupBT() { Log.notice(F("BLE interval: %d" CR), BTConfig.BLEinterval); Log.notice(F("BLE scans number before connect: %d" CR), BTConfig.BLEscanBeforeConnect); Log.notice(F("Publishing only BLE sensors: %T" CR), BTConfig.pubOnlySensors); - Log.notice(F("minrssi: %d" CR), minRssi); + Log.notice(F("minrssi: %d" CR), BTConfig.minRssi); softserial.begin(HMSerialSpeed); softserial.print(F("AT+ROLE1" CR)); delay(100); @@ -969,7 +969,7 @@ void launchBTDiscovery() { # endif void PublishDeviceData(JsonObject& BLEdata, bool processBLEData) { - if (abs((int)BLEdata["rssi"] | 0) < minRssi) { // process only the devices close enough + if (abs((int)BLEdata["rssi"] | 0) < BTConfig.minRssi) { // process only the devices close enough if (processBLEData) process_bledata(BLEdata); if (!BTConfig.pubOnlySensors || BLEdata.containsKey("model") || BLEdata.containsKey("distance")) { # if !pubBLEServiceUUID @@ -1271,10 +1271,10 @@ void MQTTtoBT(char* topicOri, JsonObject& BTdata) { // json object decoding // MinRSSI set if (BTdata.containsKey("minrssi")) { // storing Min RSSI for further use if needed - Log.trace(F("Previous minrssi: %d" CR), minRssi); + Log.trace(F("Previous minrssi: %d" CR), BTConfig.minRssi); // set Min RSSI if present if not setting default value - minRssi = abs((int)BTdata["minrssi"]); - Log.notice(F("New minrssi: %d" CR), minRssi); + BTConfig.minRssi = abs((int)BTdata["minrssi"]); + Log.notice(F("New minrssi: %d" CR), BTConfig.minRssi); } // Home Assistant presence message if (BTdata.containsKey("hasspresence")) { diff --git a/main/config_BT.h b/main/config_BT.h index 2f1e69b94c..bff429b7d2 100644 --- a/main/config_BT.h +++ b/main/config_BT.h @@ -63,7 +63,6 @@ extern int btQueueLengthCount; #define MinimumRSSI -100 //default minimum rssi value, all the devices below -90 will not be reported -int minRssi = abs(MinimumRSSI); //minimum rssi value #ifndef Scan_duration # define Scan_duration 10000 //define the time for a scan @@ -152,12 +151,14 @@ struct BTConfig_s { unsigned int BLEscanBeforeConnect; // Number of BLE scans between connection cycles bool pubOnlySensors; // Publish only the identified sensors (like temperature sensors) bool presenceEnable; // Publish into Home Assistant presence topic + int minRssi; // Minimum rssi value, all the devices below will not be reported } BTConfig_default = { .bleConnect = AttemptBLEConnect, .BLEinterval = TimeBtwRead, .BLEscanBeforeConnect = ScanBeforeConnect, .pubOnlySensors = PublishOnlySensors, .presenceEnable = HassPresence, + .minRssi = abs(MinimumRSSI), }; // Global struct to store live BT configuration data From 529bc950149b53e671684f3f5e7322e91f5cb6b0 Mon Sep 17 00:00:00 2001 From: Bad Date: Sun, 14 Aug 2022 14:14:26 +0200 Subject: [PATCH 10/18] Introduce presenceTopic into BTConfig --- main/ZgatewayBT.ino | 2 +- main/config_BT.h | 2 ++ 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/main/ZgatewayBT.ino b/main/ZgatewayBT.ino index 76ecd59930..d1bcac099b 100644 --- a/main/ZgatewayBT.ino +++ b/main/ZgatewayBT.ino @@ -121,7 +121,7 @@ void pubBTMainCore(JsonObject& data, bool haPresenceEnabled = true) { data["id"] = data["uuid"]; } # endif - String topic = String(mqtt_topic) + subjectHomePresence + String(gateway_name); + String topic = String(mqtt_topic) + BTConfig.presenceTopic + String(gateway_name); Log.trace(F("Pub HA Presence %s" CR), topic.c_str()); pub_custom_topic((char*)topic.c_str(), data, false); } diff --git a/main/config_BT.h b/main/config_BT.h index bff429b7d2..03d76a98a0 100644 --- a/main/config_BT.h +++ b/main/config_BT.h @@ -151,6 +151,7 @@ struct BTConfig_s { unsigned int BLEscanBeforeConnect; // Number of BLE scans between connection cycles bool pubOnlySensors; // Publish only the identified sensors (like temperature sensors) bool presenceEnable; // Publish into Home Assistant presence topic + String presenceTopic; // Home Assistant presence topic to publish on int minRssi; // Minimum rssi value, all the devices below will not be reported } BTConfig_default = { .bleConnect = AttemptBLEConnect, @@ -158,6 +159,7 @@ struct BTConfig_s { .BLEscanBeforeConnect = ScanBeforeConnect, .pubOnlySensors = PublishOnlySensors, .presenceEnable = HassPresence, + .presenceTopic = subjectHomePresence, .minRssi = abs(MinimumRSSI), }; From 4f08a81ad6d92eedacb4d6e710d4a93b7a49b522 Mon Sep 17 00:00:00 2001 From: Bad Date: Sun, 14 Aug 2022 14:15:43 +0200 Subject: [PATCH 11/18] Move MQTTDecodeTopic as extDecoderTopic into BTConfig --- main/ZgatewayBT.ino | 2 +- main/config_BT.h | 6 ++++++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/main/ZgatewayBT.ino b/main/ZgatewayBT.ino index d1bcac099b..2899bf051a 100644 --- a/main/ZgatewayBT.ino +++ b/main/ZgatewayBT.ino @@ -105,7 +105,7 @@ void pubBTMainCore(JsonObject& data, bool haPresenceEnabled = true) { # endif # if UseExtDecoder if (!data.containsKey("model")) - topic = MQTTDecodeTopic; // If external decoder, topic is MQTTDecodeTopic + topic = BTConfig.extDecoderTopic; // If external decoder, use this topic to send data # endif topic = subjectBTtoMQTT + String("/") + topic; pub((char*)topic.c_str(), data); diff --git a/main/config_BT.h b/main/config_BT.h index 03d76a98a0..ce4358ccf6 100644 --- a/main/config_BT.h +++ b/main/config_BT.h @@ -59,6 +59,10 @@ extern int btQueueLengthCount; # else # define UseExtDecoder false # endif +#else +# if UseExtDecoder +# define MQTTDecodeTopic "undecoded" +# endif #endif @@ -153,6 +157,7 @@ struct BTConfig_s { bool presenceEnable; // Publish into Home Assistant presence topic String presenceTopic; // Home Assistant presence topic to publish on int minRssi; // Minimum rssi value, all the devices below will not be reported + String extDecoderTopic; // Topic to send undecoded device data on } BTConfig_default = { .bleConnect = AttemptBLEConnect, .BLEinterval = TimeBtwRead, @@ -161,6 +166,7 @@ struct BTConfig_s { .presenceEnable = HassPresence, .presenceTopic = subjectHomePresence, .minRssi = abs(MinimumRSSI), + .extDecoderTopic = MQTTDecodeTopic, }; // Global struct to store live BT configuration data From 4509583425c02f92f5f869bd3f6050e746791a69 Mon Sep 17 00:00:00 2001 From: Bad Date: Sun, 14 Aug 2022 14:45:52 +0200 Subject: [PATCH 12/18] Mutate UseExtDecoder as runtime var BTConfig.extDecoderTopic --- main/ZgatewayBT.ino | 33 ++++++++++----------------------- main/config_BT.h | 9 +++++---- 2 files changed, 15 insertions(+), 27 deletions(-) diff --git a/main/ZgatewayBT.ino b/main/ZgatewayBT.ino index 2899bf051a..80db9ea096 100644 --- a/main/ZgatewayBT.ino +++ b/main/ZgatewayBT.ino @@ -69,9 +69,7 @@ BTConfig_s BTConfig = BTConfig_default; # define device_flags_isBlackL 1 << 2 # define device_flags_connect 1 << 3 -# if !UseExtDecoder TheengsDecoder decoder; -# endif struct decompose { int start; @@ -103,10 +101,8 @@ void pubBTMainCore(JsonObject& data, bool haPresenceEnabled = true) { if (data.containsKey("model_id") && data["model_id"].as() == "IBEACON") topic = data["uuid"].as(); // If model_id is IBEACON, use uuid as topic # endif -# if UseExtDecoder - if (!data.containsKey("model")) + if (BTConfig.extDecoderEnable && !data.containsKey("model")) topic = BTConfig.extDecoderTopic; // If external decoder, use this topic to send data -# endif topic = subjectBTtoMQTT + String("/") + topic; pub((char*)topic.c_str(), data); } @@ -888,8 +884,8 @@ void launchBTDiscovery() { p->sensorModel_id != TheengsDecoder::BLE_ID_NUM::GAEN) { String macWOdots = String(p->macAdr); macWOdots.replace(":", ""); -# if !UseExtDecoder - if (p->sensorModel_id > TheengsDecoder::BLE_ID_NUM::UNKNOWN_MODEL && + if (!BTConfig.extDecoderEnable && // Do not decode if an external decoter is configured + p->sensorModel_id > TheengsDecoder::BLE_ID_NUM::UNKNOWN_MODEL && p->sensorModel_id < TheengsDecoder::BLE_ID_NUM::BLE_ID_MAX && p->sensorModel_id != TheengsDecoder::BLE_ID_NUM::HHCCJCY01HHCC) { // Exception on HHCCJCY01HHCC as this one is discoverable and connectable for battery retrieving Log.trace(F("Looking for Model_id: %d" CR), p->sensorModel_id); @@ -908,11 +904,11 @@ void launchBTDiscovery() { String discovery_topic = String(subjectBTtoMQTT) + "/" + macWOdots; String entity_name = String(model_id.c_str()) + "-" + String(prop.key().c_str()); String unique_id = macWOdots + "-" + String(prop.key().c_str()); -# if OpenHABDiscovery +# if OpenHABDiscovery String value_template = "{{ value_json." + String(prop.key().c_str()) + "}}"; -# else +# else String value_template = "{{ value_json." + String(prop.key().c_str()) + " | is_defined }}"; -# endif +# endif if (p->sensorModel_id == TheengsDecoder::BLE_ID_NUM::SBS1 && !strcmp(prop.key().c_str(), "state")) { String payload_on = "{\"SBS1\":\"on\",\"mac\":\"" + String(p->macAdr) + "\"}"; String payload_off = "{\"SBS1\":\"off\",\"mac\":\"" + String(p->macAdr) + "\"}"; @@ -934,9 +930,7 @@ void launchBTDiscovery() { } } } - } else -# endif - { + } else { if (p->sensorModel_id > BLEconectable::id::MIN && p->sensorModel_id < BLEconectable::id::MAX || p->sensorModel_id == TheengsDecoder::BLE_ID_NUM::HHCCJCY01HHCC) { @@ -1000,10 +994,8 @@ void PublishDeviceData(JsonObject& BLEdata, bool processBLEData) { void process_bledata(JsonObject& BLEdata) { const char* mac = BLEdata["id"].as(); - int model_id = -1; + int model_id = BTConfig.extDecoderEnable ? -1 : decoder.decodeBLEJson(BLEdata); int mac_type = BLEdata["mac_type"].as(); -# if !UseExtDecoder - model_id = decoder.decodeBLEJson(BLEdata); if (model_id >= 0) { // Broadcaster devices Log.trace(F("Decoder found device: %s" CR), BLEdata["model_id"].as()); if (model_id == TheengsDecoder::BLE_ID_NUM::HHCCJCY01HHCC) { @@ -1011,9 +1003,7 @@ void process_bledata(JsonObject& BLEdata) { } else { createOrUpdateDevice(mac, device_flags_init, model_id, mac_type); } - } else -# endif - { + } else { if (BLEdata.containsKey("name")) { // Connectable devices std::string name = BLEdata["name"]; if (name.compare("LYWSD03MMC") == 0) @@ -1029,9 +1019,7 @@ void process_bledata(JsonObject& BLEdata) { Log.trace(F("Connectable device found: %s" CR), name.c_str()); createOrUpdateDevice(mac, device_flags_connect, model_id, mac_type); } - } -# if UseExtDecoder - else if (model_id < 0 && BLEdata.containsKey("servicedata")) { + } else if (BTConfig.extDecoderEnable && model_id < 0 && BLEdata.containsKey("servicedata")) { const char* service_data = (const char*)(BLEdata["servicedata"] | ""); if (strstr(service_data, "209800") != NULL) { model_id == TheengsDecoder::BLE_ID_NUM::HHCCJCY01HHCC; @@ -1039,7 +1027,6 @@ void process_bledata(JsonObject& BLEdata) { createOrUpdateDevice(mac, device_flags_connect, model_id, mac_type); } } -# endif } if (model_id < 0) { Log.trace(F("No device found " CR)); diff --git a/main/config_BT.h b/main/config_BT.h index ce4358ccf6..ccf13165aa 100644 --- a/main/config_BT.h +++ b/main/config_BT.h @@ -59,10 +59,9 @@ extern int btQueueLengthCount; # else # define UseExtDecoder false # endif -#else -# if UseExtDecoder -# define MQTTDecodeTopic "undecoded" -# endif +#endif +#ifndef MQTTDecodeTopic +# define MQTTDecodeTopic "undecoded" #endif @@ -157,6 +156,7 @@ struct BTConfig_s { bool presenceEnable; // Publish into Home Assistant presence topic String presenceTopic; // Home Assistant presence topic to publish on int minRssi; // Minimum rssi value, all the devices below will not be reported + bool extDecoderEnable; // Send undecoded device data to another gateway device for decoding String extDecoderTopic; // Topic to send undecoded device data on } BTConfig_default = { .bleConnect = AttemptBLEConnect, @@ -166,6 +166,7 @@ struct BTConfig_s { .presenceEnable = HassPresence, .presenceTopic = subjectHomePresence, .minRssi = abs(MinimumRSSI), + .extDecoderEnable = UseExtDecoder, .extDecoderTopic = MQTTDecodeTopic, }; From bd3412fbb1419cbbab775156d60fcd4ca893c080 Mon Sep 17 00:00:00 2001 From: Bad Date: Sun, 14 Aug 2022 14:53:42 +0200 Subject: [PATCH 13/18] Remove ServicedataMinLength (unused) Always declare AttemptBLEConnect & BLE_FILTER_CONNECTABLE --- main/config_BT.h | 18 +++++++----------- 1 file changed, 7 insertions(+), 11 deletions(-) diff --git a/main/config_BT.h b/main/config_BT.h index ccf13165aa..be75f7b1e2 100644 --- a/main/config_BT.h +++ b/main/config_BT.h @@ -36,15 +36,6 @@ extern void launchBTDiscovery(); extern int btQueueBlocked; extern int btQueueLengthSum; extern int btQueueLengthCount; -# ifndef AttemptBLEConnect -# define AttemptBLEConnect true //do we by default attempt a BLE connection to sensors with ESP32 -# endif - -// Sets whether to filter publishing of scanned devices that require a connection. -// Setting this to 1 prevents overwriting the publication of the device connection data with the advertised data (Recommended for use with OpenHAB). -# ifndef BLE_FILTER_CONNECTABLE -# define BLE_FILTER_CONNECTABLE 0 -# endif # include "NimBLEDevice.h" #endif @@ -64,6 +55,13 @@ extern int btQueueLengthCount; # define MQTTDecodeTopic "undecoded" #endif +#ifndef AttemptBLEConnect +# define AttemptBLEConnect true //do we by default attempt a BLE connection to sensors with ESP32 +#endif + +#ifndef BLE_FILTER_CONNECTABLE +# define BLE_FILTER_CONNECTABLE 0 // Sets whether to filter publishing of scanned devices that require a connection. +#endif // Setting this to 1 prevents overwriting the publication of the device connection data with the advertised data (Recommended for use with OpenHAB). #define MinimumRSSI -100 //default minimum rssi value, all the devices below -90 will not be reported @@ -112,8 +110,6 @@ extern int btQueueLengthCount; #define CRLR_Length 4 #define BLE_CNCT_TIMEOUT 3000 -#define ServicedataMinLength 27 - unsigned long scanCount = 0; #ifndef pubKnownBLEServiceData From 57e0ea333f3c1849b72841a754218c49d14f23df Mon Sep 17 00:00:00 2001 From: Bad Date: Sun, 14 Aug 2022 14:56:05 +0200 Subject: [PATCH 14/18] Mutate BLE_FILTER_CONNECTABLE as runtime parameter BTConfig.filterConnectable --- main/ZgatewayBT.ino | 4 +--- main/config_BT.h | 2 ++ 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/main/ZgatewayBT.ino b/main/ZgatewayBT.ino index 80db9ea096..2d8f9da244 100644 --- a/main/ZgatewayBT.ino +++ b/main/ZgatewayBT.ino @@ -445,12 +445,10 @@ void procBLETask(void* pvParameters) { Log.notice(F("Device detected: %s" CR), (char*)mac_adress.c_str()); BLEdevice* device = getDeviceByMac(BLEdata["id"].as()); -# if BLE_FILTER_CONNECTABLE - if (device->connect) { + if (BTConfig.filterConnectable && device->connect) { Log.notice(F("Filtered connectable device" CR)); continue; } -# endif if ((!oneWhite || isWhite(device)) && !isBlack(device)) { //if not black listed MAC we go AND if we have no white MAC or this MAC is white we go out if (advertisedDevice->haveName()) diff --git a/main/config_BT.h b/main/config_BT.h index be75f7b1e2..94f92f2d84 100644 --- a/main/config_BT.h +++ b/main/config_BT.h @@ -154,6 +154,7 @@ struct BTConfig_s { int minRssi; // Minimum rssi value, all the devices below will not be reported bool extDecoderEnable; // Send undecoded device data to another gateway device for decoding String extDecoderTopic; // Topic to send undecoded device data on + bool filterConnectable; // Sets whether to filter publishing of scanned devices that require a connection. } BTConfig_default = { .bleConnect = AttemptBLEConnect, .BLEinterval = TimeBtwRead, @@ -164,6 +165,7 @@ struct BTConfig_s { .minRssi = abs(MinimumRSSI), .extDecoderEnable = UseExtDecoder, .extDecoderTopic = MQTTDecodeTopic, + .filterConnectable = BLE_FILTER_CONNECTABLE, }; // Global struct to store live BT configuration data From a9297171538fb8f7c34792cb316e4f6cec1511fa Mon Sep 17 00:00:00 2001 From: Bad Date: Sun, 14 Aug 2022 15:11:26 +0200 Subject: [PATCH 15/18] Mutate pubKnownBLEServiceData as runtime var in BTConfig Mutate pubUnknownBLEServiceData as runtime var in BTConfig Mutate pubBLEManufacturerData as runtime var in BTConfig Mutate pubUnknownBLEManufacturerData as runtime var in BTConfig Mutate pubBLEServiceUUID as runtime var in BTConfig --- main/ZgatewayBT.ino | 28 ++++++++++++---------------- main/config_BT.h | 10 ++++++++++ 2 files changed, 22 insertions(+), 16 deletions(-) diff --git a/main/ZgatewayBT.ino b/main/ZgatewayBT.ino index 2d8f9da244..59dc8e8acd 100644 --- a/main/ZgatewayBT.ino +++ b/main/ZgatewayBT.ino @@ -964,24 +964,20 @@ void PublishDeviceData(JsonObject& BLEdata, bool processBLEData) { if (abs((int)BLEdata["rssi"] | 0) < BTConfig.minRssi) { // process only the devices close enough if (processBLEData) process_bledata(BLEdata); if (!BTConfig.pubOnlySensors || BLEdata.containsKey("model") || BLEdata.containsKey("distance")) { -# if !pubBLEServiceUUID - RemoveJsonPropertyIf(BLEdata, "servicedatauuid", BLEdata.containsKey("model") && BLEdata.containsKey("servicedatauuid")); -# endif -# if !pubKnownBLEServiceData - RemoveJsonPropertyIf(BLEdata, "servicedata", BLEdata.containsKey("model") && BLEdata.containsKey("servicedata")); -# endif -# if !pubBLEManufacturerData - RemoveJsonPropertyIf(BLEdata, "manufacturerdata", BLEdata.containsKey("model") && BLEdata.containsKey("manufacturerdata")); -# endif + if (!BTConfig.pubServiceDataUUID) + RemoveJsonPropertyIf(BLEdata, "servicedatauuid", BLEdata.containsKey("model") && BLEdata.containsKey("servicedatauuid")); + if (!BTConfig.pubKnownServiceData) + RemoveJsonPropertyIf(BLEdata, "servicedata", BLEdata.containsKey("model") && BLEdata.containsKey("servicedata")); + if (!BTConfig.pubKnownManufData) + RemoveJsonPropertyIf(BLEdata, "manufacturerdata", BLEdata.containsKey("model") && BLEdata.containsKey("manufacturerdata")); pubBT(BLEdata); } else { -# if !pubUnknownBLEServiceData - Log.trace(F("Unknown service data, removing it" CR)); - RemoveJsonPropertyIf(BLEdata, "servicedata", BLEdata.containsKey("servicedata")); -# endif -# if !pubUnknownBLEManufacturerData - RemoveJsonPropertyIf(BLEdata, "manufacturerdata", BLEdata.containsKey("model") && BLEdata.containsKey("manufacturerdata")); -# endif + if (!BTConfig.pubUnknownServiceData) { + Log.trace(F("Unknown service data, removing it" CR)); + RemoveJsonPropertyIf(BLEdata, "servicedata", BLEdata.containsKey("servicedata")); + } + if (!BTConfig.pubUnknownManufData) + RemoveJsonPropertyIf(BLEdata, "manufacturerdata", BLEdata.containsKey("model") && BLEdata.containsKey("manufacturerdata")); } } else if (BLEdata.containsKey("distance")) { pubBT(BLEdata); diff --git a/main/config_BT.h b/main/config_BT.h index 94f92f2d84..9faf4f2e7e 100644 --- a/main/config_BT.h +++ b/main/config_BT.h @@ -155,6 +155,11 @@ struct BTConfig_s { bool extDecoderEnable; // Send undecoded device data to another gateway device for decoding String extDecoderTopic; // Topic to send undecoded device data on bool filterConnectable; // Sets whether to filter publishing of scanned devices that require a connection. + bool pubKnownServiceData; // Publish service data belonging to recognised sensors + bool pubUnknownServiceData; // Publish service data belonging to unrecognised sensors (in case you are having too heavy service data) https://github.com/1technophile/OpenMQTTGateway/issues/318#issuecomment-446064707 + bool pubKnownManufData; // Publish the manufacturer's data (sometimes contains characters that aren't valid with receiving client) + bool pubUnknownManufData; // Publish the manufacturer's data (sometimes contains characters that aren't valid with receiving client) + bool pubServiceDataUUID; // Publish the service UUID data } BTConfig_default = { .bleConnect = AttemptBLEConnect, .BLEinterval = TimeBtwRead, @@ -166,6 +171,11 @@ struct BTConfig_s { .extDecoderEnable = UseExtDecoder, .extDecoderTopic = MQTTDecodeTopic, .filterConnectable = BLE_FILTER_CONNECTABLE, + .pubKnownServiceData = pubKnownBLEServiceData, + .pubUnknownServiceData = pubUnknownBLEServiceData, + .pubKnownManufData = pubBLEManufacturerData, + .pubUnknownManufData = pubUnknownBLEManufacturerData, + .pubServiceDataUUID = pubBLEServiceUUID, }; // Global struct to store live BT configuration data From 05cd7e000196adf5e0e157ae721e1b63959b7e6e Mon Sep 17 00:00:00 2001 From: Bad Date: Sun, 14 Aug 2022 15:24:55 +0200 Subject: [PATCH 16/18] Mutate useBeaconUuidForTopic as runtime var in BTConfig Mutate useBeaconUuidForPresence as runtime var in BTConfig --- main/ZgatewayBT.ino | 8 ++------ main/config_BT.h | 4 ++++ 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/main/ZgatewayBT.ino b/main/ZgatewayBT.ino index 59dc8e8acd..c07688045c 100644 --- a/main/ZgatewayBT.ino +++ b/main/ZgatewayBT.ino @@ -97,10 +97,8 @@ void pubBTMainCore(JsonObject& data, bool haPresenceEnabled = true) { if (abs((int)data["rssi"] | 0) < BTConfig.minRssi && data.containsKey("id")) { String topic = data["id"].as(); topic.replace(":", ""); // Initially publish topic ends with MAC address -# if useBeaconUuidForTopic - if (data.containsKey("model_id") && data["model_id"].as() == "IBEACON") + if (BTConfig.pubBeaconUuidForTopic && data.containsKey("model_id") && data["model_id"].as() == "IBEACON") topic = data["uuid"].as(); // If model_id is IBEACON, use uuid as topic -# endif if (BTConfig.extDecoderEnable && !data.containsKey("model")) topic = BTConfig.extDecoderTopic; // If external decoder, use this topic to send data topic = subjectBTtoMQTT + String("/") + topic; @@ -111,12 +109,10 @@ void pubBTMainCore(JsonObject& data, bool haPresenceEnabled = true) { data.remove("servicedatauuid"); if (data.containsKey("servicedata")) data.remove("servicedata"); -# if useBeaconUuidForPresence - if (data.containsKey("model_id") && data["model_id"].as() == "IBEACON") { + if (BTConfig.presenceUseBeaconUuid && data.containsKey("model_id") && data["model_id"].as() == "IBEACON") { data["mac"] = data["id"]; data["id"] = data["uuid"]; } -# endif String topic = String(mqtt_topic) + BTConfig.presenceTopic + String(gateway_name); Log.trace(F("Pub HA Presence %s" CR), topic.c_str()); pub_custom_topic((char*)topic.c_str(), data, false); diff --git a/main/config_BT.h b/main/config_BT.h index 9faf4f2e7e..a17486986b 100644 --- a/main/config_BT.h +++ b/main/config_BT.h @@ -151,6 +151,7 @@ struct BTConfig_s { bool pubOnlySensors; // Publish only the identified sensors (like temperature sensors) bool presenceEnable; // Publish into Home Assistant presence topic String presenceTopic; // Home Assistant presence topic to publish on + bool presenceUseBeaconUuid; // Use iBeacon UUID as for presence, instead of sender MAC (random) address int minRssi; // Minimum rssi value, all the devices below will not be reported bool extDecoderEnable; // Send undecoded device data to another gateway device for decoding String extDecoderTopic; // Topic to send undecoded device data on @@ -160,6 +161,7 @@ struct BTConfig_s { bool pubKnownManufData; // Publish the manufacturer's data (sometimes contains characters that aren't valid with receiving client) bool pubUnknownManufData; // Publish the manufacturer's data (sometimes contains characters that aren't valid with receiving client) bool pubServiceDataUUID; // Publish the service UUID data + bool pubBeaconUuidForTopic; // Use iBeacon UUID as topic, instead of sender (random) MAC address } BTConfig_default = { .bleConnect = AttemptBLEConnect, .BLEinterval = TimeBtwRead, @@ -167,6 +169,7 @@ struct BTConfig_s { .pubOnlySensors = PublishOnlySensors, .presenceEnable = HassPresence, .presenceTopic = subjectHomePresence, + .presenceUseBeaconUuid = useBeaconUuidForPresence, .minRssi = abs(MinimumRSSI), .extDecoderEnable = UseExtDecoder, .extDecoderTopic = MQTTDecodeTopic, @@ -176,6 +179,7 @@ struct BTConfig_s { .pubKnownManufData = pubBLEManufacturerData, .pubUnknownManufData = pubUnknownBLEManufacturerData, .pubServiceDataUUID = pubBLEServiceUUID, + .pubBeaconUuidForTopic = useBeaconUuidForTopic, }; // Global struct to store live BT configuration data From b7e18fc398cf3040492130b20fca9c68eaf2410e Mon Sep 17 00:00:00 2001 From: Bad Date: Sun, 14 Aug 2022 16:00:42 +0200 Subject: [PATCH 17/18] Small optimization --- main/ZgatewayBT.ino | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/main/ZgatewayBT.ino b/main/ZgatewayBT.ino index c07688045c..0d429ff46a 100644 --- a/main/ZgatewayBT.ino +++ b/main/ZgatewayBT.ino @@ -97,7 +97,7 @@ void pubBTMainCore(JsonObject& data, bool haPresenceEnabled = true) { if (abs((int)data["rssi"] | 0) < BTConfig.minRssi && data.containsKey("id")) { String topic = data["id"].as(); topic.replace(":", ""); // Initially publish topic ends with MAC address - if (BTConfig.pubBeaconUuidForTopic && data.containsKey("model_id") && data["model_id"].as() == "IBEACON") + if (BTConfig.pubBeaconUuidForTopic && !BTConfig.extDecoderEnable && data.containsKey("model_id") && data["model_id"].as() == "IBEACON") topic = data["uuid"].as(); // If model_id is IBEACON, use uuid as topic if (BTConfig.extDecoderEnable && !data.containsKey("model")) topic = BTConfig.extDecoderTopic; // If external decoder, use this topic to send data From 791e17722c121c7eb0beea6d887a3613ef2e8d0a Mon Sep 17 00:00:00 2001 From: Florian <1technophile@users.noreply.github.com> Date: Sun, 14 Aug 2022 18:27:59 -0500 Subject: [PATCH 18/18] Formatting of config_BT --- main/config_BT.h | 68 ++++++++++++++++++++++++------------------------ 1 file changed, 34 insertions(+), 34 deletions(-) diff --git a/main/config_BT.h b/main/config_BT.h index a17486986b..52c28aa035 100644 --- a/main/config_BT.h +++ b/main/config_BT.h @@ -145,41 +145,41 @@ unsigned long scanCount = 0; /*----------------CONFIGURABLE PARAMETERS-----------------*/ struct BTConfig_s { - bool bleConnect; // Attempt a BLE connection to sensors with ESP32 - unsigned int BLEinterval; // Time between 2 scans - unsigned int BLEscanBeforeConnect; // Number of BLE scans between connection cycles - bool pubOnlySensors; // Publish only the identified sensors (like temperature sensors) - bool presenceEnable; // Publish into Home Assistant presence topic - String presenceTopic; // Home Assistant presence topic to publish on - bool presenceUseBeaconUuid; // Use iBeacon UUID as for presence, instead of sender MAC (random) address - int minRssi; // Minimum rssi value, all the devices below will not be reported - bool extDecoderEnable; // Send undecoded device data to another gateway device for decoding - String extDecoderTopic; // Topic to send undecoded device data on - bool filterConnectable; // Sets whether to filter publishing of scanned devices that require a connection. - bool pubKnownServiceData; // Publish service data belonging to recognised sensors - bool pubUnknownServiceData; // Publish service data belonging to unrecognised sensors (in case you are having too heavy service data) https://github.com/1technophile/OpenMQTTGateway/issues/318#issuecomment-446064707 - bool pubKnownManufData; // Publish the manufacturer's data (sometimes contains characters that aren't valid with receiving client) - bool pubUnknownManufData; // Publish the manufacturer's data (sometimes contains characters that aren't valid with receiving client) - bool pubServiceDataUUID; // Publish the service UUID data - bool pubBeaconUuidForTopic; // Use iBeacon UUID as topic, instead of sender (random) MAC address + bool bleConnect; // Attempt a BLE connection to sensors with ESP32 + unsigned int BLEinterval; // Time between 2 scans + unsigned int BLEscanBeforeConnect; // Number of BLE scans between connection cycles + bool pubOnlySensors; // Publish only the identified sensors (like temperature sensors) + bool presenceEnable; // Publish into Home Assistant presence topic + String presenceTopic; // Home Assistant presence topic to publish on + bool presenceUseBeaconUuid; // Use iBeacon UUID as for presence, instead of sender MAC (random) address + int minRssi; // Minimum rssi value, all the devices below will not be reported + bool extDecoderEnable; // Send undecoded device data to another gateway device for decoding + String extDecoderTopic; // Topic to send undecoded device data on + bool filterConnectable; // Sets whether to filter publishing of scanned devices that require a connection. + bool pubKnownServiceData; // Publish service data belonging to recognised sensors + bool pubUnknownServiceData; // Publish service data belonging to unrecognised sensors (in case you are having too heavy service data) https://github.com/1technophile/OpenMQTTGateway/issues/318#issuecomment-446064707 + bool pubKnownManufData; // Publish the manufacturer's data (sometimes contains characters that aren't valid with receiving client) + bool pubUnknownManufData; // Publish the manufacturer's data (sometimes contains characters that aren't valid with receiving client) + bool pubServiceDataUUID; // Publish the service UUID data + bool pubBeaconUuidForTopic; // Use iBeacon UUID as topic, instead of sender (random) MAC address } BTConfig_default = { - .bleConnect = AttemptBLEConnect, - .BLEinterval = TimeBtwRead, - .BLEscanBeforeConnect = ScanBeforeConnect, - .pubOnlySensors = PublishOnlySensors, - .presenceEnable = HassPresence, - .presenceTopic = subjectHomePresence, - .presenceUseBeaconUuid = useBeaconUuidForPresence, - .minRssi = abs(MinimumRSSI), - .extDecoderEnable = UseExtDecoder, - .extDecoderTopic = MQTTDecodeTopic, - .filterConnectable = BLE_FILTER_CONNECTABLE, - .pubKnownServiceData = pubKnownBLEServiceData, - .pubUnknownServiceData = pubUnknownBLEServiceData, - .pubKnownManufData = pubBLEManufacturerData, - .pubUnknownManufData = pubUnknownBLEManufacturerData, - .pubServiceDataUUID = pubBLEServiceUUID, - .pubBeaconUuidForTopic = useBeaconUuidForTopic, + .bleConnect = AttemptBLEConnect, + .BLEinterval = TimeBtwRead, + .BLEscanBeforeConnect = ScanBeforeConnect, + .pubOnlySensors = PublishOnlySensors, + .presenceEnable = HassPresence, + .presenceTopic = subjectHomePresence, + .presenceUseBeaconUuid = useBeaconUuidForPresence, + .minRssi = abs(MinimumRSSI), + .extDecoderEnable = UseExtDecoder, + .extDecoderTopic = MQTTDecodeTopic, + .filterConnectable = BLE_FILTER_CONNECTABLE, + .pubKnownServiceData = pubKnownBLEServiceData, + .pubUnknownServiceData = pubUnknownBLEServiceData, + .pubKnownManufData = pubBLEManufacturerData, + .pubUnknownManufData = pubUnknownBLEManufacturerData, + .pubServiceDataUUID = pubBLEServiceUUID, + .pubBeaconUuidForTopic = useBeaconUuidForTopic, }; // Global struct to store live BT configuration data