/* ESP32 WiFi Scanning example */
#include <WiFi.h>
// Replace with your Wi-Fi credentials
const char* ssid = "your_SSID";
const char* password = "your_PASSWORD";
WiFiServer server(80); // Create a web server on port 80
const int ledPin = 2; // GPIO pin to control LED
void setup() {
pinMode(ledPin, OUTPUT); // Set LED pin as output
Serial.begin(115200); // Initialize serial communication
WiFi.begin(ssid, password); // Connect to Wi-Fi
// Wait for connection to be established
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
server.begin(); // Start the web server
Serial.println("Connected to WiFi");
Serial.println(WiFi.localIP()); // Print the ESP32's IP address
}
void loop() {
WiFiClient client = server.available(); // Listen for incoming clients
if (client) {
String request = client.readStringUntil('\r'); // Read HTTP request
client.flush(); // Clear the input buffer
// Control the LED based on the request
if (request.indexOf("/LED=ON") != -1) {
digitalWrite(ledPin, HIGH); // Turn LED on
}
if (request.indexOf("/LED=OFF") != -1) {
digitalWrite(ledPin, LOW); // Turn LED off
}
// Send HTML response
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println("Connection: close");
client.println();
client.println("<!DOCTYPE HTML>");
client.println("<html>");
client.println("<h1>LED Control</h1>");
client.println("<a href=\"/LED=ON\">Turn On</a><br>");
client.println("<a href=\"/LED=OFF\">Turn Off</a>");
client.println("</html>");
client.stop(); // Close the connection
}
}