serveur-web.ino
El sketch que convierte el NodeMCU en objeto conectado: página HTML servida en local, enlaces que pilotan el LED y reinicio remoto. Sin infraestructura: todo cabe en los 4 Mo de flash.
Hardware necesario
Un módulo ESP-12E (ESP8266EX, Wi-Fi 2,4 GHz) montado en una placa NodeMCU / Wemos D1 mini, alimentada por micro-USB. Un punto de acceso Wi-Fi 2,4 GHz (el 8266 no ve 5 GHz).
Cableado
Sin cableado obligatorio; LED en D4. La alimentación por micro-USB basta (el Wi-Fi pide ~200 mA en picos).
Funcionamiento
ESP8266WebServer registra rutas lambda; handleClient() atiende peticiones al ritmo de loop(). ESP.restart() reinicia limpiamente, útil cuando el módulo salta por watchdog. En el Arduino IDE, instale el paquete « esp8266 », elija « NodeMCU 1.0 (ESP-12E Module) » y suba (mantenga GPIO0 a LOW si falla). El ADC A0 lee 0-1 V (divisor NodeMCU: 0-3,3 V).
Para ir más lejos
Añada el DHT11 del sketch anterior a la página, o pase a modo punto de acceso para usarlo sin router.
serveur-web.ino
// Serveur web ESP8266 : page de controle LED D4 + bouton reboot.
#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>
const char *SSID = "votre-wifi";
const char *CLE = "votre-cle";
const int LED = 2;
ESP8266WebServer serveur(80);
void page() {
String h = "<h1>ESP8266</h1>"
"<p>LED : " + String(digitalRead(LED) == LOW ? "ALLUMEE" : "eteinte") + "</p>"
"<p><a href=/on>ALLUMER</a> | <a href=/off>ETEINDRE</a> | "
"<a href=/reboot>reboot</a></p>";
serveur.send(200, "text/html", h);
}
void setup() {
pinMode(LED, OUTPUT); digitalWrite(LED, HIGH); // LED eteinte (actif-bas)
Serial.begin(115200);
WiFi.mode(WIFI_STA);
WiFi.begin(SSID, CLE);
while (WiFi.status() != WL_CONNECTED) { delay(300); Serial.print("."); }
Serial.printf("\nhttp://%s/\n", WiFi.localIP().toString().c_str());
serveur.on("/", page);
serveur.on("/on", []() { digitalWrite(LED, LOW); page(); });
serveur.on("/off", []() { digitalWrite(LED, HIGH); page(); });
serveur.on("/reboot", []() { serveur.send(200, "text/plain", "bye");
ESP.restart(); });
serveur.begin();
}
void loop() { serveur.handleClient(); }