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
+35 -4
View File
@@ -85,16 +85,19 @@ const BleScanResult* getBleScanResult(int index) {
} }
int scanBleDevices(uint32_t scanSeconds) { int scanBleDevices(uint32_t scanSeconds) {
Serial.print(logTimestamp());
Serial.println(" BLE: Starting scan..."); Serial.println(" BLE: Starting scan...");
pauseBmsReconnect((scanSeconds * 1000UL) + 30000UL); pauseBmsReconnect((scanSeconds * 1000UL) + 30000UL);
if (client && client->isConnected()) { if (client && client->isConnected()) {
Serial.print(logTimestamp());
Serial.println(" BLE: Disconnecting BMS before scan"); Serial.println(" BLE: Disconnecting BMS before scan");
client->disconnect(); client->disconnect();
bmsData.connected = false; bmsData.connected = false;
Serial.println("BLE: Waiting for device advertising..."); Serial.print(logTimestamp());
delay(2000); Serial.println(" BLE: Waiting 5 seconds for device advertising...");
delay(5000);
} }
bleScanResultCount = 0; bleScanResultCount = 0;
@@ -103,15 +106,42 @@ int scanBleDevices(uint32_t scanSeconds) {
scan->setActiveScan(true); scan->setActiveScan(true);
scan->setInterval(100); scan->setInterval(100);
scan->setWindow(99); scan->setWindow(99);
scan->clearResults();
Serial.print("BLE: Scanning for "); Serial.print(logTimestamp());
Serial.print(" BLE: Starting continuous scan for ");
Serial.print(scanSeconds); Serial.print(scanSeconds);
Serial.println(" seconds..."); 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(); int resultCount = results.getCount();
Serial.print(logTimestamp());
Serial.print(" BLE: Raw devices seen: "); Serial.print(" BLE: Raw devices seen: ");
Serial.println(resultCount); Serial.println(resultCount);
@@ -146,6 +176,7 @@ int scanBleDevices(uint32_t scanSeconds) {
scan->clearResults(); scan->clearResults();
Serial.print(logTimestamp());
Serial.print(" BLE: Devices found: "); Serial.print(" BLE: Devices found: ");
Serial.println(bleScanResultCount); Serial.println(bleScanResultCount);
@@ -2,18 +2,40 @@
LogLevel currentLogLevel = LOG_INFO; 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) { void setLogLevel(LogLevel level) {
currentLogLevel = level; currentLogLevel = level;
} }
void logInfo(const String& message) { void logInfo(const String& message) {
if (currentLogLevel >= LOG_INFO) { if (currentLogLevel >= LOG_INFO) {
Serial.print(logTimestamp());
Serial.print(" ");
Serial.println(message); Serial.println(message);
} }
} }
void logDebug(const String& message) { void logDebug(const String& message) {
if (currentLogLevel >= LOG_DEBUG) { if (currentLogLevel >= LOG_DEBUG) {
Serial.print(logTimestamp());
Serial.print(" ");
Serial.println(message); Serial.println(message);
} }
} }
@@ -10,6 +10,7 @@ enum LogLevel {
extern LogLevel currentLogLevel; extern LogLevel currentLogLevel;
String logTimestamp();
void setLogLevel(LogLevel level); void setLogLevel(LogLevel level);
void logInfo(const String& message); void logInfo(const String& message);
void logDebug(const String& message); void logDebug(const String& message);