Przeglądaj źródła

Implementado Webserver para descargar los archivos sin tener que quitar la sd

dacowars 1 miesiąc temu
rodzic
commit
854cbc6255
1 zmienionych plików z 83 dodań i 2 usunięć
  1. 83 2
      main/main.ino

+ 83 - 2
main/main.ino

@@ -8,6 +8,8 @@
 #include "SPI.h"
 #include "Arduino.h"
 #include "math.h"
+#include "WiFi.h"
+#include "WebServer.h"
 
 /*
 Uncomment and set up if you want to use custom pins for the SPI communication
@@ -66,6 +68,13 @@ SensorData datosAntiguos;
 SemaphoreHandle_t dataMutex; // Mutex para proteger el acceso a latestData
 SemaphoreHandle_t buttonSemaphore; // Semáforo para la tarea del botón
 
+WebServer server(80);
+
+bool wifiActivado = false;
+unsigned long wifiLastActivity = 0;
+const unsigned long WIFI_TIMEOUT_MS = 300000; // 5 minutos
+const char* apSSID = "ESP32_GPS_Logger";
+const char* apPassword = "12345678";
 bool grabando = false; //inicia apagado
 bool finalizado = true; //indica que no hay ninguna grabacion ni iniciada ni pausada
 TaskHandle_t medicionesHandle = NULL; //para suspend/resume
@@ -255,6 +264,61 @@ void cerrar_archivo() {
   }
 }
 
+void activarWiFi(){
+  OLED_print("WiFi","Activando...");
+
+  WiFi.mode(WIFI_AP);
+  WiFi.softAP(apSSID, apPassword);
+
+  IPAddress IP = WiFi.softAPIP();
+  OLED_print("WiFi Activo", IP.toString());
+  delay(2000);
+
+  server.on("/", HTTP_GET, []() {
+    wifiLastActivity = millis();
+    String html = "<!DOCTYPE html><html><head><meta charset='UTF-8'><title>ESP32 GPS Logger</title>";
+    html += "<style>body{font-family:Arial;text-align:center;margin:50px;}";
+    html += "h1{color:#333;} a.button{background:#4CAF50;color:white;padding:20px 40px;";
+    html += "text-decoration:none;font-size:24px;border-radius:12px;display:inline-block;margin:20px;}</style></head>";
+    html += "<body><h1>GPS Logger</h1><p>Archivo listo para descargar:</p>";
+    html += "<a href='/download' class='button' download>Descargar " + String(filename).substring(1) + "</a>";
+    html += "<hr><p>IP: " + WiFi.softAPIP().toString() + "</p>";
+    html += "<p>Se apagar&aacute; en 5 min sin uso.</p></body></html>";
+    server.send(200, "text/html", html);
+  });
+
+  server.on("/download",HTTP_GET, []() {
+    wifiLastActivity = millis();
+
+    if (!SD.exists(filename)) {
+      server.send(404, "text/plain", "Archivo no encontrado");
+      return;
+    }
+
+    File file = SD.open(filename, FILE_READ);
+    if (!file) {  
+      server.send(404, "text/plain", "Error al abrir el archivo");
+      return;
+    }
+
+    server.streamFile(file, "application/gpx+xml");
+    file.close();
+  });
+
+  server.begin();
+  wifiActivado = true;
+  wifiLastActivity = millis();
+
+}
+
+void desactivarWiFi(){
+  server.stop();
+  WiFi.softAPdisconnect(true);
+  WiFi.mode(WIFI_OFF);
+  wifiActivado = false;
+  OLED_print("WiFi","Apagado");
+}
+
 void IRAM_ATTR isr_button() {
   unsigned long now = millis();
   if (now < ignore_isr_until) {
@@ -366,7 +430,12 @@ void task_ui(void *pvParameters){
                 finalizado = true;
                 break;
               case 2:
-                //implementacion blutuch
+                //implementacion wifi
+                if(!wifiActivado){
+                  activarWiFi();
+                } else {
+                  desactivarWiFi();
+                }
                 break;
             }
           } else {
@@ -384,7 +453,7 @@ void task_ui(void *pvParameters){
                 pantallaEstado_menu += 1;
               case 2:
                 if (finalizado) { 
-                  OLED_print("Conexion","blutuch 'WIP'");
+                  OLED_print("Conexion","wifi 'WIP'");
                   break;
                 }
                 pantallaEstado_menu += 1;
@@ -401,6 +470,18 @@ void task_ui(void *pvParameters){
           display.ssd1306_command(SSD1306_DISPLAYOFF); //se apaga la pantalla
           pantallaOn = false;
     }
+    //check wifi timeout
+    if (wifiActivado){
+      server.handleClient();
+      
+      if (WiFi.softAPgetStationNum() > 0){
+        wifiLastActivity = millis(); //reset si hay clientes conectados
+      }
+      
+      if ((millis() - wifiLastActivity) > WIFI_TIMEOUT_MS){
+        desactivarWiFi();
+      }
+    }
   }
 }