Joa0712

Conexión WIFI - ESP8266 + Server

Aug 28th, 2025
27
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.54 KB | Software | 0 0
  1. // Código para conectar un ESP8266 con WIFI Alumnos y ver la IP en un Server web
  2.  
  3.  
  4. #include <ESP8266WiFi.h>
  5. #include <ESP8266WebServer.h>
  6.  
  7. // ====== HW & tiempos ======
  8. #define LED                 LED_BUILTIN   // activo en LOW
  9. #define T_REINTENTO_MS      4000
  10. #define BLINK_CONECTANDO    1000
  11. #define BLINK_DESCONECTADO  500
  12. #define BLINK_CONECTADO     3000
  13.  
  14. // ====== credenciales ======
  15. const char* WIFI_SSID = "Alumnos";
  16. const char* WIFI_PASS = "";
  17.  
  18. // ====== estado ======
  19. enum EstadoWifi { DESCONECTADO, CONECTANDO, CONECTADO };
  20. EstadoWifi estado = DESCONECTADO;
  21.  
  22. ESP8266WebServer server(80);
  23. bool serverIniciado = false;
  24.  
  25. unsigned long tBlink = 0;
  26. unsigned long periodoBlink = BLINK_DESCONECTADO;
  27. bool ledState = false;
  28.  
  29. unsigned long tRetry = 0;
  30.  
  31. // ====== prototipos ======
  32. void Conectar();
  33. void GestionWifi();
  34. void BlinkLed();
  35. void IniciarServerSiConectado();
  36.  
  37. // ====== HTTP handlers ======
  38. void handleRoot() {
  39.   String html = "<h2>ESP8266 OK</h2><p>IP: " + WiFi.localIP().toString() + "</p>";
  40.   html += "<p>SSID: " + String(WIFI_SSID) + "</p>";
  41.   server.send(200, "text/html", html);
  42. }
  43.  
  44. void setup() {
  45.   pinMode(LED, OUTPUT);
  46.   digitalWrite(LED, HIGH);        // apagado (inverso)
  47.   Serial.begin(115200);
  48.   Serial.printIn("Hola");
  49.   WiFi.mode(WIFI_STA);
  50.   Conectar();
  51. }
  52.  
  53. void loop() {
  54.   GestionWifi();
  55.   BlinkLed();
  56.   IniciarServerSiConectado();
  57.   if (serverIniciado) server.handleClient();
  58. }
  59.  
  60. // ====== lógica ======
  61. void Conectar() {
  62.   Serial.printf("Conectando a %s...\n", WIFI_SSID);
  63.   WiFi.begin(WIFI_SSID, WIFI_PASS);
  64.   estado = CONECTANDO;
  65.   periodoBlink = BLINK_CONECTANDO;
  66. }
  67.  
  68. void GestionWifi() {
  69.   wl_status_t st = WiFi.status();
  70.  
  71.   if (st == WL_CONNECTED) {
  72.     if (estado != CONECTADO) {
  73.       estado = CONECTADO;
  74.       periodoBlink = BLINK_CONECTADO;
  75.       Serial.print("Conectado. IP: ");
  76.       Serial.println(WiFi.localIP());
  77.     }
  78.     return;
  79.   }
  80.  
  81.   // no conectado
  82.   if (estado != CONECTANDO) {
  83.     estado = DESCONECTADO;
  84.     periodoBlink = BLINK_DESCONECTADO;
  85.   }
  86.  
  87.   if (millis() - tRetry >= T_REINTENTO_MS) {
  88.     tRetry = millis();
  89.     Conectar();
  90.   }
  91. }
  92.  
  93. void BlinkLed() {
  94.   if (millis() - tBlink < periodoBlink) return;
  95.   tBlink = millis();
  96.   ledState = !ledState;
  97.   digitalWrite(LED, ledState ? LOW : HIGH); // LOW enciende
  98. }
  99.  
  100. void IniciarServerSiConectado() {
  101.   if (estado != CONECTADO) return;
  102.   if (serverIniciado) return;
  103.  
  104.   server.on("/", handleRoot);
  105.   server.begin();
  106.   serverIniciado = true;
  107.   Serial.println("HTTP listo -> abre http://<IP>/");
  108. }
Advertisement
Add Comment
Please, Sign In to add comment