| 123456789101112131415161718192021222324252627282930 |
- #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"
- const char *DHT_TAG = "DHT22";
- void app_main(void)
- {
- float temperature;
- float humidity;
- esp_err_t err = dht_attach_pin();
- if (err != ESP_OK) {
- ESP_LOGE(DHT_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(DHT_TAG, "Temperature: %.1f°C, Humidity: %.1f%%", temperature, humidity);
- } else {
- ESP_LOGE(DHT_TAG, "Failed to read from DHT22 sensor");
- }
- vTaskDelay(pdMS_TO_TICKS(5000)); // Wait for 5 seconds before the next reading
- }
- }
|