This commit is contained in:
2024-06-19 17:51:32 +03:00
parent 3a794774da
commit 2c856c08f2
3 changed files with 141 additions and 66 deletions

View File

@ -1,4 +1,4 @@
# ESP32 ESP-IDF and ESP8266 RTOS SDK component for DHT11/DHT22(AM2302) humidity & temperature sensor
# ESP32 ESP-IDF and ESP8266 RTOS SDK component for DHT11/DHT22/AM2302/AM2320 humidity & temperature sensor
## Tested on
@ -22,24 +22,76 @@ In the application, add the component:
## Example
Reading the sensor:
Reading the sensor with 1-wire connection (DHT11, DHT22, AM2302, AM2320):
```c
#include "zh_dht.h"
void app_main(void)
{
esp_log_level_set("zh_dht", ESP_LOG_NONE);
zh_dht_handle_t dht_handle = zh_dht_init(ZH_DHT22, GPIO_NUM_5);
float humidity = 0.0;
float temperature = 0.0;
for (;;)
{
zh_dht_read(&dht_handle, &humidity, &temperature);
printf("Humidity %0.2f\n", humidity);
printf("Temperature %0.2f\n", temperature);
vTaskDelay(5000 / portTICK_PERIOD_MS);
}
esp_log_level_set("zh_dht", ESP_LOG_NONE);
zh_dht_init_config_t zh_dht_init_config = ZH_DHT_INIT_CONFIG_DEFAULT();
zh_dht_init_config.sensor_pin = GPIO_NUM_5;
zh_dht_init(&zh_dht_init_config);
float humidity = 0.0;
float temperature = 0.0;
for (;;)
{
zh_dht_read(&humidity, &temperature);
printf("Humidity %0.2f\n", humidity);
printf("Temperature %0.2f\n", temperature);
vTaskDelay(5000 / portTICK_PERIOD_MS);
}
}
```
Reading the sensor with I2C connection (AM2320 only):
```c
#include "zh_dht.h"
#define I2C_PORT (I2C_NUM_MAX - 1)
void app_main(void)
{
esp_log_level_set("zh_dht", ESP_LOG_NONE);
#ifdef CONFIG_IDF_TARGET_ESP8266
i2c_config_t i2c_config = {
.mode = I2C_MODE_MASTER,
.sda_io_num = GPIO_NUM_4, // In accordance with used chip.
.sda_pullup_en = GPIO_PULLUP_ENABLE,
.scl_io_num = GPIO_NUM_5, // In accordance with used chip.
.scl_pullup_en = GPIO_PULLUP_ENABLE,
};
i2c_driver_install(I2C_PORT, i2c_config.mode);
i2c_param_config(I2C_PORT, &i2c_config);
#else
i2c_master_bus_config_t i2c_bus_config = {
.clk_source = I2C_CLK_SRC_DEFAULT,
.i2c_port = I2C_PORT,
.scl_io_num = GPIO_NUM_22, // In accordance with used chip.
.sda_io_num = GPIO_NUM_21, // In accordance with used chip.
.glitch_ignore_cnt = 7,
.flags.enable_internal_pullup = true,
};
i2c_master_bus_handle_t i2c_bus_handle;
i2c_new_master_bus(&i2c_bus_config, &i2c_bus_handle);
zh_dht_init_config_t zh_dht_init_config = ZH_DHT_INIT_CONFIG_DEFAULT();
#ifdef CONFIG_IDF_TARGET_ESP8266
zh_bh1750_init_config.i2c_port = I2C_PORT;
#else
zh_bh1750_init_config.i2c_handle = i2c_bus_handle;
#endif
zh_dht_init(&zh_dht_init_config);
float humidity = 0.0;
float temperature = 0.0;
for (;;)
{
zh_dht_read(&humidity, &temperature);
printf("Humidity %0.2f\n", humidity);
printf("Temperature %0.2f\n", temperature);
vTaskDelay(5000 / portTICK_PERIOD_MS);
}
}
```