-
-
Save parzibyte/28bc5b3fd60e722934cc7136ab04fbae to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <ESP8266WiFiMulti.h> | |
#include <ESP8266WebServer.h> | |
#include <Arduino.h> | |
ESP8266WiFiMulti wifiMulti; | |
ESP8266WebServer servidor(80); | |
// Prototipos de funciones | |
void rutaRaiz(); | |
void rutaNoEncontrada(); | |
void setup(void) | |
{ | |
// Aquí puedes agregar varias redes. La tarjeta se conectará a la más cercana | |
wifiMulti.addAP("red", "Contraseña"); | |
// wifiMulti.addAP("Otra red", "Contraseña"); | |
// Esperar conexión WiFi | |
while (wifiMulti.run() != WL_CONNECTED) | |
{ | |
delay(250); | |
} | |
// Configurar rutas | |
servidor.on("/", rutaRaiz); | |
servidor.onNotFound(rutaNoEncontrada); | |
// Iniciar servidor | |
servidor.begin(); | |
} | |
void loop(void) | |
{ | |
// Responder las solicitudes entrantes en caso de que haya | |
servidor.handleClient(); | |
} | |
// Servir la página web | |
void rutaRaiz() | |
{ | |
servidor.send(200, "text/html", "<!DOCTYPE html>" | |
"<html>" | |
"<head>" | |
"<meta charset='utf-8'>" | |
"<meta name='viewport' content='width=device-width'>" | |
"<title>Servidor web con ESP8266</title>" | |
"</head>" | |
"<body>" | |
"<p>Soy un servidor web embebido dentro de la NodeMCU ESP8266</p>" | |
"<a href='https://parzibyte.me/blog'>By Parzibyte</a>" | |
"</body>" | |
"</html>"); | |
} | |
// Manejador de 404 | |
void rutaNoEncontrada() | |
{ | |
servidor.send(404, "text/plain", "No encontrado"); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment