#include <WiFi.h>
// Definición de las credenciales de la red Wi-Fi virtual de Wokwi
const char* ssid = "Wokwi-GUEST";
const char* password = ""; // Contraseña vacía
// Configuración del hardware y del servidor
const int ledPin = 2; // Pin GPIO donde está conectado el LED integrado
WiFiServer server(80); // Se inicializa el servidor web en el puerto 80
void setup() {
// Inicializa la comunicación serial a 115200 bps
Serial.begin(115200);
// Configura el pin del LED como salida
pinMode(ledPin, OUTPUT);
// Comienza el proceso de conexión a la red inalámbrica configurada
WiFi.begin(ssid, password);
// Bucle de espera activa hasta que se establezca la conexión
while (WiFi.status() != WL_CONNECTED) {
delay(1000); // Pausa de 1 segundo entre intentos
Serial.println("Conectando..."); // Imprime mensaje de progreso
}
// Una vez conectado con éxito, muestra los datos de red en la consola
Serial.println("");
Serial.print("Conectado. IP: ");
Serial.println(WiFi.localIP());
// Inicia el servidor web
server.begin();
}
void loop() {
// ----------------------------------------------------
// ALTERNATIVA PARA WOKWI: Control por Consola Serial
// ----------------------------------------------------
if (Serial.available() > 0) {
char comando = Serial.read();
// Si recibe '1', 'H' o 'h', enciende el LED
if (comando == '1' || comando == 'H' || comando == 'h') {
digitalWrite(ledPin, HIGH);
Serial.println("-> COMANDO RECIBIDO: ¡LED ENCENDIDO!");
}
// Si recibe '0', 'L' o 'l', apaga el LED
else if (comando == '0' || comando == 'L' || comando == 'l') {
digitalWrite(ledPin, LOW);
Serial.println("-> COMANDO RECIBIDO: ¡LED APAGADO!");
}
}
// ----------------------------------------------------
// CONTROL POR SERVIDOR WEB (Para hardware real)
// ----------------------------------------------------
WiFiClient client = server.available();
if (client) {
Serial.println("Nuevo cliente web detectado.");
String currentLine = "";
while (client.connected()) {
if (client.available()) {
char c = client.read();
Serial.write(c);
if (c == '\n') {
if (currentLine.length() == 0) {
// Envío de la respuesta HTTP estándar hacia el navegador web del cliente
client.print("HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n");
client.print("<!DOCTYPE html><html>");
client.print("<head><meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">");
client.print("<style>body{text-align:center;font-family:sans-serif;} .button{background-color:#4CAF50;border:none;color:white;padding:16px 40px;text-decoration:none;font-size:30px;margin:2px;cursor:pointer;} .button2{background-color:#f44336;}</style></head>");
client.print("<body><h1>Control del LED ESP32</h1>");
// Botones interactivos en la página web
client.print("<p><a href=\"/H\"><button class=\"button\">ENCENDER</button></a></p>");
client.print("<p><a href=\"/L\"><button class=\"button button2\">APAGAR</button></a></p>");
client.print("</body></html>");
break;
} else {
currentLine = "";
}
} else if (c != '\r') {
currentLine += c;
}
// Acciones basadas en los clics del servidor web
if (currentLine.endsWith("GET /H")) {
digitalWrite(ledPin, HIGH);
Serial.println("-> WEB: LED ENCENDIDO");
}
if (currentLine.endsWith("GET /L")) {
digitalWrite(ledPin, LOW);
Serial.println("-> WEB: LED APAGADO");
}
}
}
// Finalización del proceso de comunicación con el cliente actual
delay(1);
client.stop();
Serial.println("Cliente desconectado.");
}
}