#include <WiFi.h>
const char* ssid = "Wokwi-GUEST";
const char* password = "";
WiFiServer server(80); // Start server on port 80
void setup() {
Serial.begin(115200);
pinMode(2, OUTPUT); // LED on GPIO 2
// Connect to WiFi
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("\nWiFi connected.");
Serial.println(WiFi.localIP()); // Print the IP address
server.begin();
}
void loop() {
WiFiClient client = server.available(); // Listen for incoming clients
if (client) {
Serial.println("New Client.");
String currentLine = "";
while (client.connected()) {
if (client.available()) {
char c = client.read();
Serial.write(c);
if (c == '\n') {
if (currentLine.length() == 0) {
// HTTP header always starts with a response code
client.println("HTTP/1.1 200 OK");
client.println("Content-type:text/html");
client.println();
// Display the web page
client.print("Click <a href=\"/H\">here</a> to turn LED ON.<br>");
client.print("Click <a href=\"/L\">here</a> to turn LED OFF.<br>");
client.println();
break;
} else { currentLine = ""; }
} else if (c != '\r') { currentLine += c; }
// Check the client request
if (currentLine.endsWith("GET /H")) { digitalWrite(2, HIGH); }
if (currentLine.endsWith("GET /L")) { digitalWrite(2, LOW); }
}
}
client.stop();
Serial.println("Client Disconnected.");
}
}