dht22.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. esp_err_t dht_attach_pin(void);
  22. static esp_err_t get_change_time(uint8_t timeout_us, bool expected_level, uint32_t *time_us);
  23. static inline esp_err_t dht_communication(uint32_t *data);
  24. static inline int16_t data_to_int16(uint8_t MSB, uint8_t LSB);
  25. esp_err_t dht_read(float *temperature, float *humidity);
  26. esp_err_t dht_attach_pin(){
  27. if (DHT_PIN < GPIO_NUM_0 || DHT_PIN >= GPIO_NUM_MAX) {
  28. ESP_LOGE(TAG, "Invalid GPIO pin number: %d", DHT_PIN);
  29. return ESP_ERR_INVALID_ARG;
  30. }
  31. gpio_set_direction(DHT_PIN, GPIO_MODE_INPUT_OUTPUT_OD);
  32. gpio_set_level(DHT_PIN, 1); // Set the pin high by default
  33. return ESP_OK;
  34. }
  35. static esp_err_t get_change_time(uint8_t timeout_us, bool expected_level, uint32_t *time_us){
  36. *time_us = 0;
  37. while (*time_us < timeout_us){
  38. if (gpio_get_level(DHT_PIN) == expected_level) {
  39. return ESP_OK;
  40. }
  41. *time_us += US_DELAY;
  42. esp_rom_delay_us(US_DELAY);
  43. }
  44. return ESP_ERR_TIMEOUT;
  45. }
  46. static inline esp_err_t dht_communication(uint32_t *data){
  47. uint32_t high_time;
  48. uint32_t low_time;
  49. // Send start signal
  50. gpio_set_direction(DHT_PIN, GPIO_MODE_INPUT_OUTPUT_OD);
  51. gpio_set_level(DHT_PIN, 0);
  52. esp_rom_delay_us(1500); // Pull low for at least 1ms
  53. gpio_set_level(DHT_PIN, 1); // Release the line
  54. CHECK_LOGE(get_change_time(44, 0, &low_time), "Failed to get dht response"); // Wait for the sensor to pull low 20-40us
  55. CHECK_LOGE(get_change_time(88, 1, &high_time), "Failed to get dht response"); // Wait for the sensor to pull high 80us
  56. CHECK_LOGE(get_change_time(88, 0, &low_time), "Failed to get dht response"); // Wait for the sensor to pull low 80us
  57. uint8_t checksum = 0;
  58. for (uint8_t i = 0; i < DHT_DATA_BITS; i++){
  59. // Wait for the sensor to pull low 50us
  60. CHECK_LOGE(get_change_time(65, 1, &low_time), "Failed dht transmission %d h", i);
  61. // Wait for the sensor to pull high 26-28us for 0 or 70us for 1
  62. CHECK_LOGE(get_change_time(80, 0, &high_time), "Failed dht transmission %d l", i);
  63. if (i < 32){
  64. *data |= ((high_time > low_time) << (31 - i)); // Set the bit if high_time is greater than low_time
  65. }
  66. else checksum |= (high_time > low_time) << (39 - i); // Calculate checksum in the last 8 bits
  67. }
  68. if (checksum != (uint8_t)((*data >> 24) + (*data >> 16 & 0xFF) + (*data >> 8 & 0xFF) + (*data & 0xFF))) {
  69. ESP_LOGE(TAG, "DHT22 checksum mismatch: calculated %02X, received %02X", checksum, *data & 0xFF);
  70. return ESP_ERR_INVALID_CRC;
  71. }
  72. return ESP_OK;
  73. }
  74. static inline int16_t data_to_int16(uint8_t MSB, uint8_t LSB){
  75. return (int16_t)((MSB << 8) | LSB);
  76. }
  77. esp_err_t dht_read(float *temperature, float *humidity){
  78. uint32_t data = 0;
  79. gpio_set_direction(DHT_PIN, GPIO_MODE_INPUT_OUTPUT_OD);
  80. gpio_set_level(DHT_PIN, 1);
  81. portENTER_CRITICAL(&mux);
  82. esp_err_t err = dht_communication(&data);
  83. portEXIT_CRITICAL(&mux);
  84. gpio_set_direction(DHT_PIN, GPIO_MODE_INPUT_OUTPUT_OD);
  85. gpio_set_level(DHT_PIN, 1);
  86. if (err != ESP_OK) return err;
  87. *humidity = data_to_int16(data >> 24 & 0xFF, data >> 16 & 0xFF) / 10.0;
  88. *temperature = data_to_int16(data >> 8 & 0xFF, data & 0xFF) / 10.0;
  89. return ESP_OK;
  90. }