From 98ac623423a4fdf4a171a763a26123561afc4b5c Mon Sep 17 00:00:00 2001 From: Michael Holzmann Date: Sat, 14 Nov 2026 14:47:08 +0000 Subject: [PATCH] fix: stop BLE scan from starving WiFi - Scan continuously with a 50% duty cycle (interval 100ms / window 50ms) instead of a 99% window that leaves almost no airtime for WiFi - Drop the 5s scan / 10s idle loop cadence, which created regular 10s holes in BLE reception - Restart the scan only when the controller reports it as stopped - setMaxResults(0): callbacks only, no per-device allocations - Disable WiFi modem sleep and auto-reconnect on - Initialise WiFi before BLE and create the MQTT task afterwards so the two radio drivers are never brought up concurrently --- include/example.config.h | 6 ++++-- src/main.cpp | 40 +++++++++++++++++++++++++++++++--------- 2 files changed, 35 insertions(+), 11 deletions(-) diff --git a/include/example.config.h b/include/example.config.h index d56594b..f6273dc 100644 --- a/include/example.config.h +++ b/include/example.config.h @@ -31,8 +31,10 @@ // How often to publish to MQTT (milliseconds) #define PUBLISH_INTERVAL_MS 10000 -// BLE scan window (seconds) -#define BLE_SCAN_DURATION_S 5 +// BLE passive scan timing (milliseconds). Window must be <= interval. +// 50/100 = 50% duty cycle: reliable reception without starving WiFi. +#define BLE_SCAN_INTERVAL_MS 100 +#define BLE_SCAN_WINDOW_MS 50 // How often to publish the diagnostics payload (milliseconds) #define DIAG_INTERVAL_MS 60000 diff --git a/src/main.cpp b/src/main.cpp index 269dec9..76c2b65 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -39,6 +39,17 @@ static volatile bool s_bleSeen = false; #define DIAG_INTERVAL_MS 60000UL #endif +// BLE scan timing. A 99% duty cycle (window ~= interval) starves the shared +// 2.4 GHz radio and breaks WiFi; 50% is plenty to catch adverts every few +// seconds and leaves airtime for MQTT. +#ifndef BLE_SCAN_INTERVAL_MS +#define BLE_SCAN_INTERVAL_MS 100 +#endif + +#ifndef BLE_SCAN_WINDOW_MS +#define BLE_SCAN_WINDOW_MS 50 +#endif + // ── Helpers ────────────────────────────────────────────────────────────────── static bool hexToBin(const char* hex, uint8_t* out, size_t outLen) { @@ -333,9 +344,11 @@ static void bleInit() { s_scan = NimBLEDevice::getScan(); s_scan->setAdvertisedDeviceCallbacks(new VictronAdvertisedDeviceCallbacks(), true); s_scan->setActiveScan(false); - s_scan->setInterval(100); - s_scan->setWindow(99); - Serial.println("[BLE] Scanner initialised"); + s_scan->setMaxResults(0); // callbacks only, don't store devices + s_scan->setInterval(BLE_SCAN_INTERVAL_MS); + s_scan->setWindow(BLE_SCAN_WINDOW_MS); + Serial.printf("[BLE] Scanner initialised (interval %d ms, window %d ms)\n", + BLE_SCAN_INTERVAL_MS, BLE_SCAN_WINDOW_MS); } // ── Setup & Loop ───────────────────────────────────────────────────────────── @@ -351,17 +364,26 @@ void setup() { } Serial.println("[Boot] Config OK"); + // Initialise WiFi (radio/PHY) before BLE so the two drivers are never + // brought up concurrently from different cores. + WiFi.persistent(false); + WiFi.mode(WIFI_STA); + WiFi.setAutoReconnect(true); + WiFi.setSleep(false); // modem sleep adds latency and drops pings WiFi.onEvent(onWiFiEvent); - xTaskCreatePinnedToCore(mqttTask, "mqttTask", 8192, NULL, 1, NULL, 0); bleInit(); + + xTaskCreatePinnedToCore(mqttTask, "mqttTask", 8192, NULL, 1, NULL, 0); } void loop() { - Serial.println("[BLE] Scanning..."); - if (!s_scan->start(BLE_SCAN_DURATION_S, false)) { - Serial.println("[BLE] Scan start FAILED"); + // Continuous passive scan. Restart only if the controller/host stopped it. + if (!s_scan->isScanning()) { + Serial.println("[BLE] Starting continuous scan"); + if (!s_scan->start(0, nullptr, true)) { + Serial.println("[BLE] Scan start FAILED"); + } } - s_scan->clearResults(); - delay(PUBLISH_INTERVAL_MS); + delay(5000); } \ No newline at end of file