Add timestamps and fix BLE scan timing

This commit is contained in:
2026-06-04 02:24:28 -06:00
parent d39e308cda
commit 25d9152fe0
3 changed files with 62 additions and 8 deletions
+39 -8
View File
@@ -85,16 +85,19 @@ const BleScanResult* getBleScanResult(int index) {
}
int scanBleDevices(uint32_t scanSeconds) {
Serial.println("BLE: Starting scan...");
Serial.print(logTimestamp());
Serial.println(" BLE: Starting scan...");
pauseBmsReconnect((scanSeconds * 1000UL) + 30000UL);
if (client && client->isConnected()) {
Serial.println("BLE: Disconnecting BMS before scan");
Serial.print(logTimestamp());
Serial.println(" BLE: Disconnecting BMS before scan");
client->disconnect();
bmsData.connected = false;
Serial.println("BLE: Waiting for device advertising...");
delay(2000);
Serial.print(logTimestamp());
Serial.println(" BLE: Waiting 5 seconds for device advertising...");
delay(5000);
}
bleScanResultCount = 0;
@@ -103,16 +106,43 @@ int scanBleDevices(uint32_t scanSeconds) {
scan->setActiveScan(true);
scan->setInterval(100);
scan->setWindow(99);
scan->clearResults();
Serial.print("BLE: Scanning for ");
Serial.print(logTimestamp());
Serial.print(" BLE: Starting continuous scan for ");
Serial.print(scanSeconds);
Serial.println(" seconds...");
NimBLEScanResults results = scan->getResults(scanSeconds, false);
scan->start(scanSeconds, false, false);
unsigned long startMs = millis();
unsigned long durationMs = scanSeconds * 1000UL;
unsigned long lastProgressMs = 0;
while (millis() - startMs < durationMs) {
unsigned long elapsed = millis() - startMs;
if (elapsed - lastProgressMs >= 1000) {
lastProgressMs = elapsed;
Serial.print(logTimestamp());
Serial.print(" BLE: scanning... ");
Serial.print(elapsed / 1000);
Serial.print("/");
Serial.print(scanSeconds);
Serial.println(" sec");
}
delay(50);
}
scan->stop();
NimBLEScanResults results = scan->getResults();
int resultCount = results.getCount();
Serial.print("BLE: Raw devices seen: ");
Serial.print(logTimestamp());
Serial.print(" BLE: Raw devices seen: ");
Serial.println(resultCount);
for (int i = 0; i < resultCount && bleScanResultCount < MAX_BLE_SCAN_RESULTS; i++) {
@@ -146,7 +176,8 @@ int scanBleDevices(uint32_t scanSeconds) {
scan->clearResults();
Serial.print("BLE: Devices found: ");
Serial.print(logTimestamp());
Serial.print(" BLE: Devices found: ");
Serial.println(bleScanResultCount);
if (bleScanResultCount == 0) {
@@ -2,18 +2,40 @@
LogLevel currentLogLevel = LOG_INFO;
String logTimestamp() {
unsigned long seconds = millis() / 1000;
unsigned long minutes = seconds / 60;
unsigned long hours = minutes / 60;
char buffer[16];
snprintf(
buffer,
sizeof(buffer),
"[%02lu:%02lu:%02lu]",
hours % 100,
minutes % 60,
seconds % 60
);
return String(buffer);
}
void setLogLevel(LogLevel level) {
currentLogLevel = level;
}
void logInfo(const String& message) {
if (currentLogLevel >= LOG_INFO) {
Serial.print(logTimestamp());
Serial.print(" ");
Serial.println(message);
}
}
void logDebug(const String& message) {
if (currentLogLevel >= LOG_DEBUG) {
Serial.print(logTimestamp());
Serial.print(" ");
Serial.println(message);
}
}
@@ -10,6 +10,7 @@ enum LogLevel {
extern LogLevel currentLogLevel;
String logTimestamp();
void setLogLevel(LogLevel level);
void logInfo(const String& message);
void logDebug(const String& message);