|
|
1 月之前 | |
|---|---|---|
| .. | ||
| include | 1 月之前 | |
| src | 1 月之前 | |
| CMakeLists.txt | 1 月之前 | |
| Kconfig | 1 月之前 | |
| README.md | 1 月之前 | |
Driver para el sensor de temperatura y humedad DHT22 (AM2302) compatible con ESP-IDF.
menuconfigAgrega la siguiente dependencia en tu archivo idf_component.yml:
dependencies:
dht22:
git: https://dacogogs.duckdns.org/dacowars/libreria_dht22.git
version: "*"
Ejecuta:
idf.py menuconfig
Ve a:
Component config → DHT22 configuration → DHT pin configuration
#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
}
}
| DHT22 Pin | ESP32 Pin | Descripción |
|---|---|---|
| VCC | 3.3V | Alimentación |
| GND | GND | Masa |
| DATA | GPIO4 (o el GPIO configurado) | Datos |
Este proyecto está licenciado bajo la Licencia MIT.