diff --git a/include/APIClient.h b/include/APIClient.h index a0a2c2b..b4a89c4 100644 --- a/include/APIClient.h +++ b/include/APIClient.h @@ -14,6 +14,7 @@ public: APIClient(); bool uploadMeasurement(const String& filePath); + bool fetchWiFiConfig(); }; #endif diff --git a/src/APIClient.cpp b/src/APIClient.cpp index eb888fa..9ba373a 100644 --- a/src/APIClient.cpp +++ b/src/APIClient.cpp @@ -126,3 +126,78 @@ bool APIClient::uploadMeasurement(const String &filePath) { return false; } + +bool APIClient::fetchWiFiConfig() { + if (WiFi.status() != WL_CONNECTED) { + return false; + } + + HTTPClient http; + String url = String(config.restURL) + ":" + String(config.restPort) + "/config/wifi?sn=" + String(config.restUser); + + ESP_LOGI(TAG_API, "Fetching WiFi config from: %s", url.c_str()); + + http.begin(url); + http.setAuthorization(config.restUser, config.restPass); + + int httpCode = http.GET(); + if (httpCode == HTTP_CODE_OK || httpCode == 200) { + String payload = http.getString(); + ESP_LOGI(TAG_API, "Received config payload: %s", payload.c_str()); + + // Proste wyciąganie danych z JSON bez ArduinoJson (oszczędność pamięci) + // Oczekiwany format: {"ssid": "MojaSiec", "password": "MojeHaslo"} + int ssidStart = payload.indexOf("\"ssid\""); + int passStart = payload.indexOf("\"password\""); + + if (ssidStart >= 0 && passStart >= 0) { + int ssidValStart = payload.indexOf("\"", ssidStart + 6); // pomiń "ssid" + ssidValStart = payload.indexOf("\"", ssidValStart + 1) + 1; // znajdź początek wartości + int ssidValEnd = payload.indexOf("\"", ssidValStart); + String newSSID = payload.substring(ssidValStart, ssidValEnd); + + int passValStart = payload.indexOf("\"", passStart + 10); + passValStart = payload.indexOf("\"", passValStart + 1) + 1; + int passValEnd = payload.indexOf("\"", passValStart); + String newPass = payload.substring(passValStart, passValEnd); + + if (newSSID.length() > 0) { + bool alreadyExists = false; + File fr = SD.open("/wifi.txt", FILE_READ); + if (fr) { + while(fr.available()) { + String line = fr.readStringUntil('\n'); + line.trim(); + if (line == (newSSID + ";" + newPass)) { + alreadyExists = true; + break; + } + } + fr.close(); + } + + if (!alreadyExists) { + File fw = SD.open("/wifi.txt", FILE_APPEND); + if (fw) { + fw.println(newSSID + ";" + newPass); + fw.close(); + ESP_LOGI(TAG_API, "Saved new WiFi config to SD: %s", newSSID.c_str()); + http.end(); + return true; + } + } else { + ESP_LOGI(TAG_API, "WiFi config already exists, skipping."); + } + } + } else { + ESP_LOGE(TAG_API, "Invalid JSON format received (missing ssid/password keys)"); + } + } else if (httpCode == 404) { + ESP_LOGI(TAG_API, "No new WiFi config available (HTTP 404)"); + } else { + ESP_LOGE(TAG_API, "fetchWiFiConfig failed, HTTP code: %d", httpCode); + } + + http.end(); + return false; +} diff --git a/src/main.cpp b/src/main.cpp index 8836b87..596d40d 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -55,6 +55,7 @@ Thread wifiTestThread = Thread(); // Cykliczny test WiFi Thread offlineThread = Thread(); // Jeśli offline Thread measureThread = Thread(); // Pomiar i zapis na SD Thread uploadThread = Thread(); // Background upload +Thread configSyncThread = Thread(); // Polling WiFi config z API //////// PROTOTYPY ///////////////// void setup(); @@ -229,6 +230,9 @@ void setup() { if(config.connect){ uploadThread.onRun([]() { uploadManager.processPendingUploads(); }); uploadThread.setInterval(60000); // Co 1 minutę sprawdzaj zaległe + + configSyncThread.onRun([]() { apiClient.fetchWiFiConfig(); }); + configSyncThread.setInterval(60000); // Odpytywanie o WiFi też co minutę } // Dodanie taska loop do WDT @@ -450,10 +454,10 @@ void loop() { if(wifiTestThread.shouldRun() && (config.connect)) wifiTestThread.run(); - if(uploadThread.shouldRun() && config.connect) + if(config.connect){ uploadThread.run(); - - if(offlineThread.shouldRun() && (!config.connect)){ + configSyncThread.run(); + } if(offlineThread.shouldRun() && (!config.connect)){ offlineThread.run(); }