User Tools

Site Tools


informatica:arduino:esp32:ejemplos

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
informatica:arduino:esp32:ejemplos [2025/05/02 20:22] – [Versión 2] joseinformatica:arduino:esp32:ejemplos [2025/05/02 20:23] (current) – [Versión 1] jose
Line 130: Line 130:
  
 ====== Encender LED desde página WEB ====== ====== Encender LED desde página WEB ======
-===== Versión 1 ===== 
-El mensaje final cuando actualiza está con mala codificación 
 <code> <code>
 #include <WiFi.h> #include <WiFi.h>
Line 137: Line 135:
 #include <Update.h> #include <Update.h>
  
-// WiFi +// =================== HTMLS ===================
-const char* ssid "pitufina"; +
-const char* password "reyvisigodo";+
  
-// LED pin 
-#define RELAY_PIN 13 
- 
-// Estados 
-bool ledState = false; 
-bool autoBlink = true; 
- 
-// Temporizador para parpadeo 
-unsigned long previousMillis = 0; 
-const unsigned long interval = 500; 
- 
-// IP estática (opcional) 
-IPAddress ip(192, 168, 1, 112); 
-IPAddress gateway(192, 168, 1, 1); 
-IPAddress subnet(255, 255, 255, 0); 
-IPAddress dns(8, 8, 8, 8); 
- 
-WebServer server(80); 
- 
-// Página principal 
 const char* mainPage = R"rawliteral( const char* mainPage = R"rawliteral(
 <!DOCTYPE html> <!DOCTYPE html>
Line 203: Line 179:
       <form method="POST" action="/update" enctype="multipart/form-data">       <form method="POST" action="/update" enctype="multipart/form-data">
         <input type="file" name="update">         <input type="file" name="update">
-        <input type="submit" value="Actualizar">+        <input type="submit" value="Actualizar firmware">
       </form>       </form>
     </div>     </div>
Line 210: Line 186:
 )rawliteral"; )rawliteral";
  
-// Página de éxito OTA 
 const char* successPage = R"rawliteral( const char* successPage = R"rawliteral(
 <!DOCTYPE html> <!DOCTYPE html>
 <html> <html>
-  <head><meta charset="utf-8"><title>Actualizado</title></head> +  <head> 
-  <body> +    <meta charset="utf-8"> 
-    <h2>✅ Actualización completada con éxito</h2+    <title>Actualización exitosa</title> 
-    <p>Reiniciando ESP32...</p>+    <meta http-equiv="refresh" content="5;url=/" /> 
 +  </head> 
 +  <body style="font-family: sans-serif; background-color: #f4f4f4; padding: 30px;"
 +    <div style="background: white; padding: 20px; border-radius: 8px; box-shadow: 0 0 10px #ccc;"> 
 +      <h1>✅ Actualización exitosa</h1
 +      <p>El ESP32 se reiniciará automáticamente en unos segundos...</p
 +    </div>
   </body>   </body>
 </html> </html>
 )rawliteral"; )rawliteral";
  
-// Reemplazo de variables + 
-String processor(const String& var) { +// Configuración WiFi 
-  if (var == "LED_STATE") return ledState ? "Encendido""Apagado"; +const char* ssid = "XXXXXX"
-  if (var == "LED_STATUS"return ledState ? "on" : "off"+const char* password = "XXXXXXXX"; 
-  if (var == "BTN_TEXT"return ledState ? "Apagar" : "Encender"+ 
-  if (var == "BTN_ON_CLASS"return ledState ? "btn-off" : "btn-on"+// IP estática (opcional) 
-  if (var == "AUTO_TEXT"return autoBlink ? "Parpadeo: ON" : "Parpadeo: OFF"+IPAddress ip(192, 168, 1, 112); 
-  return String(); +IPAddress gateway(192, 168, 1, 1); 
-}+IPAddress subnet(255, 255, 255, 0); 
 +IPAddress dns(8, 8, 8, 8); 
 + 
 +#define RELAY_PIN 13 
 + 
 +WebServer server(80); 
 + 
 +bool ledState = false; 
 +bool autoBlink = true; 
 + 
 +unsigned long previousMillis = 0; 
 +const unsigned long interval = 500;
  
 void setup() { void setup() {
 +  Serial.begin(115200);
   pinMode(RELAY_PIN, OUTPUT);   pinMode(RELAY_PIN, OUTPUT);
   digitalWrite(RELAY_PIN, LOW);   digitalWrite(RELAY_PIN, LOW);
-  Serial.begin(115200); 
  
   WiFi.config(ip, gateway, subnet, dns);   WiFi.config(ip, gateway, subnet, dns);
   WiFi.begin(ssid, password);   WiFi.begin(ssid, password);
-  while (WiFi.status() != WL_CONNECTED) +  while (WiFi.status() != WL_CONNECTED) delay(100); 
-    delay(500); +  Serial.print("Conectado: "); Serial.println(WiFi.localIP());
-    Serial.print("."); +
-  } +
-  Serial.println("\nWiFi conectado: " + WiFi.localIP().toString());+
  
-  // Página principal 
   server.on("/", []() {   server.on("/", []() {
     String html = mainPage;     String html = mainPage;
-    html.replace("%LED_STATE%", processor("LED_STATE")); +    html.replace("%LED_STATE%", ledState ? "ENCENDIDO" : "APAGADO"); 
-    html.replace("%LED_STATUS%", processor("LED_STATUS")); +    html.replace("%LED_STATUS%", ledState ? "on" : "off"); 
-    html.replace("%BTN_TEXT%", processor("BTN_TEXT")); +    html.replace("%BTN_TEXT%", ledState ? "Apagar" : "Encender"); 
-    html.replace("%BTN_ON_CLASS%", processor("BTN_ON_CLASS")); +    html.replace("%BTN_ON_CLASS%", ledState ? "btn-off" : "btn-on"); 
-    html.replace("%AUTO_TEXT%", processor("AUTO_TEXT")); +    html.replace("%AUTO_TEXT%", autoBlink ? "Auto: ON" : "Auto: OFF"); 
-    server.send(200, "text/html", html);+    server.send(200, "text/html; charset=utf-8", html);
   });   });
  
-  // Toggle LED 
   server.on("/toggle", []() {   server.on("/toggle", []() {
     ledState = !ledState;     ledState = !ledState;
-    digitalWrite(RELAY_PIN, ledState ? HIGH : LOW); 
     autoBlink = false;     autoBlink = false;
 +    digitalWrite(RELAY_PIN, ledState);
     server.send(200, "text/plain", "OK");     server.send(200, "text/plain", "OK");
   });   });
  
-  // Toggle parpadeo 
   server.on("/auto", []() {   server.on("/auto", []() {
     autoBlink = !autoBlink;     autoBlink = !autoBlink;
Line 270: Line 256:
   });   });
  
-  // OTA Upload 
   server.on("/update", HTTP_POST, []() {   server.on("/update", HTTP_POST, []() {
     server.sendHeader("Connection", "close");     server.sendHeader("Connection", "close");
-    server.send(200, "text/html", successPage); +    server.send(200, "text/html; charset=utf-8", successPage); 
-    delay(1500);  // Espera antes de reiniciar+    delay(2000);
     ESP.restart();     ESP.restart();
   }, []() {   }, []() {
     HTTPUpload& upload = server.upload();     HTTPUpload& upload = server.upload();
     if (upload.status == UPLOAD_FILE_START) {     if (upload.status == UPLOAD_FILE_START) {
-      Serial.printf("Actualizando: %s\n", upload.filename.c_str()); +      Serial.printf("OTA iniciada: %s\n", upload.filename.c_str()); 
-      if (!Update.begin(UPDATE_SIZE_UNKNOWN)) +      if (!Update.begin(UPDATE_SIZE_UNKNOWN)) Update.printError(Serial);
-        Update.printError(Serial); +
-      }+
     } else if (upload.status == UPLOAD_FILE_WRITE) {     } else if (upload.status == UPLOAD_FILE_WRITE) {
-      if (Update.write(upload.buf, upload.currentSize) != upload.currentSize) +      if (Update.write(upload.buf, upload.currentSize) != upload.currentSize) Update.printError(Serial);
-        Update.printError(Serial); +
-      }+
     } else if (upload.status == UPLOAD_FILE_END) {     } else if (upload.status == UPLOAD_FILE_END) {
-      if (Update.end(true)) +      if (Update.end(true)) Serial.println("OTA finalizada correctamente"); 
-        Serial.println("Actualización exitosa"); +      else Update.printError(Serial);
-      else +
-        Update.printError(Serial); +
-      }+
     }     }
   });   });
Line 307: Line 285:
       previousMillis = currentMillis;       previousMillis = currentMillis;
       ledState = !ledState;       ledState = !ledState;
-      digitalWrite(RELAY_PIN, ledState ? HIGH : LOW);+      digitalWrite(RELAY_PIN, ledState);
     }     }
   }   }
 } }
 +
 </code> </code>
- 
- 
informatica/arduino/esp32/ejemplos.txt · Last modified: 2025/05/02 20:23 by jose