#include <WiFi.h>
#include <WebServer.h>
const char* SSID = "Wokwi-GUEST";
WebServer server(80);
const int led1 =26;
const int led2 =27;
const char htmlPage[] PROGMEM = R"rawliteral(
<!DOCTYPE html>
<html>
<head>
<title>ESP32 LED Control</title>
<style>
body { font-family: Arial; text-align: center; margin-top: 50px; }
button { padding: 20px; font-size: 18px; margin: 10px; }
</style>
</head>
<body>
<h1>ESP32 LED ONE Control</h1>
<p>LED 1 is %STATE1%</p>
<form action="/on1"><button style="background: green; color: white;">Turn ON LED 1</button></form>
<form action="/off1"><button style="background: red; color: white;">Turn OFF LED 1</button></form>
<h1>ESP32 LED TWO Control</h1>
<p>LED 2 is %STATE2%</p>
<form action="/on2"><button style="background: green; color: white;">Turn ON LED 2</button></form>
<form action="/off2"><button style="background: red; color: white;">Turn OFF LED 2</button></form>
</body>
</html>
)rawliteral";
void handleRoot() {
String html = htmlPage;
html.replace("%STATE1%", digitalRead(led1) ? "ON" : "OFF");
html.replace("%STATE2%", digitalRead(led2) ? "ON" : "OFF");
server.send(200, "text/html", html);
}
void handleOn1() {
digitalWrite(led1, HIGH);
handleRoot();
}
void handleOff1() {
digitalWrite(led1, LOW);
handleRoot();
}
void handleOn2() {
digitalWrite(led2, HIGH);
handleRoot();
}
void handleOff2() {
digitalWrite(led2, LOW);
handleRoot();
}
void setup() {
Serial.begin(115200);
pinMode(led1, OUTPUT);
pinMode(led2, OUTPUT);
digitalWrite(led1, LOW);
digitalWrite(led2, LOW);
WiFi.begin(SSID);
Serial.print("Connecting to WiFi");
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.print(".");
}
Serial.println("\nConnected to WiFi");
Serial.print("IP Address: ");
Serial.println(WiFi.localIP());
server.on("/", handleRoot);
server.on("/on1", handleOn1);
server.on("/off1", handleOff1);
server.on("/on2", handleOn2);
server.on("/off2", handleOff2);
server.begin();
Serial.println("HTTP server started");
}
void loop() {
// put your main code here, to run repeatedly:
server.handleClient();
delay(10); // this speeds up the simulation
}