dht22.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. #include <stdio.h>
  2. #include "dht22.h"
  3. #include "freertos/FreeRTOS.h"
  4. #include "freertos/task.h"
  5. #include "driver/gpio.h"
  6. #include "esp_rom_sys.h"
  7. #include "esp_log.h"
  8. #define DHT_PIN CONFIG_DHT_PIN
  9. #define US_DELAY 3
  10. #define DHT_DATA_BITS 40
  11. static const char *TAG = "DHT22";
  12. static portMUX_TYPE mux = portMUX_INITIALIZER_UNLOCKED;
  13. // Macro to check the return value of a function and log an error if it is not ESP_OK
  14. #define CHECK_LOGE(x, msg, ...) do { \
  15. esp_err_t __; \
  16. if ((__ = x) != ESP_OK) { \
  17. ESP_LOGE(TAG, msg, ## __VA_ARGS__); \
  18. return __; \
  19. } \
  20. } while (0)
  21. static esp_err_t dht_attach_pin(){
  22. if (DHT_PIN < GPIO_NUM_0 || DHT_PIN >= GPIO_NUM_MAX) {
  23. ESP_LOGE(TAG, "Invalid GPIO pin number: %d", DHT_PIN);
  24. return ESP_ERR_INVALID_ARG;
  25. }
  26. gpio_set_direction(DHT_PIN, GPIO_MODE_INPUT_OUTPUT_OD);
  27. gpio_set_level(DHT_PIN, 1); // Set the pin high by default
  28. return ESP_OK;
  29. }
  30. static esp_err_t get_change_time(uint8_t timeout_us, bool expected_level, uint32_t *time_us){
  31. *time_us = 0;
  32. while (*time_us < timeout_us){
  33. if (gpio_get_level(DHT_PIN) == expected_level) {
  34. return ESP_OK;
  35. }
  36. *time_us += US_DELAY;
  37. esp_rom_delay_us(US_DELAY);
  38. }
  39. return ESP_ERR_TIMEOUT;
  40. }
  41. static inline esp_err_t dht_communication(uint32_t *data){
  42. uint32_t high_time;
  43. uint32_t low_time;
  44. // Send start signal
  45. gpio_set_direction(DHT_PIN, GPIO_MODE_INPUT_OUTPUT_OD);
  46. gpio_set_level(DHT_PIN, 0);
  47. esp_rom_delay_us(1500); // Pull low for at least 1ms
  48. gpio_set_level(DHT_PIN, 1); // Release the line
  49. CHECK_LOGE(get_change_time(44, 0, &low_time), "Failed to get dht response"); // Wait for the sensor to pull low 20-40us
  50. CHECK_LOGE(get_change_time(88, 1, &high_time), "Failed to get dht response"); // Wait for the sensor to pull high 80us
  51. CHECK_LOGE(get_change_time(88, 0, &low_time), "Failed to get dht response"); // Wait for the sensor to pull low 80us
  52. uint8_t checksum = 0;
  53. for (uint8_t i = 0; i < DHT_DATA_BITS; i++){
  54. // Wait for the sensor to pull low 50us
  55. CHECK_LOGE(get_change_time(65, 1, &low_time), "Failed dht transmission %d h", i);
  56. // Wait for the sensor to pull high 26-28us for 0 or 70us for 1
  57. CHECK_LOGE(get_change_time(80, 0, &high_time), "Failed dht transmission %d l", i);
  58. if (i < 32){
  59. *data |= ((high_time > low_time) << (31 - i)); // Set the bit if high_time is greater than low_time
  60. }
  61. else checksum |= (high_time > low_time) << (39 - i); // Calculate checksum in the last 8 bits
  62. }
  63. if (checksum != (uint8_t)((*data >> 24) + (*data >> 16 & 0xFF) + (*data >> 8 & 0xFF) + (*data & 0xFF))) {
  64. ESP_LOGE(TAG, "DHT22 checksum mismatch: calculated %02X, received %02X", checksum, *data & 0xFF);
  65. return ESP_ERR_INVALID_CRC;
  66. }
  67. return ESP_OK;
  68. }
  69. static inline int16_t data_to_int16(uint8_t MSB, uint8_t LSB){
  70. return (int16_t)((MSB << 8) | LSB);
  71. }
  72. esp_err_t dht_read(float *temperature, float *humidity){
  73. uint32_t data = 0;
  74. gpio_set_direction(DHT_PIN, GPIO_MODE_INPUT_OUTPUT_OD);
  75. gpio_set_level(DHT_PIN, 1);
  76. portENTER_CRITICAL(&mux);
  77. esp_err_t err = dht_communication(&data);
  78. portEXIT_CRITICAL(&mux);
  79. gpio_set_direction(DHT_PIN, GPIO_MODE_INPUT_OUTPUT_OD);
  80. gpio_set_level(DHT_PIN, 1);
  81. if (err != ESP_OK) return err;
  82. *humidity = data_to_int16(data >> 24 & 0xFF, data >> 16 & 0xFF) / 10.0;
  83. *temperature = data_to_int16(data >> 8 & 0xFF, data & 0xFF) / 10.0;
  84. return ESP_OK;
  85. }