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:
parent
780df35131
commit
98ac623423
|
|
@ -31,8 +31,10 @@
|
||||||
// How often to publish to MQTT (milliseconds)
|
// How often to publish to MQTT (milliseconds)
|
||||||
#define PUBLISH_INTERVAL_MS 10000
|
#define PUBLISH_INTERVAL_MS 10000
|
||||||
|
|
||||||
// BLE scan window (seconds)
|
// BLE passive scan timing (milliseconds). Window must be <= interval.
|
||||||
#define BLE_SCAN_DURATION_S 5
|
// 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)
|
// How often to publish the diagnostics payload (milliseconds)
|
||||||
#define DIAG_INTERVAL_MS 60000
|
#define DIAG_INTERVAL_MS 60000
|
||||||
|
|
|
||||||
38
src/main.cpp
38
src/main.cpp
|
|
@ -39,6 +39,17 @@ static volatile bool s_bleSeen = false;
|
||||||
#define DIAG_INTERVAL_MS 60000UL
|
#define DIAG_INTERVAL_MS 60000UL
|
||||||
#endif
|
#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 ──────────────────────────────────────────────────────────────────
|
// ── Helpers ──────────────────────────────────────────────────────────────────
|
||||||
|
|
||||||
static bool hexToBin(const char* hex, uint8_t* out, size_t outLen) {
|
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 = NimBLEDevice::getScan();
|
||||||
s_scan->setAdvertisedDeviceCallbacks(new VictronAdvertisedDeviceCallbacks(), true);
|
s_scan->setAdvertisedDeviceCallbacks(new VictronAdvertisedDeviceCallbacks(), true);
|
||||||
s_scan->setActiveScan(false);
|
s_scan->setActiveScan(false);
|
||||||
s_scan->setInterval(100);
|
s_scan->setMaxResults(0); // callbacks only, don't store devices
|
||||||
s_scan->setWindow(99);
|
s_scan->setInterval(BLE_SCAN_INTERVAL_MS);
|
||||||
Serial.println("[BLE] Scanner initialised");
|
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 ─────────────────────────────────────────────────────────────
|
// ── Setup & Loop ─────────────────────────────────────────────────────────────
|
||||||
|
|
@ -351,17 +364,26 @@ void setup() {
|
||||||
}
|
}
|
||||||
Serial.println("[Boot] Config OK");
|
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);
|
WiFi.onEvent(onWiFiEvent);
|
||||||
|
|
||||||
xTaskCreatePinnedToCore(mqttTask, "mqttTask", 8192, NULL, 1, NULL, 0);
|
|
||||||
bleInit();
|
bleInit();
|
||||||
|
|
||||||
|
xTaskCreatePinnedToCore(mqttTask, "mqttTask", 8192, NULL, 1, NULL, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
void loop() {
|
void loop() {
|
||||||
Serial.println("[BLE] Scanning...");
|
// Continuous passive scan. Restart only if the controller/host stopped it.
|
||||||
if (!s_scan->start(BLE_SCAN_DURATION_S, false)) {
|
if (!s_scan->isScanning()) {
|
||||||
|
Serial.println("[BLE] Starting continuous scan");
|
||||||
|
if (!s_scan->start(0, nullptr, true)) {
|
||||||
Serial.println("[BLE] Scan start FAILED");
|
Serial.println("[BLE] Scan start FAILED");
|
||||||
}
|
}
|
||||||
s_scan->clearResults();
|
}
|
||||||
delay(PUBLISH_INTERVAL_MS);
|
delay(5000);
|
||||||
}
|
}
|
||||||
Loading…
Reference in New Issue