diff --git a/README.md b/README.md index 38871eb..d08d1d7 100644 --- a/README.md +++ b/README.md @@ -5,6 +5,10 @@ 1. ESP8266 RTOS_SDK v3.4 2. ESP32 ESP-IDF v5.2 +## Features + +1. Autodetect sensor type. + ## Using In an existing project, run the following command to install the component: diff --git a/zh_aht.c b/zh_aht.c index b963969..e4baca4 100644 --- a/zh_aht.c +++ b/zh_aht.c @@ -44,18 +44,18 @@ esp_err_t zh_aht_init(const zh_aht_init_config_t *config) } #endif esp_err_t err = ESP_OK; - uint8_t command = I2C_STATUS_READ_COMMAND; + uint8_t status_read_command = I2C_STATUS_READ_COMMAND; uint8_t sensor_data = 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, I2C_ADDRESS << 1 | I2C_MASTER_WRITE, true); - i2c_master_write_byte(i2c_cmd_handle, command, true); + i2c_master_write_byte(i2c_cmd_handle, status_read_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); #else - err = i2c_master_transmit(_aht_handle, &command, sizeof(command), 1000 / portTICK_PERIOD_MS); + err = i2c_master_transmit(_aht_handle, &status_read_command, sizeof(status_read_command), 1000 / portTICK_PERIOD_MS); #endif if (err != ESP_OK) { @@ -80,29 +80,26 @@ esp_err_t zh_aht_init(const zh_aht_init_config_t *config) } if ((sensor_data & 0x08) == 0) // If sensor is not calibrated. { - sensor_data = 0; - uint8_t command[] = {I2C_INIT_COMMAND}; - command[0] = I2C_INIT_AHT2X_FIRST_BYTE; + uint8_t init_command[] = {I2C_INIT_COMMAND}; + uint8_t check_command = I2C_INIT_AHT2X_FIRST_BYTE; bool is_first_check = false; INIT_SENSOR:; #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, I2C_ADDRESS << 1 | I2C_MASTER_WRITE, true); - i2c_master_write_byte(i2c_cmd_handle, command[0], true); - i2c_master_write_byte(i2c_cmd_handle, command[1], true); - i2c_master_write_byte(i2c_cmd_handle, command[2], true); + i2c_master_write_byte(i2c_cmd_handle, check_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); #else - err = i2c_master_transmit(_aht_handle, command, sizeof(command), 1000 / portTICK_PERIOD_MS); + err = i2c_master_transmit(_aht_handle, &check_command, sizeof(check_command), 1000 / portTICK_PERIOD_MS); #endif if (err != ESP_OK) { if (err == ESP_ERR_INVALID_STATE && is_first_check == false) { - command[0] = I2C_INIT_AHT1X_FIRST_BYTE; + check_command = I2C_INIT_AHT1X_FIRST_BYTE; is_first_check = true; goto INIT_SENSOR; } @@ -112,6 +109,25 @@ esp_err_t zh_aht_init(const zh_aht_init_config_t *config) return ESP_ERR_INVALID_RESPONSE; } } + init_command[0] = check_command; +#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, I2C_ADDRESS << 1 | I2C_MASTER_WRITE, true); + i2c_master_write_byte(i2c_cmd_handle, init_command[0], true); + i2c_master_write_byte(i2c_cmd_handle, init_command[1], true); + i2c_master_write_byte(i2c_cmd_handle, init_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 + err = i2c_master_transmit(_aht_handle, init_command, sizeof(init_command), 1000 / portTICK_PERIOD_MS); +#endif + if (err != ESP_OK) + { + ESP_LOGE(TAG, "AHT initialization fail. I2C driver error at line %d.", __LINE__); + return ESP_ERR_INVALID_RESPONSE; + } } ESP_LOGI(TAG, "AHT initialization success."); _is_initialized = true; @@ -133,19 +149,19 @@ esp_err_t zh_aht_read(float *humidity, float *temperature) } esp_err_t err = ESP_OK; uint8_t sensor_data[I2C_MAX_DATA_SIZE] = {0}; - uint8_t command[] = {I2C_DATA_READ_COMMAND}; + uint8_t data_read_command[] = {I2C_DATA_READ_COMMAND}; #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, I2C_ADDRESS << 1 | I2C_MASTER_WRITE, true); - i2c_master_write_byte(i2c_cmd_handle, command[0], true); - i2c_master_write_byte(i2c_cmd_handle, command[1], true); - i2c_master_write_byte(i2c_cmd_handle, command[2], true); + i2c_master_write_byte(i2c_cmd_handle, data_read_command[0], true); + i2c_master_write_byte(i2c_cmd_handle, data_read_command[1], true); + i2c_master_write_byte(i2c_cmd_handle, data_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 - err = i2c_master_transmit(_aht_handle, command, sizeof(command), 1000 / portTICK_PERIOD_MS); + err = i2c_master_transmit(_aht_handle, data_read_command, sizeof(data_read_command), 1000 / portTICK_PERIOD_MS); #endif if (err != ESP_OK) { @@ -200,17 +216,17 @@ esp_err_t zh_aht_reset(void) return ESP_ERR_NOT_FOUND; } esp_err_t err = ESP_OK; - uint8_t command = I2C_RESET_COMMAND; + uint8_t reset_command = I2C_RESET_COMMAND; #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, I2C_ADDRESS << 1 | I2C_MASTER_WRITE, true); - i2c_master_write_byte(i2c_cmd_handle, command, true); + i2c_master_write_byte(i2c_cmd_handle, reset_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); #else - err = i2c_master_transmit(_aht_handle, &command, sizeof(command), 1000 / portTICK_PERIOD_MS); + err = i2c_master_transmit(_aht_handle, &reset_command, sizeof(reset_command), 1000 / portTICK_PERIOD_MS); #endif if (err != ESP_OK) {