#include <WiFi.h>
#include <WebServer.h>
const char* ssid = "Wokwi-GUEST";
const char* password = "";
WebServer server(80);
const int ledPin = 4;
void handleRoot() {
server.send(200, "text/html", "<h1>You are connected</h1><p><a href=\"/LED_ON\"><button>Turn On LED</button></a></p><p><a href=\"/LED_OFF\"><button>Turn Off LED</button></a></p>");
}
void handleLEDOn() {
digitalWrite(ledPin, HIGH);
server.send(200, "text/html", "<h1>LED is ON</h1><p><a href=\"/\"><button>Go Back</button></a></p>");
}
void handleLEDOff() {
digitalWrite(ledPin, LOW);
server.send(200, "text/html", "<h1>LED is OFF</h1><p><a href=\"/\"><button>Go Back</button></a></p>");
}
void setup() {
Serial.begin(115200);
pinMode(ledPin, OUTPUT);
digitalWrite(ledPin, LOW);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(100);
Serial.print(".");
}
Serial.println("");
Serial.print("Connected to ");
Serial.println(ssid);
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
server.on("/", handleRoot);
server.on("/LED_ON", handleLEDOn);
server.on("/LED_OFF", handleLEDOff);
server.begin();
Serial.println("HTTP server started");
}
void loop() {
server.handleClient();
}