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 20:11] – jose | informatica: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 | ||
| < | < | ||
| #include < | #include < | ||
| Line 137: | Line 135: | ||
| #include < | #include < | ||
| - | // WiFi | + | // =================== HTMLS =================== |
| - | const char* ssid = " | + | |
| - | const char* password | + | |
| - | // 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, | ||
| - | IPAddress subnet(255, 255, 255, 0); | ||
| - | IPAddress dns(8, 8, 8, 8); | ||
| - | |||
| - | WebServer server(80); | ||
| - | |||
| - | // Página principal | ||
| const char* mainPage = R" | const char* mainPage = R" | ||
| < | < | ||
| Line 203: | Line 179: | ||
| <form method=" | <form method=" | ||
| <input type=" | <input type=" | ||
| - | <input type=" | + | <input type=" |
| </ | </ | ||
| </ | </ | ||
| Line 210: | Line 186: | ||
| )rawliteral"; | )rawliteral"; | ||
| - | // Página de éxito OTA | ||
| const char* successPage = R" | const char* successPage = R" | ||
| < | < | ||
| < | < | ||
| - | < | + | < |
| - | < | + | |
| - | <h2>✅ Actualización | + | |
| - | <p>Reiniciando | + | <meta http-equiv=" |
| + | | ||
| + | < | ||
| + | <div style=" | ||
| + | <h1>✅ Actualización | ||
| + | <p>El ESP32 se reiniciará automáticamente en unos segundos...</p> | ||
| + | </div> | ||
| </ | </ | ||
| </ | </ | ||
| )rawliteral"; | )rawliteral"; | ||
| - | // Reemplazo de variables | ||
| - | String processor(const String& var) { | ||
| - | if (var == " | ||
| - | if (var == " | ||
| - | if (var == " | ||
| - | if (var == " | ||
| - | if (var == " | ||
| - | return String(); | ||
| - | } | ||
| - | void setup() { | + | // Configuración |
| - | pinMode(RELAY_PIN, | + | const char* ssid = "XXXXXX"; |
| - | digitalWrite(RELAY_PIN, | + | const char* password = "XXXXXXXX"; |
| - | Serial.begin(115200); | + | |
| - | + | ||
| - | WiFi.config(ip, | + | |
| - | WiFi.begin(ssid, | + | |
| - | while (WiFi.status() != WL_CONNECTED) { | + | |
| - | delay(500); | + | |
| - | Serial.print(" | + | |
| - | } | + | |
| - | Serial.println(" | + | |
| - | + | ||
| - | // Página principal | + | |
| - | server.on("/", | + | |
| - | String html = mainPage; | + | |
| - | html.replace(" | + | |
| - | html.replace(" | + | |
| - | html.replace(" | + | |
| - | html.replace(" | + | |
| - | html.replace(" | + | |
| - | server.send(200, | + | |
| - | }); | + | |
| - | + | ||
| - | // Toggle LED | + | |
| - | server.on("/ | + | |
| - | ledState = !ledState; | + | |
| - | digitalWrite(RELAY_PIN, | + | |
| - | autoBlink = false; | + | |
| - | server.send(200, | + | |
| - | }); | + | |
| - | + | ||
| - | // Toggle parpadeo | + | |
| - | server.on("/ | + | |
| - | autoBlink = !autoBlink; | + | |
| - | server.send(200, | + | |
| - | }); | + | |
| - | + | ||
| - | // OTA Upload | + | |
| - | server.on("/ | + | |
| - | server.sendHeader(" | + | |
| - | server.send(200, | + | |
| - | delay(1500); | + | |
| - | ESP.restart(); | + | |
| - | }, []() { | + | |
| - | HTTPUpload& | + | |
| - | if (upload.status == UPLOAD_FILE_START) { | + | |
| - | Serial.printf(" | + | |
| - | if (!Update.begin(UPDATE_SIZE_UNKNOWN)) { | + | |
| - | Update.printError(Serial); | + | |
| - | } | + | |
| - | } else if (upload.status == UPLOAD_FILE_WRITE) { | + | |
| - | if (Update.write(upload.buf, | + | |
| - | Update.printError(Serial); | + | |
| - | } | + | |
| - | } else if (upload.status == UPLOAD_FILE_END) { | + | |
| - | if (Update.end(true)) { | + | |
| - | Serial.println(" | + | |
| - | } else { | + | |
| - | Update.printError(Serial); | + | |
| - | } | + | |
| - | } | + | |
| - | }); | + | |
| - | + | ||
| - | server.begin(); | + | |
| - | } | + | |
| - | + | ||
| - | void loop() { | + | |
| - | server.handleClient(); | + | |
| - | + | ||
| - | if (autoBlink) { | + | |
| - | unsigned long currentMillis = millis(); | + | |
| - | if (currentMillis - previousMillis >= interval) { | + | |
| - | previousMillis = currentMillis; | + | |
| - | ledState = !ledState; | + | |
| - | digitalWrite(RELAY_PIN, | + | |
| - | } | + | |
| - | } | + | |
| - | } | + | |
| - | </ | + | |
| - | + | ||
| - | ===== Versión 2 ===== | + | |
| - | Tarda mucho en actualizar | + | |
| - | + | ||
| - | < | + | |
| - | #include < | + | |
| - | #include < | + | |
| - | #include < | + | |
| - | + | ||
| - | // WiFi | + | |
| - | 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) | // IP estática (opcional) | ||
| Line 341: | Line 213: | ||
| IPAddress subnet(255, 255, 255, 0); | IPAddress subnet(255, 255, 255, 0); | ||
| IPAddress dns(8, 8, 8, 8); | IPAddress dns(8, 8, 8, 8); | ||
| + | |||
| + | #define RELAY_PIN 13 | ||
| WebServer server(80); | WebServer server(80); | ||
| - | // Página principal | + | bool ledState |
| - | const char* mainPage | + | bool autoBlink |
| - | < | + | |
| - | < | + | |
| - | < | + | |
| - | < | + | |
| - | <meta charset=" | + | |
| - | <meta name=" | + | |
| - | < | + | |
| - | body { font-family: | + | |
| - | .card { background: white; padding: 20px; border-radius: | + | |
| - | .btn { padding: 10px 20px; border: none; border-radius: | + | |
| - | .btn-on { background-color: | + | |
| - | .btn-on: | + | |
| - | .btn-off { background-color: | + | |
| - | .btn-off: | + | |
| - | .btn-auto { background-color: | + | |
| - | .btn-auto: | + | |
| - | .status { padding: 10px; margin: 10px 0; border-radius: | + | |
| - | .on { background-color: | + | |
| - | .off { background-color: | + | |
| - | </ | + | |
| - | < | + | |
| - | function toggleLED() { | + | |
| - | fetch("/ | + | |
| - | } | + | |
| - | function toggleAuto() { | + | |
| - | fetch("/ | + | |
| - | } | + | |
| - | </ | + | |
| - | </ | + | |
| - | < | + | |
| - | < | + | |
| - | <div class=" | + | |
| - | < | + | |
| - | <div class=" | + | |
| - | <button class=" | + | |
| - | <button class=" | + | |
| - | </ | + | |
| - | <div class=" | + | |
| - | < | + | |
| - | <form method=" | + | |
| - | <input type=" | + | |
| - | <input type=" | + | |
| - | </ | + | |
| - | </ | + | |
| - | </ | + | |
| - | </ | + | |
| - | )rawliteral" | + | |
| - | // Página de éxito OTA | + | unsigned long previousMillis |
| - | const char* successPage | + | const unsigned long interval |
| - | < | + | |
| - | < | + | |
| - | < | + | |
| - | < | + | |
| - | < | + | |
| - | < | + | |
| - | </ | + | |
| - | </ | + | |
| - | )rawliteral" | + | |
| - | + | ||
| - | // Reemplazo de variables | + | |
| - | String processor(const String& var) { | + | |
| - | if (var == " | + | |
| - | if (var == " | + | |
| - | if (var == " | + | |
| - | if (var == " | + | |
| - | if (var == " | + | |
| - | return String(); | + | |
| - | } | + | |
| void setup() { | void setup() { | ||
| + | Serial.begin(115200); | ||
| pinMode(RELAY_PIN, | pinMode(RELAY_PIN, | ||
| digitalWrite(RELAY_PIN, | digitalWrite(RELAY_PIN, | ||
| - | Serial.begin(115200); | ||
| WiFi.config(ip, | WiFi.config(ip, | ||
| WiFi.begin(ssid, | WiFi.begin(ssid, | ||
| - | while (WiFi.status() != WL_CONNECTED) | + | while (WiFi.status() != WL_CONNECTED) delay(100); |
| - | | + | Serial.print(" |
| - | Serial.print(" | + | |
| - | } | + | |
| - | | + | |
| - | // Página principal | ||
| server.on("/", | server.on("/", | ||
| String html = mainPage; | String html = mainPage; | ||
| - | html.replace(" | + | html.replace(" |
| - | html.replace(" | + | html.replace(" |
| - | html.replace(" | + | html.replace(" |
| - | html.replace(" | + | html.replace(" |
| - | html.replace(" | + | html.replace(" |
| - | server.send(200, | + | server.send(200, |
| }); | }); | ||
| - | // Toggle LED | ||
| server.on("/ | server.on("/ | ||
| ledState = !ledState; | ledState = !ledState; | ||
| - | digitalWrite(RELAY_PIN, | ||
| autoBlink = false; | autoBlink = false; | ||
| + | digitalWrite(RELAY_PIN, | ||
| server.send(200, | server.send(200, | ||
| }); | }); | ||
| - | // Toggle parpadeo | ||
| server.on("/ | server.on("/ | ||
| autoBlink = !autoBlink; | autoBlink = !autoBlink; | ||
| Line 454: | Line 256: | ||
| }); | }); | ||
| - | // OTA Upload | ||
| server.on("/ | server.on("/ | ||
| server.sendHeader(" | server.sendHeader(" | ||
| - | server.send(200, | + | server.send(200, |
| - | delay(1500); // Espera antes de reiniciar | + | delay(2000); |
| ESP.restart(); | ESP.restart(); | ||
| }, []() { | }, []() { | ||
| HTTPUpload& | HTTPUpload& | ||
| if (upload.status == UPLOAD_FILE_START) { | if (upload.status == UPLOAD_FILE_START) { | ||
| - | Serial.printf(" | + | Serial.printf(" |
| - | if (!Update.begin(UPDATE_SIZE_UNKNOWN)) | + | if (!Update.begin(UPDATE_SIZE_UNKNOWN)) Update.printError(Serial); |
| - | | + | |
| - | } | + | |
| } else if (upload.status == UPLOAD_FILE_WRITE) { | } else if (upload.status == UPLOAD_FILE_WRITE) { | ||
| - | if (Update.write(upload.buf, | + | if (Update.write(upload.buf, |
| - | | + | |
| - | } | + | |
| } else if (upload.status == UPLOAD_FILE_END) { | } else if (upload.status == UPLOAD_FILE_END) { | ||
| - | if (Update.end(true)) | + | if (Update.end(true)) Serial.println(" |
| - | | + | else Update.printError(Serial); |
| - | | + | |
| - | | + | |
| - | } | + | |
| } | } | ||
| }); | }); | ||
| Line 491: | Line 285: | ||
| previousMillis = currentMillis; | previousMillis = currentMillis; | ||
| ledState = !ledState; | ledState = !ledState; | ||
| - | digitalWrite(RELAY_PIN, | + | digitalWrite(RELAY_PIN, |
| } | } | ||
| } | } | ||
informatica/arduino/esp32/ejemplos.1746216714.txt.gz · Last modified: by jose
