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
This commit is contained in:
Michael Holzmann 2026-11-14 14:47:08 +00:00
parent 780df35131
commit 98ac623423
2 changed files with 35 additions and 11 deletions

View File

@ -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

View File

@ -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)) {
// 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);
}