No description
  • C 97.2%
  • CMake 2.8%
Find a file
2026-04-02 16:29:29 +02:00
include static quitado 2026-04-02 16:29:29 +02:00
CMakeLists.txt Componente dht22 2026-04-02 15:05:54 +02:00
dht22.c refactorizacion de la libreria 2026-04-02 16:14:46 +02:00
idf_component.yml Componente dht22 2026-04-02 15:05:54 +02:00
Kconfig Componente dht22 2026-04-02 15:05:54 +02:00
README.md Componente dht22 2026-04-02 15:05:54 +02:00

DHT22 Component for ESP-IDF

Driver para el sensor de temperatura y humedad DHT22 (AM2302) compatible con ESP-IDF.

Características

  • Lectura de temperatura (°C) y humedad relativa (%)
  • Comunicación bit-banging optimizada con protección contra interrupciones
  • Configuración del pin GPIO a través de menuconfig
  • Detección de errores (timeout y checksum)
  • Estructura lista para usar como componente reutilizable

Requisitos

  • ESP-IDF v5.0 o superior

Cómo añadir el componente a tu proyecto

Agrega la siguiente dependencia en tu archivo idf_component.yml:

dependencies:
  dht22:
    git: https://dacogogs.duckdns.org/dacowars/libreria_dht22.git
    version: "*"

Configuración

  1. Ejecuta:
idf.py menuconfig
  1. Ve a:

Component config → DHT22 configuration → DHT pin configuration

  1. Selecciona el GPIO donde está conectado el sensor (por defecto: GPIO4).

Uso básico

#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
    }
}

Conexiones (Wiring)

DHT22 Pin ESP32 Pin Descripción
VCC 3.3V Alimentación
GND GND Masa
DATA GPIO4 (o el GPIO configurado) Datos
  • Comprobar: resistencia pull-up entre VCC y DATA, recomendado de 4.7kΩ a 10kΩ.

Licencia

Este proyecto está licenciado bajo la Licencia MIT.