#include #include "freertos/FreeRTOS.h" #include "freertos/task.h" #include "driver/gpio.h" #include "esp_rom_sys.h" #include "esp_log.h" #include "dht22.h" #include "GPS_parser.h" #include "driver/uart.h" #include #include #include #include "esp_vfs_fat.h" #include "sdmmc_cmd.h" #include "ssd1306.h" #include const char *DHT_TAG = "DHT22"; const char *GPS_TAG = "GPS_PARSER"; const char *SD_TAG = "SD_CARD"; const char *OLED_TAG = "SSD1306OLED"; #define MOUNT_POINT "/sdcard" #define PIN_NUM_MISO CONFIG_PIN_MISO #define PIN_NUM_MOSI CONFIG_PIN_MOSI #define PIN_NUM_CLK CONFIG_PIN_CLK #define PIN_NUM_CS CONFIG_PIN_CS #define PIN_NUM_SDA CONFIG_PIN_SDA #define PIN_NUM_SCL CONFIG_PIN_SCL //definicion de tiempos de pulsacion #define PULSACION_LARGA_MS 2000 #define DURACION_WATCHDOG_MS 10000 #define MEASUREMENT_INTERVAL_S 1 const int uart_buffer_size = (1024 * 2); int length = 0; uint8_t data; static gps_parser_t gps; void uart_configurator(); static esp_err_t s_example_write_file(const char *path, char *data); static i2c_master_bus_handle_t i2c_bus0_init(gpio_num_t sda, gpio_num_t scl, uint32_t hz); struct SensorData { double latitude; double longitude; float altura; char tiempo; float velocidad; float temperature; float humidity; float pressure; }; struct SensorData latestData; struct SensorData datosAntiguos; SemaphoreHandle_t dataMutex; // Mutex para proteger el acceso a latestData SemaphoreHandle_t buttonSemaphore; // Semáforo para la tarea del botón 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 int pantallaEstado_grab = -1; //maquina de estados cuando se graba ruta int pantallaEstado_menu = -1; //maquina de estados cuando no se esta grabando ruta float distancia_total = 0.0; volatile unsigned long ignore_isr_until = 0; //para debounce char filename[13] = "/panchas.gpx"; ssd1306_handle_t d = NULL; void OLED_test(){ i2c_master_bus_handle_t i2c_bus = i2c_bus0_init(PIN_NUM_SDA, PIN_NUM_SCL, 400000); ssd1306_config_t cfg = { .width = 128, .height = 64, .fb = NULL, // let driver allocate internally .fb_len = 0, .iface.i2c = { .port = I2C_NUM_0, .addr = 0x3C, // typical SSD1306 I2C address .rst_gpio = GPIO_NUM_NC, // no reset pin }, }; ESP_ERROR_CHECK(ssd1306_new_i2c(&cfg, &d)); ESP_ERROR_CHECK(ssd1306_clear(d)); ESP_ERROR_CHECK(ssd1306_draw_rect(d, cfg.width / 2 - 50, cfg.height / 2 - 10, 100, 20, false)); ESP_ERROR_CHECK(ssd1306_display(d)); ESP_ERROR_CHECK(ssd1306_draw_circle(d, cfg.width / 2, cfg.height / 2, 20, true)); ESP_ERROR_CHECK(ssd1306_display(d)); ESP_ERROR_CHECK(ssd1306_draw_text_scaled(d, 0, 0, "Iniciando...", true, )); ESP_ERROR_CHECK(ssd1306_display(d)); vTaskDelay(pdMS_TO_TICKS(1000)); } void OLED_print(const char& line1, const char& line2){ ESP_ERROR_CHECK(ssd1306_clear(d)); ESP_ERROR_CHECK(ssd1306_draw_text_scaled(d, 0, 0, line1, true, 2)); ESP_ERROR_CHECK(ssd1306_draw_text_scaled(d, 0, 40, line2, true, 2)); ESP_ERROR_CHECK(ssd1306_display(d)); } void DHT_test (){ float t; float h; static esp_err_t err = dht_attach_pin(); if (err != ESP_OK) { OLED_print("DHT22", "Error"); ESP_LOGE(DHT_TAG, "Failed to attach DHT pin: %s", esp_err_to_name(err)); vTaskDelay(pdMS_TO_TICKS(5000)); // Wait for 5 seconds before retrying DHT_test(); //Reintentar } err = dht_read(&t, &h); if (err != ESP_OK) { OLED_print("DHT22", "Error"); ESP_LOGE(DHT_TAG, "Failed to read DHT : %s", esp_err_to_name(err)); vTaskDelay(pdMS_TO_TICKS(5000)); // Wait for 5 seconds before retrying DHT_test(); //Reintentar } else OLED_print("DHT22", "Correcto"); } void SD_test(){ } void app_main(void) { // Initialize DHT22 sensor /* float temperature; float humidity; esp_err_t err = dht_attach_pin(); if (err != ESP_OK) { ESP_LOGE(DHT_TAG, "Failed to attach DHT pin: %s", esp_err_to_name(err)); vTaskDelay(pdMS_TO_TICKS(5000)); // Wait for 5 seconds before retrying return; } vTaskDelay(pdMS_TO_TICKS(2000)); // Wait for 2 seconds before starting the main loop while (1) { if (dht_read(&temperature, &humidity) == ESP_OK) { ESP_LOGI(DHT_TAG, "Temperature: %.1f°C, Humidity: %.1f%%", temperature, humidity); } else { ESP_LOGE(DHT_TAG, "Failed to read from DHT22 sensor"); } vTaskDelay(pdMS_TO_TICKS(5000)); // Wait for 5 seconds before the next reading } */ // Initialize GPS parser /* uart_configurator(); double latitude, longitude; gps_parser_init(&gps); while(1){ while (uart_read_bytes(UART_NUM_2, &data, 1, 0)){ gps_parser_encode(&gps, data); } if (gps_location_is_valid(&gps.location)) { double lat = gps_location_lat(&gps.location); double lng = gps_location_lng(&gps.location); ESP_LOGI(GPS_TAG, "Lat: %.8f, Lng: %.8f", lat, lng); } if (gps_date_is_valid(&gps.date) && gps_time_is_valid(&gps.time)) { ESP_LOGI(GPS_TAG, "Fecha: %02u/%02u/%04u Hora: %02u:%02u:%02u\n", gps_date_day(&gps.date), gps_date_month(&gps.date), gps_date_year(&gps.date), gps_time_hour(&gps.time), gps_time_minute(&gps.time), gps_time_second(&gps.time)); } vTaskDelay(pdMS_TO_TICKS(2000)); // Wait for 1 second before the next reading } */ // Initialize SD card /* esp_vfs_fat_sdmmc_mount_config_t mount_config = { .format_if_mount_failed = false, .max_files = 5, .allocation_unit_size = 16 * 1024 }; sdmmc_card_t *card; const char mount_point[] = MOUNT_POINT; sdmmc_host_t host = SDSPI_HOST_DEFAULT(); host.max_freq_khz = 9000; //a mas freq no funciona, porque pipas spi_bus_config_t bus_cfg = { .mosi_io_num = PIN_NUM_MOSI, .miso_io_num = PIN_NUM_MISO, .sclk_io_num = PIN_NUM_CLK, .quadwp_io_num = -1, .quadhd_io_num = -1, .max_transfer_sz = 4092, }; esp_err_t ret = spi_bus_initialize(host.slot, &bus_cfg, SDSPI_DEFAULT_DMA); if (ret != ESP_OK) { ESP_LOGE(SD_TAG, "Failed to initialize bus."); return; } sdspi_device_config_t slot_config = SDSPI_DEVICE_CONFIG_DEFAULT(); slot_config.gpio_cs = PIN_NUM_CS; slot_config.host_id = host.slot; ESP_LOGI(SD_TAG, "Mounting filesystem"); ret = esp_vfs_fat_sdspi_mount(mount_point, &host, &slot_config, &mount_config, &card); if (ret != ESP_OK) { if (ret == ESP_FAIL) { ESP_LOGE(SD_TAG, "Failed to mount filesystem. " "If you want the card to be formatted, set the CONFIG_EXAMPLE_FORMAT_IF_MOUNT_FAILED menuconfig option."); } else { ESP_LOGE(SD_TAG, "Failed to initialize the card (%s). " "Make sure SD card lines have pull-up resistors in place.", esp_err_to_name(ret)); } return; } ESP_LOGI(SD_TAG, "Filesystem mounted"); // Card has been initialized, print its properties sdmmc_card_print_info(stdout, card); const char *file_foo = MOUNT_POINT"/foo.txt"; // Check if destination file exists before renaming struct stat st; if (stat(file_foo, &st) == 0) { // Delete it if it exists unlink(file_foo); } const char *file_nihao = MOUNT_POINT"/nihao.txt"; // All done, unmount partition and disable SPI peripheral esp_vfs_fat_sdcard_unmount(mount_point, card); ESP_LOGI(SD_TAG, "Card unmounted"); //deinitialize the bus after all devices are removed spi_bus_free(host.slot); */ // Initialize display /* i2c_master_bus_handle_t i2c_bus = i2c_bus0_init(GPIO_NUM_21, GPIO_NUM_22, 400000); ssd1306_config_t cfg = { .width = 128, .height = 64, .fb = NULL, // let driver allocate internally .fb_len = 0, .iface.i2c = { .port = I2C_NUM_0, .addr = 0x3C, // typical SSD1306 I2C address .rst_gpio = GPIO_NUM_NC, // no reset pin }, }; ssd1306_handle_t d = NULL; ESP_ERROR_CHECK(ssd1306_new_i2c(&cfg, &d)); // ----- Clear screen ----- ESP_ERROR_CHECK(ssd1306_clear(d)); // ----- Draw pixels in corners of screen ----- ESP_ERROR_CHECK(ssd1306_draw_pixel(d, 0, 0, true)); ESP_ERROR_CHECK(ssd1306_draw_pixel(d, cfg.width - 1, 0, true)); ESP_ERROR_CHECK(ssd1306_draw_pixel(d, 0, cfg.height - 1, true)); ESP_ERROR_CHECK(ssd1306_draw_pixel(d, cfg.width - 1, cfg.height - 1, true)); // ----- Draw rectangles ----- ESP_ERROR_CHECK(ssd1306_draw_rect(d, 2, 2, 40, 20, false)); ESP_ERROR_CHECK(ssd1306_draw_rect(d, 2, 24, 32, 16, true)); // ----- Draw circles ----- ESP_ERROR_CHECK(ssd1306_draw_circle(d, 32, 52, 8, true)); ESP_ERROR_CHECK(ssd1306_draw_circle(d, 100, 52, 4, false)); // ----- Draw lines ----- ESP_ERROR_CHECK(ssd1306_draw_line(d, 2, 2, 40, 20, true)); ESP_ERROR_CHECK(ssd1306_draw_line(d, 32, 52, 100, 52, true)); // ----- Draw text ----- ESP_ERROR_CHECK(ssd1306_draw_text(d, 48, 2, "OK!", true)); ESP_ERROR_CHECK( ssd1306_draw_text_scaled(d, 48, 10, "Hello\nWorld!", true, 2)); ESP_ERROR_CHECK(ssd1306_display(d)); ESP_LOGI(OLED_TAG, "Display updated successfully."); */ } void uart_configurator() { // Configure UART parameters uart_config_t uart_config = { .baud_rate = 115200, .data_bits = UART_DATA_8_BITS, .parity = UART_PARITY_DISABLE, .stop_bits = UART_STOP_BITS_1, .flow_ctrl = UART_HW_FLOWCTRL_DISABLE }; QueueHandle_t uart_queue; // Install UART driver using an event queue here ESP_ERROR_CHECK(uart_driver_install(UART_NUM_2, uart_buffer_size, uart_buffer_size, 10, &uart_queue, 0)); ESP_ERROR_CHECK(uart_param_config(UART_NUM_2, &uart_config)); ESP_ERROR_CHECK(uart_set_pin(UART_NUM_2, GPIO_NUM_17, GPIO_NUM_16, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE)); } static esp_err_t s_example_write_file(const char *path, char *data) { ESP_LOGI(SD_TAG, "Opening file %s", path); FILE *f = fopen(path, "w"); if (f == NULL) { ESP_LOGE(SD_TAG, "Failed to open file for writing"); return ESP_FAIL; } fprintf(f, data); fclose(f); ESP_LOGI(SD_TAG, "File written"); return ESP_OK; } // Init i2c static i2c_master_bus_handle_t i2c_bus0_init(gpio_num_t sda, gpio_num_t scl, uint32_t hz) { i2c_master_bus_config_t bus_cfg = { .i2c_port = I2C_NUM_0, .sda_io_num = sda, .scl_io_num = scl, .clk_source = I2C_CLK_SRC_DEFAULT, .glitch_ignore_cnt = 0, .flags.enable_internal_pullup = true, }; i2c_master_bus_handle_t bus = NULL; ESP_ERROR_CHECK(i2c_new_master_bus(&bus_cfg, &bus)); // NOTE: per-device speed is set when adding the device (in driver bind). return bus; }