main.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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. #define DHT_PIN GPIO_NUM_4
  8. #define US_DELAY 3
  9. #define DHT_DATA_BITS 40
  10. static const char *TAG = "DHT22";
  11. static portMUX_TYPE mux = portMUX_INITIALIZER_UNLOCKED;
  12. // Macro to check the return value of a function and log an error if it is not ESP_OK
  13. // portEXIT_CRITICAL(&mux); despues del if
  14. #define CHECK_LOGE(x, msg, ...) do { \
  15. esp_err_t __; \
  16. if ((__ = x) != ESP_OK) { \
  17. portEXIT_CRITICAL(&mux); \
  18. ESP_LOGE(TAG, msg, ## __VA_ARGS__); \
  19. return __; \
  20. } \
  21. } while (0)
  22. gpio_num_t dht_pin = DHT_PIN;
  23. static esp_err_t dht_attach_pin(gpio_num_t pin){
  24. if (pin < GPIO_NUM_0 || pin >= GPIO_NUM_MAX) {
  25. ESP_LOGE(TAG, "Invalid GPIO pin number: %d", pin);
  26. return ESP_ERR_INVALID_ARG;
  27. }
  28. gpio_set_direction(pin, GPIO_MODE_INPUT_OUTPUT_OD);
  29. gpio_set_level(pin, 1); // Set the pin high by default
  30. return ESP_OK;
  31. }
  32. static esp_err_t get_change_time(gpio_num_t pin, uint8_t timeout_us, bool expected_level, uint32_t *time_us){
  33. *time_us = 0;
  34. while (*time_us < timeout_us){
  35. if (gpio_get_level(pin) == expected_level) {
  36. return ESP_OK;
  37. }
  38. *time_us += US_DELAY;
  39. esp_rom_delay_us(US_DELAY);
  40. }
  41. return ESP_ERR_TIMEOUT;
  42. }
  43. static inline esp_err_t dht_communication(gpio_num_t pin, uint32_t *data){
  44. uint32_t high_time;
  45. uint32_t low_time;
  46. // Send start signal
  47. gpio_set_direction(pin, GPIO_MODE_INPUT_OUTPUT_OD);
  48. gpio_set_level(pin, 0);
  49. esp_rom_delay_us(1500); // Pull low for at least 1ms
  50. gpio_set_level(pin, 1); // Release the line
  51. CHECK_LOGE(get_change_time(pin, 44, 0, &low_time), "Failed to get dht response"); // Wait for the sensor to pull low 20-40us
  52. CHECK_LOGE(get_change_time(pin, 88, 1, &high_time), "Failed to get dht response"); // Wait for the sensor to pull high 80us
  53. CHECK_LOGE(get_change_time(pin, 88, 0, &low_time), "Failed to get dht response"); // Wait for the sensor to pull low 80us
  54. uint8_t checksum = 0;
  55. for (uint8_t i = 0; i < DHT_DATA_BITS; i++){
  56. // Wait for the sensor to pull low 50us
  57. CHECK_LOGE(get_change_time(pin, 65, 1, &low_time), "Failed dht transmission %d h", i);
  58. // Wait for the sensor to pull high 26-28us for 0 or 70us for 1
  59. CHECK_LOGE(get_change_time(pin, 80, 0, &high_time), "Failed dht transmission %d l", i);
  60. if (i < 32){
  61. *data |= ((high_time > low_time) << (31 - i)); // Set the bit if high_time is greater than low_time
  62. }
  63. else checksum |= (high_time > low_time) << (39 - i); // Calculate checksum in the last 8 bits
  64. }
  65. if (checksum != (uint8_t)((*data >> 24) + (*data >> 16 & 0xFF) + (*data >> 8 & 0xFF) + (*data & 0xFF))) {
  66. ESP_LOGE(TAG, "DHT22 checksum mismatch: calculated %02X, received %02X", checksum, *data & 0xFF);
  67. return ESP_ERR_INVALID_CRC;
  68. }
  69. return ESP_OK;
  70. }
  71. static inline int16_t data_to_int16(uint8_t MSB, uint8_t LSB){
  72. return (int16_t)((MSB << 8) | LSB);
  73. }
  74. esp_err_t dht_read(gpio_num_t pin, float *temperature, float *humidity){
  75. uint32_t data = 0;
  76. gpio_set_direction(pin, GPIO_MODE_INPUT_OUTPUT_OD);
  77. gpio_set_level(pin, 1);
  78. portENTER_CRITICAL(&mux);
  79. esp_err_t err = dht_communication(pin, &data);
  80. portEXIT_CRITICAL(&mux);
  81. gpio_set_direction(pin, GPIO_MODE_INPUT_OUTPUT_OD);
  82. gpio_set_level(pin, 1);
  83. if (err != ESP_OK) return err;
  84. *humidity = data_to_int16(data >> 24 & 0xFF, data >> 16 & 0xFF) / 10.0;
  85. *temperature = data_to_int16(data >> 8 & 0xFF, data & 0xFF) / 10.0;
  86. return ESP_OK;
  87. }
  88. void app_main(void)
  89. {
  90. float temperature;
  91. float humidity;
  92. esp_err_t err = dht_attach_pin(dht_pin);
  93. if (err != ESP_OK) {
  94. ESP_LOGE(TAG, "Failed to attach DHT pin: %s", esp_err_to_name(err));
  95. vTaskDelay(pdMS_TO_TICKS(5000)); // Wait for 5 seconds before retrying
  96. return;
  97. }
  98. vTaskDelay(pdMS_TO_TICKS(2000)); // Wait for 2 seconds before starting the main loop
  99. while (1) {
  100. if (dht_read(dht_pin, &temperature, &humidity) == ESP_OK) {
  101. ESP_LOGI(TAG, "Temperature: %.1f°C, Humidity: %.1f%%", temperature, humidity);
  102. } else {
  103. ESP_LOGE(TAG, "Failed to read from DHT22 sensor");
  104. }
  105. vTaskDelay(pdMS_TO_TICKS(5000)); // Wait for 5 seconds before the next reading
  106. }
  107. }