main.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  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. #include "ssd1306.h"
  16. #include <driver/i2c_master.h>
  17. const char *DHT_TAG = "DHT22";
  18. const char *GPS_TAG = "GPS_PARSER";
  19. const char *SD_TAG = "SD_CARD";
  20. const char *OLED_TAG = "SSD1306OLED";
  21. #define MOUNT_POINT "/sdcard"
  22. #define PIN_NUM_MISO CONFIG_PIN_MISO
  23. #define PIN_NUM_MOSI CONFIG_PIN_MOSI
  24. #define PIN_NUM_CLK CONFIG_PIN_CLK
  25. #define PIN_NUM_CS CONFIG_PIN_CS
  26. const int uart_buffer_size = (1024 * 2);
  27. int length = 0;
  28. uint8_t data;
  29. static gps_parser_t gps;
  30. void uart_configurator();
  31. static esp_err_t s_example_write_file(const char *path, char *data);
  32. static i2c_master_bus_handle_t i2c_bus0_init(gpio_num_t sda, gpio_num_t scl, uint32_t hz);
  33. void app_main(void)
  34. {
  35. // Initialize DHT22 sensor
  36. /*
  37. float temperature;
  38. float humidity;
  39. esp_err_t err = dht_attach_pin();
  40. if (err != ESP_OK) {
  41. ESP_LOGE(DHT_TAG, "Failed to attach DHT pin: %s", esp_err_to_name(err));
  42. vTaskDelay(pdMS_TO_TICKS(5000)); // Wait for 5 seconds before retrying
  43. return;
  44. }
  45. vTaskDelay(pdMS_TO_TICKS(2000)); // Wait for 2 seconds before starting the main loop
  46. while (1) {
  47. if (dht_read(&temperature, &humidity) == ESP_OK) {
  48. ESP_LOGI(DHT_TAG, "Temperature: %.1f°C, Humidity: %.1f%%", temperature, humidity);
  49. } else {
  50. ESP_LOGE(DHT_TAG, "Failed to read from DHT22 sensor");
  51. }
  52. vTaskDelay(pdMS_TO_TICKS(5000)); // Wait for 5 seconds before the next reading
  53. }
  54. */
  55. // Initialize GPS parser
  56. /*
  57. uart_configurator();
  58. double latitude, longitude;
  59. gps_parser_init(&gps);
  60. while(1){
  61. while (uart_read_bytes(UART_NUM_2, &data, 1, 0)){
  62. gps_parser_encode(&gps, data);
  63. }
  64. if (gps_location_is_valid(&gps.location)) {
  65. double lat = gps_location_lat(&gps.location);
  66. double lng = gps_location_lng(&gps.location);
  67. ESP_LOGI(GPS_TAG, "Lat: %.8f, Lng: %.8f", lat, lng);
  68. }
  69. if (gps_date_is_valid(&gps.date) && gps_time_is_valid(&gps.time)) {
  70. ESP_LOGI(GPS_TAG, "Fecha: %02u/%02u/%04u Hora: %02u:%02u:%02u\n",
  71. gps_date_day(&gps.date),
  72. gps_date_month(&gps.date),
  73. gps_date_year(&gps.date),
  74. gps_time_hour(&gps.time),
  75. gps_time_minute(&gps.time),
  76. gps_time_second(&gps.time));
  77. }
  78. vTaskDelay(pdMS_TO_TICKS(2000)); // Wait for 1 second before the next reading
  79. }
  80. */
  81. // Initialize SD card
  82. /*
  83. esp_vfs_fat_sdmmc_mount_config_t mount_config = {
  84. .format_if_mount_failed = false,
  85. .max_files = 5,
  86. .allocation_unit_size = 16 * 1024
  87. };
  88. sdmmc_card_t *card;
  89. const char mount_point[] = MOUNT_POINT;
  90. sdmmc_host_t host = SDSPI_HOST_DEFAULT();
  91. host.max_freq_khz = 9000; //a mas freq no funciona, porque pipas
  92. spi_bus_config_t bus_cfg = {
  93. .mosi_io_num = PIN_NUM_MOSI,
  94. .miso_io_num = PIN_NUM_MISO,
  95. .sclk_io_num = PIN_NUM_CLK,
  96. .quadwp_io_num = -1,
  97. .quadhd_io_num = -1,
  98. .max_transfer_sz = 4092,
  99. };
  100. esp_err_t ret = spi_bus_initialize(host.slot, &bus_cfg, SDSPI_DEFAULT_DMA);
  101. if (ret != ESP_OK) {
  102. ESP_LOGE(SD_TAG, "Failed to initialize bus.");
  103. return;
  104. }
  105. sdspi_device_config_t slot_config = SDSPI_DEVICE_CONFIG_DEFAULT();
  106. slot_config.gpio_cs = PIN_NUM_CS;
  107. slot_config.host_id = host.slot;
  108. ESP_LOGI(SD_TAG, "Mounting filesystem");
  109. ret = esp_vfs_fat_sdspi_mount(mount_point, &host, &slot_config, &mount_config, &card);
  110. if (ret != ESP_OK) {
  111. if (ret == ESP_FAIL) {
  112. ESP_LOGE(SD_TAG, "Failed to mount filesystem. "
  113. "If you want the card to be formatted, set the CONFIG_EXAMPLE_FORMAT_IF_MOUNT_FAILED menuconfig option.");
  114. } else {
  115. ESP_LOGE(SD_TAG, "Failed to initialize the card (%s). "
  116. "Make sure SD card lines have pull-up resistors in place.", esp_err_to_name(ret));
  117. }
  118. return;
  119. }
  120. ESP_LOGI(SD_TAG, "Filesystem mounted");
  121. // Card has been initialized, print its properties
  122. sdmmc_card_print_info(stdout, card);
  123. const char *file_foo = MOUNT_POINT"/foo.txt";
  124. // Check if destination file exists before renaming
  125. struct stat st;
  126. if (stat(file_foo, &st) == 0) {
  127. // Delete it if it exists
  128. unlink(file_foo);
  129. }
  130. const char *file_nihao = MOUNT_POINT"/nihao.txt";
  131. // All done, unmount partition and disable SPI peripheral
  132. esp_vfs_fat_sdcard_unmount(mount_point, card);
  133. ESP_LOGI(SD_TAG, "Card unmounted");
  134. //deinitialize the bus after all devices are removed
  135. spi_bus_free(host.slot);
  136. */
  137. // Initialize display
  138. /*
  139. i2c_master_bus_handle_t i2c_bus =
  140. i2c_bus0_init(GPIO_NUM_21, GPIO_NUM_22, 400000);
  141. ssd1306_config_t cfg = {
  142. .width = 128,
  143. .height = 64,
  144. .fb = NULL, // let driver allocate internally
  145. .fb_len = 0,
  146. .iface.i2c =
  147. {
  148. .port = I2C_NUM_0,
  149. .addr = 0x3C, // typical SSD1306 I2C address
  150. .rst_gpio = GPIO_NUM_NC, // no reset pin
  151. },
  152. };
  153. ssd1306_handle_t d = NULL;
  154. ESP_ERROR_CHECK(ssd1306_new_i2c(&cfg, &d));
  155. // ----- Clear screen -----
  156. ESP_ERROR_CHECK(ssd1306_clear(d));
  157. // ----- Draw pixels in corners of screen -----
  158. ESP_ERROR_CHECK(ssd1306_draw_pixel(d, 0, 0, true));
  159. ESP_ERROR_CHECK(ssd1306_draw_pixel(d, cfg.width - 1, 0, true));
  160. ESP_ERROR_CHECK(ssd1306_draw_pixel(d, 0, cfg.height - 1, true));
  161. ESP_ERROR_CHECK(ssd1306_draw_pixel(d, cfg.width - 1, cfg.height - 1, true));
  162. // ----- Draw rectangles -----
  163. ESP_ERROR_CHECK(ssd1306_draw_rect(d, 2, 2, 40, 20, false));
  164. ESP_ERROR_CHECK(ssd1306_draw_rect(d, 2, 24, 32, 16, true));
  165. // ----- Draw circles -----
  166. ESP_ERROR_CHECK(ssd1306_draw_circle(d, 32, 52, 8, true));
  167. ESP_ERROR_CHECK(ssd1306_draw_circle(d, 100, 52, 4, false));
  168. // ----- Draw lines -----
  169. ESP_ERROR_CHECK(ssd1306_draw_line(d, 2, 2, 40, 20, true));
  170. ESP_ERROR_CHECK(ssd1306_draw_line(d, 32, 52, 100, 52, true));
  171. // ----- Draw text -----
  172. ESP_ERROR_CHECK(ssd1306_draw_text(d, 48, 2, "OK!", true));
  173. ESP_ERROR_CHECK(
  174. ssd1306_draw_text_scaled(d, 48, 10, "Hello\nWorld!", true, 2));
  175. ESP_ERROR_CHECK(ssd1306_display(d));
  176. ESP_LOGI(OLED_TAG, "Display updated successfully.");
  177. */
  178. }
  179. void uart_configurator() {
  180. // Configure UART parameters
  181. uart_config_t uart_config = {
  182. .baud_rate = 115200,
  183. .data_bits = UART_DATA_8_BITS,
  184. .parity = UART_PARITY_DISABLE,
  185. .stop_bits = UART_STOP_BITS_1,
  186. .flow_ctrl = UART_HW_FLOWCTRL_DISABLE
  187. };
  188. QueueHandle_t uart_queue;
  189. // Install UART driver using an event queue here
  190. ESP_ERROR_CHECK(uart_driver_install(UART_NUM_2, uart_buffer_size, uart_buffer_size, 10, &uart_queue, 0));
  191. ESP_ERROR_CHECK(uart_param_config(UART_NUM_2, &uart_config));
  192. ESP_ERROR_CHECK(uart_set_pin(UART_NUM_2, GPIO_NUM_17, GPIO_NUM_16, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE));
  193. }
  194. static esp_err_t s_example_write_file(const char *path, char *data)
  195. {
  196. ESP_LOGI(SD_TAG, "Opening file %s", path);
  197. FILE *f = fopen(path, "w");
  198. if (f == NULL) {
  199. ESP_LOGE(SD_TAG, "Failed to open file for writing");
  200. return ESP_FAIL;
  201. }
  202. fprintf(f, data);
  203. fclose(f);
  204. ESP_LOGI(SD_TAG, "File written");
  205. return ESP_OK;
  206. }
  207. // Init i2c
  208. static i2c_master_bus_handle_t i2c_bus0_init(gpio_num_t sda, gpio_num_t scl, uint32_t hz) {
  209. i2c_master_bus_config_t bus_cfg = {
  210. .i2c_port = I2C_NUM_0,
  211. .sda_io_num = sda,
  212. .scl_io_num = scl,
  213. .clk_source = I2C_CLK_SRC_DEFAULT,
  214. .glitch_ignore_cnt = 0,
  215. .flags.enable_internal_pullup = true,
  216. };
  217. i2c_master_bus_handle_t bus = NULL;
  218. ESP_ERROR_CHECK(i2c_new_master_bus(&bus_cfg, &bus));
  219. // NOTE: per-device speed is set when adding the device (in driver bind).
  220. return bus;
  221. }