From 05f83c5f80da24e07c8030d82eb5b159f396cf92 Mon Sep 17 00:00:00 2001 From: DigiH Date: Tue, 7 Feb 2023 19:25:40 +0100 Subject: [PATCH] Active Scan Interval (#1433) --- docs/use/ble.md | 12 ++++++++++++ main/ZgatewayBT.ino | 16 ++++++++++++++-- main/config_BT.h | 8 ++++++-- 3 files changed, 32 insertions(+), 4 deletions(-) diff --git a/docs/use/ble.md b/docs/use/ble.md index 38a5c7c..c925782 100644 --- a/docs/use/ble.md +++ b/docs/use/ble.md @@ -146,6 +146,18 @@ With Home Assistant, this command is directly available through MQTT auto discov If false, the gateway will only do a passive scanning (not request for sensor broadcasts), some sensors may not advertize their data with passive scanning. +## Setting the time between active scanning + +If you have passive scanning activated, but also have some devices which require active scanning, this defines the time interval between two intermittent active scans. + +If you want to change the time between active scans you can change it by MQTT. For setting the active scan interval time to every 5 minutes: + +`mosquitto_pub -t home/OpenMQTTGateway/commands/MQTTtoBT/config -m '{"intervalacts":300000}'` + +::: tip +Setting `{"intervalacts":0}` deactivates intermittent active scanning to only have consistent passive scanning if `'{"activescan":false}'`. +::: + ## Setting if the gateway connects to BLE devices eligibles on ESP32 If you want to change this characteristic: diff --git a/main/ZgatewayBT.ino b/main/ZgatewayBT.ino index 159a906..84c25cf 100644 --- a/main/ZgatewayBT.ino +++ b/main/ZgatewayBT.ino @@ -83,6 +83,7 @@ void BTConfig_init() { BTConfig.bleConnect = AttemptBLEConnect; BTConfig.BLEinterval = TimeBtwRead; BTConfig.activeScan = ActiveBLEScan; + BTConfig.intervalActiveScan = TimeBtwActive; BTConfig.intervalConnect = TimeBtwConnect; BTConfig.pubOnlySensors = PublishOnlySensors; BTConfig.presenceEnable = HassPresence; @@ -98,6 +99,7 @@ void BTConfig_init() { } unsigned long timeBetweenConnect = 0; +unsigned long timeBetweenActive = 0; template // Declared here to avoid pre-compilation issue (missing "template" in auto declaration by pio) void BTConfig_update(JsonObject& data, const char* key, T& var); @@ -119,6 +121,7 @@ void stateBTMeasures(bool start) { jo["bleconnect"] = BTConfig.bleConnect; jo["interval"] = BTConfig.BLEinterval; jo["activescan"] = BTConfig.activeScan; + jo["intervalacts"] = BTConfig.intervalActiveScan; jo["intervalcnct"] = BTConfig.intervalConnect; jo["onlysensors"] = BTConfig.pubOnlySensors; jo["hasspresence"] = BTConfig.presenceEnable; @@ -153,7 +156,9 @@ void BTConfig_fromJson(JsonObject& BTdata, bool startup = false) { BTConfig_update(BTdata, "interval", BTConfig.BLEinterval); // Define if the scan is active or passive BTConfig_update(BTdata, "activescan", BTConfig.activeScan); - // Number of scan before a connect set + // Time before before active scan + BTConfig_update(BTdata, "intervalacts", BTConfig.intervalActiveScan); + // Time before a connect set BTConfig_update(BTdata, "intervalcnct", BTConfig.intervalConnect); // publish all BLE devices discovered or only the identified sensors (like temperature sensors) BTConfig_update(BTdata, "onlysensors", BTConfig.pubOnlySensors); @@ -195,6 +200,7 @@ void BTConfig_fromJson(JsonObject& BTdata, bool startup = false) { jo["bleconnect"] = BTConfig.bleConnect; jo["interval"] = BTConfig.BLEinterval; jo["activescan"] = BTConfig.activeScan; + jo["intervalacts"] = BTConfig.intervalActiveScan; jo["intervalcnct"] = BTConfig.intervalConnect; jo["onlysensors"] = BTConfig.pubOnlySensors; jo["hasspresence"] = BTConfig.presenceEnable; @@ -619,7 +625,12 @@ void BLEscan() { BLEScan* pBLEScan = BLEDevice::getScan(); MyAdvertisedDeviceCallbacks myCallbacks; pBLEScan->setAdvertisedDeviceCallbacks(&myCallbacks); - pBLEScan->setActiveScan(BTConfig.activeScan); + if (millis() > (timeBetweenActive + BTConfig.intervalActiveScan) && !BTConfig.activeScan && BTConfig.intervalActiveScan != 0) { + pBLEScan->setActiveScan(true); + timeBetweenActive = millis(); + } else { + pBLEScan->setActiveScan(BTConfig.activeScan); + } pBLEScan->setInterval(BLEScanInterval); pBLEScan->setWindow(BLEScanWindow); BLEScanResults foundDevices = pBLEScan->start(Scan_duration / 1000, false); @@ -800,6 +811,7 @@ void setupBT() { Log.notice(F("BLE connects interval: %d" CR), BTConfig.intervalConnect); Log.notice(F("Publishing only BLE sensors: %T" CR), BTConfig.pubOnlySensors); Log.notice(F("Active BLE scan: %T" CR), BTConfig.activeScan); + Log.notice(F("Active BLE scan interval: %d" CR), BTConfig.intervalActiveScan); Log.notice(F("minrssi: %d" CR), -abs(BTConfig.minRssi)); Log.notice(F("Low Power Mode: %d" CR), lowpowermode); diff --git a/main/config_BT.h b/main/config_BT.h index 12d7699..bfa8410 100644 --- a/main/config_BT.h +++ b/main/config_BT.h @@ -66,7 +66,7 @@ extern int btQueueLengthCount; #define MinimumRSSI -100 //default minimum rssi value, all the devices below -100 will not be reported #ifndef Scan_duration -# define Scan_duration 10000 //define the time for a scan); in milliseconds +# define Scan_duration 10000 //define the time for a scan; in milliseconds #endif #ifndef BLEScanInterval # define BLEScanInterval 52 // How often the scan occurs / switches channels; in milliseconds, @@ -77,6 +77,9 @@ extern int btQueueLengthCount; #ifndef ActiveBLEScan # define ActiveBLEScan true // Set active scanning, this will get more data from the advertiser. #endif +#ifndef TimeBtwActive +# define TimeBtwActive 55555 //define default time between two BLE active scans when general passive scanning is selected; in milliseconds +#endif #ifndef TimeBtwConnect # define TimeBtwConnect 3600000 //define default time between BLE connection attempt (not used for immediate actions); in milliseconds #endif @@ -85,7 +88,7 @@ extern int btQueueLengthCount; # define BLEScanDuplicateCacheSize 200 #endif #ifndef TimeBtwRead -# define TimeBtwRead 55555 //define default time between 2 scans); in milliseconds +# define TimeBtwRead 55555 //define default time between 2 scans; in milliseconds #endif #ifndef PublishOnlySensors @@ -131,6 +134,7 @@ unsigned long scanCount = 0; struct BTConfig_s { bool bleConnect; // Attempt a BLE connection to sensors with ESP32 bool activeScan; + unsigned long intervalActiveScan; // Time between 2 active scans when generally passive scanning unsigned long BLEinterval; // Time between 2 scans unsigned long intervalConnect; // Time between 2 connects bool pubOnlySensors; // Publish only the identified sensors (like temperature sensors)