This commit is contained in:
2024-06-19 17:55:42 +03:00
parent 2c856c08f2
commit 3d7987b803

145
zh_dht.c
View File

@ -35,23 +35,23 @@ esp_err_t zh_dht_init(const zh_dht_init_config_t *config)
_init_config = *config; _init_config = *config;
if (_init_config.sensor_pin != 0xFF) if (_init_config.sensor_pin != 0xFF)
{ {
gpio_config_t cfg = {0}; gpio_config_t one_wire_config = {0};
cfg.intr_type = GPIO_INTR_DISABLE; one_wire_config.intr_type = GPIO_INTR_DISABLE;
cfg.mode = GPIO_MODE_INPUT; one_wire_config.mode = GPIO_MODE_INPUT;
cfg.pin_bit_mask = (1ULL << _init_config.sensor_pin); one_wire_config.pin_bit_mask = (1ULL << _init_config.sensor_pin);
cfg.pull_down_en = GPIO_PULLDOWN_DISABLE; one_wire_config.pull_down_en = GPIO_PULLDOWN_DISABLE;
cfg.pull_up_en = GPIO_PULLUP_ENABLE; one_wire_config.pull_up_en = GPIO_PULLUP_ENABLE;
if (gpio_config(&cfg) != ESP_OK) if (gpio_config(&one_wire_config) != ESP_OK)
{ {
ESP_LOGE(TAG, "DHT initialization fail. Incorrect GPIO number."); ESP_LOGE(TAG, "DHT initialization fail. Incorrect GPIO number.");
return ESP_FAIL; return ESP_ERR_INVALID_ARG;
} }
ESP_LOGI(TAG, "DHT initialization success. 1-wire connection.");
} }
else else
{ {
// I2C. // I2C.
} }
ESP_LOGI(TAG, "DHT initialization success.");
_is_initialized = true; _is_initialized = true;
return ESP_OK; return ESP_OK;
} }
@ -69,81 +69,84 @@ esp_err_t zh_dht_read(float *humidity, float *temperature)
ESP_LOGE(TAG, "DHT read fail. BH1750 not initialized."); ESP_LOGE(TAG, "DHT read fail. BH1750 not initialized.");
return ESP_ERR_NOT_FOUND; return ESP_ERR_NOT_FOUND;
} }
if (gpio_get_level(_init_config.sensor_pin) != 1) if (_init_config.sensor_pin != 0xFF)
{ {
ESP_LOGE(TAG, "DHT read fail. Bus is busy."); if (gpio_get_level(_init_config.sensor_pin) != 1)
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."); ESP_LOGE(TAG, "DHT read fail. Bus is busy.");
return ESP_ERR_TIMEOUT; return ESP_ERR_INVALID_RESPONSE;
} }
++time; gpio_set_direction(_init_config.sensor_pin, GPIO_MODE_OUTPUT);
esp_delay_us(1); gpio_set_level(_init_config.sensor_pin, 0);
} vTaskDelay(10 / portTICK_PERIOD_MS);
time = 0; gpio_set_level(_init_config.sensor_pin, 1);
while (gpio_get_level(_init_config.sensor_pin) == 0) gpio_set_direction(_init_config.sensor_pin, GPIO_MODE_INPUT);
{ uint8_t time = 0;
if (time > RESPONSE_MAX_DURATION) while (gpio_get_level(_init_config.sensor_pin) == 1)
{ {
ESP_LOGE(TAG, "DHT read fail. Timeout exceeded."); if (time > MASTER_RELEASE_MAX_DURATION)
return ESP_ERR_TIMEOUT; {
ESP_LOGE(TAG, "DHT read fail. Timeout exceeded.");
return ESP_ERR_TIMEOUT;
}
++time;
esp_delay_us(1);
} }
++time; time = 0;
esp_delay_us(1); while (gpio_get_level(_init_config.sensor_pin) == 0)
}
time = 0;
while (gpio_get_level(_init_config.sensor_pin) == 1)
{
if (time > RESPONSE_MAX_DURATION)
{ {
ESP_LOGE(TAG, "DHT read fail. Timeout exceeded."); if (time > RESPONSE_MAX_DURATION)
return ESP_ERR_TIMEOUT; {
ESP_LOGE(TAG, "DHT read fail. Timeout exceeded.");
return ESP_ERR_TIMEOUT;
}
++time;
esp_delay_us(1);
} }
++time; time = 0;
esp_delay_us(1); while (gpio_get_level(_init_config.sensor_pin) == 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."); if (time > RESPONSE_MAX_DURATION)
return ESP_ERR_TIMEOUT; {
ESP_LOGE(TAG, "DHT read fail. Timeout exceeded.");
return ESP_ERR_TIMEOUT;
}
++time;
esp_delay_us(1);
} }
dht_data[byte_index] |= (bit << bit_index); uint8_t dht_data[DATA_SIZE / 8] = {0};
if (bit_index == 0) uint8_t byte_index = 0;
uint8_t bit_index = 7;
for (uint8_t i = 0; i < 40; ++i)
{ {
bit_index = 7; bool bit = 0;
++byte_index; 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;
}
} }
else if (dht_data[4] != ((dht_data[0] + dht_data[1] + dht_data[2] + dht_data[3])))
{ {
--bit_index; 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;
} }
}
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."); ESP_LOGI(TAG, "DHT read success.");
return ESP_OK; return ESP_OK;