|
|
il y a 1 mois | |
|---|---|---|
| .. | ||
| include | il y a 1 mois | |
| src | il y a 1 mois | |
| CMakeLists.txt | il y a 1 mois | |
| Kconfig | il y a 1 mois | |
| README.md | il y a 1 mois | |
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://github.com/TU_USUARIO/TU_REPOSITORIO.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 "esp_log.h"
#include "dht22.h"
static const char *TAG = "DHT22";
void app_main(void)
{
float temperature = 0.0;
float humidity = 0.0;
esp_err_t ret;
ESP_LOGI(TAG, "Iniciando sensor DHT22...");
while (1) {
ret = dht_read(&temperature, &humidity);
if (ret == ESP_OK) {
ESP_LOGI(TAG, "Temperatura: %.1f °C | Humedad: %.1f %%", temperature, humidity);
} else {
ESP_LOGE(TAG, "Error al leer el sensor: %s", esp_err_to_name(ret));
}
// El DHT22 requiere al menos 2 segundos entre lecturas
vTaskDelay(pdMS_TO_TICKS(2500));
}
}
| DHT22 Pin | ESP32 Pin | Descripción |
|---|---|---|
| VCC | 3.3V | Alimentación |
| GND | GND | Tierra |
| DATA | GPIO4 (o el GPIO configurado) | Datos |
Se incluye un ejemplo funcional en la carpeta examples/dht22_example.
Este proyecto está licenciado bajo la Licencia MIT.