WIP
This commit is contained in:
parent
a454e5bb42
commit
1afca3c9ec
49
zh_aht.c
49
zh_aht.c
@ -43,7 +43,7 @@ esp_err_t zh_aht_init(const zh_aht_init_config_t *config)
|
|||||||
return ESP_ERR_NOT_FOUND;
|
return ESP_ERR_NOT_FOUND;
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
esp_err_t esp_err = ESP_OK;
|
esp_err_t err = ESP_OK;
|
||||||
uint8_t command = I2C_STATUS_READ_COMMAND;
|
uint8_t command = I2C_STATUS_READ_COMMAND;
|
||||||
uint8_t sensor_data = 0;
|
uint8_t sensor_data = 0;
|
||||||
#ifdef CONFIG_IDF_TARGET_ESP8266
|
#ifdef CONFIG_IDF_TARGET_ESP8266
|
||||||
@ -55,9 +55,9 @@ esp_err_t zh_aht_init(const zh_aht_init_config_t *config)
|
|||||||
esp_err = i2c_master_cmd_begin(_init_config.i2c_port, i2c_cmd_handle, 1000 / portTICK_PERIOD_MS);
|
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_link_delete(i2c_cmd_handle);
|
||||||
#else
|
#else
|
||||||
esp_err = i2c_master_transmit(_aht_handle, &command, sizeof(command), 1000 / portTICK_PERIOD_MS);
|
err = i2c_master_transmit(_aht_handle, &command, sizeof(command), 1000 / portTICK_PERIOD_MS);
|
||||||
#endif
|
#endif
|
||||||
if (esp_err != ESP_OK)
|
if (err != ESP_OK)
|
||||||
{
|
{
|
||||||
ESP_LOGE(TAG, "AHT initialization fail. I2C driver error at line %d.", __LINE__);
|
ESP_LOGE(TAG, "AHT initialization fail. I2C driver error at line %d.", __LINE__);
|
||||||
return ESP_ERR_INVALID_RESPONSE;
|
return ESP_ERR_INVALID_RESPONSE;
|
||||||
@ -71,24 +71,20 @@ esp_err_t zh_aht_init(const zh_aht_init_config_t *config)
|
|||||||
esp_err = i2c_master_cmd_begin(_init_config.i2c_port, i2c_cmd_handle, 1000 / portTICK_PERIOD_MS);
|
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_link_delete(i2c_cmd_handle);
|
||||||
#else
|
#else
|
||||||
esp_err = i2c_master_receive(_aht_handle, &sensor_data, sizeof(sensor_data), 1000 / portTICK_PERIOD_MS);
|
err = i2c_master_receive(_aht_handle, &sensor_data, sizeof(sensor_data), 1000 / portTICK_PERIOD_MS);
|
||||||
#endif
|
#endif
|
||||||
if (esp_err != ESP_OK)
|
if (err != ESP_OK)
|
||||||
{
|
{
|
||||||
ESP_LOGE(TAG, "AHT initialization fail. I2C driver error at line %d.", __LINE__);
|
ESP_LOGE(TAG, "AHT initialization fail. I2C driver error at line %d.", __LINE__);
|
||||||
return ESP_ERR_INVALID_RESPONSE;
|
return ESP_ERR_INVALID_RESPONSE;
|
||||||
}
|
}
|
||||||
if ((sensor_data & 0x08) == 0) // If sensor is not calibrated.
|
if ((sensor_data & 0x08) == 0) // If sensor is not calibrated.
|
||||||
{
|
{
|
||||||
|
sensor_data = 0;
|
||||||
uint8_t command[] = {I2C_INIT_COMMAND};
|
uint8_t command[] = {I2C_INIT_COMMAND};
|
||||||
if (_init_config.sensor_type == ZH_AHT1X)
|
|
||||||
{
|
|
||||||
command[0] = I2C_INIT_AHT1X_FIRST_BYTE;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
command[0] = I2C_INIT_AHT2X_FIRST_BYTE;
|
command[0] = I2C_INIT_AHT2X_FIRST_BYTE;
|
||||||
}
|
bool is_first_check = false;
|
||||||
|
INIT_SENSOR:;
|
||||||
#ifdef CONFIG_IDF_TARGET_ESP8266
|
#ifdef CONFIG_IDF_TARGET_ESP8266
|
||||||
i2c_cmd_handle_t i2c_cmd_handle = i2c_cmd_link_create();
|
i2c_cmd_handle_t i2c_cmd_handle = i2c_cmd_link_create();
|
||||||
i2c_master_start(i2c_cmd_handle);
|
i2c_master_start(i2c_cmd_handle);
|
||||||
@ -100,14 +96,23 @@ esp_err_t zh_aht_init(const zh_aht_init_config_t *config)
|
|||||||
esp_err = i2c_master_cmd_begin(_init_config.i2c_port, i2c_cmd_handle, 1000 / portTICK_PERIOD_MS);
|
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_link_delete(i2c_cmd_handle);
|
||||||
#else
|
#else
|
||||||
esp_err = i2c_master_transmit(_aht_handle, command, sizeof(command), 1000 / portTICK_PERIOD_MS);
|
err = i2c_master_transmit(_aht_handle, command, sizeof(command), 1000 / portTICK_PERIOD_MS);
|
||||||
#endif
|
#endif
|
||||||
if (esp_err != ESP_OK)
|
if (err != ESP_OK)
|
||||||
|
{
|
||||||
|
if (err == ESP_ERR_INVALID_STATE && is_first_check == false)
|
||||||
|
{
|
||||||
|
command[0] = I2C_INIT_AHT1X_FIRST_BYTE;
|
||||||
|
is_first_check = true;
|
||||||
|
goto INIT_SENSOR;
|
||||||
|
}
|
||||||
|
else
|
||||||
{
|
{
|
||||||
ESP_LOGE(TAG, "AHT initialization fail. I2C driver error at line %d.", __LINE__);
|
ESP_LOGE(TAG, "AHT initialization fail. I2C driver error at line %d.", __LINE__);
|
||||||
return ESP_ERR_INVALID_RESPONSE;
|
return ESP_ERR_INVALID_RESPONSE;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
ESP_LOGI(TAG, "AHT initialization success.");
|
ESP_LOGI(TAG, "AHT initialization success.");
|
||||||
_is_initialized = true;
|
_is_initialized = true;
|
||||||
return ESP_OK;
|
return ESP_OK;
|
||||||
@ -126,7 +131,7 @@ esp_err_t zh_aht_read(float *humidity, float *temperature)
|
|||||||
ESP_LOGE(TAG, "AHT read fail. AHT not initialized.");
|
ESP_LOGE(TAG, "AHT read fail. AHT not initialized.");
|
||||||
return ESP_ERR_NOT_FOUND;
|
return ESP_ERR_NOT_FOUND;
|
||||||
}
|
}
|
||||||
esp_err_t esp_err = ESP_OK;
|
esp_err_t err = ESP_OK;
|
||||||
uint8_t sensor_data[I2C_MAX_DATA_SIZE] = {0};
|
uint8_t sensor_data[I2C_MAX_DATA_SIZE] = {0};
|
||||||
uint8_t command[] = {I2C_DATA_READ_COMMAND};
|
uint8_t command[] = {I2C_DATA_READ_COMMAND};
|
||||||
#ifdef CONFIG_IDF_TARGET_ESP8266
|
#ifdef CONFIG_IDF_TARGET_ESP8266
|
||||||
@ -140,9 +145,9 @@ esp_err_t zh_aht_read(float *humidity, float *temperature)
|
|||||||
esp_err = i2c_master_cmd_begin(_init_config.i2c_port, i2c_cmd_handle, 1000 / portTICK_PERIOD_MS);
|
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_link_delete(i2c_cmd_handle);
|
||||||
#else
|
#else
|
||||||
esp_err = i2c_master_transmit(_aht_handle, command, sizeof(command), 1000 / portTICK_PERIOD_MS);
|
err = i2c_master_transmit(_aht_handle, command, sizeof(command), 1000 / portTICK_PERIOD_MS);
|
||||||
#endif
|
#endif
|
||||||
if (esp_err != ESP_OK)
|
if (err != ESP_OK)
|
||||||
{
|
{
|
||||||
ESP_LOGE(TAG, "AHT read fail. I2C driver error at line %d.", __LINE__);
|
ESP_LOGE(TAG, "AHT read fail. I2C driver error at line %d.", __LINE__);
|
||||||
return ESP_ERR_INVALID_RESPONSE;
|
return ESP_ERR_INVALID_RESPONSE;
|
||||||
@ -160,9 +165,9 @@ esp_err_t zh_aht_read(float *humidity, float *temperature)
|
|||||||
esp_err = i2c_master_cmd_begin(_init_config.i2c_port, i2c_cmd_handle, 1000 / portTICK_PERIOD_MS);
|
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_link_delete(i2c_cmd_handle);
|
||||||
#else
|
#else
|
||||||
esp_err = i2c_master_receive(_aht_handle, sensor_data, sizeof(sensor_data), 1000 / portTICK_PERIOD_MS);
|
err = i2c_master_receive(_aht_handle, sensor_data, sizeof(sensor_data), 1000 / portTICK_PERIOD_MS);
|
||||||
#endif
|
#endif
|
||||||
if (esp_err != ESP_OK)
|
if (err != ESP_OK)
|
||||||
{
|
{
|
||||||
ESP_LOGE(TAG, "AHT read fail. I2C driver error at line %d.", __LINE__);
|
ESP_LOGE(TAG, "AHT read fail. I2C driver error at line %d.", __LINE__);
|
||||||
return ESP_ERR_INVALID_RESPONSE;
|
return ESP_ERR_INVALID_RESPONSE;
|
||||||
@ -194,7 +199,7 @@ esp_err_t zh_aht_reset(void)
|
|||||||
ESP_LOGE(TAG, "AHT reset fail. AHT not initialized.");
|
ESP_LOGE(TAG, "AHT reset fail. AHT not initialized.");
|
||||||
return ESP_ERR_NOT_FOUND;
|
return ESP_ERR_NOT_FOUND;
|
||||||
}
|
}
|
||||||
esp_err_t esp_err = ESP_OK;
|
esp_err_t err = ESP_OK;
|
||||||
uint8_t command = I2C_RESET_COMMAND;
|
uint8_t command = I2C_RESET_COMMAND;
|
||||||
#ifdef CONFIG_IDF_TARGET_ESP8266
|
#ifdef CONFIG_IDF_TARGET_ESP8266
|
||||||
i2c_cmd_handle_t i2c_cmd_handle = i2c_cmd_link_create();
|
i2c_cmd_handle_t i2c_cmd_handle = i2c_cmd_link_create();
|
||||||
@ -205,9 +210,9 @@ esp_err_t zh_aht_reset(void)
|
|||||||
esp_err = i2c_master_cmd_begin(_init_config.i2c_port, i2c_cmd_handle, 1000 / portTICK_PERIOD_MS);
|
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_link_delete(i2c_cmd_handle);
|
||||||
#else
|
#else
|
||||||
esp_err = i2c_master_transmit(_aht_handle, &command, sizeof(command), 1000 / portTICK_PERIOD_MS);
|
err = i2c_master_transmit(_aht_handle, &command, sizeof(command), 1000 / portTICK_PERIOD_MS);
|
||||||
#endif
|
#endif
|
||||||
if (esp_err != ESP_OK)
|
if (err != ESP_OK)
|
||||||
{
|
{
|
||||||
ESP_LOGE(TAG, "AHT reset fail. I2C driver error at line %d.", __LINE__);
|
ESP_LOGE(TAG, "AHT reset fail. I2C driver error at line %d.", __LINE__);
|
||||||
return ESP_ERR_INVALID_RESPONSE;
|
return ESP_ERR_INVALID_RESPONSE;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user