informatica:arduino:esp32:ejemplos
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
informatica:arduino:esp32:ejemplos [2025/05/02 19:40] – jose | informatica:arduino:esp32:ejemplos [2025/05/02 20:23] (current) – [Versión 1] jose | ||
---|---|---|---|
Line 135: | Line 135: | ||
#include < | #include < | ||
- | // Config WiFi | + | // =================== HTMLS =================== |
- | const char* ssid = " | + | |
- | const char* password | + | |
- | #define RELAY_PIN | ||
- | |||
- | // Variables de estado | ||
- | bool ledState = false; | ||
- | bool autoBlink = true; // Estado del parpadeo automático | ||
- | |||
- | // IP fija (opcional) | ||
- | IPAddress ip(192, 168, 1, 112); | ||
- | IPAddress gateway(192, | ||
- | IPAddress subnet(255, 255, 255, 0); | ||
- | IPAddress dns(8, 8, 8, 8); | ||
- | |||
- | WebServer server(80); | ||
- | |||
- | // HTML para la página principal con control del LED | ||
const char* mainPage = R" | const char* mainPage = R" | ||
< | < | ||
Line 164: | Line 147: | ||
body { font-family: | body { font-family: | ||
.card { background: white; padding: 20px; border-radius: | .card { background: white; padding: 20px; border-radius: | ||
- | .btn { | + | .btn { padding: 10px 20px; border: none; border-radius: |
- | | + | |
- | | + | |
- | | + | |
- | | + | |
- | | + | |
- | | + | |
- | | + | |
- | | + | |
.btn-on { background-color: | .btn-on { background-color: | ||
.btn-on: | .btn-on: | ||
Line 179: | Line 154: | ||
.btn-auto { background-color: | .btn-auto { background-color: | ||
.btn-auto: | .btn-auto: | ||
- | .status { | + | .status { padding: 10px; margin: 10px 0; border-radius: |
- | | + | |
- | | + | |
- | | + | |
- | | + | |
- | | + | |
.on { background-color: | .on { background-color: | ||
.off { background-color: | .off { background-color: | ||
- | h2 { color: #333; } | ||
- | a { color: #2196F3; text-decoration: | ||
- | a:hover { text-decoration: | ||
</ | </ | ||
< | < | ||
function toggleLED() { | function toggleLED() { | ||
- | | + | |
- | xhr.open(" | + | |
- | xhr.send(); | + | |
- | setTimeout(function(){ location.reload(); }, 300); | + | |
} | } | ||
- | | ||
function toggleAuto() { | function toggleAuto() { | ||
- | | + | |
- | xhr.open(" | + | |
- | xhr.send(); | + | |
- | setTimeout(function(){ location.reload(); }, 300); | + | |
} | } | ||
</ | </ | ||
Line 209: | Line 169: | ||
< | < | ||
< | < | ||
- | | ||
<div class=" | <div class=" | ||
< | < | ||
Line 216: | Line 175: | ||
<button class=" | <button class=" | ||
</ | </ | ||
- | | ||
<div class=" | <div class=" | ||
< | < | ||
- | <p>Para actualizar el firmware, visita | + | <form method=" |
+ | | ||
+ | | ||
+ | | ||
</ | </ | ||
</ | </ | ||
Line 225: | Line 186: | ||
)rawliteral"; | )rawliteral"; | ||
- | // HTML para la página OTA | + | const char* successPage |
- | const char* otaForm | + | |
< | < | ||
< | < | ||
< | < | ||
- | < | ||
<meta charset=" | <meta charset=" | ||
- | <style> | + | <title> |
- | body { font-family: sans-serif; background-color: | + | < |
- | form { background: white; padding: 20px; border-radius: | + | |
- | input[type="submit"] { padding: 10px 20px; } | + | |
- | </style> | + | |
</ | </ | ||
- | <body> | + | < |
- | < | + | <div style="background: white; padding: 20px; border-radius: |
- | <form method="POST" action="/ | + | <h1>✅ Actualización exitosa</h1> |
- | <input type="file" name=" | + | <p>El ESP32 se reiniciará automáticamente en unos segundos...</p> |
- | <input type=" | + | |
- | | + | |
- | < | + | |
</ | </ | ||
</ | </ | ||
)rawliteral"; | )rawliteral"; | ||
- | String getMainPage() { | ||
- | String page = mainPage; | ||
- | | ||
- | // Reemplazar placeholders según el estado actual | ||
- | page.replace(" | ||
- | page.replace(" | ||
- | page.replace(" | ||
- | page.replace(" | ||
- | page.replace(" | ||
- | | ||
- | return page; | ||
- | } | ||
- | void handleRoot() { | + | // Configuración WiFi |
- | | + | const char* ssid = "XXXXXX"; |
- | } | + | const char* password = " |
- | void handleToggle() { | + | // IP estática |
- | | + | IPAddress ip(192, 168, 1, 112); |
- | | + | IPAddress gateway(192, 168, 1, 1); |
- | | + | IPAddress subnet(255, 255, 255, 0); |
- | | + | IPAddress dns(8, 8, 8, 8); |
- | } | + | |
- | void handleAuto() { | + | #define RELAY_PIN 13 |
- | autoBlink = !autoBlink; | + | |
- | server.send(200, | + | |
- | } | + | |
- | void handleUpdateForm() { | + | WebServer |
- | server.sendHeader(" | + | |
- | | + | |
- | } | + | |
- | void handleUpdateUpload() { | + | bool ledState = false; |
- | | + | bool autoBlink |
- | if (upload.status | + | unsigned long previousMillis |
- | Serial.printf(" | + | const unsigned long interval |
- | if (!Update.begin(UPDATE_SIZE_UNKNOWN)) { | + | |
- | Update.printError(Serial); | + | |
- | } | + | |
- | } else if (upload.status | + | |
- | if (Update.write(upload.buf, | + | |
- | Update.printError(Serial); | + | |
- | } | + | |
- | Serial.print(" | + | |
- | } else if (upload.status == UPLOAD_FILE_END) { | + | |
- | if (Update.end(true)) { | + | |
- | Serial.printf(" | + | |
- | } else { | + | |
- | Update.printError(Serial); | + | |
- | } | + | |
- | } | + | |
- | yield(); | + | |
- | } | + | |
- | + | ||
- | void handleUpdateFinished() { | + | |
- | if (Update.hasError()) { | + | |
- | server.send(200, | + | |
- | } else { | + | |
- | server.send(200, | + | |
- | server.client().stop(); | + | |
- | delay(5000); | + | |
- | ESP.restart(); | + | |
- | } | + | |
- | } | + | |
void setup() { | void setup() { | ||
+ | Serial.begin(115200); | ||
pinMode(RELAY_PIN, | pinMode(RELAY_PIN, | ||
- | digitalWrite(RELAY_PIN, | + | digitalWrite(RELAY_PIN, |
- | Serial.begin(115200); | + | |
- | delay(1000); | + | |
- | // WiFi | ||
WiFi.config(ip, | WiFi.config(ip, | ||
WiFi.begin(ssid, | WiFi.begin(ssid, | ||
- | Serial.print(" | + | |
+ | | ||
- | | + | |
- | | + | String html = mainPage; |
- | | + | html.replace(" |
- | } | + | |
+ | | ||
+ | html.replace(" | ||
+ | html.replace(" | ||
+ | server.send(200, | ||
+ | }); | ||
- | | + | |
- | | + | ledState = !ledState; |
+ | | ||
+ | digitalWrite(RELAY_PIN, ledState); | ||
+ | server.send(200, " | ||
+ | }); | ||
- | | + | server.on("/ |
- | | + | autoBlink = !autoBlink; |
- | server.on("/toggle", | + | server.send(200, "text/plain", |
- | server.on("/ | + | }); |
- | server.on("/update", | + | |
- | server.on("/update", | + | server.on("/ |
+ | server.sendHeader(" | ||
+ | server.send(200, "text/html; charset=utf-8", | ||
+ | delay(2000); | ||
+ | ESP.restart(); | ||
+ | | ||
+ | HTTPUpload& | ||
+ | if (upload.status == UPLOAD_FILE_START) { | ||
+ | Serial.printf("OTA iniciada: %s\n", | ||
+ | if (!Update.begin(UPDATE_SIZE_UNKNOWN)) Update.printError(Serial); | ||
+ | } else if (upload.status == UPLOAD_FILE_WRITE) { | ||
+ | if (Update.write(upload.buf, upload.currentSize) != upload.currentSize) Update.printError(Serial); | ||
+ | } else if (upload.status == UPLOAD_FILE_END) { | ||
+ | if (Update.end(true)) Serial.println(" | ||
+ | else Update.printError(Serial); | ||
+ | } | ||
+ | }); | ||
server.begin(); | server.begin(); | ||
- | Serial.println(" | ||
} | } | ||
void loop() { | void loop() { | ||
server.handleClient(); | server.handleClient(); | ||
- | | + | |
if (autoBlink) { | if (autoBlink) { | ||
- | | + | |
- | Serial.println(" | + | |
- | | + | previousMillis = currentMillis; |
- | ledState = true; | + | ledState = !ledState; |
- | | + | digitalWrite(RELAY_PIN, |
- | + | } | |
- | Serial.println(" | + | |
- | | + | |
- | | + | |
- | delay(5000); | + | |
- | | + | |
- | // Pequeña pausa para no saturar el procesador | + | |
- | delay(100); | + | |
} | } | ||
} | } | ||
+ | |||
</ | </ |
informatica/arduino/esp32/ejemplos.1746214849.txt.gz · Last modified: 2025/05/02 19:40 by jose