#include <WiFi.h>
#include <Servo.h>
const char* ssid = "Wokwi-GUEST"; // Enter your WiFi SSID here
const char* password = ""; // Enter your WiFi password here
WiFiServer server(80);
Servo servo;
const int LED_PIN = 2; // Change to GPIO 2
void setup() {
Serial.begin(115200);
pinMode(LED_PIN, OUTPUT); // Pin connected to the LED
servo.attach(2); // Pin connected to the servo (D2 on most ESP32 boards)
servo.write(90);
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
Serial.begin(9600);
Serial.print("Connecting to WiFi");
WiFi.begin("Wokwi-GUEST", "", 6);
while (WiFi.status() != WL_CONNECTED) {
delay(100);
Serial.print("."); }
Serial.println("WiFi connected");
Serial.print("IP Address: ");
Serial.println(WiFi.localIP());
// Start the server
server.begin();
Serial.println("Server started");
}
void loop() {
WiFiClient client = server.available();
if (!client) {
return;
}
Serial.println("New client");
while (!client.available()) {
delay(1);
}
String request = client.readStringUntil('\r');
Serial.println(request);
client.flush();
// Match the request
if (request.indexOf("/on") != -1) {
digitalWrite(LED_PIN, HIGH); // Turn on the LED
servo.write(0); // Turn the servo to full speed
} else if (request.indexOf("/off") != -1) {
digitalWrite(LED_PIN, LOW); // Turn off the LED
servo.write(90); // Stop the servo
}
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println("");
client.println("<!DOCTYPE HTML>");
client.println("<html>");
client.println("<br><a href=\"/on\"><button>ON</button></a>");
client.println("<br><a href=\"/off\"><button>OFF</button></a>");
client.println("</html>");
delay(1);
Serial.println("Client disconnected");
}