/*
ESP32 WiFi Access Point (Server)
This sketch creates a WiFi network (Access Point) and starts a server.
It waits for a client to connect and then listens for messages from it.
When a message is received, it prints it to the Serial Monitor and
sends a confirmation message back to the client.
*/
#include <WiFi.h>
// --- WiFi Network Settings ---
// Set the name (SSID) and password for the WiFi network you want to create.
const char* ssid = "ESP32-AP";
const char* password = "password123";
// --- Server Settings ---
// The server will listen on port 80, the standard port for HTTP.
WiFiServer server(80);
void setup() {
// Start the Serial Monitor at a baud rate of 115200.
Serial.begin(115200);
Serial.println("\nConfiguring Access Point...");
// Start the WiFi Access Point.
// The 'true' parameter makes the network visible.
WiFi.softAP(ssid, password);
// Get the IP address of the Access Point.
// In Wokwi, this will always be 192.168.4.1.
IPAddress myIP = WiFi.softAPIP();
Serial.print("AP IP address: ");
Serial.println(myIP);
// Start the server.
server.begin();
Serial.println("Server started. Waiting for clients...");
}
void loop() {
// Check if a client has connected to the server.
WiFiClient client = server.available();
// If no client is connected, the 'client' object will be false.
if (!client) {
return; // Exit the loop and check again.
}
// A client has connected!
Serial.println("New client connected!");
// Wait until the client sends some data.
while (!client.available()) {
delay(1);
}
// Read the first line of the request from the client.
String request = client.readStringUntil('\r');
Serial.print("Received from client: ");
Serial.println(request);
// Send a response back to the client.
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println(""); // Important: empty line signifies end of headers
client.println("<!DOCTYPE HTML>");
client.println("<html>");
client.println("<head><title>ESP32 Server</title></head>");
client.println("<body><h1>Hello from the ESP32 Server!</h1></body>");
client.println("</html>");
// Flush the client buffer and wait a moment.
client.flush();
delay(5);
Serial.println("Response sent to client.");
Serial.println("Client disconnected.");
Serial.println("------------------------");
}