forked from Akcelerometry_drgania_WMT/PI_mikrokontroler
Dodanie REST API w tle (endpoint /api/wifi) sluzacego do zdalnego dopisywania nowej sieci WiFi do pliku konfiguracyjnego w czasie normalnej pracy
This commit is contained in:
@@ -46,6 +46,7 @@ class WiFiManager {
|
||||
|
||||
void handleClient();
|
||||
void startCaptivePortal();
|
||||
void handleApiWifi();
|
||||
void handleRoot();
|
||||
void handleSave();
|
||||
void handleNotFound();
|
||||
@@ -70,6 +71,7 @@ class WiFiManager {
|
||||
private:
|
||||
bool isAccessPoint = false;
|
||||
bool captivePortalActive = false;
|
||||
bool serverActive = false;
|
||||
WebServer server{80};
|
||||
DNSServer dnsServer;
|
||||
int expectedCaptchaAnswer = 0;
|
||||
|
||||
@@ -107,6 +107,12 @@ void WiFiManager::connectToWiFi() {
|
||||
ESP_LOGI(WIFI, "%s", gatewayInfo.c_str());
|
||||
String dnsInfo = "DNS: " + WiFi.dnsIP().toString();
|
||||
ESP_LOGI(WIFI, "%s", dnsInfo.c_str());
|
||||
|
||||
// Start background API server
|
||||
server.on("/api/wifi", HTTP_POST, std::bind(&WiFiManager::handleApiWifi, this));
|
||||
server.begin();
|
||||
serverActive = true;
|
||||
ESP_LOGI(WIFI, "REST API Server started on port 80");
|
||||
} else {
|
||||
String infoWiFi = "WIFI CONNECTION ERROR: ALL FAILED";
|
||||
ESP_LOGI(WIFI, "%s", infoWiFi.c_str());
|
||||
@@ -300,10 +306,33 @@ void WiFiManager::startCaptivePortal() {
|
||||
void WiFiManager::handleClient() {
|
||||
if (captivePortalActive) {
|
||||
dnsServer.processNextRequest();
|
||||
}
|
||||
if (captivePortalActive || serverActive) {
|
||||
server.handleClient();
|
||||
}
|
||||
}
|
||||
|
||||
void WiFiManager::handleApiWifi() {
|
||||
if (!server.hasArg("ssid") || !server.hasArg("password")) {
|
||||
server.send(400, "text/plain", "Error: Missing 'ssid' or 'password' parameter.");
|
||||
return;
|
||||
}
|
||||
|
||||
String newSSID = server.arg("ssid");
|
||||
String newPass = server.arg("password");
|
||||
|
||||
File f = SD.open("/wifi.txt", FILE_APPEND);
|
||||
if (f) {
|
||||
f.println(newSSID + ";" + newPass);
|
||||
f.close();
|
||||
server.send(200, "text/plain", "Success: WiFi credentials saved to SD card.");
|
||||
ESP_LOGI(WIFI, "API received new WiFi config: %s", newSSID.c_str());
|
||||
} else {
|
||||
server.send(500, "text/plain", "Error: Cannot open wifi.txt on SD card.");
|
||||
ESP_LOGE(WIFI, "API Error: Cannot open wifi.txt");
|
||||
}
|
||||
}
|
||||
|
||||
void WiFiManager::handleRoot() {
|
||||
int bases[] = {2, 2, 2, 2, 3, 3, 10, 10};
|
||||
int vals[] = {4, 8, 16, 32, 9, 27, 100, 1000};
|
||||
|
||||
Reference in New Issue
Block a user