main.ino

Arduino (C++) · 150 lines

#include <WiFi.h>
#include <WebServer.h>
#include <DNSServer.h>

// =======================
// WIFI ACCESS POINT CONFIG
// =======================

// Open WiFi network (no password)
const char* ap_ssid = "ESP32-PORTAL";

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);

// =======================
// APP STATE (simple web notepad)
// =======================

// Text stored on the ESP32 and shown on the page
String storedText = "Hello from ESP32!";

// =======================
// HTML PAGE
// =======================

String htmlPage(const String& currentText) {
  String html =
    "<!DOCTYPE html><html><head>"
    "<meta charset='UTF-8'>"
    "<meta name='viewport' content='width=device-width, initial-scale=1.0'>"
    "<title>ESP32 Captive Portal</title>"
    "<style>"
    "body{font-family:sans-serif;margin:20px;}"
    "textarea{width:100%;height:120px;padding:8px;box-sizing:border-box;}"
    "input[type=submit]{padding:8px 16px;margin-top:8px;}"
    "pre{background:#eee;padding:8px;white-space:pre-wrap;word-wrap:break-word;}"
    "</style>"
    "</head><body>";

  html += "<h2>ESP32 Captive Portal Notepad</h2>";
  html += "<p>Current stored text:</p>";
  html += "<pre>" + currentText + "</pre>";

  html += "<form action='/save' method='POST'>";
  html += "<label>New text to store:</label><br>";
  html += "<textarea name='msg' placeholder='Type something...'></textarea><br>";
  html += "<input type='submit' value='Save text'>";
  html += "</form>";

  html += "<hr>";
  html += "<p>Connect to WiFi network <b>ESP32-PORTAL</b> (no password).</p>";
  html += "<p>Your phone/computer should open this page 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(storedText));
}

void handleSave() {
  if (server.hasArg("msg")) {
    storedText = server.arg("msg");
    if (storedText.length() == 0) {
      storedText = "(empty)";
    }
  }
  server.send(200, "text/html", htmlPage(storedText));
}

void handleNotFound() {
  // For any unknown path, redirect to root – typical 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);

  Serial.println();
  Serial.println("Starting ESP32 Captive Portal Notepad...");

  // Start WiFi in AP mode (open network)
  WiFi.mode(WIFI_AP);
  WiFi.softAPConfig(apIP, apIP, netMsk);
  WiFi.softAP(ap_ssid);

  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 IP
  dnsServer.start(DNS_PORT, "*", apIP);

  // HTTP routes
  server.on("/", HTTP_GET, handleRoot);
  server.on("/save", HTTP_POST, handleSave);

  // Captive portal detection URLs
  server.on("/generate_204", HTTP_GET, handleGenerate204);
  server.on("/hotspot-detect.html", HTTP_GET, handleHotspotDetect);
  server.on("/ncsi.txt", HTTP_GET, handleNCsi);

  server.onNotFound(handleNotFound);

  server.begin();
  Serial.println("Web server + DNS captive portal started.");
}

// =======================
// LOOP
// =======================

void loop() {
  dnsServer.processNextRequest();
  server.handleClient();
}