main.c 958 B

12345678910111213141516171819202122232425262728293031323334
  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. static const char *TAG = "DHT22";
  9. void app_main(void)
  10. {
  11. float temperature;
  12. float humidity;
  13. esp_err_t err = dht_attach_pin();
  14. if (err != ESP_OK) {
  15. ESP_LOGE(TAG, "Failed to attach DHT pin: %s", esp_err_to_name(err));
  16. vTaskDelay(pdMS_TO_TICKS(5000)); // Wait for 5 seconds before retrying
  17. return;
  18. }
  19. vTaskDelay(pdMS_TO_TICKS(2000)); // Wait for 2 seconds before starting the main loop
  20. while (1) {
  21. if (dht_read(&temperature, &humidity) == ESP_OK) {
  22. ESP_LOGI(TAG, "Temperature: %.1f°C, Humidity: %.1f%%", temperature, humidity);
  23. } else {
  24. ESP_LOGE(TAG, "Failed to read from DHT22 sensor");
  25. }
  26. vTaskDelay(pdMS_TO_TICKS(5000)); // Wait for 5 seconds before the next reading
  27. }
  28. }