Skip to content

Commit

Permalink
don't save wrong password
Browse files Browse the repository at this point in the history
  • Loading branch information
yuichiroaoki committed Oct 28, 2023
1 parent b09c91e commit 4b069c1
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 10 deletions.
28 changes: 20 additions & 8 deletions lib/ConfigManager/ConfigManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ void startAPServer()
server.on("/", HTTP_GET, handleRoot);
server.on("/connect", HTTP_GET, handleConnect);
server.on("/config", HTTP_POST, handleConfig);

server.begin();
}

Expand Down Expand Up @@ -128,7 +127,12 @@ void handleConfig()
String ssid = server.arg("ssid");
String password = server.arg("password");

saveWiFiCredentials(ssid, password);
// Send message to client
String message = htmlHeader;
message += "<h2>Connecting to Wi-Fi...</h2>";
message += "<h2>Wi-Fiに接続しています...</h2>";
message += htmlFooter;
server.send(200, "text/html", message);

// Switch to Station mode and connect to the user's Wi-Fi network
WiFi.softAPdisconnect();
Expand All @@ -144,6 +148,7 @@ void handleConfig()
startAPServer();
return;
} else {
saveWiFiCredentials(ssid, password);
Serial.println("Connected to the WiFi network");
server.close();
}
Expand Down Expand Up @@ -171,20 +176,27 @@ bool isThresholdValid(int threshold)

void scanNetworks() {
int n = WiFi.scanNetworks();
scanResults += "<h2>Select Wi-Fi Network</h2>";
scanResults += "<h2>Wi-Fiを選択してください</h2>";
for (int i = 0; i < n; i++) {
scanResults += "<p><a href='/connect?ssid=" + WiFi.SSID(i) + "'>" + WiFi.SSID(i) + "</a></p>";
Serial.println("n: " + String(n));
if (n <= 0) {
scanResults += "<h2>No Wi-Fi Networks found</h2>";
scanResults += "<h2>Wi-Fiが見つかりませんでした</h2>";
} else {
scanResults += "<h2>Select Wi-Fi Network</h2>";
scanResults += "<h2>Wi-Fiを選択してください</h2>";
for (int i = 0; i < n; i++) {
scanResults += "<p><a href='/connect?ssid=" + WiFi.SSID(i) + "'>" + WiFi.SSID(i) + "</a></p>";
}
}
scanCompleted = true;
WiFi.scanDelete();
scanCompleted = true;
}

bool checkIfScanCompleted() {
return scanCompleted;
}

void resetWifiCredentials() {
void resetWifiCredentialsWithWs() {
preferences.begin("credentials", false);
preferences.clear();
// disconnect from Wi-Fi
WiFi.disconnect(true);
Expand Down
2 changes: 1 addition & 1 deletion lib/ConfigManager/ConfigManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@ bool isIntervalValid(int interval);
bool isThresholdValid(int threshold);
void scanNetworks();
bool checkIfScanCompleted();
void resetWifiCredentials();
void resetWifiCredentialsWithWs();
2 changes: 1 addition & 1 deletion src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ void webSocketEvent(uint8_t num, WStype_t type, uint8_t *payload, size_t length)
esp_deep_sleep_start();
} else if (strcmp(command, "resetWifi") == 0) {
Serial.println("Resetting Wi-Fi credentials");
resetWifiCredentials();
resetWifiCredentialsWithWs();
}
}

Expand Down

0 comments on commit 4b069c1

Please sign in to comment.