feat: added statistics
This commit is contained in:
1 parent
b0e565d180
commit
2afcddfa0e
4 files changed
+42
-4
No files matched your search
@@ -66,6 +66,8 @@ void app_main(void)
|
|||||||
zh_aht_read(&aht_handle, &humidity, &temperature);
|
zh_aht_read(&aht_handle, &humidity, &temperature);
|
||||||
printf("Humidity %0.2f\n", humidity);
|
printf("Humidity %0.2f\n", humidity);
|
||||||
printf("Temperature %0.2f\n", temperature);
|
printf("Temperature %0.2f\n", temperature);
|
||||||
|
const zh_aht_stats_t *stats = zh_aht_get_stats();
|
||||||
|
printf("Number of i2c driver error: %ld.\n", stats->i2c_driver_error);
|
||||||
vTaskDelay(5000 / portTICK_PERIOD_MS);
|
vTaskDelay(5000 / portTICK_PERIOD_MS);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -41,6 +41,14 @@ extern "C"
|
|||||||
i2c_master_dev_handle_t dev_handle; /*!< Unique I2C device handle. */
|
i2c_master_dev_handle_t dev_handle; /*!< Unique I2C device handle. */
|
||||||
} zh_aht_handle_t;
|
} zh_aht_handle_t;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Structure for error statistics storage.
|
||||||
|
*/
|
||||||
|
typedef struct
|
||||||
|
{
|
||||||
|
uint32_t i2c_driver_error; /*!< Number of i2c driver error. */
|
||||||
|
} zh_aht_stats_t;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Initialize AHT sensor.
|
* @brief Initialize AHT sensor.
|
||||||
*
|
*
|
||||||
@@ -77,6 +85,18 @@ extern "C"
|
|||||||
*/
|
*/
|
||||||
esp_err_t zh_aht_reset(zh_aht_handle_t *handle);
|
esp_err_t zh_aht_reset(zh_aht_handle_t *handle);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Get error statistics.
|
||||||
|
*
|
||||||
|
* @return Pointer to the statistics structure.
|
||||||
|
*/
|
||||||
|
const zh_aht_stats_t *zh_aht_get_stats(void);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Reset error statistics.
|
||||||
|
*/
|
||||||
|
void zh_aht_reset_stats(void);
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
+1
-1
@@ -1 +1 @@
|
|||||||
3.1.0
|
3.2.0
|
||||||
@@ -16,6 +16,7 @@ static const char *TAG = "zh_aht";
|
|||||||
static const uint8_t _aht_reset_command = 0xBA;
|
static const uint8_t _aht_reset_command = 0xBA;
|
||||||
static const uint8_t _aht_read_command[] = {0xAC, 0x33, 0x00};
|
static const uint8_t _aht_read_command[] = {0xAC, 0x33, 0x00};
|
||||||
static const uint8_t _aht_init_command[] = {0xBE, 0x08, 0x00};
|
static const uint8_t _aht_init_command[] = {0xBE, 0x08, 0x00};
|
||||||
|
static zh_aht_stats_t _stats = {0};
|
||||||
|
|
||||||
static esp_err_t _zh_aht_validate_config(const zh_aht_init_config_t *config);
|
static esp_err_t _zh_aht_validate_config(const zh_aht_init_config_t *config);
|
||||||
static esp_err_t _zh_aht_i2c_init(const zh_aht_init_config_t *config, zh_aht_handle_t *handle);
|
static esp_err_t _zh_aht_i2c_init(const zh_aht_init_config_t *config, zh_aht_handle_t *handle);
|
||||||
@@ -38,9 +39,11 @@ esp_err_t zh_aht_read(zh_aht_handle_t *handle, float *humidity, float *temperatu
|
|||||||
ZH_ERROR_CHECK(humidity != NULL && temperature != NULL && handle != NULL, ESP_ERR_INVALID_ARG, NULL, "AHT read fail. Invalid argument.");
|
ZH_ERROR_CHECK(humidity != NULL && temperature != NULL && handle != NULL, ESP_ERR_INVALID_ARG, NULL, "AHT read fail. Invalid argument.");
|
||||||
ZH_ERROR_CHECK(handle->is_initialized == true, ESP_ERR_NOT_FOUND, NULL, "AHT read fail. AHT not initialized.");
|
ZH_ERROR_CHECK(handle->is_initialized == true, ESP_ERR_NOT_FOUND, NULL, "AHT read fail. AHT not initialized.");
|
||||||
uint8_t sensor_data[7] = {0};
|
uint8_t sensor_data[7] = {0};
|
||||||
ZH_ERROR_CHECK(i2c_master_transmit(handle->dev_handle, _aht_read_command, sizeof(_aht_read_command), 1000 / portTICK_PERIOD_MS) == ESP_OK, ESP_FAIL, NULL, "AHT read fail. I2C driver error.");
|
ZH_ERROR_CHECK(i2c_master_transmit(handle->dev_handle, _aht_read_command, sizeof(_aht_read_command), 1000 / portTICK_PERIOD_MS) == ESP_OK, ESP_FAIL,
|
||||||
|
++_stats.i2c_driver_error, "AHT read fail. I2C driver error.");
|
||||||
vTaskDelay(80 / portTICK_PERIOD_MS);
|
vTaskDelay(80 / portTICK_PERIOD_MS);
|
||||||
ZH_ERROR_CHECK(i2c_master_receive(handle->dev_handle, sensor_data, sizeof(sensor_data), 1000 / portTICK_PERIOD_MS) == ESP_OK, ESP_FAIL, NULL, "AHT read fail. I2C driver error.");
|
ZH_ERROR_CHECK(i2c_master_receive(handle->dev_handle, sensor_data, sizeof(sensor_data), 1000 / portTICK_PERIOD_MS) == ESP_OK, ESP_FAIL,
|
||||||
|
++_stats.i2c_driver_error, "AHT read fail. I2C driver error.");
|
||||||
ZH_ERROR_CHECK((sensor_data[0] & 0x80) == 0, ESP_ERR_TIMEOUT, NULL, "AHT read fail. Timeout exceeded.");
|
ZH_ERROR_CHECK((sensor_data[0] & 0x80) == 0, ESP_ERR_TIMEOUT, NULL, "AHT read fail. Timeout exceeded.");
|
||||||
ZH_ERROR_CHECK(_zh_aht_calc_crc(sensor_data, 6) == sensor_data[6], ESP_ERR_INVALID_CRC, NULL, "AHT read fail. Invalid CRC.");
|
ZH_ERROR_CHECK(_zh_aht_calc_crc(sensor_data, 6) == sensor_data[6], ESP_ERR_INVALID_CRC, NULL, "AHT read fail. Invalid CRC.");
|
||||||
*humidity = (((((uint32_t)sensor_data[1]) << 16) | (((uint32_t)sensor_data[2]) << 8) | (((uint32_t)sensor_data[3]) << 0)) >> 4) / 1048576.0 * 100.0;
|
*humidity = (((((uint32_t)sensor_data[1]) << 16) | (((uint32_t)sensor_data[2]) << 8) | (((uint32_t)sensor_data[3]) << 0)) >> 4) / 1048576.0 * 100.0;
|
||||||
@@ -54,12 +57,25 @@ esp_err_t zh_aht_reset(zh_aht_handle_t *handle)
|
|||||||
ZH_LOGI("AHT reset begin.");
|
ZH_LOGI("AHT reset begin.");
|
||||||
ZH_ERROR_CHECK(handle != NULL, ESP_ERR_INVALID_ARG, NULL, "AHT reset fail. Invalid argument.");
|
ZH_ERROR_CHECK(handle != NULL, ESP_ERR_INVALID_ARG, NULL, "AHT reset fail. Invalid argument.");
|
||||||
ZH_ERROR_CHECK(handle->is_initialized == true, ESP_ERR_NOT_FOUND, NULL, "AHT reset fail. AHT not initialized.");
|
ZH_ERROR_CHECK(handle->is_initialized == true, ESP_ERR_NOT_FOUND, NULL, "AHT reset fail. AHT not initialized.");
|
||||||
ZH_ERROR_CHECK(i2c_master_transmit(handle->dev_handle, &_aht_reset_command, sizeof(_aht_reset_command), 1000 / portTICK_PERIOD_MS) == ESP_OK, ESP_FAIL, NULL, "AHT reset fail. I2C driver error.");
|
ZH_ERROR_CHECK(i2c_master_transmit(handle->dev_handle, &_aht_reset_command, sizeof(_aht_reset_command), 1000 / portTICK_PERIOD_MS) == ESP_OK, ESP_FAIL,
|
||||||
|
++_stats.i2c_driver_error, "AHT reset fail. I2C driver error.");
|
||||||
vTaskDelay(20 / portTICK_PERIOD_MS);
|
vTaskDelay(20 / portTICK_PERIOD_MS);
|
||||||
ZH_LOGI("AHT reset completed successfully.");
|
ZH_LOGI("AHT reset completed successfully.");
|
||||||
return ESP_OK;
|
return ESP_OK;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const zh_aht_stats_t *zh_aht_get_stats(void)
|
||||||
|
{
|
||||||
|
return &_stats;
|
||||||
|
}
|
||||||
|
|
||||||
|
void zh_aht_reset_stats(void)
|
||||||
|
{
|
||||||
|
ZH_LOGI("Error statistic reset started.");
|
||||||
|
_stats.i2c_driver_error = 0;
|
||||||
|
ZH_LOGI("Error statistic reset successfully.");
|
||||||
|
}
|
||||||
|
|
||||||
static esp_err_t _zh_aht_validate_config(const zh_aht_init_config_t *config)
|
static esp_err_t _zh_aht_validate_config(const zh_aht_init_config_t *config)
|
||||||
{
|
{
|
||||||
ZH_ERROR_CHECK(config->i2c_address == 0x38 || config->i2c_address == 0x39, ESP_ERR_INVALID_ARG, NULL, "Invalid I2C address.");
|
ZH_ERROR_CHECK(config->i2c_address == 0x38 || config->i2c_address == 0x39, ESP_ERR_INVALID_ARG, NULL, "Invalid I2C address.");
|
||||||
|
|||||||
Reference in new issue
Block a user