# main.ino

- Project: [XIAO-WEB-LED-CONTROLLER-AP](https://flashesp.com/explore/OMO1I40jKgXoQxSbpmXTR.md) by FlashESP
- Board: XIAO_ESP32S3
- Language: Arduino (C++)
- Web page: https://flashesp.com/explore/OMO1I40jKgXoQxSbpmXTR/files/tiPk7R2I1GuJjdpw0Jn5S

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

// ===== Configuration =====
const char* AP_SSID = "XIAO-AP-WEBLED";  // Open AP, no password
const int LED_PIN = 21;                  // Onboard LED (active LOW)

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

bool ledOn = false;

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

String buildPage() {
  String html = R"HTML(
<!DOCTYPE html>
<html>
<head>
  <meta charset="UTF-8">
  <title>XIAO Web LED Controller</title>
  <meta name="viewport" content="width=device-width,initial-scale=1">
  <style>
    body {
      font-family: sans-serif;
      background: #101018;
      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; }
    .btn {
      display: inline-block;
      padding: 1rem 2rem;
      margin: 0.5rem;
      border-radius: 0.75rem;
      border: none;
      font-size: 1.1rem;
      cursor: pointer;
    }
    .on-btn  { background: #2ecc71; color: #000; }
    .off-btn { background: #e74c3c; color: #000; }
    .ap { opacity: 0.8; margin-top: 1rem; }
  </style>
</head>
<body>
  <h1>XIAO Web LED Controller</h1>
  <p class="ap">You are connected to an open AP hosted by the board.</p>

  <div class="state">
    LED is: <b id="ledState">UNKNOWN</b>
    <span id="ledIndicator" class="indicator"></span>
  </div>

  <button class="btn on-btn"  onclick="sendCmd('/on')">Turn LED ON</button>
  <button class="btn off-btn" onclick="sendCmd('/off')">Turn LED OFF</button>

  <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
      }
    }

    async function sendCmd(path) {
      try {
        await fetch(path);    // fire-and-forget
        setTimeout(updateState, 100); // small delay and refresh state
      } catch (e) {
        // ignore
      }
    }

    setInterval(updateState, 500);
    updateState();
  </script>
</body>
</html>
)HTML";

  return html;
}

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

void handleOn() {
  ledOn = true;
  applyLedState();
  // Minimal response – page itself will refresh via /state polling
  server.send(200, "text/plain", "OK");
}

void handleOff() {
  ledOn = false;
  applyLedState();
  server.send(200, "text/plain", "OK");
}

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

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.mode(WIFI_AP);
  WiFi.softAP(AP_SSID);
  IPAddress apIP = WiFi.softAPIP();

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

  dnsServer.start(DNS_PORT, "*", apIP);

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

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