#include <ESP8266WiFi.h>
#include <WiFiManager.h>

WiFiServer server(80);

bool ledState = false; // false = OFF, true = ON

void setup() {
  Serial.begin(115200);
  delay(1000);

  pinMode(LED_BUILTIN, OUTPUT);
  digitalWrite(LED_BUILTIN, HIGH); // ESP8266 내장 LED는 HIGH가 OFF

  WiFi.mode(WIFI_STA);

  WiFiManager wm;

  // WiFi 저장 정보 초기화하고 싶을 때만 주석 해제
  // wm.resetSettings();

  bool res = wm.autoConnect("ESP8266_Setup", "12345678");

  if (!res) {
    Serial.println("WiFi connection failed");
    ESP.restart();
  }

  Serial.println("WiFi connected!");
  Serial.print("IP: ");
  Serial.println(WiFi.localIP());

  server.begin();
  Serial.println("Web server started");
}

void loop() {
  WiFiClient client = server.available();
  if (!client) return;

  String request = client.readStringUntil('\r');
  client.flush();

  if (request.indexOf("GET /on") != -1) {
    ledState = true;
    digitalWrite(LED_BUILTIN, LOW);   // ON
  }

  if (request.indexOf("GET /off") != -1) {
    ledState = false;
    digitalWrite(LED_BUILTIN, HIGH);  // OFF
  }

  client.println("HTTP/1.1 200 OK");
  client.println("Content-Type: text/html; charset=UTF-8");
  client.println("Connection: close");
  client.println();

  client.println(R"rawliteral(
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
  body {
    font-family: Arial, sans-serif;
    text-align: center;
    margin-top: 60px;
    background: #f4f4f4;
  }
  .card {
    background: white;
    max-width: 360px;
    margin: auto;
    padding: 28px;
    border-radius: 18px;
    box-shadow: 0 4px 18px rgba(0,0,0,0.12);
  }
  h1 { font-size: 26px; }
  button {
    font-size: 22px;
    padding: 14px 28px;
    margin: 10px;
    border: none;
    border-radius: 12px;
    cursor: pointer;
  }
  .on { background: #222; color: white; }
  .off { background: #ddd; color: #222; }
</style>
</head>
<body>
<div class="card">
  <h1>ESP8266 LED Control</h1>
  <p id="status">LED 상태 확인 중...</p>
  <button class="on" onclick="setLed('on')">LED ON</button>
  <button class="off" onclick="setLed('off')">LED OFF</button>
</div>

<script>
function setLed(state) {
  fetch('/' + state)
    .then(() => {
      document.getElementById('status').innerText =
        state === 'on' ? 'LED 상태: ON' : 'LED 상태: OFF';
    });
}
</script>
</body>
</html>
)rawliteral");

  delay(1);
  client.stop();
}
20:11:29.955 -> *wm:connectTimeout not set, ESP waitForConnectResult... 
20:11:30.324 -> *wm:AutoConnect: SUCCESS 
20:11:30.324 -> *wm:STA IP Address: 172.30.1.23
20:11:30.370 -> WiFi connected!
20:11:30.370 -> IP: 172.30.1.23
20:18:35.667 -> *wm:AutoConnect 
20:18:35.667 -> *wm:Connecting to SAVED AP: KT_WiFi_84B1
20:18:36.175 -> *wm:connectTimeout not set, ESP waitForConnectResult... 
20:18:36.546 -> *wm:AutoConnect: SUCCESS 
20:18:36.546 -> *wm:STA IP Address: 172.30.1.23
20:18:36.546 -> WiFi connected!
20:18:36.546 -> IP: 172.30.1.23
20:18:36.546 -> Web server started

위 서버로 들어가면

image.png

이 페이지가 나온다

image.png

image.png

실제로도 LED가 켜고 꺼지는걸 확인할 수 있다.