Skip to content

Commit

Permalink
#58 Receive "scan_evt timeout" on serial monitor
Browse files Browse the repository at this point in the history
  • Loading branch information
redakker committed Nov 22, 2023
1 parent e88e769 commit 0cd2092
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 18 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
26 changes: 11 additions & 15 deletions src/bluetooth.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#include "bluetooth.h"


BlueTooth::BlueTooth(Log& rlog, Led& led) : logger(rlog, "[BLUE]") {

lastRun = 0;
Expand Down Expand Up @@ -62,25 +61,20 @@ void BlueTooth::setup(Database &database, Signal<MQTTMessage> &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++) {

Expand Down Expand Up @@ -225,3 +219,5 @@ void BlueTooth::handleDeviceChange(Device dev) {
}
}



5 changes: 4 additions & 1 deletion src/bluetooth.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ class BlueTooth: public BLEAdvertisedDeviceCallbacks {
LinkedList<Device> devices;
LinkedList<int> devicesToRemove;

// For Bluetooth scan task
TaskHandle_t scan_handle;

public:
BlueTooth(Log& rlog, Led& led);
void setup(Database &database, Signal<MQTTMessage> &mqttMessageSend, Signal<Device> &deviceChanged);
Expand All @@ -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
20 changes: 20 additions & 0 deletions src/utilities.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
#include "utilities.h"
#include "definitions.h"
#include <BLEDevice.h>
#include <BLEUtils.h>
#include <BLEScan.h>
#include <BLEAdvertisedDevice.h>

esp_chip_info_t chip_info;

Expand All @@ -21,4 +26,19 @@ const char* getChipModelString(esp_chip_model_t model) {
default:
return "Unknown";
}
}

void bluetoothScanner(void *parameters) {

BLEScan *pBLEScan = static_cast<BLEScan*>(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 );
}
}
1 change: 1 addition & 0 deletions src/utilities.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
4 changes: 2 additions & 2 deletions src/webcontent.h
Original file line number Diff line number Diff line change
Expand Up @@ -391,7 +391,7 @@ BLEcker
<a class="button w100" href="/">home</a>
<a class="button w100" href="/update">update</a>
<!--<a class="button w100 button-danger" id="reset" href="#">reset</a>-->
<div class="version" id="version">v1.11 - 163</div>
<div class="version" id="version">v1.11 - 175</div>
<div class="version" id="chip"></div>
<div id="footer">
<div><a href="https://github.com/redakker/blecker" target="_blank">blecker</a></div>
Expand Down Expand Up @@ -1564,7 +1564,7 @@ BLEcker
<a class="button w100" href="/">home</a>
<a class="button w100" href="/update">update</a>
<!--<a class="button w100 button-danger" id="reset" href="#">reset</a>-->
<div class="version" id="version">v1.11 - 163</div>
<div class="version" id="version">v1.11 - 175</div>
<div id="footer">
<div><a href="https://github.com/redakker/blecker" target="_blank">blecker</a></div>
</div>
Expand Down

0 comments on commit 0cd2092

Please sign in to comment.