From daf75ec9d62851d399556eb57a37619e7b82eaa2 Mon Sep 17 00:00:00 2001 From: Alexey Zholtikov Date: Sat, 22 Jun 2024 17:07:14 +0300 Subject: [PATCH] WIP --- README.md | 14 ++++++------- include/zh_dht.h | 17 ++++++++++++---- zh_dht.c | 52 +++++++++++++++++++++++++++++++++--------------- 3 files changed, 56 insertions(+), 27 deletions(-) diff --git a/README.md b/README.md index 29ca0ac..4a80b11 100644 --- a/README.md +++ b/README.md @@ -30,9 +30,9 @@ Reading the sensor with 1-wire connection (DHT11, DHT22, AM2302, AM2320): void app_main(void) { 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); + zh_dht_init_config_t dht_init_config = ZH_DHT_INIT_CONFIG_DEFAULT(); + dht_init_config.sensor_pin = GPIO_NUM_5; + zh_dht_init(&dht_init_config); float humidity = 0.0; float temperature = 0.0; for (;;) @@ -76,14 +76,14 @@ void app_main(void) }; 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(); + zh_dht_init_config_t dht_init_config = ZH_DHT_INIT_CONFIG_DEFAULT(); #endif #ifdef CONFIG_IDF_TARGET_ESP8266 - zh_dht_init_config.i2c_port = I2C_PORT; + dht_init_config.i2c_port = I2C_PORT; #else - zh_dht_init_config.i2c_handle = i2c_bus_handle; + dht_init_config.i2c_handle = i2c_bus_handle; #endif - zh_dht_init(&zh_dht_init_config); + zh_dht_init(&dht_init_config); float humidity = 0.0; float temperature = 0.0; for (;;) diff --git a/include/zh_dht.h b/include/zh_dht.h index d42ee2b..7fbd99a 100644 --- a/include/zh_dht.h +++ b/include/zh_dht.h @@ -36,24 +36,33 @@ extern "C" /** * @brief Initialize DHT sensor. * - * @param[in] sensor_type Sensor type. - * @param[in] sensor_pin Sensor connection gpio. + * @param[in] config Pointer to DHT initialized configuration structure. Can point to a temporary variable. * - * @return Handle of the sensor + * @attention I2C driver must be initialized first (for I2C connection only). + * + * @note Before initialize the sensor recommend initialize zh_dht_init_config_t structure with default values. + * + * @code zh_dht_init_config_t config = ZH_DHT_INIT_CONFIG_DEFAULT() @endcode + * + * @return + * - ESP_OK if initialization was success + * - ESP_ERR_INVALID_ARG if parameter error + * - ESP_ERR_NOT_FOUND if sensor not connected or not responded (for I2C connection only) */ esp_err_t zh_dht_init(const zh_dht_init_config_t *config); /** * @brief Read DHT sensor. * - * @param[in] dht_handle Pointer for handle of the sensor. * @param[out] humidity Pointer for DHT sensor reading data of humidity. * @param[out] temperature Pointer for DHT sensor reading data of temperature. * * @return * - ESP_OK if read was success * - ESP_ERR_INVALID_ARG if parameter error + * - ESP_ERR_NOT_FOUND if DHT is not initialized * - ESP_ERR_INVALID_RESPONSE if the bus is busy + * - ESP_ERR_INVALID_STATE if I2C driver not installed or not in master mode * - ESP_ERR_TIMEOUT if operation timeout * - ESP_ERR_INVALID_CRC if check CRC is fail */ diff --git a/zh_dht.c b/zh_dht.c index d4c6b14..6cf71a2 100644 --- a/zh_dht.c +++ b/zh_dht.c @@ -82,7 +82,7 @@ esp_err_t zh_dht_read(float *humidity, float *temperature) } if (_is_initialized == false) { - ESP_LOGE(TAG, "DHT read fail. BH1750 not initialized."); + ESP_LOGE(TAG, "DHT read fail. DHT not initialized."); return ESP_ERR_NOT_FOUND; } if (_init_config.sensor_pin != 0xFF) @@ -171,13 +171,27 @@ esp_err_t zh_dht_read(float *humidity, float *temperature) uint8_t read_command[] = {I2C_DATA_READ_COMMAND}; uint8_t wakeup_command = {0}; #ifdef CONFIG_IDF_TARGET_ESP8266 - // i2c_cmd_handle_t i2c_cmd_handle = i2c_cmd_link_create(); - // i2c_master_start(i2c_cmd_handle); - // i2c_master_write_byte(i2c_cmd_handle, _init_config.i2c_address << 1 | I2C_MASTER_WRITE, true); - // i2c_master_write_byte(i2c_cmd_handle, _command, true); - // i2c_master_stop(i2c_cmd_handle); - // esp_err = i2c_master_cmd_begin(_init_config.i2c_port, i2c_cmd_handle, 1000 / portTICK_PERIOD_MS); - // i2c_cmd_link_delete(i2c_cmd_handle); + i2c_cmd_handle_t i2c_cmd_handle = i2c_cmd_link_create(); + i2c_master_start(i2c_cmd_handle); + i2c_master_write_byte(i2c_cmd_handle, I2C_ADDRESS << 1 | I2C_MASTER_WRITE, true); + i2c_master_write_byte(i2c_cmd_handle, wakeup_command, true); + i2c_master_stop(i2c_cmd_handle); + esp_err = i2c_master_cmd_begin(_init_config.i2c_port, i2c_cmd_handle, 1000 / portTICK_PERIOD_MS); + i2c_cmd_link_delete(i2c_cmd_handle); + if (esp_err != ESP_OK) + { + ESP_LOGE(TAG, "DHT read fail. I2C driver error."); + return esp_err; + } + i2c_cmd_handle = i2c_cmd_link_create(); + i2c_master_start(i2c_cmd_handle); + i2c_master_write_byte(i2c_cmd_handle, I2C_ADDRESS << 1 | I2C_MASTER_WRITE, true); + i2c_master_write_byte(i2c_cmd_handle, read_command[0], true); + i2c_master_write_byte(i2c_cmd_handle, read_command[1], true); + i2c_master_write_byte(i2c_cmd_handle, read_command[2], true); + i2c_master_stop(i2c_cmd_handle); + esp_err = i2c_master_cmd_begin(_init_config.i2c_port, i2c_cmd_handle, 1000 / portTICK_PERIOD_MS); + i2c_cmd_link_delete(i2c_cmd_handle); #else esp_err = i2c_master_transmit(_dht_handle, &wakeup_command, sizeof(wakeup_command), 1000 / portTICK_PERIOD_MS); if (esp_err != ESP_OK) @@ -193,14 +207,20 @@ esp_err_t zh_dht_read(float *humidity, float *temperature) return esp_err; } #ifdef CONFIG_IDF_TARGET_ESP8266 - // i2c_cmd_handle = i2c_cmd_link_create(); - // i2c_master_start(i2c_cmd_handle); - // i2c_master_write_byte(i2c_cmd_handle, _init_config.i2c_address << 1 | I2C_MASTER_READ, true); - // i2c_master_read_byte(i2c_cmd_handle, &sensor_data[0], I2C_MASTER_ACK); - // i2c_master_read_byte(i2c_cmd_handle, &sensor_data[1], I2C_MASTER_NACK); - // i2c_master_stop(i2c_cmd_handle); - // esp_err = i2c_master_cmd_begin(_init_config.i2c_port, i2c_cmd_handle, 1000 / portTICK_PERIOD_MS); - // i2c_cmd_link_delete(i2c_cmd_handle); + i2c_cmd_handle = i2c_cmd_link_create(); + i2c_master_start(i2c_cmd_handle); + i2c_master_write_byte(i2c_cmd_handle, I2C_ADDRESS << 1 | I2C_MASTER_READ, true); + i2c_master_read_byte(i2c_cmd_handle, &dht_data[0], I2C_MASTER_ACK); + i2c_master_read_byte(i2c_cmd_handle, &dht_data[1], I2C_MASTER_ACK); + i2c_master_read_byte(i2c_cmd_handle, &dht_data[2], I2C_MASTER_ACK); + i2c_master_read_byte(i2c_cmd_handle, &dht_data[3], I2C_MASTER_ACK); + i2c_master_read_byte(i2c_cmd_handle, &dht_data[4], I2C_MASTER_ACK); + i2c_master_read_byte(i2c_cmd_handle, &dht_data[5], I2C_MASTER_ACK); + i2c_master_read_byte(i2c_cmd_handle, &dht_data[6], I2C_MASTER_ACK); + i2c_master_read_byte(i2c_cmd_handle, &dht_data[7], I2C_MASTER_NACK); + i2c_master_stop(i2c_cmd_handle); + esp_err = i2c_master_cmd_begin(_init_config.i2c_port, i2c_cmd_handle, 1000 / portTICK_PERIOD_MS); + i2c_cmd_link_delete(i2c_cmd_handle); #else esp_err = i2c_master_receive(_dht_handle, dht_data, sizeof(dht_data), 1000 / portTICK_PERIOD_MS); #endif