main.c 4.0 KB

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