feat(client): scan+connect statemachine replaces hardcoded MAC

This commit is contained in:
eddy 2026-08-04 00:31:38 +02:00
commit a51936f819

View file

@ -97,6 +97,7 @@ class MyClientCallbacks : public NimBLEClientCallbacks {
} }
void onDisconnect(NimBLEClient *client, int reason) override { void onDisconnect(NimBLEClient *client, int reason) override {
connected = false; connected = false;
disconnectTime = millis();
relay1SendState = false; relay1SendState = false;
relay2SendState = false; relay2SendState = false;
pCmdChar = nullptr; pCmdChar = nullptr;
@ -359,25 +360,37 @@ void setup() {
if (allProvisioned) { if (allProvisioned) {
NimBLEDevice::init("SylDa_Client"); NimBLEDevice::init("SylDa_Client");
bleReady = true; bleReady = true;
ESP_LOGI(TAG, "BLE: initialized, connecting...");
serverAddress = NimBLEAddress("44:1b:f6:f4:cf:15", BLE_ADDR_PUBLIC); preferences.begin(NVS_NAMESPACE, true);
pClient = NimBLEDevice::createClient(); uint8_t addrBuf[6];
pClient->setConnectTimeout(10); size_t addrLen = preferences.getBytes(NVS_ADDR_NAME, addrBuf, 6);
pClient->setClientCallbacks(&clientCallbacks); preferences.end();
if (pClient->connect(serverAddress)) { if (addrLen == 6) {
connected = true; storedServerAddr = NimBLEAddress(addrBuf, BLE_ADDR_RANDOM);
NimBLERemoteService *pSvc = pClient->getService(SERVICE_UUID); serverAddrStored = true;
if (pSvc) pCmdChar = pSvc->getCharacteristic(CHARACTERISTIC_UUID); ESP_LOGI(TAG, "BLE: stored server address %s", storedServerAddr.toString().c_str());
ESP_LOGI(TAG, "BLE: connected!");
} else {
ESP_LOGE(TAG, "BLE: connect failed");
} }
ESP_LOGI(TAG, "BLE: initialized, will scan for server");
} }
ESP_LOGI(TAG, "Setup done."); ESP_LOGI(TAG, "Setup done.");
} }
static void startScan() {
if (scanning) return;
NimBLEScan *pScan = NimBLEDevice::getScan();
pScan->setScanCallbacks(new MyScanCallbacks());
pScan->setActiveScan(true);
pScan->setInterval(32);
pScan->setWindow(16);
serverFound = false;
scanning = true;
scanStarted = millis();
pScan->start(SCAN_DURATION_MS / 100, false);
ESP_LOGI(TAG, "BLE: scanning started");
}
void loop() { void loop() {
if (Serial.available()) { if (Serial.available()) {
static String serialBuf; static String serialBuf;
@ -551,32 +564,63 @@ void loop() {
} }
} }
static uint32_t lastConnectAttempt = 0; static uint32_t lastScanAttempt = 0;
static uint32_t statusLog = 0; static uint32_t statusLog = 0;
if (bleReady && millis() - statusLog > 2000) { if (bleReady && millis() - statusLog > 2000) {
ESP_LOGI(TAG, "BLE: connected=%d, pClient=%p, isConn=%d", ESP_LOGI(TAG, "BLE: connected=%d scanning=%d ecdh=%d serverFound=%d",
connected, (void*)pClient, pClient ? pClient->isConnected() : 0); connected, scanning, ecdhReady, serverFound);
statusLog = millis(); statusLog = millis();
} }
if (!connected && bleReady) { if (!connected && bleReady) {
if (millis() - lastConnectAttempt > SCAN_INTERVAL_MS) { if (!scanning) {
ESP_LOGI(TAG, "BLE: connecting to server..."); if (millis() - lastScanAttempt > SCAN_INTERVAL_MS) {
if (pClient == nullptr) { if (serverAddrStored) {
ESP_LOGI(TAG, "BLE: trying stored address %s", storedServerAddr.toString().c_str());
serverAddress = storedServerAddr;
if (connectToServer()) {
connected = true;
ESP_LOGI(TAG, "BLE: connected via stored address");
} else {
ESP_LOGI(TAG, "BLE: stored address failed, starting scan");
serverAddrStored = false;
startScan();
}
} else {
startScan();
}
lastScanAttempt = millis();
}
} else {
if (serverFound) {
scanning = false;
ESP_LOGI(TAG, "BLE: connecting to found server at %s", serverAddress.toString().c_str());
if (pClient != nullptr && pClient->isConnected()) {
pClient->disconnect();
delay(100);
}
pClient = NimBLEDevice::createClient(); pClient = NimBLEDevice::createClient();
pClient->setConnectTimeout(10); pClient->setConnectTimeout(10);
pClient->setClientCallbacks(&clientCallbacks); pClient->setClientCallbacks(&clientCallbacks);
} if (connectToServer()) {
if (pClient->connect(serverAddress)) {
connected = true; connected = true;
NimBLERemoteService *pSvc = pClient->getService(SERVICE_UUID); uint8_t addrBuf[6];
if (pSvc) pCmdChar = pSvc->getCharacteristic(CHARACTERISTIC_UUID); memcpy(addrBuf, serverAddress.getVal(), 6);
ESP_LOGI(TAG, "BLE: connected!"); preferences.begin(NVS_NAMESPACE, false);
preferences.putBytes(NVS_ADDR_NAME, addrBuf, 6);
preferences.end();
storedServerAddr = serverAddress;
serverAddrStored = true;
ESP_LOGI(TAG, "BLE: server address stored in NVS");
} else { } else {
ESP_LOGW(TAG, "BLE: connect failed"); ESP_LOGW(TAG, "BLE: connectToServer failed");
}
} else if (millis() - scanStarted > SCAN_DURATION_MS) {
NimBLEDevice::getScan()->stop();
scanning = false;
ESP_LOGI(TAG, "BLE: scan timeout, no server found");
} }
lastConnectAttempt = millis();
} }
} }