main.c 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. const char *DHT_TAG = "DHT22";
  11. const char *GPS_TAG = "GPS_PARSER";
  12. const int uart_buffer_size = (1024 * 2);
  13. int length = 0;
  14. uint8_t data;
  15. static gps_parser_t gps;
  16. void read_uart_char();
  17. void uart_configurator();
  18. void app_main(void)
  19. {
  20. // Initialize DHT22 sensor
  21. /*
  22. float temperature;
  23. float humidity;
  24. esp_err_t err = dht_attach_pin();
  25. if (err != ESP_OK) {
  26. ESP_LOGE(DHT_TAG, "Failed to attach DHT pin: %s", esp_err_to_name(err));
  27. vTaskDelay(pdMS_TO_TICKS(5000)); // Wait for 5 seconds before retrying
  28. return;
  29. }
  30. vTaskDelay(pdMS_TO_TICKS(2000)); // Wait for 2 seconds before starting the main loop
  31. while (1) {
  32. if (dht_read(&temperature, &humidity) == ESP_OK) {
  33. ESP_LOGI(DHT_TAG, "Temperature: %.1f°C, Humidity: %.1f%%", temperature, humidity);
  34. } else {
  35. ESP_LOGE(DHT_TAG, "Failed to read from DHT22 sensor");
  36. }
  37. vTaskDelay(pdMS_TO_TICKS(5000)); // Wait for 5 seconds before the next reading
  38. }
  39. */
  40. // Initialize GPS parser
  41. uart_configurator();
  42. double latitude, longitude;
  43. gps_parser_init(&gps);
  44. while(1){
  45. while (uart_read_bytes(UART_NUM_2, &data, 1, 0)){
  46. gps_parser_encode(&gps, data);
  47. }
  48. if (gps_location_is_valid(&gps.location)) {
  49. double lat = gps_location_lat(&gps.location);
  50. double lng = gps_location_lng(&gps.location);
  51. ESP_LOGI(GPS_TAG, "Lat: %.8f, Lng: %.8f", lat, lng);
  52. }
  53. if (gps_date_is_valid(&gps.date) && gps_time_is_valid(&gps.time)) {
  54. ESP_LOGI(GPS_TAG, "Fecha: %02u/%02u/%04u Hora: %02u:%02u:%02u\n",
  55. gps_date_day(&gps.date),
  56. gps_date_month(&gps.date),
  57. gps_date_year(&gps.date),
  58. gps_time_hour(&gps.time),
  59. gps_time_minute(&gps.time),
  60. gps_time_second(&gps.time));
  61. }
  62. vTaskDelay(pdMS_TO_TICKS(2000)); // Wait for 1 second before the next reading
  63. }
  64. }
  65. void uart_configurator() {
  66. // Configure UART parameters
  67. uart_config_t uart_config = {
  68. .baud_rate = 115200,
  69. .data_bits = UART_DATA_8_BITS,
  70. .parity = UART_PARITY_DISABLE,
  71. .stop_bits = UART_STOP_BITS_1,
  72. .flow_ctrl = UART_HW_FLOWCTRL_DISABLE
  73. };
  74. QueueHandle_t uart_queue;
  75. // Install UART driver using an event queue here
  76. ESP_ERROR_CHECK(uart_driver_install(UART_NUM_2, uart_buffer_size, uart_buffer_size, 10, &uart_queue, 0));
  77. ESP_ERROR_CHECK(uart_param_config(UART_NUM_2, &uart_config));
  78. ESP_ERROR_CHECK(uart_set_pin(UART_NUM_2, GPIO_NUM_17, GPIO_NUM_16, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE));
  79. }
  80. void read_uart_char() {
  81. ESP_ERROR_CHECK(uart_get_buffered_data_len(UART_NUM_2, (size_t*)&length));
  82. // Read one byte from UART
  83. }