#include <WiFi.h>
#include <WebServer.h>
// WiFi credentials
const char* ssid = "Wokwi-Guest";
const char* password = "";
WebServer server(80);
int led = 2;
String webpage = R"rawliteral(
<!DOCTYPE html>
<html>
<head>
<title>ESP32 LED Control</title>
</head>
<body>
<h2>ESP32 Web Server</h2>
<a href="/ON">
<button>LED ON</button>
</a>
<a href="/OFF">
<button>LED OFF</button>
</a>
</body>
</html>
)rawliteral";
void handleRoot() {
server.send(200, "text/html", webpage);
}
void handleON() {
digitalWrite(led, HIGH);
server.send(200, "text/html", webpage);
}
void handleOFF() {
digitalWrite(led, LOW);
server.send(200, "text/html", webpage);
}
void setup() {
Serial.begin(115200);
pinMode(led, OUTPUT);
WiFi.begin(ssid, password);
Serial.print("Connecting");
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi Connected");
Serial.println(WiFi.localIP());
server.on("/", handleRoot);
server.on("/ON", handleON);
server.on("/OFF", handleOFF);
server.begin();
Serial.println("Server Started");
}
void loop() {
server.handleClient();
}