#include <WiFi.h>
// needed to connect to wi-fi, start a server, and create a web page.
const char* ssid = "Wokwi-GUEST";
// ssid - sevice set identifier, it is the name of the wifi that we want to connect to
// wokwi-GUEST is the free wifi name available in wokwi
const char* password = "";
// we have not keep any password because wokwi-guest doesn't need a password
WiFiServer server(80);
// creating a web server object called server
// 80 is the port number for HTTP
void setup() {
Serial.begin(115200);
WiFi.begin(ssid, password);
// tells esp32 to start connecting to the wifi using ssid and password
Serial.println("Connecting to WiFi...");
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
// this loop keeps on checking until wifi is connected
Serial.println("\nWiFi Connected!");
Serial.print("IP Address: ");
Serial.println(WiFi.localIP());
// this will print the IP address of esp32 and we can open this ip adderss in browser
server.begin();
// this will the the esp32 to start the web server
}
void loop() {
WiFiClient client = server.available();
// waits until someone open the IP
if (client) {
// checks whether someone has connected or not
Serial.println("New Client Connected");
String request = client.readStringUntil('\r');
// this will read the first line of the browser's request
Serial.println(request);
client.flush();
// this will clear out the rest of the client's request that we don't care about
client.println("HTTP/1.1 200 OK");
// will tell the browser the that i am giving you a web page
client.println("Content-Type: text/html");
// clarify to browser that the content is in html
client.println();
client.println("<!DOCTYPE html>");
client.println("<html>");
client.println("<head><title>ESP32 Web</title></head>");
client.println("<body>");
client.println("<h1>Wecome to Hitali's web page</h1>");
client.println("<p>This page is served by ESP32 over Wi-Fi.</p>");
client.println("</body>");
client.println("</html>");
delay(1);
client.stop();
// this will end the connection with the browser
Serial.println("Client Disconnected");
}
}