Add BMS reconnect backoff

This commit is contained in:
2026-06-04 17:05:16 -06:00
parent aa0bc50acb
commit fa96a327a6
+56 -21
View File
@@ -14,6 +14,14 @@ static NimBLEClient* client = nullptr;
static NimBLERemoteCharacteristic* notifyChar = nullptr; static NimBLERemoteCharacteristic* notifyChar = nullptr;
static NimBLERemoteCharacteristic* writeChar = nullptr; static NimBLERemoteCharacteristic* writeChar = nullptr;
static unsigned long lastBmsAttemptMs = 0;
static unsigned long bmsRetryIntervalMs = 30000;
static const unsigned long BMS_RETRY_INTERVAL_CONNECTED_MS = 5000;
static const unsigned long BMS_RETRY_INTERVAL_FAILED_MS = 30000;
static const unsigned long BMS_RETRY_INTERVAL_LONG_FAIL_MS = 60000;
static int consecutiveBmsFailures = 0;
static uint8_t responseBuffer[128]; static uint8_t responseBuffer[128];
static size_t responseLength = 0; static size_t responseLength = 0;
static bool responseReady = false; static bool responseReady = false;
@@ -503,31 +511,58 @@ void initBms() {
} }
void updateBms() { void updateBms() {
if (bmsSetupMode) { if (!appConfig.bms.enabled || appConfig.bms.address.length() == 0) {
bmsData.connected = false;
return; return;
} }
if ((long)(bmsReconnectPausedUntil - millis()) > 0) { unsigned long now = millis();
if (!client || !client->isConnected()) {
bmsData.connected = false;
if (now - lastBmsAttemptMs < bmsRetryIntervalMs) {
return;
}
lastBmsAttemptMs = now;
if (!connectBms()) {
consecutiveBmsFailures++;
if (consecutiveBmsFailures >= 3) {
bmsRetryIntervalMs = BMS_RETRY_INTERVAL_LONG_FAIL_MS;
} else {
bmsRetryIntervalMs = BMS_RETRY_INTERVAL_FAILED_MS;
}
return;
}
consecutiveBmsFailures = 0;
bmsRetryIntervalMs = BMS_RETRY_INTERVAL_CONNECTED_MS;
}
if (!requestBmsData()) {
bmsData.connected = false;
consecutiveBmsFailures++;
if (client) {
client->disconnect();
}
if (consecutiveBmsFailures >= 3) {
bmsRetryIntervalMs = BMS_RETRY_INTERVAL_LONG_FAIL_MS;
} else {
bmsRetryIntervalMs = BMS_RETRY_INTERVAL_FAILED_MS;
}
lastBmsAttemptMs = now;
return; return;
} }
if (millis() - lastReadAttempt < READ_INTERVAL_MS) { consecutiveBmsFailures = 0;
return; bmsRetryIntervalMs = BMS_RETRY_INTERVAL_CONNECTED_MS;
} bmsData.connected = true;
lastReadAttempt = millis();
if (!connectBms()) {
return;
}
if (requestPacket(JBD_STATUS_REQUEST, sizeof(JBD_STATUS_REQUEST))) {
parseBasicInfo();
}
delay(100);
if (requestPacket(JBD_CELL_REQUEST, sizeof(JBD_CELL_REQUEST))) {
parseCellVoltages();
}
} }