#include <WiFi.h>
#include <WebServer.h>
const char* ssid = "Redmi9s";
const char* password = "kaax3932";
const int ledPin = 23;
WebServer server(80);
void setup() {
Serial.begin(115200);
// Configure la ESP32 para conectarse a la red Wi-Fi
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi conectado");
// Configure el pin GPIO como salida
pinMode(ledPin, OUTPUT);
// Agregue una página web a su servidor web
server.on("/", handleRoot);
// Inicie el servidor web
server.begin();
}
void loop() {
server.handleClient();
}
void handleRoot() {
// Obtenga el estado actual del LED
bool ledState = digitalRead(ledPin);
// Cree la página web
String html = "<html>";
html += "<head><title>Control de LED</title></head>";
html += "<body>";
html += "<h1>Control de LED</h1>";
html += "<p>";
html += ledState ? "LED encendido" : "LED apagado";
html += "</p>";
html += "<form action=\"/\" method=\"post\">";
html += "<input type=\"submit\" name=\"action\" value=\"Encendido\">";
html += "<input type=\"submit\" name=\"action\" value=\"Apagado\">";
html += "</form>";
html += "</body>";
html += "</html>";
// Envíe la página web al cliente
server.send(200, "text/html", html);
// Obtenga la acción del formulario
String action = server.arg("action");
// Encienda el LED si la acción es "Encendido"
if (action == "Encendido") {
digitalWrite(ledPin, HIGH);
}
// Apague el LED si la acción es "Apagado"
else if (action == "Apagado") {
digitalWrite(ledPin, LOW);
}
}