|
@@ -21,7 +21,7 @@ Agrega la siguiente dependencia en tu archivo `idf_component.yml`:
|
|
|
```yaml
|
|
```yaml
|
|
|
dependencies:
|
|
dependencies:
|
|
|
dht22:
|
|
dht22:
|
|
|
- git: https://github.com/TU_USUARIO/TU_REPOSITORIO.git
|
|
|
|
|
|
|
+ git: https://dacogogs.duckdns.org/dacowars/libreria_dht22.git
|
|
|
version: "*"
|
|
version: "*"
|
|
|
```
|
|
```
|
|
|
|
|
|
|
@@ -45,30 +45,33 @@ idf.py menuconfig
|
|
|
#include <stdio.h>
|
|
#include <stdio.h>
|
|
|
#include "freertos/FreeRTOS.h"
|
|
#include "freertos/FreeRTOS.h"
|
|
|
#include "freertos/task.h"
|
|
#include "freertos/task.h"
|
|
|
|
|
+#include "driver/gpio.h"
|
|
|
|
|
+#include "esp_rom_sys.h"
|
|
|
#include "esp_log.h"
|
|
#include "esp_log.h"
|
|
|
#include "dht22.h"
|
|
#include "dht22.h"
|
|
|
|
|
|
|
|
|
|
+
|
|
|
static const char *TAG = "DHT22";
|
|
static const char *TAG = "DHT22";
|
|
|
|
|
|
|
|
|
|
+
|
|
|
void app_main(void)
|
|
void app_main(void)
|
|
|
{
|
|
{
|
|
|
- float temperature = 0.0;
|
|
|
|
|
- float humidity = 0.0;
|
|
|
|
|
- esp_err_t ret;
|
|
|
|
|
-
|
|
|
|
|
- ESP_LOGI(TAG, "Iniciando sensor DHT22...");
|
|
|
|
|
-
|
|
|
|
|
|
|
+ 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) {
|
|
while (1) {
|
|
|
- ret = dht_read(&temperature, &humidity);
|
|
|
|
|
-
|
|
|
|
|
- if (ret == ESP_OK) {
|
|
|
|
|
- ESP_LOGI(TAG, "Temperatura: %.1f °C | Humedad: %.1f %%", temperature, humidity);
|
|
|
|
|
|
|
+ if (dht_read(&temperature, &humidity) == ESP_OK) {
|
|
|
|
|
+ ESP_LOGI(TAG, "Temperature: %.1f°C, Humidity: %.1f%%", temperature, humidity);
|
|
|
} else {
|
|
} else {
|
|
|
- ESP_LOGE(TAG, "Error al leer el sensor: %s", esp_err_to_name(ret));
|
|
|
|
|
|
|
+ ESP_LOGE(TAG, "Failed to read from DHT22 sensor");
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
- // El DHT22 requiere al menos 2 segundos entre lecturas
|
|
|
|
|
- vTaskDelay(pdMS_TO_TICKS(2500));
|
|
|
|
|
|
|
+ vTaskDelay(pdMS_TO_TICKS(5000)); // Wait for 5 seconds before the next reading
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
```
|
|
```
|
|
@@ -78,14 +81,10 @@ void app_main(void)
|
|
|
| DHT22 Pin | ESP32 Pin | Descripción |
|
|
| DHT22 Pin | ESP32 Pin | Descripción |
|
|
|
|-----------|-----------|-------------|
|
|
|-----------|-----------|-------------|
|
|
|
| VCC | 3.3V | Alimentación |
|
|
| VCC | 3.3V | Alimentación |
|
|
|
-| GND | GND | Tierra |
|
|
|
|
|
|
|
+| GND | GND | Masa |
|
|
|
| DATA | GPIO4 (o el GPIO configurado) | Datos |
|
|
| DATA | GPIO4 (o el GPIO configurado) | Datos |
|
|
|
|
|
|
|
|
-- Recomendado: añadir una resistencia pull-up de 4.7kΩ a 10kΩ entre VCC y DATA.
|
|
|
|
|
-
|
|
|
|
|
-## Ejemplo completo
|
|
|
|
|
-
|
|
|
|
|
-Se incluye un ejemplo funcional en la carpeta `examples/dht22_example`.
|
|
|
|
|
|
|
+- Comprobar: resistencia pull-up entre VCC y DATA, recomendado de 4.7kΩ a 10kΩ.
|
|
|
|
|
|
|
|
## Licencia
|
|
## Licencia
|
|
|
|
|
|