// #include <WiFi.h>
// #include <WebServer.h>
// #include <SPIFFS.h> // Include the SPIFFS library
// const char* ssid = "ESP32_AP";
// const char* password = "password";
// const char* passwordFile = "/wifi_password.txt"; // File to store the WiFi password
// WebServer server(80);
// void handleRoot() {
// String content = "<form action='/connect' method='POST'>SSID:<br><input type='text' name='ssid'><br>Password:<br><input type='text' name='password'><br><input type='submit'></form>";
// content += "<br><br>Available Networks:<br>";
// int numNetworks = WiFi.scanNetworks();
// if (numNetworks == 0) {
// content += "No networks found";
// } else {
// for (int i = 0; i < numNetworks; i++) {
// content += "<input type='radio' name='ssid' value='" + WiFi.SSID(i) + "'>" + WiFi.SSID(i) + "<br>";
// }
// }
// server.send(200, "text/html", content);
// }
// void handleConnect() {
// String ssid = server.arg("ssid");
// String password = server.arg("password");
// // Store the password in a file
// File file = SPIFFS.open(passwordFile, "w");
// if (!file) {
// server.send(500, "text/plain", "Failed to save password to file");
// return;
// }
// file.println(password);
// file.close();
// WiFi.begin(ssid.c_str(), password.c_str());
// int attempts = 0;
// while (WiFi.status() != WL_CONNECTED && attempts < 20) {
// delay(500);
// attempts++;
// }
// if (WiFi.status() == WL_CONNECTED) {
// server.send(200, "text/plain", "Connected to WiFi! IP address: " + WiFi.localIP().toString());
// } else {
// server.send(200, "text/plain", "Failed to connect to WiFi!");
// }
// }
// void setup() {
// Serial.begin(115200);
// // Initialize SPIFFS
// if (!SPIFFS.begin(true)) {
// Serial.println("An error occurred while mounting SPIFFS");
// return;
// }
// WiFi.softAP(ssid, password);
// server.on("/", handleRoot);
// server.on("/connect", handleConnect);
// server.begin();
// }
// void loop() {
// server.handleClient();
// }
// #include <WiFi.h>
// #include <WebServer.h>
// #include <SPIFFS.h> // Include the SPIFFS library
// const char* ssid = "ESP32_AP";
// const char* password = "password";
// const char* passwordFile = "/wifi_password.txt"; // File to store the WiFi password
// WebServer server(80);
// void handleRoot() {
// String content = "<form action='/connect' method='POST'>SSID:<br><input type='text' name='ssid'><br>Password:<br><input type='text' name='password'><br><input type='submit'></form>";
// content += "<br><br>Available Networks:<br>";
// int numNetworks = WiFi.scanNetworks();
// if (numNetworks == 0) {
// content += "No networks found";
// } else {
// for (int i = 0; i < numNetworks; i++) {
// content += "<input type='radio' name='ssid' value='" + WiFi.SSID(i) + "'>" + WiFi.SSID(i) + "<br>";
// }
// }
// if (server.hasArg("status")) {
// String status = server.arg("status");
// content += "<br><br><b>Status:</b> " + status + "<br>";
// }
// server.send(200, "text/html", content);
// }
// void handleConnect() {
// String ssid = server.arg("ssid");
// String password = server.arg("password");
// // Store the password in a file
// File file = SPIFFS.open(passwordFile, "w");
// if (!file) {
// server.send(500, "text/plain", "Failed to save password to file");
// return;
// }
// file.println(password);
// file.close();
// WiFi.begin(ssid.c_str(), password.c_str());
// int attempts = 0;
// while (WiFi.status() != WL_CONNECTED && attempts < 20) {
// delay(500);
// attempts++;
// }
// if (WiFi.status() == WL_CONNECTED) {
// server.send(200, "text/plain", "Connected to WiFi! IP address: " + WiFi.localIP().toString());
// } else {
// server.sendHeader("Location", "/?status=Failed to connect to WiFi!", true); //Redirect to the root with a status parameter
// server.send(302, "text/plain", "");
// }
// }
// void setup() {
// Serial.begin(115200);
// // Initialize SPIFFS
// if (!SPIFFS.begin(true)) {
// Serial.println("An error occurred while mounting SPIFFS");
// return;
// }
// WiFi.softAP(ssid, password);
// server.on("/", handleRoot);
// server.on("/connect", handleConnect);
// server.begin();
// }
// void loop() {
// server.handleClient();
// }
#include <WiFi.h>
#include <WebServer.h>
#include <SPIFFS.h> // Include the SPIFFS library
const char* ssid = "ESP32_AP";
const char* password = "password";
const char* passwordFile = "/wifi_password.txt"; // File to store the WiFi password
WebServer server(80);
void setupWiFi() {
WiFi.softAP(ssid, password);
server.on("/", handleRoot);
server.on("/connect", handleConnect);
server.begin();
}
void handleRoot() {
String content = "<form action='/connect' method='POST'>SSID:<br><input type='text' name='ssid'><br>Password:<br><input type='text' name='password'><br><input type='submit'></form>";
content += "<br><br>Available Networks:<br>";
int numNetworks = WiFi.scanNetworks();
if (numNetworks == 0) {
content += "No networks found";
} else {
for (int i = 0; i < numNetworks; i++) {
content += "<input type='radio' name='ssid' value='" + WiFi.SSID(i) + "'>" + WiFi.SSID(i) + "<br>";
}
}
server.send(200, "text/html", content);
}
void handleConnect() {
String ssid = server.arg("ssid");
String password = server.arg("password");
// Store the password in a file
File file = SPIFFS.open(passwordFile, "w");
if (!file) {
server.send(500, "text/plain", "Failed to save password to file");
return;
}
file.println(password);
file.close();
WiFi.begin(ssid.c_str(), password.c_str());
int attempts = 0;
while (WiFi.status() != WL_CONNECTED && attempts < 20) {
delay(500);
attempts++;
}
if (WiFi.status() == WL_CONNECTED) {
server.send(200, "text/plain", "Connected to WiFi! IP address: " + WiFi.localIP().toString());
} else {
// Failed to connect, start the HTTP server
server.stop();
setupWiFi();
}
}
void setup() {
Serial.begin(115200);
// Initialize SPIFFS
if (!SPIFFS.begin(true)) {
Serial.println("An error occurred while mounting SPIFFS");
return;
}
// Attempt to connect to WiFi
WiFi.begin();
int attempts = 0;
while (WiFi.status() != WL_CONNECTED && attempts < 20) {
delay(500);
attempts++;
}
// If WiFi connection fails, start the HTTP server
if (WiFi.status() != WL_CONNECTED) {
setupWiFi();
}
}
void loop() {
server.handleClient();
}