#include <WiFi.h>
#include <WebServer.h>
const char* ssid = "Wokwi-GUEST";
const char* password = "";
#define LED_PIN 2
WebServer server(80);
bool ledState = false;
// ---------- Web Page ----------
String webpage() {
return R"rawliteral(
<!DOCTYPE html>
<html>
<head>
<title>ESP32 LED Control</title>
<style>
body {
font-family: Arial;
text-align: center;
margin-top: 50px;
background-color: #f2f2f2;
}
h3 {
color: #333;
}
button {
width: 150px;
height: 50px;
font-size: 18px;
border: none;
border-radius: 8px;
cursor: pointer;
margin: 10px;
color: white;
}
.on {
background-color: #28a745;
}
.off {
background-color: #dc3545;
}
button:hover {
opacity: 0.85;
}
</style>
</head>
<body>
<h3>ESP32 LED Control</h3>
<form action="/on">
<button class="on">LED ON</button>
</form>
<form action="/off">
<button class="off">LED OFF</button>
</form>
</body>
</html>
)rawliteral";
}
// ---------- Page Handlers ----------
void handleRoot() {
server.send(200, "text/html", webpage());
}
void handleOn() {
ledState = true;
digitalWrite(LED_PIN, HIGH);
server.send(200, "text/html", webpage());
}
void handleOff() {
ledState = false;
digitalWrite(LED_PIN, LOW);
server.send(200, "text/html", webpage());
}
void setup() {
Serial.begin(115200);
pinMode(LED_PIN, OUTPUT);
digitalWrite(LED_PIN, LOW);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
}
Serial.print("ESP32 IP Address: ");
Serial.println(WiFi.localIP());
server.on("/", handleRoot);
server.on("/on", handleOn);
server.on("/off", handleOff);
server.begin();
}
void loop() {
server.handleClient();
}