[BT] - Adaptive scan (#1440)
* [BT] Adaptive scan Add adaptive scan to avoid users setting parameters and deduct automatically the scan requirements from the list of devices --------- Co-authored-by: DigiH <github@digih.net>
This commit is contained in:
1 parent
05f83c5f80
commit
f283327998
7 files changed
+51
-44
No files matched your search
+23
-7
@@ -82,7 +82,7 @@ static bool oneWhite = false;
|
||||
void BTConfig_init() {
|
||||
BTConfig.bleConnect = AttemptBLEConnect;
|
||||
BTConfig.BLEinterval = TimeBtwRead;
|
||||
BTConfig.activeScan = ActiveBLEScan;
|
||||
BTConfig.adaptiveScan = AdaptiveBLEScan;
|
||||
BTConfig.intervalActiveScan = TimeBtwActive;
|
||||
BTConfig.intervalConnect = TimeBtwConnect;
|
||||
BTConfig.pubOnlySensors = PublishOnlySensors;
|
||||
@@ -120,7 +120,7 @@ void stateBTMeasures(bool start) {
|
||||
JsonObject jo = jsonBuffer.to<JsonObject>();
|
||||
jo["bleconnect"] = BTConfig.bleConnect;
|
||||
jo["interval"] = BTConfig.BLEinterval;
|
||||
jo["activescan"] = BTConfig.activeScan;
|
||||
jo["adaptivescan"] = BTConfig.adaptiveScan;
|
||||
jo["intervalacts"] = BTConfig.intervalActiveScan;
|
||||
jo["intervalcnct"] = BTConfig.intervalConnect;
|
||||
jo["onlysensors"] = BTConfig.pubOnlySensors;
|
||||
@@ -155,7 +155,7 @@ void BTConfig_fromJson(JsonObject& BTdata, bool startup = false) {
|
||||
if (BTdata.containsKey("interval") && BTdata["interval"] != 0)
|
||||
BTConfig_update(BTdata, "interval", BTConfig.BLEinterval);
|
||||
// Define if the scan is active or passive
|
||||
BTConfig_update(BTdata, "activescan", BTConfig.activeScan);
|
||||
BTConfig_update(BTdata, "adaptivescan", BTConfig.adaptiveScan);
|
||||
// Time before before active scan
|
||||
BTConfig_update(BTdata, "intervalacts", BTConfig.intervalActiveScan);
|
||||
// Time before a connect set
|
||||
@@ -199,7 +199,7 @@ void BTConfig_fromJson(JsonObject& BTdata, bool startup = false) {
|
||||
JsonObject jo = jsonBuffer.to<JsonObject>();
|
||||
jo["bleconnect"] = BTConfig.bleConnect;
|
||||
jo["interval"] = BTConfig.BLEinterval;
|
||||
jo["activescan"] = BTConfig.activeScan;
|
||||
jo["adaptivescan"] = BTConfig.adaptiveScan;
|
||||
jo["intervalacts"] = BTConfig.intervalActiveScan;
|
||||
jo["intervalcnct"] = BTConfig.intervalConnect;
|
||||
jo["onlysensors"] = BTConfig.pubOnlySensors;
|
||||
@@ -625,11 +625,11 @@ void BLEscan() {
|
||||
BLEScan* pBLEScan = BLEDevice::getScan();
|
||||
MyAdvertisedDeviceCallbacks myCallbacks;
|
||||
pBLEScan->setAdvertisedDeviceCallbacks(&myCallbacks);
|
||||
if (millis() > (timeBetweenActive + BTConfig.intervalActiveScan) && !BTConfig.activeScan && BTConfig.intervalActiveScan != 0) {
|
||||
if (millis() > (timeBetweenActive + BTConfig.intervalActiveScan)) {
|
||||
pBLEScan->setActiveScan(true);
|
||||
timeBetweenActive = millis();
|
||||
} else {
|
||||
pBLEScan->setActiveScan(BTConfig.activeScan);
|
||||
pBLEScan->setActiveScan(false);
|
||||
}
|
||||
pBLEScan->setInterval(BLEScanInterval);
|
||||
pBLEScan->setWindow(BLEScanWindow);
|
||||
@@ -810,7 +810,7 @@ void setupBT() {
|
||||
Log.notice(F("BLE scans interval: %d" CR), BTConfig.BLEinterval);
|
||||
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("Adaptive BLE scan: %T" CR), BTConfig.adaptiveScan);
|
||||
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);
|
||||
@@ -1002,6 +1002,22 @@ void process_bledata(JsonObject& BLEdata) {
|
||||
createOrUpdateDevice(mac, device_flags_connect, model_id, mac_type); // Device that broadcast and can be connected
|
||||
} else {
|
||||
createOrUpdateDevice(mac, device_flags_init, model_id, mac_type);
|
||||
if (BTConfig.adaptiveScan == true && (BTConfig.BLEinterval != MinTimeBtwScan || BTConfig.intervalActiveScan != MinTimeBtwScan)) {
|
||||
if (BLEdata.containsKey("acts") && BLEdata.containsKey("cont")) {
|
||||
if (BLEdata["acts"] && BLEdata["cont"]) {
|
||||
BTConfig.BLEinterval = MinTimeBtwScan;
|
||||
BTConfig.intervalActiveScan = MinTimeBtwScan;
|
||||
Log.notice(F("Active and continuous scanning required, paramaters adapted" CR));
|
||||
stateBTMeasures(false);
|
||||
}
|
||||
} else if (BLEdata.containsKey("cont")) {
|
||||
if (BLEdata["cont"]) {
|
||||
BTConfig.BLEinterval = MinTimeBtwScan;
|
||||
Log.notice(F("Passive continuous scanning required, paramaters adapted" CR));
|
||||
stateBTMeasures(false);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
if (BLEdata.containsKey("name")) { // Connectable devices
|
||||
|
||||
+12
-3
@@ -940,6 +940,15 @@ void pubMqttDiscovery() {
|
||||
"", "", "", "", false, // device name, device manufacturer, device model, device ID, retain,
|
||||
stateClassNone //State Class
|
||||
);
|
||||
createDiscovery("number", //set Type
|
||||
subjectBTtoMQTT, "BT: Interval between active scans", (char*)getUniqueId("intervalacts", "").c_str(), //set state_topic,name,uniqueId
|
||||
"", "", "{{ value_json.intervalacts/1000 }}", //set availability_topic,device_class,value_template,
|
||||
"{\"intervalacts\":{{value*1000}},\"save\":true}", "", "s", //set,payload_on,payload_off,unit_of_meas,
|
||||
0, //set off_delay
|
||||
"", "", true, subjectMQTTtoBTset, //set,payload_available,payload_not available ,is a gateway entity, command topic
|
||||
"", "", "", "", false, // device name, device manufacturer, device model, device ID, retain,
|
||||
stateClassNone //State Class
|
||||
);
|
||||
createDiscovery("number", //set Type
|
||||
subjectBTtoMQTT, "BT: Connnect interval", (char*)getUniqueId("intervalcnct", "").c_str(), //set state_topic,name,uniqueId
|
||||
"", "", "{{ value_json.intervalcnct/60000 }}", //set availability_topic,device_class,value_template,
|
||||
@@ -978,9 +987,9 @@ void pubMqttDiscovery() {
|
||||
"false", "true" //state_off, state_on
|
||||
);
|
||||
createDiscovery("switch", //set Type
|
||||
subjectBTtoMQTT, "BT: Active scan", (char*)getUniqueId("active_scan", "").c_str(), //set state_topic,name,uniqueId
|
||||
"", "", "{{ value_json.activescan }}", //set availability_topic,device_class,value_template,
|
||||
"{\"activescan\":true,\"save\":true}", "{\"activescan\":false,\"save\":true}", "", //set,payload_on,payload_off,unit_of_meas,
|
||||
subjectBTtoMQTT, "BT: Adaptive scan", (char*)getUniqueId("adaptive_scan", "").c_str(), //set state_topic,name,uniqueId
|
||||
"", "", "{{ value_json.adaptivescan }}", //set availability_topic,device_class,value_template,
|
||||
"{\"adaptivescan\":true,\"save\":true}", "{\"adaptivescan\":false,\"save\":true}", "", //set,payload_on,payload_off,unit_of_meas,
|
||||
0, //set off_delay
|
||||
Gateway_AnnouncementMsg, will_Message, true, subjectMQTTtoBTset, //set,payload_available,payload_not available ,is a gateway entity, command topic
|
||||
"", "", "", "", false, // device name, device manufacturer, device model, device MAC, retain
|
||||
|
||||
+6
-3
@@ -74,12 +74,15 @@ extern int btQueueLengthCount;
|
||||
#ifndef BLEScanWindow
|
||||
# define BLEScanWindow 30 // How long to scan during the interval; in milliseconds.
|
||||
#endif
|
||||
#ifndef ActiveBLEScan
|
||||
# define ActiveBLEScan true // Set active scanning, this will get more data from the advertiser.
|
||||
#ifndef AdaptiveBLEScan
|
||||
# define AdaptiveBLEScan true // Sets adaptive scanning, this will automatically decide on the best passive and active scanning intervals
|
||||
#endif
|
||||
#ifndef TimeBtwActive
|
||||
# define TimeBtwActive 55555 //define default time between two BLE active scans when general passive scanning is selected; in milliseconds
|
||||
#endif
|
||||
#ifndef MinTimeBtwScan
|
||||
# define MinTimeBtwScan 100 //define the time between two scans; in milliseconds
|
||||
#endif
|
||||
#ifndef TimeBtwConnect
|
||||
# define TimeBtwConnect 3600000 //define default time between BLE connection attempt (not used for immediate actions); in milliseconds
|
||||
#endif
|
||||
@@ -133,7 +136,7 @@ unsigned long scanCount = 0;
|
||||
/*----------------CONFIGURABLE PARAMETERS-----------------*/
|
||||
struct BTConfig_s {
|
||||
bool bleConnect; // Attempt a BLE connection to sensors with ESP32
|
||||
bool activeScan;
|
||||
bool adaptiveScan;
|
||||
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
|
||||
|
||||
Reference in new issue
Block a user