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);
}
}
```

View File

@ -5,25 +5,33 @@
#include "esp_err.h"
#include "esp_log.h"
#include "driver/gpio.h"
#ifdef CONFIG_IDF_TARGET_ESP8266
#include "driver/i2c.h"
#else
#include "driver/i2c_master.h"
#endif
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#define ZH_DHT_INIT_CONFIG_DEFAULT() \
{ \
.sensor_pin = 0xFF, \
.i2c_port = 0 \
}
#ifdef __cplusplus
extern "C"
{
#endif
typedef enum // Enumeration of supported sensor types.
typedef struct
{
ZH_DHT11, // Sensor type DHT11.
ZH_DHT22 // Sensor type DHT22 or AM2302.
} zh_dht_sensor_type_t;
typedef struct // Unique handle of the sensor.
{
uint8_t sensor_pin; // Sensor GPIO connection.
zh_dht_sensor_type_t sensor_type; // Sensor type.
} zh_dht_handle_t;
uint8_t sensor_pin; // Sensor GPIO connection.
bool i2c_port; // I2C port.
#ifndef CONFIG_IDF_TARGET_ESP8266
i2c_master_bus_handle_t i2c_handle; // Unique I2C bus handle.
#endif
} zh_dht_init_config_t;
/**
* @brief Initialize DHT sensor.
@ -33,7 +41,7 @@ extern "C"
*
* @return Handle of the sensor
*/
zh_dht_handle_t zh_dht_init(const zh_dht_sensor_type_t sensor_type, const uint8_t sensor_pin);
esp_err_t zh_dht_init(const zh_dht_init_config_t *config);
/**
* @brief Read DHT sensor.
@ -49,7 +57,7 @@ extern "C"
* - ESP_ERR_TIMEOUT if operation timeout
* - ESP_ERR_INVALID_CRC if check CRC is fail
*/
esp_err_t zh_dht_read(const zh_dht_handle_t *dht_handle, float *humidity, float *temperature);
esp_err_t zh_dht_read(float *humidity, float *temperature);
#ifdef __cplusplus
}

View File

@ -5,7 +5,8 @@
#define DATA_BIT_START_TRANSFER_MAX_DURATION 55 // Signal "0", "1" low time.
#define RESPONSE_MAX_DURATION 85 // Response to low time. Response to high time.
#define MASTER_RELEASE_MAX_DURATION 200 // Bus master has released time.
#define DATA_SIZE 40
#define DATA_SIZE 40 // Sensor data size for 1-wire connection.
#define I2C_ADDRESS 100 // Sensor address size for I2C connection.
#ifdef CONFIG_IDF_TARGET_ESP8266
#define esp_delay_us(x) os_delay_us(x)
@ -13,52 +14,73 @@
#define esp_delay_us(x) esp_rom_delay_us(x)
#endif
static zh_dht_init_config_t _init_config = {0};
static bool _is_initialized = false;
#ifndef CONFIG_IDF_TARGET_ESP8266
static i2c_master_dev_handle_t _dht_handle = {0};
#endif
static const char *TAG = "zh_dht";
static esp_err_t _read_bit(const zh_dht_handle_t *dht_handle, bool *bit);
static esp_err_t _read_bit(bool *bit);
zh_dht_handle_t zh_dht_init(const zh_dht_sensor_type_t sensor_type, const uint8_t sensor_pin)
esp_err_t zh_dht_init(const zh_dht_init_config_t *config)
{
ESP_LOGI(TAG, "DHT initialization begin.");
zh_dht_handle_t zh_dht_handle = {
.sensor_type = sensor_type,
.sensor_pin = sensor_pin};
gpio_config_t config = {0};
config.intr_type = GPIO_INTR_DISABLE;
config.mode = GPIO_MODE_INPUT;
config.pin_bit_mask = (1ULL << sensor_pin);
config.pull_down_en = GPIO_PULLDOWN_DISABLE;
config.pull_up_en = GPIO_PULLUP_ENABLE;
if (gpio_config(&config) != ESP_OK)
if (config == NULL)
{
zh_dht_handle.sensor_pin = 0xFF;
ESP_LOGE(TAG, "DHT initialization fail. Incorrect GPIO number.");
return zh_dht_handle;
ESP_LOGE(TAG, "DHT initialization fail. Invalid argument.");
return ESP_ERR_INVALID_ARG;
}
_init_config = *config;
if (_init_config.sensor_pin != 0xFF)
{
gpio_config_t cfg = {0};
cfg.intr_type = GPIO_INTR_DISABLE;
cfg.mode = GPIO_MODE_INPUT;
cfg.pin_bit_mask = (1ULL << _init_config.sensor_pin);
cfg.pull_down_en = GPIO_PULLDOWN_DISABLE;
cfg.pull_up_en = GPIO_PULLUP_ENABLE;
if (gpio_config(&cfg) != ESP_OK)
{
ESP_LOGE(TAG, "DHT initialization fail. Incorrect GPIO number.");
return ESP_FAIL;
}
}
else
{
// I2C.
}
ESP_LOGI(TAG, "DHT initialization success.");
return zh_dht_handle;
_is_initialized = true;
return ESP_OK;
}
esp_err_t zh_dht_read(const zh_dht_handle_t *dht_handle, float *humidity, float *temperature)
esp_err_t zh_dht_read(float *humidity, float *temperature)
{
ESP_LOGI(TAG, "DHT read begin.");
if (dht_handle == NULL || humidity == NULL || temperature == NULL || dht_handle->sensor_pin == 0xFF)
if (humidity == NULL || temperature == NULL)
{
ESP_LOGE(TAG, "DHT read fail. Invalid argument.");
return ESP_ERR_INVALID_ARG;
}
if (gpio_get_level(dht_handle->sensor_pin) != 1)
if (_is_initialized == false)
{
ESP_LOGE(TAG, "DHT read fail. BH1750 not initialized.");
return ESP_ERR_NOT_FOUND;
}
if (gpio_get_level(_init_config.sensor_pin) != 1)
{
ESP_LOGE(TAG, "DHT read fail. Bus is busy.");
return ESP_ERR_INVALID_RESPONSE;
}
gpio_set_direction(dht_handle->sensor_pin, GPIO_MODE_OUTPUT);
gpio_set_level(dht_handle->sensor_pin, 0);
gpio_set_direction(_init_config.sensor_pin, GPIO_MODE_OUTPUT);
gpio_set_level(_init_config.sensor_pin, 0);
vTaskDelay(10 / portTICK_PERIOD_MS);
gpio_set_level(dht_handle->sensor_pin, 1);
gpio_set_direction(dht_handle->sensor_pin, GPIO_MODE_INPUT);
gpio_set_level(_init_config.sensor_pin, 1);
gpio_set_direction(_init_config.sensor_pin, GPIO_MODE_INPUT);
uint8_t time = 0;
while (gpio_get_level(dht_handle->sensor_pin) == 1)
while (gpio_get_level(_init_config.sensor_pin) == 1)
{
if (time > MASTER_RELEASE_MAX_DURATION)
{
@ -69,7 +91,7 @@ esp_err_t zh_dht_read(const zh_dht_handle_t *dht_handle, float *humidity, float
esp_delay_us(1);
}
time = 0;
while (gpio_get_level(dht_handle->sensor_pin) == 0)
while (gpio_get_level(_init_config.sensor_pin) == 0)
{
if (time > RESPONSE_MAX_DURATION)
{
@ -80,7 +102,7 @@ esp_err_t zh_dht_read(const zh_dht_handle_t *dht_handle, float *humidity, float
esp_delay_us(1);
}
time = 0;
while (gpio_get_level(dht_handle->sensor_pin) == 1)
while (gpio_get_level(_init_config.sensor_pin) == 1)
{
if (time > RESPONSE_MAX_DURATION)
{
@ -96,7 +118,7 @@ esp_err_t zh_dht_read(const zh_dht_handle_t *dht_handle, float *humidity, float
for (uint8_t i = 0; i < 40; ++i)
{
bool bit = 0;
if (_read_bit(dht_handle, &bit) != ESP_OK)
if (_read_bit(&bit) != ESP_OK)
{
ESP_LOGE(TAG, "DHT read fail. Timeout exceeded.");
return ESP_ERR_TIMEOUT;
@ -118,26 +140,19 @@ esp_err_t zh_dht_read(const zh_dht_handle_t *dht_handle, float *humidity, float
return ESP_ERR_INVALID_CRC;
}
*humidity = (dht_data[0] << 8 | dht_data[1]) / 10.0;
if (dht_handle->sensor_type == ZH_DHT22)
*temperature = ((dht_data[2] & 0x7F) << 8 | dht_data[3]) / 10.0;
if ((dht_data[2] & 0x80) != 0)
{
*temperature = ((dht_data[2] & 0b01111111) << 8 | dht_data[3]) / 10.0;
if ((dht_data[2] & 0b10000000) != 0)
{
*temperature *= -1;
}
}
else
{
*temperature = (dht_data[2] << 8 | dht_data[3]) / 10.0;
*temperature *= -1;
}
ESP_LOGI(TAG, "DHT read success.");
return ESP_OK;
}
static esp_err_t _read_bit(const zh_dht_handle_t *dht_handle, bool *bit)
static esp_err_t _read_bit(bool *bit)
{
uint8_t time = 0;
while (gpio_get_level(dht_handle->sensor_pin) == 0)
while (gpio_get_level(_init_config.sensor_pin) == 0)
{
if (time > DATA_BIT_START_TRANSFER_MAX_DURATION)
{
@ -147,7 +162,7 @@ static esp_err_t _read_bit(const zh_dht_handle_t *dht_handle, bool *bit)
esp_delay_us(1);
}
time = 0;
while (gpio_get_level(dht_handle->sensor_pin) == 1)
while (gpio_get_level(_init_config.sensor_pin) == 1)
{
if (time > BIT_1_TRANSFER_MAX_DURATION)
{