#include "TinyGPSPlus.h" #include "DHT.h" #include #include #include #include "FS.h" #include "SD.h" #include "SPI.h" #include "Arduino.h" #include "math.h" /* Uncomment and set up if you want to use custom pins for the SPI communication #define REASSIGN_PINS int sck = -1; int miso = -1; int mosi = -1; int cs = -1; */ // Inicia I2C en pines default ESP32 (21 SDA, 22 SCL) #define SCREEN_WIDTH 128 // Ancho píxeles #define SCREEN_HEIGHT 64 // Alto píxeles #define OLED_RESET -1 // Reset pin (no usado) // Pines para UART2 (Serial2) #define RX_PIN 16 // RX del ESP32 conectado a TX del GPS #define TX_PIN 17 // TX del ESP32 conectado a RX del GPS #define GPS_BAUD 115200 #define DHTPIN 4 // Digital pin connected to the DHT sensor #define DHTTYPE DHT22 #define BUTTON_PIN 27 // pin para pulsador //definicion de tiempos de pulsacion #define PULASCION_LARGA_MS 2000 #define DURACION_WATCHDOG_MS 10000 #define MEASUREMENT_INTERVAL_S 1 //separación entre mediciones (s) #define DEG2RAD (M_PI/180.0) // Objeto TinyGPS++ TinyGPSPlus gps; HardwareSerial gpsSerial(2); // Usar UART2 DHT dht(DHTPIN, DHTTYPE); Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET); //valores de los sensores struct SensorData { double latitude = 0.0; double longitude = 0.0; float temperature = 0.0; float humidity = 0.0; float pressure = 0.0; float altura = 0.0; }; SensorData latestData; SensorData datosAntiguos; SemaphoreHandle_t dataMutex; // Mutex para proteger el acceso a latestData SemaphoreHandle_t buttonSemaphore; // Semáforo para la tarea del botón //flag para reducir las lecturas de DHT y presion uint8_t readSensorsFlag = 0; bool grabando = false; //inicia apagado TaskHandle_t medicionesHandle = NULL; //para suspend/resume int pantallaEstado = 0; //maquina de estados float distancia_total = 0.0; char filename[13]; void OLED_print(const String& line1, const String& line2) { display.clearDisplay(); display.setTextSize(2); display.setTextColor(SSD1306_WHITE); display.setCursor(0, 0); display.println(line1); display.println(line2); display.display(); } void DHT_test() { dht.begin(); float h = dht.readHumidity(); float t = dht.readTemperature(); if (isnan(h) || isnan(t)) { Serial.println("Failed to read from DHT sensor!"); OLED_print("DHT22", "Error"); delay(5000); DHT_test(); // Reintentar } else OLED_print("DHT22", "Correcto"); } void OLED_test() { //pantallazo a blanco y luego iniciando // Inicia I2C en pines default ESP32 (21 SDA, 22 SCL) Wire.begin(); if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { // Dirección común: 0x3C Serial.println(F("Error: OLED no encontrado!")); for (;;); // Para siempre } display.clearDisplay(); display.fillScreen(SSD1306_WHITE); // Pantalla blanca delay(500); display.display(); display.clearDisplay(); display.setTextSize(2); // Tamaño texto display.setTextColor(SSD1306_WHITE); display.setCursor(0, 0); // Posición display.println("Iniciando..."); display.display(); // Muestra delay(1000); } void SD_test(){ if (!SD.begin()) { Serial.println("SD Card Mount Failed"); OLED_print("SD Card", "Error\nInserte"); while (!SD.begin()); OLED_print("SD Card", "Insertada"); } else { OLED_print("SD Card", "Correcto"); Serial.println("SD Card initialized."); } uint8_t cardType = SD.cardType(); if (cardType == CARD_NONE) { Serial.println("No SD card attached"); OLED_print("SD Card", "No detectada"); while (cardType == CARD_NONE) { delay(1000); cardType = SD.cardType(); } OLED_print("SD Card", "Detectada"); } uint8_t cardSize = SD.cardSize() / (1024 * 1024); Serial.printf("SD Card Size: %uMB\n", cardSize); int num = 1; sprintf(filename, "/data%03d.txt", num); while (SD.exists(filename)) { Serial.printf("File %s already exists. Trying next.\n", filename); num++; sprintf(filename, "/data%03d.txt", num); } Serial.printf("Using file: %s\n", filename); File file = SD.open(filename, FILE_WRITE); if (file) { Serial.println("File created successfully"); file.println("Latitud,Longitud,Temperatura,Humedad,Presion"); file.close(); } else { Serial.println("Error creando archivo"); } } void GPS_test_wait() { // Iniciar Serial2 para GPS gpsSerial.begin(GPS_BAUD, SERIAL_8N1, RX_PIN, TX_PIN); while ((gpsSerial.available() > 0) && !gps.location.isValid()) { gps.encode(gpsSerial.read()); delay(10); OLED_print("GPS", "Esperando."); delay(100); OLED_print("GPS", "Esperando.."); delay(100); OLED_print("GPS", "Esperando..."); } OLED_print("GPS", "Encontrado"); } float calcular_delta_dist(float lat1, float long1, float lat2, float long2){ float R = 6371.0; // Radio de la Tierra en km float delta_lat = (lat2 - lat1) * DEG2RAD; float delta_long = (long2 - long1) * DEG2RAD; lat1 = lat1 * DEG2RAD; lat2 = lat2 * DEG2RAD; float a = sin(delta_lat/2)*sin(delta_lat/2)+cos(lat1)*cos(lat2)*sin(delta_long/2)*sin(delta_long/2); float c = 2 * atan2(sqrt(a),sqrt(1-a)); return R * c; //En km } void task_mediciones(void *pvParameters) { TickType_t xLastWakeTime = xTaskGetTickCount(); while(1) { // se leen los valores antes de utilizar el semaphore while (gpsSerial.available() > 0) { gps.encode(gpsSerial.read()); } float new_latitude = gps.location.lat(); float new_longitude = gps.location.lng(); float new_temp = (readSensorsFlag % 60 == 0) ? dht.readTemperature() : latestData.temperature; float new_hum = (readSensorsFlag % 120 == 0) ? dht.readHumidity() : latestData.humidity; 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); } if (xSemaphoreTake(dataMutex, portMAX_DELAY) == pdTRUE) { latestData.latitude = new_latitude; latestData.longitude = new_longitude; latestData.temperature = new_temp; latestData.humidity = new_hum; latestData.pressure = new_press; datosAntiguos = latestData; xSemaphoreGive(dataMutex); } File file = SD.open(filename, FILE_APPEND); if (file) { //Crear la string para escribir en el archivo String frase = String(datosAntiguos.latitude) + "," + String(datosAntiguos.longitude) + "," + String(datosAntiguos.temperature) + "," + String(datosAntiguos.humidity) + "," + String(datosAntiguos.pressure); // Escribir datos en el archivo file.println(frase); file.close(); } if (!(readSensorsFlag % 120)) readSensorsFlag = 0; readSensorsFlag++; vTaskDelayUntil(&xLastWakeTime, pdMS_TO_TICKS(MEASUREMENT_INTERVAL_S*1000)); // Espera x*1000 milisegundos } } void IRAM_ATTR isr_button() { static unsigned long lastInterrupt = 0; if ((millis() - lastInterrupt) > 200 ){ // debounce de 200 ms BaseType_t xHigherPriorityTaskWoken = pdFALSE; xSemaphoreGiveFromISR(buttonSemaphore, NULL); lastInterrupt = millis(); if (xHigherPriorityTaskWoken) { portYIELD_FROM_ISR(); } } } void task_ui(void *pvParameters){ unsigned long pressTime = 0; unsigned long lastActivity = millis(); bool pantallaOn = true; //comprobar el estado inicial, no se cual sera while(1){ if (xSemaphoreTake(buttonSemaphore, pdMS_TO_TICKS(200)) == pdTRUE){ //button pressed pressTime = millis(); if (!pantallaOn){ display.begin(SSD1306_SWITCHCAPVCC, 0x3C); pantallaOn = true; } bool timed_out = false; while (digitalRead(BUTTON_PIN) == LOW){ vTaskDelay(pdMS_TO_TICKS(10)); if ((millis() - pressTime) > DURACION_WATCHDOG_MS){ //10s timeout para evitar bloqueos timed_out = true; break; } } if (timed_out){ OLED_print("Apagando","pantalla"); delay(1000); display.ssd1306_command(SSD1306_DISPLAYOFF); //se apaga la pantallaOn = false; } else { unsigned long duration = millis() - pressTime; if (duration >= PULASCION_LARGA_MS){ //pulsacion larga: cabia entre grabacion y no grabacion de datos grabando != grabando; if (grabando){ vTaskResume(medicionesHandle); //Mostrar que empieza la grabación OLED_print("Ruta","iniciada"); } else { vTaskSuspend(medicionesHandle); //Mostrar que se pausa/finaliza la grabación OLED_print("Ruta","pausada"); } } else { //Pulsacion corta + grabando datos, cicla datos if (grabando) { pantallaEstado = (pantallaEstado + 1) % 3; //cicla entre 0-2 SensorData currentData; if(xSemaphoreTake(dataMutex, portMAX_DELAY) == pdTRUE){ currentData = latestData; xSemaphoreGive(dataMutex); } switch (pantallaEstado){ case 0: OLED_print("Posicion",String(currentData.longitude) + "," + String(currentData.latitude)); break; case 1: OLED_print("Distancia",String(distancia_total)+"km"); break; case 2: OLED_print("Altitud",String(gps.altitude.meters(), 1)+"m"); break; } } else { OLED_print("Grabacion","pausada"); } } } lastActivity = millis(); //reset watchdog } //check watchdog fuera del boton if (pantallaOn && ((millis() - lastActivity) > DURACION_WATCHDOG_MS)){ display.ssd1306_command(SSD1306_DISPLAYOFF); //se apaga la pantalla pantallaOn = false; } vTaskDelay(pdMS_TO_TICKS(100)); //pequeño delay para no busy waiting } } void setup() { Serial.begin(115200); pinMode(BUTTON_PIN, INPUT_PULLUP); attachInterrupt(digitalPinToInterrupt(BUTTON_PIN), isr_button, FALLING); buttonSemaphore = xSemaphoreCreateBinary(); dataMutex = xSemaphoreCreateMutex(); // OLED check OLED_test(); delay(1000); // DHT check DHT_test(); delay(1000); // SD Card check SD_test(); delay(1000); // GPS check GPS_test_wait(); delay(2000); // Crear tarea para mediciones xTaskCreatePinnedToCore( task_mediciones, // Función de la tarea "Mediciones", // Nombre de la tarea 2048, // Tamaño del stack NULL, // Parámetro de la tarea 10, // Prioridad de la tarea &medicionesHandle, // Handle de la tarea 0 // Núcleo donde se ejecuta ); xTaskCreatePinnedToCore( task_ui, // Función de la tarea "UI", // Nombre de la tarea 8192, // Tamaño del stack NULL, // Parámetro de la tarea 5, // Prioridad de la tarea NULL, // Handle de la tarea 1 // Núcleo donde se ejecuta ); vTaskSuspend(medicionesHandle); //inicia suspendida } void loop() { vTaskDelay(pdMS_TO_TICKS(1000)); // Espera 1 segundo }