informatica:arduino:esp32:ejemplos
Differences
This shows you the differences between two versions of the page.
Next revision | Previous revision | ||
informatica:arduino:esp32:ejemplos [2025/05/02 19:28] – created jose | informatica:arduino:esp32:ejemplos [2025/05/02 20:23] (current) – [Versión 1] jose | ||
---|---|---|---|
Line 127: | Line 127: | ||
delay(5000); | delay(5000); | ||
} | } | ||
+ | </ | ||
+ | |||
+ | ====== Encender LED desde página WEB ====== | ||
+ | < | ||
+ | #include < | ||
+ | #include < | ||
+ | #include < | ||
+ | |||
+ | // =================== HTMLS =================== | ||
+ | |||
+ | const char* mainPage = R" | ||
+ | < | ||
+ | < | ||
+ | < | ||
+ | < | ||
+ | <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"; | ||
+ | |||
+ | const char* successPage = R" | ||
+ | < | ||
+ | < | ||
+ | < | ||
+ | <meta charset=" | ||
+ | < | ||
+ | <meta http-equiv=" | ||
+ | </ | ||
+ | <body style=" | ||
+ | <div style=" | ||
+ | < | ||
+ | <p>El ESP32 se reiniciará automáticamente en unos segundos...</ | ||
+ | </ | ||
+ | </ | ||
+ | </ | ||
+ | )rawliteral"; | ||
+ | |||
+ | |||
+ | // Configuración WiFi | ||
+ | const char* ssid = " | ||
+ | const char* password = " | ||
+ | |||
+ | // 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); | ||
+ | |||
+ | #define RELAY_PIN 13 | ||
+ | |||
+ | WebServer server(80); | ||
+ | |||
+ | bool ledState = false; | ||
+ | bool autoBlink = true; | ||
+ | |||
+ | unsigned long previousMillis = 0; | ||
+ | const unsigned long interval = 500; | ||
+ | |||
+ | void setup() { | ||
+ | Serial.begin(115200); | ||
+ | pinMode(RELAY_PIN, | ||
+ | digitalWrite(RELAY_PIN, | ||
+ | |||
+ | WiFi.config(ip, | ||
+ | WiFi.begin(ssid, | ||
+ | while (WiFi.status() != WL_CONNECTED) delay(100); | ||
+ | Serial.print(" | ||
+ | |||
+ | server.on("/", | ||
+ | String html = mainPage; | ||
+ | html.replace(" | ||
+ | html.replace(" | ||
+ | html.replace(" | ||
+ | html.replace(" | ||
+ | html.replace(" | ||
+ | server.send(200, | ||
+ | }); | ||
+ | |||
+ | server.on("/ | ||
+ | ledState = !ledState; | ||
+ | autoBlink = false; | ||
+ | digitalWrite(RELAY_PIN, | ||
+ | server.send(200, | ||
+ | }); | ||
+ | |||
+ | server.on("/ | ||
+ | autoBlink = !autoBlink; | ||
+ | server.send(200, | ||
+ | }); | ||
+ | |||
+ | server.on("/ | ||
+ | server.sendHeader(" | ||
+ | server.send(200, | ||
+ | delay(2000); | ||
+ | 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, | ||
+ | } 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, | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | |||
</ | </ |
informatica/arduino/esp32/ejemplos.1746214105.txt.gz · Last modified: 2025/05/02 19:28 by jose