| 12345678910111213141516171819202122232425262728293031323334 |
- #include <stdio.h>
- #include "freertos/FreeRTOS.h"
- #include "freertos/task.h"
- #include "driver/gpio.h"
- #include "esp_rom_sys.h"
- #include "esp_log.h"
- #include "dht22.h"
- static const char *TAG = "DHT22";
- void app_main(void)
- {
-
- float temperature;
- float humidity;
- esp_err_t err = dht_attach_pin();
- if (err != ESP_OK) {
- ESP_LOGE(TAG, "Failed to attach DHT pin: %s", esp_err_to_name(err));
- vTaskDelay(pdMS_TO_TICKS(5000)); // Wait for 5 seconds before retrying
- return;
- }
- vTaskDelay(pdMS_TO_TICKS(2000)); // Wait for 2 seconds before starting the main loop
- while (1) {
- if (dht_read(&temperature, &humidity) == ESP_OK) {
- ESP_LOGI(TAG, "Temperature: %.1f°C, Humidity: %.1f%%", temperature, humidity);
- } else {
- ESP_LOGE(TAG, "Failed to read from DHT22 sensor");
- }
- vTaskDelay(pdMS_TO_TICKS(5000)); // Wait for 5 seconds before the next reading
- }
- }
|