informatica:arduino:esp32:ejemplos
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revision | |||
informatica:arduino:esp32:ejemplos [2025/05/02 20:22] – [Versión 2] 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) { | + | // Configuración WiFi |
- | if (var == "LED_STATE") return ledState ? " | + | const char* ssid = "XXXXXX"; |
- | | + | const char* password = "XXXXXXXX"; |
- | | + | |
- | | + | // 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); | ||
+ | |||
+ | #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, | 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 270: | 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 307: | Line 285: | ||
previousMillis = currentMillis; | previousMillis = currentMillis; | ||
ledState = !ledState; | ledState = !ledState; | ||
- | digitalWrite(RELAY_PIN, | + | digitalWrite(RELAY_PIN, |
} | } | ||
} | } | ||
} | } | ||
+ | |||
</ | </ | ||
- | |||
- |
informatica/arduino/esp32/ejemplos.txt · Last modified: 2025/05/02 20:23 by jose