소스 검색

Version estable, sin lightsleep

dacowars 1 개월 전
부모
커밋
f67b2d2723
1개의 변경된 파일33개의 추가작업 그리고 8개의 파일을 삭제
  1. 33 8
      main/main.ino

+ 33 - 8
main/main.ino

@@ -10,6 +10,8 @@
 #include "math.h"
 #include "WiFi.h"
 #include "WebServer.h"
+#include "esp_pm.h"
+#include "esp_sleep.h"
 
 /*
 Uncomment and set up if you want to use custom pins for the SPI communication
@@ -189,9 +191,11 @@ float calcular_delta_dist(float lat1, float  long1, float lat2, float long2){
 void task_mediciones(void *pvParameters) {
   TickType_t xLastWakeTime = xTaskGetTickCount();
   while(1) {
+    unsigned long startMillis = millis();
     // se leen los valores antes de utilizar el semaphore    
     while (gpsSerial.available() > 0) {
-      gps.encode(gpsSerial.read());
+      char c = gpsSerial.read();
+      Serial.print(c);
     }
 
     float new_latitude = gps.location.lat();
@@ -200,15 +204,14 @@ void task_mediciones(void *pvParameters) {
     String new_fecha = String(gps.date.year())+"-"+String(gps.date.month())+"-"+
                    String(gps.date.day())+"T"+String(gps.time.hour())+":"+
                    String(gps.time.minute())+":"+String(gps.time.second())+"."+
-                   String(gps.time.centisecond());
+                   String(gps.time.centisecond(),3);
     float new_speed = gps.speed.kmph();
     float new_temp = dht.readTemperature();
     float new_hum = dht.readHumidity();
     float new_press = 0.0; // Placeholder, no hay sensor de presión 
     
-    if (gps.location.isValid() && datosAntiguos.latitude != 0.0) {
-      distancia_total += calcular_delta_dist(datosAntiguos.latitude, datosAntiguos.longitude, new_latitude, new_longitude);
-    }
+    distancia_total += calcular_delta_dist(datosAntiguos.latitude, datosAntiguos.longitude, new_latitude, new_longitude);
+    
 
     if (xSemaphoreTake(dataMutex, portMAX_DELAY) == pdTRUE) {
       latestData.latitude = new_latitude;
@@ -264,8 +267,11 @@ void task_mediciones(void *pvParameters) {
       
       file.close();
     }
+    unsigned long elapsedMillis = millis() - startMillis;
+    Serial.println(elapsedMillis);
 
     vTaskDelayUntil(&xLastWakeTime, pdMS_TO_TICKS(MEASUREMENT_INTERVAL_S*1000)); // Espera x*1000 milisegundos
+    
   }
 }
 
@@ -464,13 +470,13 @@ void task_ui(void *pvParameters){
                 OLED_print("Distancia",String(distancia_total)+"km");
                 break;
               case 2:
-                OLED_print("Altitud",String(gps.altitude.meters(), 1)+"m");
+                OLED_print("Altitud",String(currentData.altura, 1)+"m");
                 break;
               case 3:
                 OLED_print("Temp/Hum",String(currentData.temperature,1)+"C/"+String(currentData.humidity,1)+"%");
                 break;  
               case 4:
-                OLED_print("Velocidad",String(gps.speed.kmph())+"km/h");
+                OLED_print("Velocidad",String(currentData.velocidad, 1)+"km/h");
                 break;
             }
           }
@@ -556,6 +562,7 @@ void task_ui(void *pvParameters){
 
 
 void setup() {
+  Serial.begin(115200);
   pinMode(BUTTON_PIN, INPUT_PULLUP);
   attachInterrupt(digitalPinToInterrupt(BUTTON_PIN), isr_button, FALLING);
   
@@ -572,6 +579,10 @@ void setup() {
   SD_test();
   delay(1000);
   // GPS check
+  // Enable RMC and VTG at 1 Hz
+  gpsSerial.println("$PMTK314,0,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0*29");  // Enables GGA, RMC, VTG
+  gpsSerial.println("$PMTK220,1000*1F");  // Set update rate to 1 Hz (1000ms)
+  delay(1000);  // Give time to apply
   GPS_test_wait();
   delay(2000);
 
@@ -596,6 +607,20 @@ void setup() {
     1                    // Núcleo donde se ejecuta
   );
 
+  esp_pm_config_esp32_t pm_config = {
+    .max_freq_mhz = 240,
+    .min_freq_mhz = 80,
+    .light_sleep_enable = true
+  };
+
+  esp_err_t err = esp_pm_configure(&pm_config);
+
+  if (err != ESP_OK) {
+    Serial.println("Error configuring power management");
+  }
+
+  WiFi.setSleep(true); // Desactiva el modo de ahorro de energía del WiFi
+
   vTaskSuspend(medicionesHandle); //inicia suspendida
 
 }
@@ -604,5 +629,5 @@ void setup() {
 
 void loop() {
 
-  vTaskDelay(pdMS_TO_TICKS(1000)); // Espera 1 segundo
+  vTaskDelete(NULL); // Deshabilita el loop
 }