//-------------------------INCLUSION DE BIBLIOTECAS-------------------------------
#include <ESP8266WiFi.h> // Incluye la biblioteca para la conexión WiFi
#include <ESP8266WebServer.h> // Incluye la biblioteca para el servidor web
//--------------------------CREDENCIALES WIFI--------------------------------------
const char* ssid = "Vllamada"; // Nombre de tu red WiFi
const char* password = "Vllamada_2021"; // Contraseña de tu red WiFi
//-------------------------SERVIDOR WEB----------------------------------------------
ESP8266WebServer server(80); // Crea una instancia del servidor web en el puerto 80
//------------------------- DEFINIMOS PINES-------------------------------------------
const int ledPin = 32; // Define el pin del LED (GPIO2)
//------------------------ FUNCION VOID SETUP-----------------------------------------
void setup() {
Serial.begin(115200); // Inicia la comunicación serial a 115200 baudios
pinMode(ledPin, OUTPUT); // Configura el pin del LED como salida
digitalWrite(ledPin, LOW); // Asegura que el LED esté apagado al inicio
//-------------------------CONECTANDOSE A LA RED WIFI----------------------------------
WiFi.begin(ssid, password); // Conecta a la red WiFi
while (WiFi.status() != WL_CONNECTED) { // Espera hasta que se conecte a la red WiFi
delay(500); // Espera 500 ms antes de verificar nuevamente
Serial.print("."); // Imprime un punto en el monitor serial para indicar que está intentando conectarse
}
Serial.println("conectado"); // Imprime un mensaje cuando se conecta a la red WiFi
Serial.print("Esta es la IP para conectar: ");
Serial.print("http://");
Serial.println(WiFi.localIP());
server.on("/", HTTP_GET, handleRoot); // Define la ruta raíz y la función que la manejará para solicitudes GET
server.on("/LED", HTTP_POST, handleLED); // Define la ruta para manejar solicitudes POST para el LED
server.begin(); // Inicia el servidor web
Serial.println("INICIANDO SERVIDOR"); // Imprime un mensaje indicando que el servidor HTTP ha comenzado
}
//-------------------------------FUNCION VOID LOOP--------------------------------------------------------
void loop() {
server.handleClient();// Maneja las solicitudes de los clientes
handleLED();
}
void handleRoot() {
// Crea una página HTML simple con un formulario para encender y apagar el LED
String html = "<html><body><h1>Control de LED</h1>"
"<form action=\"/LED\" method=\"POST\">" // Formulario que envía datos mediante POST
"<input type=\"submit\" name=\"state\" value=\"Encender\">" // Botón para encender el LED
"<input type=\"submit\" name=\"state\" value=\"Apagar\">" // Botón para apagar el LED
"</form></body></html>";
server.send(200, "text/html", html); // Envía la página HTML al cliente
}
void handleLED() {
if (server.hasArg("state")) { // Verifica si se ha enviado el argumento "state"
String state = server.arg("state"); // Obtiene el valor del argumento "state"
if (state == "Encender") { // Si el valor es "Encender"
digitalWrite(ledPin, LOW); // Enciende el LED
server.send(200, "text/html", "<html><body><h1>LED Encendido</h1><p><a href=\"/\">Volver</a></p></body></html>"); // Envía una respuesta indicando que el LED está encendido
} else if (state == "Apagar") { // Si el valor es "Apagar"
digitalWrite(ledPin, HIGH); // Apaga el LED
server.send(200, "text/html", "<html><body><h1>LED Apagado</h1><p><a href=\"/\">Volver</a></p></body></html>"); // Envía una respuesta indicando que el LED está apagado
}
}
}