diff --git a/lib/ConfigManager/ConfigManager.cpp b/lib/ConfigManager/ConfigManager.cpp index 6711e9b..5e6731d 100644 --- a/lib/ConfigManager/ConfigManager.cpp +++ b/lib/ConfigManager/ConfigManager.cpp @@ -9,10 +9,16 @@ const char *apPassword = "opencmm1sensor"; const char *ssidKey = "wifi_ssid"; const char *passwordKey = "wifi_password"; +boolean scanCompleted = false; +String scanResults = ""; + IPAddress local_ip(192, 168, 1, 1); IPAddress gateway(192, 168, 1, 1); IPAddress subnet(255, 255, 255, 0); +const char *htmlHeader = "
OpenCMM WiFI Settings
"; +const char *htmlFooter = ""; + void checkWifiInfo() { preferences.begin("credentials", false); @@ -46,7 +52,6 @@ void startAPServer() server.on("/", HTTP_GET, handleRoot); server.on("/connect", HTTP_GET, handleConnect); server.on("/config", HTTP_POST, handleConfig); - server.begin(); } @@ -64,49 +69,39 @@ void connectToWiFi() // Attempt to connect to Wi-Fi WiFi.begin(ssid.c_str(), password.c_str()); Serial.print("Connecting to WiFi .."); - while (WiFi.status() != WL_CONNECTED) - { + + int i = 0; + while (WiFi.status() != WL_CONNECTED && i++ < 5) { delay(1000); Serial.print('.'); } + } else { + Serial.println("No Wi-Fi credentials found."); + } + + if (WiFi.status() == WL_CONNECTED) { Serial.println("Successfully connected to WiFi."); Serial.println(WiFi.localIP()); - } - else - { - Serial.println("No Wi-Fi credentials found."); + } else { + Serial.println("Could not connect to WiFi."); + startAPServer(); } } void handleRoot() { - String html = ""; - int n = WiFi.scanNetworks(); - Serial.println("Scan done"); - - if (n == 0) - { - Serial.println("no networks found"); - html += "

No Wi-Fi Networks Found

"; - html += "

Refresh the page to scan again.

"; - } - else - { - Serial.print(n); - Serial.println(" networks found"); - delay(10); - html += "

Select SSID

"; - - for (int i = 0; i < n; i++) - { - html += "

" + WiFi.SSID(i) + "

"; - } + String html = htmlHeader; + if (scanCompleted) { + html += scanResults; + scanResults = ""; // Clear the results + scanCompleted = false; + } else { + html += "

Scanning for Wi-Fi Networks...

"; + html += "

周辺のWifiの情報を取得しています...

"; } - html += ""; - - Serial.println(""); - WiFi.scanDelete(); + html += htmlFooter; + Serial.println("Sending Wi-Fi scan results"); server.send(200, "text/html", html); } @@ -114,15 +109,16 @@ void handleConnect() { String ssid = server.arg("ssid"); Serial.println("ssid: " + ssid); - String html = ""; + String html = htmlHeader; html += "

Enter Wi-Fi Credentials

"; + html += "

Wi-Fiのパスワードを入力してください

"; html += "

SSID: " + ssid + "

"; html += "
"; html += ""; html += "Password:
"; - html += ""; + html += ""; html += "
"; - html += ""; + html += htmlFooter; server.send(200, "text/html", html); } @@ -131,23 +127,31 @@ void handleConfig() String ssid = server.arg("ssid"); String password = server.arg("password"); - saveWiFiCredentials(ssid, password); + // Send message to client + String message = htmlHeader; + message += "

Connecting to Wi-Fi...

"; + message += "

Wi-Fiに接続しています...

"; + message += htmlFooter; + server.send(200, "text/html", message); // Switch to Station mode and connect to the user's Wi-Fi network WiFi.softAPdisconnect(); WiFi.begin(ssid.c_str(), password.c_str()); - while (WiFi.status() != WL_CONNECTED) - { + int i = 0; + while (WiFi.status() != WL_CONNECTED && i++ < 5) { delay(1000); Serial.println("Connecting to WiFi..."); } - Serial.println("Connected to the WiFi network"); - server.close(); - - // Redirect to a success page or any other action you want - server.sendHeader("Location", "/"); - server.send(302, "text/plain", "credentials saved"); + if (WiFi.status() != WL_CONNECTED) { + Serial.println("Failed to connect to WiFi."); + startAPServer(); + return; + } else { + saveWiFiCredentials(ssid, password); + Serial.println("Connected to the WiFi network"); + server.close(); + } } void saveWiFiCredentials(String ssid, String password) @@ -168,4 +172,33 @@ bool isIntervalValid(int interval) bool isThresholdValid(int threshold) { return threshold >= 1 && threshold <= 20000; +} + +void scanNetworks() { + int n = WiFi.scanNetworks(); + Serial.println("n: " + String(n)); + if (n <= 0) { + scanResults += "

No Wi-Fi Networks found

"; + scanResults += "

Wi-Fiが見つかりませんでした

"; + } else { + scanResults += "

Select Wi-Fi Network

"; + scanResults += "

Wi-Fiを選択してください

"; + for (int i = 0; i < n; i++) { + scanResults += "

" + WiFi.SSID(i) + "

"; + } + } + WiFi.scanDelete(); + scanCompleted = true; +} + +bool checkIfScanCompleted() { + return scanCompleted; +} + +void resetWifiCredentialsWithWs() { + preferences.begin("credentials", false); + preferences.clear(); + // disconnect from Wi-Fi + WiFi.disconnect(true); + startAPServer(); } \ No newline at end of file diff --git a/lib/ConfigManager/ConfigManager.h b/lib/ConfigManager/ConfigManager.h index 5978e7d..288ffc5 100644 --- a/lib/ConfigManager/ConfigManager.h +++ b/lib/ConfigManager/ConfigManager.h @@ -11,4 +11,7 @@ void handleConfig(); void handleConnect(); void connectToWiFi(); bool isIntervalValid(int interval); -bool isThresholdValid(int threshold); \ No newline at end of file +bool isThresholdValid(int threshold); +void scanNetworks(); +bool checkIfScanCompleted(); +void resetWifiCredentialsWithWs(); \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index 15c4bb4..9e27647 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -3,6 +3,7 @@ #include #include #include +#include #include void ledBlink(); @@ -11,6 +12,7 @@ const int touchPin = 4; // GPIO4 as the touch-sensitive pin int sensorData = 0; int interval = 1000; // 1 second int threshold = 100; +const char* hostname = "opencmm"; WebSocketsServer webSocket(81); @@ -67,6 +69,9 @@ void webSocketEvent(uint8_t num, WStype_t type, uint8_t *payload, size_t length) switchSensor(false); delay(1000); esp_deep_sleep_start(); + } else if (strcmp(command, "resetWifi") == 0) { + Serial.println("Resetting Wi-Fi credentials"); + resetWifiCredentialsWithWs(); } } @@ -89,7 +94,7 @@ void setup() { Serial.begin(115200); checkWifiInfo(); - + MDNS.begin(hostname); setupSensor(); touchAttachInterrupt(touchPin, touchCallback, 40); // Attach touch interrupt @@ -124,11 +129,12 @@ void loop() webSocket.broadcastTXT(dataStr.c_str(), dataStr.length()); delay(interval); } - } - else - { + } else { runServer(); delay(1000); + if (!checkIfScanCompleted()) { + scanNetworks(); + } } } @@ -138,4 +144,4 @@ void ledBlink() digitalWrite(LED_BUILTIN, HIGH); delay(500); digitalWrite(LED_BUILTIN, LOW); -} \ No newline at end of file +}