diff --git a/README.md b/README.md index 8830f8b..ea5e9e0 100644 --- a/README.md +++ b/README.md @@ -308,6 +308,7 @@ Status message topic from this version is /blecker/status/[device-mac] - adjust scan time - adjust status message (lastSeenMs is added) - wifi reconnect changes (thanks to [@Goodwillson](https://github.com/Goodwillson)) https://github.com/redakker/blecker/pull/59 +- run the BLE scanner on another core of the ESP32 CPU, it probably eliminates the bug #58 (thanks for the idea to [@dpnebert](https://github.com/dpnebert)) Not product logic related - #55 Create an action to create a new build from master diff --git a/src/bluetooth.cpp b/src/bluetooth.cpp index f8f6674..5878ac8 100644 --- a/src/bluetooth.cpp +++ b/src/bluetooth.cpp @@ -1,6 +1,5 @@ #include "bluetooth.h" - BlueTooth::BlueTooth(Log& rlog, Led& led) : logger(rlog, "[BLUE]") { lastRun = 0; @@ -62,25 +61,20 @@ void BlueTooth::setup(Database &database, Signal &mqttMessageSend, this -> mqttBaseTopic = this -> database -> getValueAsString(String(DB_MQTT_TOPIC_PREFIX), false) + MQTT_TOPIC; detailedReport = (database.getValueAsInt(DB_DETAILED_REPORT) > 0) ? true : false; + + // Run the BLE scanner on another core as a separated task + xTaskCreatePinnedToCore(bluetoothScanner, // Method name + "BLE Scan Task", // Only for humans for debug + 1024*2, // How many bytes should be alloted. + pBLEScan, // Pass in variable reference here (or NULL) + 8, // Priority of task + &scan_handle, // Reference to Task handle. Ex: to delete the scan task, it would look like: "vTaskDelete(scan_handle);" + 0); } void BlueTooth::loop() { - // Need to pause between scan intervals, because the scan stuck the process. Leave 2 seconds to sevre the web and any other services - if (millis() - lastRun > (BT_DEFAULT_SCAN_DURATION_IN_SECONDS * 1000) + 2000) { - // Otherwise makes no sens to scan and sent it over - if (networkConnected && mqttConnected) { - // Clear result is before the scan, because there is an assumption that the clear result makes an issue right after the scan - // Details: - https://github.com/redakker/blecker/issues/58 - // - https://github.com/espressif/arduino-esp32/issues/5860 - // - pBLEScan->clearResults(); // delete results fromBLEScan buffer to release memory - - BLEScanResults foundDevices = pBLEScan->start(BT_DEFAULT_SCAN_DURATION_IN_SECONDS, false); - lastRun = millis(); - } - } // Find the expired devices for (int i = 0; i < this -> devices.size(); i++) { @@ -225,3 +219,5 @@ void BlueTooth::handleDeviceChange(Device dev) { } } + + diff --git a/src/bluetooth.h b/src/bluetooth.h index 0c1594d..27e8096 100644 --- a/src/bluetooth.h +++ b/src/bluetooth.h @@ -46,6 +46,9 @@ class BlueTooth: public BLEAdvertisedDeviceCallbacks { LinkedList devices; LinkedList devicesToRemove; + // For Bluetooth scan task + TaskHandle_t scan_handle; + public: BlueTooth(Log& rlog, Led& led); void setup(Database &database, Signal &mqttMessageSend, Signal &deviceChanged); @@ -56,7 +59,7 @@ class BlueTooth: public BLEAdvertisedDeviceCallbacks { private: void onResult(BLEAdvertisedDevice advertisedDevice); void fillDevices(String devicesString); - void handleDeviceChange(Device dev); + void handleDeviceChange(Device dev); }; #endif diff --git a/src/utilities.cpp b/src/utilities.cpp index 66c71ac..6662872 100644 --- a/src/utilities.cpp +++ b/src/utilities.cpp @@ -1,4 +1,9 @@ #include "utilities.h" +#include "definitions.h" +#include +#include +#include +#include esp_chip_info_t chip_info; @@ -21,4 +26,19 @@ const char* getChipModelString(esp_chip_model_t model) { default: return "Unknown"; } +} + +void bluetoothScanner(void *parameters) { + + BLEScan *pBLEScan = static_cast(parameters); + + for( ;; ) { + pBLEScan -> start(BT_DEFAULT_SCAN_DURATION_IN_SECONDS, false); + + // Tell the task how long to delay for: + vTaskDelay(2000 / portTICK_PERIOD_MS ); + + pBLEScan -> clearResults(); // delete results fromBLEScan buffer to release memory + vTaskDelay(20 / portTICK_PERIOD_MS ); + } } \ No newline at end of file diff --git a/src/utilities.h b/src/utilities.h index 047970d..a54bdc7 100644 --- a/src/utilities.h +++ b/src/utilities.h @@ -29,5 +29,6 @@ struct Device { extern esp_chip_info_t chip_info; void setChipInfo(); const char* getChipModelString(esp_chip_model_t model); +void bluetoothScanner(void *parameters); #endif \ No newline at end of file diff --git a/src/webcontent.h b/src/webcontent.h index b169497..fcde37f 100644 --- a/src/webcontent.h +++ b/src/webcontent.h @@ -391,7 +391,7 @@ BLEcker home update -
v1.11 - 163
+
v1.11 - 175