main.ino
Arduino (C++) · 170 lines
#include <WiFi.h>
#include <WebServer.h>
#include <DNSServer.h>
// =======================
// LED CONFIGURATION
// =======================
#define LED_PIN 2 // On-board LED pin (adjust if needed)
bool ledState = false; // false = OFF, true = ON
// =======================
// WIFI ACCESS POINT CONFIG
// =======================
const char* ap_ssid = "ESP32-LED"; // open AP (no password)
IPAddress apIP(192, 168, 4, 1);
IPAddress netMsk(255, 255, 255, 0);
// =======================
// DNS / CAPTIVE PORTAL
// =======================
const byte DNS_PORT = 53;
DNSServer dnsServer;
WebServer server(80);
// =======================
// LED CONTROL
// =======================
void updateLed() {
digitalWrite(LED_PIN, ledState ? HIGH : LOW);
}
// =======================
// HTML PAGE
// =======================
String htmlPage(bool currentLedState) {
String html = "<!DOCTYPE html><html><head>"
"<meta charset='UTF-8'>"
"<meta name='viewport' content='width=device-width, initial-scale=1.0'>"
"<title>ESP32 LED Control</title>"
"<style>"
"body{font-family:sans-serif;margin:20px;}"
"button{padding:10px 20px;margin:5px;font-size:16px;}"
".status{padding:10px;background:#eee;display:inline-block;margin-bottom:10px;}"
"</style>"
"</head><body>";
html += "<h2>ESP32 LED Control</h2>";
html += "<p class='status'>LED is currently: <b>";
html += currentLedState ? "ON" : "OFF";
html += "</b></p><br>";
html += "<form action='/set' method='GET' style='display:inline-block;'>";
html += "<input type='hidden' name='state' value='on'>";
html += "<button type='submit'>Turn ON</button>";
html += "</form>";
html += "<form action='/set' method='GET' style='display:inline-block;'>";
html += "<input type='hidden' name='state' value='off'>";
html += "<button type='submit'>Turn OFF</button>";
html += "</form>";
html += "<hr><p>Connect to WiFi network <b>ESP32-LED</b> (open network, no password).</p>";
html += "<p>This page should open automatically as a captive portal.</p>";
html += "</body></html>";
return html;
}
// =======================
// CAPTIVE PORTAL REDIRECT
// =======================
void redirectToRoot() {
String url = "http://" + apIP.toString() + "/";
server.sendHeader("Location", url, true);
server.send(302, "text/plain", "");
}
// =======================
// HTTP HANDLERS
// =======================
void handleRoot() {
server.send(200, "text/html", htmlPage(ledState));
}
void handleSetLed() {
if (server.hasArg("state")) {
String state = server.arg("state");
if (state == "on") {
ledState = true;
} else if (state == "off") {
ledState = false;
}
updateLed();
}
server.send(200, "text/html", htmlPage(ledState));
}
void handleNotFound() {
// Redirect any unknown path to the main page (captive portal behavior)
redirectToRoot();
}
// Captive portal triggers used by Android, iOS, Windows
void handleGenerate204() {
handleRoot();
} // /generate_204 (Android)
void handleHotspotDetect() {
handleRoot();
} // /hotspot-detect.html (iOS/macOS)
void handleNCsi() {
handleRoot();
} // /ncsi.txt (Windows)
// =======================
// SETUP
// =======================
void setup() {
Serial.begin(115200);
delay(100);
// Initialize LED
pinMode(LED_PIN, OUTPUT);
ledState = false;
updateLed();
// Start WiFi in AP mode (open network)
WiFi.mode(WIFI_AP);
WiFi.softAPConfig(apIP, apIP, netMsk);
WiFi.softAP(ap_ssid);
Serial.println();
Serial.print("AP started. SSID: ");
Serial.println(ap_ssid);
Serial.print("AP IP address: ");
Serial.println(apIP);
// Start DNS server -> redirect all domains to our AP
dnsServer.start(DNS_PORT, "*", apIP);
// Setup web server routes
server.on("/", handleRoot);
server.on("/set", handleSetLed);
// Captive portal paths
server.on("/generate_204", handleGenerate204);
server.on("/hotspot-detect.html", handleHotspotDetect);
server.on("/ncsi.txt", handleNCsi);
server.onNotFound(handleNotFound);
server.begin();
Serial.println("Web server + DNS captive portal started.");
}
// =======================
// LOOP
// =======================
void loop() {
dnsServer.processNextRequest();
server.handleClient();
}