main.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. #include <stdio.h>
  2. #include "freertos/FreeRTOS.h"
  3. #include "freertos/task.h"
  4. #include "driver/gpio.h"
  5. #include "esp_rom_sys.h"
  6. #include "esp_log.h"
  7. #include "dht22.h"
  8. #include "GPS_parser.h"
  9. #include "driver/uart.h"
  10. #include <string.h>
  11. #include <sys/unistd.h>
  12. #include <sys/stat.h>
  13. #include "esp_vfs_fat.h"
  14. #include "sdmmc_cmd.h"
  15. const char *DHT_TAG = "DHT22";
  16. const char *GPS_TAG = "GPS_PARSER";
  17. const char *SD_TAG = "SD_CARD";
  18. #define MOUNT_POINT "/sdcard"
  19. #define PIN_NUM_MISO CONFIG_PIN_MISO
  20. #define PIN_NUM_MOSI CONFIG_PIN_MOSI
  21. #define PIN_NUM_CLK CONFIG_PIN_CLK
  22. #define PIN_NUM_CS CONFIG_PIN_CS
  23. const int uart_buffer_size = (1024 * 2);
  24. int length = 0;
  25. uint8_t data;
  26. static gps_parser_t gps;
  27. void uart_configurator();
  28. static esp_err_t s_example_write_file(const char *path, char *data);
  29. void app_main(void)
  30. {
  31. // Initialize DHT22 sensor
  32. /*
  33. float temperature;
  34. float humidity;
  35. esp_err_t err = dht_attach_pin();
  36. if (err != ESP_OK) {
  37. ESP_LOGE(DHT_TAG, "Failed to attach DHT pin: %s", esp_err_to_name(err));
  38. vTaskDelay(pdMS_TO_TICKS(5000)); // Wait for 5 seconds before retrying
  39. return;
  40. }
  41. vTaskDelay(pdMS_TO_TICKS(2000)); // Wait for 2 seconds before starting the main loop
  42. while (1) {
  43. if (dht_read(&temperature, &humidity) == ESP_OK) {
  44. ESP_LOGI(DHT_TAG, "Temperature: %.1f°C, Humidity: %.1f%%", temperature, humidity);
  45. } else {
  46. ESP_LOGE(DHT_TAG, "Failed to read from DHT22 sensor");
  47. }
  48. vTaskDelay(pdMS_TO_TICKS(5000)); // Wait for 5 seconds before the next reading
  49. }
  50. */
  51. // Initialize GPS parser
  52. /*
  53. uart_configurator();
  54. double latitude, longitude;
  55. gps_parser_init(&gps);
  56. while(1){
  57. while (uart_read_bytes(UART_NUM_2, &data, 1, 0)){
  58. gps_parser_encode(&gps, data);
  59. }
  60. if (gps_location_is_valid(&gps.location)) {
  61. double lat = gps_location_lat(&gps.location);
  62. double lng = gps_location_lng(&gps.location);
  63. ESP_LOGI(GPS_TAG, "Lat: %.8f, Lng: %.8f", lat, lng);
  64. }
  65. if (gps_date_is_valid(&gps.date) && gps_time_is_valid(&gps.time)) {
  66. ESP_LOGI(GPS_TAG, "Fecha: %02u/%02u/%04u Hora: %02u:%02u:%02u\n",
  67. gps_date_day(&gps.date),
  68. gps_date_month(&gps.date),
  69. gps_date_year(&gps.date),
  70. gps_time_hour(&gps.time),
  71. gps_time_minute(&gps.time),
  72. gps_time_second(&gps.time));
  73. }
  74. vTaskDelay(pdMS_TO_TICKS(2000)); // Wait for 1 second before the next reading
  75. }
  76. */
  77. // Initialize SD card
  78. /*
  79. esp_vfs_fat_sdmmc_mount_config_t mount_config = {
  80. .format_if_mount_failed = false,
  81. .max_files = 5,
  82. .allocation_unit_size = 16 * 1024
  83. };
  84. sdmmc_card_t *card;
  85. const char mount_point[] = MOUNT_POINT;
  86. sdmmc_host_t host = SDSPI_HOST_DEFAULT();
  87. host.max_freq_khz = 9000; //a mas freq no funciona, porque pipas
  88. spi_bus_config_t bus_cfg = {
  89. .mosi_io_num = PIN_NUM_MOSI,
  90. .miso_io_num = PIN_NUM_MISO,
  91. .sclk_io_num = PIN_NUM_CLK,
  92. .quadwp_io_num = -1,
  93. .quadhd_io_num = -1,
  94. .max_transfer_sz = 4092,
  95. };
  96. esp_err_t ret = spi_bus_initialize(host.slot, &bus_cfg, SDSPI_DEFAULT_DMA);
  97. if (ret != ESP_OK) {
  98. ESP_LOGE(SD_TAG, "Failed to initialize bus.");
  99. return;
  100. }
  101. sdspi_device_config_t slot_config = SDSPI_DEVICE_CONFIG_DEFAULT();
  102. slot_config.gpio_cs = PIN_NUM_CS;
  103. slot_config.host_id = host.slot;
  104. ESP_LOGI(SD_TAG, "Mounting filesystem");
  105. ret = esp_vfs_fat_sdspi_mount(mount_point, &host, &slot_config, &mount_config, &card);
  106. if (ret != ESP_OK) {
  107. if (ret == ESP_FAIL) {
  108. ESP_LOGE(SD_TAG, "Failed to mount filesystem. "
  109. "If you want the card to be formatted, set the CONFIG_EXAMPLE_FORMAT_IF_MOUNT_FAILED menuconfig option.");
  110. } else {
  111. ESP_LOGE(SD_TAG, "Failed to initialize the card (%s). "
  112. "Make sure SD card lines have pull-up resistors in place.", esp_err_to_name(ret));
  113. }
  114. return;
  115. }
  116. ESP_LOGI(SD_TAG, "Filesystem mounted");
  117. // Card has been initialized, print its properties
  118. sdmmc_card_print_info(stdout, card);
  119. const char *file_foo = MOUNT_POINT"/foo.txt";
  120. // Check if destination file exists before renaming
  121. struct stat st;
  122. if (stat(file_foo, &st) == 0) {
  123. // Delete it if it exists
  124. unlink(file_foo);
  125. }
  126. const char *file_nihao = MOUNT_POINT"/nihao.txt";
  127. // All done, unmount partition and disable SPI peripheral
  128. esp_vfs_fat_sdcard_unmount(mount_point, card);
  129. ESP_LOGI(SD_TAG, "Card unmounted");
  130. //deinitialize the bus after all devices are removed
  131. spi_bus_free(host.slot);
  132. */
  133. }
  134. void uart_configurator() {
  135. // Configure UART parameters
  136. uart_config_t uart_config = {
  137. .baud_rate = 115200,
  138. .data_bits = UART_DATA_8_BITS,
  139. .parity = UART_PARITY_DISABLE,
  140. .stop_bits = UART_STOP_BITS_1,
  141. .flow_ctrl = UART_HW_FLOWCTRL_DISABLE
  142. };
  143. QueueHandle_t uart_queue;
  144. // Install UART driver using an event queue here
  145. ESP_ERROR_CHECK(uart_driver_install(UART_NUM_2, uart_buffer_size, uart_buffer_size, 10, &uart_queue, 0));
  146. ESP_ERROR_CHECK(uart_param_config(UART_NUM_2, &uart_config));
  147. ESP_ERROR_CHECK(uart_set_pin(UART_NUM_2, GPIO_NUM_17, GPIO_NUM_16, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE));
  148. }
  149. static esp_err_t s_example_write_file(const char *path, char *data)
  150. {
  151. ESP_LOGI(SD_TAG, "Opening file %s", path);
  152. FILE *f = fopen(path, "w");
  153. if (f == NULL) {
  154. ESP_LOGE(SD_TAG, "Failed to open file for writing");
  155. return ESP_FAIL;
  156. }
  157. fprintf(f, data);
  158. fclose(f);
  159. ESP_LOGI(SD_TAG, "File written");
  160. return ESP_OK;
  161. }