Add dedicated BMS setup scan mode
This commit is contained in:
@@ -19,6 +19,7 @@ static bool responseReady = false;
|
|||||||
|
|
||||||
static unsigned long lastReadAttempt = 0;
|
static unsigned long lastReadAttempt = 0;
|
||||||
static unsigned long bmsReconnectPausedUntil = 0;
|
static unsigned long bmsReconnectPausedUntil = 0;
|
||||||
|
static bool bmsSetupMode = false;
|
||||||
static const unsigned long READ_INTERVAL_MS = 5000;
|
static const unsigned long READ_INTERVAL_MS = 5000;
|
||||||
|
|
||||||
BmsData bmsData;
|
BmsData bmsData;
|
||||||
@@ -124,6 +125,45 @@ void pauseBmsReconnect(uint32_t pauseMs) {
|
|||||||
bmsReconnectPausedUntil = millis() + pauseMs;
|
bmsReconnectPausedUntil = millis() + pauseMs;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void enterBmsSetupMode() {
|
||||||
|
Serial.print(logTimestamp());
|
||||||
|
Serial.println(" BMS setup: entering setup mode");
|
||||||
|
|
||||||
|
bmsSetupMode = true;
|
||||||
|
pauseBmsReconnect(3600000UL);
|
||||||
|
|
||||||
|
if (client && client->isConnected()) {
|
||||||
|
Serial.print(logTimestamp());
|
||||||
|
Serial.println(" BMS setup: disconnecting BMS");
|
||||||
|
client->disconnect();
|
||||||
|
}
|
||||||
|
|
||||||
|
if (client) {
|
||||||
|
NimBLEDevice::deleteClient(client);
|
||||||
|
client = nullptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
notifyChar = nullptr;
|
||||||
|
writeChar = nullptr;
|
||||||
|
bmsData.connected = false;
|
||||||
|
|
||||||
|
Serial.print(logTimestamp());
|
||||||
|
Serial.println(" BMS setup: ready for BLE scans");
|
||||||
|
}
|
||||||
|
|
||||||
|
void exitBmsSetupMode() {
|
||||||
|
Serial.print(logTimestamp());
|
||||||
|
Serial.println(" BMS setup: exiting setup mode");
|
||||||
|
|
||||||
|
bmsSetupMode = false;
|
||||||
|
bmsReconnectPausedUntil = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool isBmsSetupMode() {
|
||||||
|
return bmsSetupMode;
|
||||||
|
}
|
||||||
|
|
||||||
int getBleScanResultCount() {
|
int getBleScanResultCount() {
|
||||||
return bleScanResultCount;
|
return bleScanResultCount;
|
||||||
}
|
}
|
||||||
@@ -138,61 +178,85 @@ const BleScanResult* getBleScanResult(int index) {
|
|||||||
|
|
||||||
int scanBleDevices(uint32_t scanSeconds) {
|
int scanBleDevices(uint32_t scanSeconds) {
|
||||||
Serial.print(logTimestamp());
|
Serial.print(logTimestamp());
|
||||||
Serial.println(" BLE: Starting scan...");
|
Serial.println(" BLE: Starting setup scan...");
|
||||||
pauseBmsReconnect(60000UL);
|
|
||||||
|
|
||||||
if (client && client->isConnected()) {
|
if (!bmsSetupMode) {
|
||||||
Serial.print(logTimestamp());
|
Serial.print(logTimestamp());
|
||||||
Serial.println(" BLE: Disconnecting BMS before scan");
|
Serial.println(" BLE: auto-entering BMS setup mode for scan");
|
||||||
client->disconnect();
|
enterBmsSetupMode();
|
||||||
bmsData.connected = false;
|
|
||||||
|
|
||||||
Serial.print(logTimestamp());
|
|
||||||
Serial.println(" BLE: Waiting 5 seconds for device advertising...");
|
|
||||||
delay(5000);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bleScanResultCount = 0;
|
bleScanResultCount = 0;
|
||||||
|
|
||||||
|
Serial.print(logTimestamp());
|
||||||
|
Serial.println(" BLE: waiting 5 seconds before scan");
|
||||||
|
delay(5000);
|
||||||
|
|
||||||
NimBLEScan* scan = NimBLEDevice::getScan();
|
NimBLEScan* scan = NimBLEDevice::getScan();
|
||||||
scan->setScanCallbacks(&bleScanCallbacks, false);
|
|
||||||
scan->setActiveScan(true);
|
scan->setActiveScan(true);
|
||||||
scan->setInterval(100);
|
scan->setInterval(100);
|
||||||
scan->setWindow(99);
|
scan->setWindow(99);
|
||||||
scan->clearResults();
|
scan->clearResults();
|
||||||
|
|
||||||
Serial.print(logTimestamp());
|
Serial.print(logTimestamp());
|
||||||
Serial.print(" BLE: Blocking scan requested for ");
|
Serial.print(" BLE: scanning for ");
|
||||||
Serial.print(scanSeconds);
|
Serial.print(scanSeconds);
|
||||||
Serial.println(" seconds...");
|
Serial.println(" seconds...");
|
||||||
|
|
||||||
unsigned long scanStart = millis();
|
unsigned long scanStart = millis();
|
||||||
|
|
||||||
bool started = scan->start(scanSeconds, false, true);
|
NimBLEScanResults results = scan->getResults(scanSeconds, false);
|
||||||
|
|
||||||
unsigned long scanElapsed = (millis() - scanStart) / 1000;
|
unsigned long elapsed = (millis() - scanStart) / 1000;
|
||||||
|
|
||||||
Serial.print(logTimestamp());
|
Serial.print(logTimestamp());
|
||||||
Serial.print(" BLE: scan call returned after ");
|
Serial.print(" BLE: scan returned after ");
|
||||||
Serial.print(scanElapsed);
|
Serial.print(elapsed);
|
||||||
Serial.println(" seconds");
|
Serial.println(" seconds");
|
||||||
|
|
||||||
if (!started) {
|
int resultCount = results.getCount();
|
||||||
|
|
||||||
Serial.print(logTimestamp());
|
Serial.print(logTimestamp());
|
||||||
Serial.println(" BLE: scan failed to start");
|
Serial.print(" BLE: raw devices seen: ");
|
||||||
return 0;
|
Serial.println(resultCount);
|
||||||
|
|
||||||
|
for (int i = 0; i < resultCount && bleScanResultCount < MAX_BLE_SCAN_RESULTS; i++) {
|
||||||
|
const NimBLEAdvertisedDevice* device = results.getDevice(i);
|
||||||
|
|
||||||
|
String address = device->getAddress().toString().c_str();
|
||||||
|
|
||||||
|
bool alreadySeen = false;
|
||||||
|
for (int existing = 0; existing < bleScanResultCount; existing++) {
|
||||||
|
if (bleScanResults[existing].address == address) {
|
||||||
|
alreadySeen = true;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (alreadySeen) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
String name = "";
|
||||||
|
if (device->haveName()) {
|
||||||
|
name = device->getName().c_str();
|
||||||
|
}
|
||||||
|
|
||||||
|
bleScanResults[bleScanResultCount].address = address;
|
||||||
|
bleScanResults[bleScanResultCount].name = name;
|
||||||
|
bleScanResults[bleScanResultCount].rssi = device->getRSSI();
|
||||||
|
bleScanResultCount++;
|
||||||
}
|
}
|
||||||
|
|
||||||
scan->stop();
|
|
||||||
scan->clearResults();
|
scan->clearResults();
|
||||||
|
|
||||||
Serial.print(logTimestamp());
|
Serial.print(logTimestamp());
|
||||||
Serial.print(" BLE: Devices found: ");
|
Serial.print(" BLE: devices found: ");
|
||||||
Serial.println(bleScanResultCount);
|
Serial.println(bleScanResultCount);
|
||||||
|
|
||||||
if (bleScanResultCount == 0) {
|
if (bleScanResultCount == 0) {
|
||||||
Serial.println("No BLE devices found.");
|
Serial.println("No BLE devices found.");
|
||||||
return bleScanResultCount;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
Serial.println("BLE devices:");
|
Serial.println("BLE devices:");
|
||||||
@@ -214,6 +278,7 @@ int scanBleDevices(uint32_t scanSeconds) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
Serial.println("Use: select bms <number>");
|
Serial.println("Use: select bms <number>");
|
||||||
|
Serial.println("Use: exit setup when finished");
|
||||||
|
|
||||||
return bleScanResultCount;
|
return bleScanResultCount;
|
||||||
}
|
}
|
||||||
@@ -429,6 +494,10 @@ void initBms() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void updateBms() {
|
void updateBms() {
|
||||||
|
if (bmsSetupMode) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
if ((long)(bmsReconnectPausedUntil - millis()) > 0) {
|
if ((long)(bmsReconnectPausedUntil - millis()) > 0) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -42,3 +42,6 @@ int scanBleDevices(uint32_t scanSeconds = 10);
|
|||||||
int getBleScanResultCount();
|
int getBleScanResultCount();
|
||||||
const BleScanResult* getBleScanResult(int index);
|
const BleScanResult* getBleScanResult(int index);
|
||||||
void pauseBmsReconnect(uint32_t pauseMs);
|
void pauseBmsReconnect(uint32_t pauseMs);
|
||||||
|
void enterBmsSetupMode();
|
||||||
|
void exitBmsSetupMode();
|
||||||
|
bool isBmsSetupMode();
|
||||||
|
|||||||
@@ -374,39 +374,18 @@ void handleDebugSerial() {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (command == "scan ble") {
|
if (command == "enter setup") {
|
||||||
int count = scanBleDevices(20);
|
enterBmsSetupMode();
|
||||||
|
|
||||||
if (count == 0) {
|
|
||||||
Serial.println("No BLE devices found.");
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
Serial.println("BLE devices:");
|
if (command == "exit setup") {
|
||||||
|
exitBmsSetupMode();
|
||||||
for (int i = 0; i < count; i++) {
|
return;
|
||||||
const BleScanResult* result = getBleScanResult(i);
|
|
||||||
|
|
||||||
if (!result) {
|
|
||||||
continue;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Serial.print(i + 1);
|
if (command == "scan ble") {
|
||||||
Serial.print(") ");
|
scanBleDevices(20);
|
||||||
|
|
||||||
if (result->name.length() > 0) {
|
|
||||||
Serial.print(result->name);
|
|
||||||
} else {
|
|
||||||
Serial.print("(no name)");
|
|
||||||
}
|
|
||||||
|
|
||||||
Serial.print(" | ");
|
|
||||||
Serial.print(result->address);
|
|
||||||
Serial.print(" | RSSI ");
|
|
||||||
Serial.println(result->rssi);
|
|
||||||
}
|
|
||||||
|
|
||||||
Serial.println("Use: select bms <number>");
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -442,6 +421,7 @@ void handleDebugSerial() {
|
|||||||
Serial.print(" / ");
|
Serial.print(" / ");
|
||||||
Serial.println(appConfig.bms.address);
|
Serial.println(appConfig.bms.address);
|
||||||
Serial.println("BMS will reconnect on next update cycle.");
|
Serial.println("BMS will reconnect on next update cycle.");
|
||||||
|
exitBmsSetupMode();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user