#include <WiFi.h>#include <WebServer.h>
const char* ssid_ap = "ESP32 Hotspot"; // Name of the ESP32 hotspot
const char* password_ap = "abcd12345"; // Hotspot password
String ssid_target = ""; // Target WiFi SSID
String password_target = ""; // Target WiFi password
// Create Web Server instance
WebServer server(80); // Web server listens on port 80
// HTML form page for user to input WiFi information
const char* html_form = R"(
WiFi Configuration
");
}
}
// Handle root path, display WiFi configuration form
void handleRoot() {
server.send(200, "text/html", html_form);
}
void setup() {
Serial.begin(115200);
WiFi.mode(WIFI_AP_STA); // Set WiFi mode to STA+AP
// Set ESP32 as hotspot
WiFi.softAP(ssid_ap, password_ap);
Serial.println("ESP32 started as hotspot");
// Set up web server routes
server.on("/", HTTP_GET, handleRoot); // Display form
server.on("/save", HTTP_POST, handleWifiConfig); // Handle form submission
// Start web server
server.begin();
Serial.println("Web server started, waiting for connections...");
// If ESP32 is already connected to target WiFi, print its IP address
if (WiFi.status() == WL_CONNECTED) {
Serial.print("WiFi Connected, IP Address:");
Serial.println(WiFi.localIP());
}
}
void loop() {
// Handle web requests
server.handleClient();
// If WiFi is connected, print IP address
if (WiFi.status() == WL_CONNECTED) {
Serial.print("WiFi Connected, IP Address:");
Serial.println(WiFi.localIP());
}
}