176 lines
4.3 KiB
C
176 lines
4.3 KiB
C
#include "zh_dht.h"
|
|
|
|
#define BIT_1_TRANSFER_MAX_DURATION 75 // Signal "1" high time.
|
|
#define BIT_0_TRANSFER_MAX_DURATION 30 // Signal "0" high time.
|
|
#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 // 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)
|
|
#else
|
|
#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(bool *bit);
|
|
|
|
esp_err_t zh_dht_init(const zh_dht_init_config_t *config)
|
|
{
|
|
ESP_LOGI(TAG, "DHT initialization begin.");
|
|
if (config == NULL)
|
|
{
|
|
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.");
|
|
_is_initialized = true;
|
|
return ESP_OK;
|
|
}
|
|
|
|
esp_err_t zh_dht_read(float *humidity, float *temperature)
|
|
{
|
|
ESP_LOGI(TAG, "DHT read begin.");
|
|
if (humidity == NULL || temperature == NULL)
|
|
{
|
|
ESP_LOGE(TAG, "DHT read fail. Invalid argument.");
|
|
return ESP_ERR_INVALID_ARG;
|
|
}
|
|
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(_init_config.sensor_pin, GPIO_MODE_OUTPUT);
|
|
gpio_set_level(_init_config.sensor_pin, 0);
|
|
vTaskDelay(10 / portTICK_PERIOD_MS);
|
|
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(_init_config.sensor_pin) == 1)
|
|
{
|
|
if (time > MASTER_RELEASE_MAX_DURATION)
|
|
{
|
|
ESP_LOGE(TAG, "DHT read fail. Timeout exceeded.");
|
|
return ESP_ERR_TIMEOUT;
|
|
}
|
|
++time;
|
|
esp_delay_us(1);
|
|
}
|
|
time = 0;
|
|
while (gpio_get_level(_init_config.sensor_pin) == 0)
|
|
{
|
|
if (time > RESPONSE_MAX_DURATION)
|
|
{
|
|
ESP_LOGE(TAG, "DHT read fail. Timeout exceeded.");
|
|
return ESP_ERR_TIMEOUT;
|
|
}
|
|
++time;
|
|
esp_delay_us(1);
|
|
}
|
|
time = 0;
|
|
while (gpio_get_level(_init_config.sensor_pin) == 1)
|
|
{
|
|
if (time > RESPONSE_MAX_DURATION)
|
|
{
|
|
ESP_LOGE(TAG, "DHT read fail. Timeout exceeded.");
|
|
return ESP_ERR_TIMEOUT;
|
|
}
|
|
++time;
|
|
esp_delay_us(1);
|
|
}
|
|
uint8_t dht_data[DATA_SIZE / 8] = {0};
|
|
uint8_t byte_index = 0;
|
|
uint8_t bit_index = 7;
|
|
for (uint8_t i = 0; i < 40; ++i)
|
|
{
|
|
bool bit = 0;
|
|
if (_read_bit(&bit) != ESP_OK)
|
|
{
|
|
ESP_LOGE(TAG, "DHT read fail. Timeout exceeded.");
|
|
return ESP_ERR_TIMEOUT;
|
|
}
|
|
dht_data[byte_index] |= (bit << bit_index);
|
|
if (bit_index == 0)
|
|
{
|
|
bit_index = 7;
|
|
++byte_index;
|
|
}
|
|
else
|
|
{
|
|
--bit_index;
|
|
}
|
|
}
|
|
if (dht_data[4] != ((dht_data[0] + dht_data[1] + dht_data[2] + dht_data[3])))
|
|
{
|
|
ESP_LOGE(TAG, "DHT read fail. Invalid CRC.");
|
|
return ESP_ERR_INVALID_CRC;
|
|
}
|
|
*humidity = (dht_data[0] << 8 | dht_data[1]) / 10.0;
|
|
*temperature = ((dht_data[2] & 0x7F) << 8 | dht_data[3]) / 10.0;
|
|
if ((dht_data[2] & 0x80) != 0)
|
|
{
|
|
*temperature *= -1;
|
|
}
|
|
ESP_LOGI(TAG, "DHT read success.");
|
|
return ESP_OK;
|
|
}
|
|
|
|
static esp_err_t _read_bit(bool *bit)
|
|
{
|
|
uint8_t time = 0;
|
|
while (gpio_get_level(_init_config.sensor_pin) == 0)
|
|
{
|
|
if (time > DATA_BIT_START_TRANSFER_MAX_DURATION)
|
|
{
|
|
return ESP_ERR_TIMEOUT;
|
|
}
|
|
++time;
|
|
esp_delay_us(1);
|
|
}
|
|
time = 0;
|
|
while (gpio_get_level(_init_config.sensor_pin) == 1)
|
|
{
|
|
if (time > BIT_1_TRANSFER_MAX_DURATION)
|
|
{
|
|
return ESP_ERR_TIMEOUT;
|
|
}
|
|
++time;
|
|
esp_delay_us(1);
|
|
}
|
|
*bit = (time > BIT_0_TRANSFER_MAX_DURATION) ? 1 : 0;
|
|
return ESP_OK;
|
|
} |