main.ino
Arduino (C++) · 159 lines
#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>
#include <DNSServer.h>
// =======================
// LED CONFIGURATION
// =======================
// NodeMCU built-in LED is on GPIO2 (D4) and uses inverted logic
#define LED_PIN 2 // D4 (GPIO2)
bool ledState = false; // false = OFF, true = ON
// =======================
// WIFI ACCESS POINT CONFIG
// =======================
const char* ap_ssid = "ESP8266-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;
ESP8266WebServer server(80);
// =======================
// LED CONTROL
// =======================
void updateLed() {
// NodeMCU LED logic: LOW = ON, HIGH = OFF
digitalWrite(LED_PIN, ledState ? LOW : HIGH);
}
// =======================
// 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>ESP8266 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>ESP8266 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>ESP8266-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 all unknown paths to the main page (captive portal behavior)
redirectToRoot();
}
// Captive portal detection URLs
void handleGenerate204() { handleRoot(); } // Android
void handleHotspotDetect() { handleRoot(); } // iOS/macOS
void handleNCsi() { handleRoot(); } // Windows
// =======================
// SETUP
// =======================
void setup() {
Serial.begin(115200);
delay(100);
// Initialize LED
pinMode(LED_PIN, OUTPUT);
ledState = false;
updateLed();
// Start WiFi in Access Point mode
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 AP IP)
dnsServer.start(DNS_PORT, "*", apIP);
// Configure web server routes
server.on("/", handleRoot);
server.on("/set", handleSetLed);
// Captive portal routes
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();
}