User Tools

Site Tools


informatica:arduino:esp32:ejemplos

This is an old revision of the document!


LED parpadeando

OTA con LED parpadeando en el pin 13

#include <WiFi.h>
#include <WebServer.h>
#include <Update.h>

// Config WiFi
const char* ssid = "XXXXXXXX";
const char* password = "XXXXXXXX";

#define RELAY_PIN  13  // usa cualquier pin digital disponible

// IP fija (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);

// HTML para la página OTA
const char* otaForm = R"rawliteral(
<!DOCTYPE html>
<html>
  <head>
    <title>ESP32 OTA Update</title>
    <meta charset="utf-8">
    <style>
      body { font-family: sans-serif; background-color: #f4f4f4; padding: 30px; }
      form { background: white; padding: 20px; border-radius: 8px; box-shadow: 0 0 10px #ccc; }
      input[type="submit"] { padding: 10px 20px; }
    </style>
  </head>
  <body>
    <h2>ESP32 OTA Firmware Update</h2>
    <form method="POST" action="/update" enctype="multipart/form-data">
      <input type="file" name="update">
      <input type="submit" value="Subir firmware">
    </form>
  </body>
</html>
)rawliteral";

void handleRoot() {
  server.send(200, "text/html", "<h1>ESP32 OTA disponible en <a href='/update'>/update</a></h1>");
}

void handleUpdateForm() {
  server.sendHeader("Connection", "close");
  server.send(200, "text/html", otaForm);
}

void handleUpdateUpload() {
  HTTPUpload& upload = server.upload();

  if (upload.status == UPLOAD_FILE_START) {
    Serial.printf("\nIniciando actualización: %s\n", upload.filename.c_str());
    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);
    }
    Serial.print(".");
  } else if (upload.status == UPLOAD_FILE_END) {
    if (Update.end(true)) {
      Serial.printf("\nActualización completa: %u bytes\n", upload.totalSize);
    } else {
      Update.printError(Serial);
    }
  }
  yield();
}

void handleUpdateFinished() {
  if (Update.hasError()) {
    server.send(200, "text/html", "<h1>❌ Fallo en la actualización</h1><p>Revisa la consola serie para más detalles.</p>");
  } else {
    server.send(200, "text/html", "<h1>✅ Actualización exitosa</h1><p>Reiniciando en 5 segundos...</p>");
    server.client().stop(); // cerrar conexión limpiamente
    delay(5000);
    ESP.restart();
  }
}

void setup() {
  pinMode(RELAY_PIN, OUTPUT);
  digitalWrite(RELAY_PIN, LOW); // asegúrate de que esté apagado al inicio
  Serial.begin(115200);
  delay(1000);

  // WiFi
  WiFi.config(ip, gateway, subnet, dns);
  WiFi.begin(ssid, password);
  Serial.print("Conectando a WiFi");

  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }

  Serial.println("\nWiFi conectado. IP: ");
  Serial.println(WiFi.localIP());

  // Rutas web
  server.on("/", handleRoot);
  server.on("/update", HTTP_GET, handleUpdateForm);
  server.on("/update", HTTP_POST, handleUpdateFinished, handleUpdateUpload);

  server.begin();
  Serial.println("Servidor web OTA iniciado");
}

void loop() {
  server.handleClient();

  Serial.println("Encendiendo Led");
  digitalWrite(RELAY_PIN, HIGH); // activa el relé
  delay(5000); // bomba encendida 5 segundos

  Serial.println("Apagando Led");
  digitalWrite(RELAY_PIN, LOW);  // desactiva el relé
  delay(5000); // bomba apagada 5 segundos
}
informatica/arduino/esp32/ejemplos.1746214105.txt.gz · Last modified: by jose