# main.ino

- Project: [XIAO-WIFI-BLINK-AP](https://flashesp.com/explore/3KE3rP1yynVxNA9HTntGY.md) by FlashESP
- Board: XIAO_ESP32S3
- Language: Arduino (C++)
- Web page: https://flashesp.com/explore/3KE3rP1yynVxNA9HTntGY/files/MDpexVEnkgVLlZVF8KTBV

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

// ===== Configuration =====
const char* AP_SSID = "XIAO-AP-BLINK";   // Open AP, no password
const int LED_PIN = 21;                  // Built-in user LED on XIAO ESP32S3 (active LOW)

WebServer server(80);
DNSServer dnsServer;
const byte DNS_PORT = 53;

// LED is active LOW, so we store a logical state and map it
bool ledOn = false;
unsigned long lastBlink = 0;
const unsigned long blinkInterval = 500; // ms

void applyLedState() {
  // active LOW: LOW = ON, HIGH = OFF
  digitalWrite(LED_PIN, ledOn ? LOW : HIGH);
}

// HTML page with live status via JavaScript polling `/state`
String htmlPage() {
  String html = R"HTML(
<!DOCTYPE html>
<html>
<head>
  <meta charset="UTF-8">
  <title>XIAO WiFi Blink AP</title>
  <meta name="viewport" content="width=device-width,initial-scale=1">
  <style>
    body {
      font-family: sans-serif;
      background: #111;
      color: #eee;
      text-align: center;
      margin: 0;
      padding: 2rem;
    }
    h1 { margin-bottom: 0.5rem; }
    .state {
      font-size: 2rem;
      margin: 1.5rem 0;
    }
    .indicator {
      display: inline-block;
      width: 1.5rem;
      height: 1.5rem;
      border-radius: 50%;
      margin-left: 0.5rem;
      background: #555;
    }
    .on  { background: #2ecc71; }
    .off { background: #e74c3c; }
    .ap {
      opacity: 0.8;
      margin-top: 1rem;
    }
  </style>
</head>
<body>
  <h1>XIAO WiFi Blink AP</h1>
  <p class="ap">You are connected to an open AP hosted by the board.</p>
  <div class="state">
    LED is currently: <b id="ledState">UNKNOWN</b>
    <span id="ledIndicator" class="indicator"></span>
  </div>
  <p>Blinking is automatic (fixed 500 ms interval).</p>

  <script>
    async function updateState() {
      try {
        const res = await fetch('/state');
        if (!res.ok) return;
        const data = await res.json();
        const ledState = data.led === 'ON' ? 'ON' : 'OFF';
        document.getElementById('ledState').textContent = ledState;

        const ind = document.getElementById('ledIndicator');
        ind.classList.remove('on', 'off');
        ind.classList.add(ledState === 'ON' ? 'on' : 'off');
      } catch (e) {
        // ignore errors (e.g. connection lost)
      }
    }

    // Poll state 4 times per second
    setInterval(updateState, 250);
    // Initial call
    updateState();
  </script>
</body>
</html>
)HTML";

  return html;
}

// HTTP handlers
void handleRoot() {
  server.send(200, "text/html", htmlPage());
}

// JSON endpoint for live LED status
void handleState() {
  String json = String("{\"led\":\"") + (ledOn ? "ON" : "OFF") + "\"}";
  server.send(200, "application/json", json);
}

// Captive-portal style redirect for unknown paths
void handleNotFound() {
  String redirectUrl = "http://" + WiFi.softAPIP().toString() + "/";
  server.sendHeader("Location", redirectUrl, true);
  server.send(302, "text/plain", "");
}

void setup() {
  pinMode(LED_PIN, OUTPUT);
  ledOn = false;
  applyLedState();

  Serial.begin(115200);
  delay(1000);

  // WiFi AP mode, open network (no password)
  WiFi.mode(WIFI_AP);
  WiFi.softAP(AP_SSID);
  IPAddress apIP = WiFi.softAPIP();

  Serial.println();
  Serial.println("XIAO WiFi Blink AP (Live + Captive Portal)");
  Serial.print("AP SSID: ");
  Serial.println(AP_SSID);
  Serial.print("AP IP address: ");
  Serial.println(apIP);

  // Start DNS server for captive portal (redirect all domains to our AP IP)
  dnsServer.start(DNS_PORT, "*", apIP);

  // Web server routes
  server.on("/", handleRoot);
  server.on("/state", handleState);
  server.onNotFound(handleNotFound);
  server.begin();
  Serial.println("HTTP server started");
}

void loop() {
  // Blink the LED using non-blocking timing
  unsigned long now = millis();
  if (now - lastBlink >= blinkInterval) {
    lastBlink = now;
    ledOn = !ledOn;
    applyLedState();
  }

  dnsServer.processNextRequest();
  server.handleClient();
}
```
