feat!: added unique sensor handle

This commit is contained in:
aZholtikov committed 2026-04-05 10:38:13 +03:00
1 parent 89ad22346c
commit 5269a4bade
4 files changed
+61 -36

No files matched your search

+16 -5
View File
@@ -8,6 +8,15 @@
1. Support of AHT10/AHT15/AHT20/AHT21/AHT25/AHT30. 1. Support of AHT10/AHT15/AHT20/AHT21/AHT25/AHT30.
## Attention
For correct operation, please enable the following settings in the menuconfig:
```text
I2C_ISR_IRAM_SAFE
I2C_MASTER_ISR_HANDLER_IN_IRAM
```
## Using ## Using
In an existing project, run the following command to install the component: In an existing project, run the following command to install the component:
@@ -32,6 +41,8 @@ Reading the sensor:
#define I2C_PORT (I2C_NUM_MAX - 1) #define I2C_PORT (I2C_NUM_MAX - 1)
zh_aht_handle_t aht_handle = {0};
void app_main(void) void app_main(void)
{ {
esp_log_level_set("zh_aht", ESP_LOG_ERROR); esp_log_level_set("zh_aht", ESP_LOG_ERROR);
@@ -43,16 +54,16 @@ void app_main(void)
.glitch_ignore_cnt = 7, .glitch_ignore_cnt = 7,
.flags.enable_internal_pullup = true, .flags.enable_internal_pullup = true,
}; };
i2c_master_bus_handle_t i2c_bus_handle; i2c_master_bus_handle_t i2c_bus_handle = NULL;
i2c_new_master_bus(&i2c_bus_config, &i2c_bus_handle); i2c_new_master_bus(&i2c_bus_config, &i2c_bus_handle);
zh_aht_init_config_t aht_init_config = ZH_AHT_INIT_CONFIG_DEFAULT(); zh_aht_init_config_t config = ZH_AHT_INIT_CONFIG_DEFAULT();
aht_init_config.i2c_handle = i2c_bus_handle; config.i2c_handle = i2c_bus_handle;
zh_aht_init(&aht_init_config); zh_aht_init(&config, &aht_handle);
float humidity = 0.0; float humidity = 0.0;
float temperature = 0.0; float temperature = 0.0;
for (;;) for (;;)
{ {
zh_aht_read(&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);
vTaskDelay(5000 / portTICK_PERIOD_MS); vTaskDelay(5000 / portTICK_PERIOD_MS);
+18 -4
View File
@@ -14,7 +14,7 @@
*/ */
#define ZH_AHT_INIT_CONFIG_DEFAULT() \ #define ZH_AHT_INIT_CONFIG_DEFAULT() \
{ \ { \
} .i2c_address = 0x38}
#ifdef __cplusplus #ifdef __cplusplus
extern "C" extern "C"
@@ -27,12 +27,23 @@ extern "C"
typedef struct typedef struct
{ {
i2c_master_bus_handle_t i2c_handle; /*!< Unique I2C bus handle. */ i2c_master_bus_handle_t i2c_handle; /*!< Unique I2C bus handle. */
uint8_t i2c_address; /*!< Sensor I2C address. */
} zh_aht_init_config_t; } zh_aht_init_config_t;
/**
* @brief AHT sensor handle.
*/
typedef struct
{
bool is_initialized; /*!< Sensor initialization flag. */
i2c_master_dev_handle_t dev_handle; /*!< Unique I2C device handle. */
} zh_aht_handle_t;
/** /**
* @brief Initialize AHT sensor. * @brief Initialize AHT sensor.
* *
* @param[in] config Pointer to AHT initialized configuration structure. Can point to a temporary variable. * @param[in] config Pointer to AHT initialized configuration structure. Can point to a temporary variable.
* @param[out] handle Pointer to unique AHT handle.
* *
* @attention I2C driver must be initialized first. * @attention I2C driver must be initialized first.
* *
@@ -42,24 +53,27 @@ extern "C"
* *
* @return ESP_OK if success or an error code otherwise. * @return ESP_OK if success or an error code otherwise.
*/ */
esp_err_t zh_aht_init(const zh_aht_init_config_t *config); esp_err_t zh_aht_init(const zh_aht_init_config_t *config, zh_aht_handle_t *handle);
/** /**
* @brief Read AHT sensor. * @brief Read AHT sensor.
* *
* @param[in] handle Pointer to unique AHT handle.
* @param[out] humidity Pointer for AHT sensor reading data of humidity. * @param[out] humidity Pointer for AHT sensor reading data of humidity.
* @param[out] temperature Pointer for AHT sensor reading data of temperature. * @param[out] temperature Pointer for AHT sensor reading data of temperature.
* *
* @return ESP_OK if success or an error code otherwise. * @return ESP_OK if success or an error code otherwise.
*/ */
esp_err_t zh_aht_read(float *humidity, float *temperature); esp_err_t zh_aht_read(zh_aht_handle_t *handle, float *humidity, float *temperature);
/** /**
* @brief Reset AHT sensor. * @brief Reset AHT sensor.
* *
* @param[in] handle Pointer to unique AHT handle.
*
* @return ESP_OK if success or an error code otherwise. * @return ESP_OK if success or an error code otherwise.
*/ */
esp_err_t zh_aht_reset(void); esp_err_t zh_aht_reset(zh_aht_handle_t *handle);
#ifdef __cplusplus #ifdef __cplusplus
} }
+1 -1
View File
@@ -1 +1 @@
2.0.0 3.0.0
+26 -26
View File
@@ -16,35 +16,31 @@ 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_init_config_t _init_config = {0};
volatile static bool _is_initialized = false;
static i2c_master_dev_handle_t _aht_handle = {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); static esp_err_t _zh_aht_i2c_init(const zh_aht_init_config_t *config, zh_aht_handle_t *handle);
static uint8_t _zh_aht_calc_crc(const uint8_t *buf, size_t len); static uint8_t _zh_aht_calc_crc(const uint8_t *buf, size_t len);
esp_err_t zh_aht_init(const zh_aht_init_config_t *config) esp_err_t zh_aht_init(const zh_aht_init_config_t *config, zh_aht_handle_t *handle)
{ {
ZH_LOGI("AHT initialization begin."); ZH_LOGI("AHT initialization begin.");
ZH_ERROR_CHECK(config != NULL, ESP_ERR_INVALID_ARG, NULL, "AHT initialization failed. Invalid argument."); ZH_ERROR_CHECK(config != NULL && handle != NULL, ESP_ERR_INVALID_ARG, NULL, "AHT initialization failed. Invalid argument.");
ZH_ERROR_CHECK(_zh_aht_validate_config(config) == ESP_OK, ESP_FAIL, NULL, "AHT initialization failed. Initial configuration check failed."); ZH_ERROR_CHECK(_zh_aht_validate_config(config) == ESP_OK, ESP_FAIL, NULL, "AHT initialization failed. Initial configuration check failed.");
ZH_ERROR_CHECK(_zh_aht_i2c_init(config) == ESP_OK, ESP_FAIL, NULL, "AHT initialization failed. Failed to add I2C device."); ZH_ERROR_CHECK(_zh_aht_i2c_init(config, handle) == ESP_OK, ESP_FAIL, NULL, "AHT initialization failed. Failed to add I2C device.");
_init_config = *config; handle->is_initialized = true;
_is_initialized = true;
ZH_LOGI("AHT initialization completed successfully."); ZH_LOGI("AHT initialization completed successfully.");
return ESP_OK; return ESP_OK;
} }
esp_err_t zh_aht_read(float *humidity, float *temperature) esp_err_t zh_aht_read(zh_aht_handle_t *handle, float *humidity, float *temperature)
{ {
ZH_LOGI("AHT read begin."); ZH_LOGI("AHT read begin.");
ZH_ERROR_CHECK(_is_initialized == true, ESP_ERR_NOT_FOUND, NULL, "AHT read fail. AHT not initialized."); 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, 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.");
uint8_t sensor_data[7] = {0}; uint8_t sensor_data[7] = {0};
ZH_ERROR_CHECK(i2c_master_transmit(_aht_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, NULL, "AHT read fail. I2C driver error.");
vTaskDelay(80 / portTICK_PERIOD_MS); vTaskDelay(80 / portTICK_PERIOD_MS);
ZH_ERROR_CHECK(i2c_master_receive(_aht_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, NULL, "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;
@@ -53,11 +49,12 @@ esp_err_t zh_aht_read(float *humidity, float *temperature)
return ESP_OK; return ESP_OK;
} }
esp_err_t zh_aht_reset(void) esp_err_t zh_aht_reset(zh_aht_handle_t *handle)
{ {
ZH_LOGI("AHT reset begin."); ZH_LOGI("AHT reset begin.");
ZH_ERROR_CHECK(_is_initialized == true, ESP_ERR_NOT_FOUND, NULL, "AHT reset fail. AHT not initialized."); ZH_ERROR_CHECK(handle != NULL, ESP_ERR_INVALID_ARG, NULL, "AHT reset fail. Invalid argument.");
ZH_ERROR_CHECK(i2c_master_transmit(_aht_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(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.");
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;
@@ -65,30 +62,33 @@ esp_err_t zh_aht_reset(void)
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_handle != NULL, ESP_ERR_INVALID_ARG, NULL, "Invalid I2C handle."); ZH_ERROR_CHECK(config->i2c_handle != NULL, ESP_ERR_INVALID_ARG, NULL, "Invalid I2C handle.");
return ESP_OK; return ESP_OK;
} }
static esp_err_t _zh_aht_i2c_init(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)
{ {
i2c_device_config_t aht_config = { i2c_device_config_t aht_config = {
.dev_addr_length = I2C_ADDR_BIT_LEN_7, .dev_addr_length = I2C_ADDR_BIT_LEN_7,
.device_address = 0x38, .device_address = config->i2c_address,
.scl_speed_hz = 100000, .scl_speed_hz = 100000,
}; };
ZH_ERROR_CHECK(i2c_master_bus_add_device(config->i2c_handle, &aht_config, &_aht_handle) == ESP_OK, ESP_FAIL, NULL, "Add I2C device failed."); i2c_master_dev_handle_t _dev_handle = NULL;
ZH_ERROR_CHECK(i2c_master_bus_add_device(config->i2c_handle, &aht_config, &_dev_handle) == ESP_OK, ESP_FAIL, NULL, "Add I2C device failed.");
vTaskDelay(100 / portTICK_PERIOD_MS); vTaskDelay(100 / portTICK_PERIOD_MS);
ZH_ERROR_CHECK(i2c_master_probe(config->i2c_handle, 0x38, 1000 / portTICK_PERIOD_MS) == ESP_OK, ESP_ERR_NOT_FOUND, ZH_ERROR_CHECK(i2c_master_probe(config->i2c_handle, config->i2c_address, 1000 / portTICK_PERIOD_MS) == ESP_OK, ESP_ERR_NOT_FOUND,
ZH_ERROR_CHECK(i2c_master_bus_rm_device(_aht_handle) == ESP_OK, ESP_FAIL, NULL, "I2C remove device failed."), "Sensor not connected or not responded."); ZH_ERROR_CHECK(i2c_master_bus_rm_device(_dev_handle) == ESP_OK, ESP_FAIL, NULL, "I2C remove device failed."), "Sensor not connected or not responded.");
uint8_t sensor_data = 0; uint8_t sensor_data = 0;
ZH_ERROR_CHECK(i2c_master_receive(_aht_handle, &sensor_data, sizeof(sensor_data), 1000 / portTICK_PERIOD_MS) == ESP_OK, ESP_FAIL, ZH_ERROR_CHECK(i2c_master_receive(_dev_handle, &sensor_data, sizeof(sensor_data), 1000 / portTICK_PERIOD_MS) == ESP_OK, ESP_FAIL,
ZH_ERROR_CHECK(i2c_master_bus_rm_device(_aht_handle) == ESP_OK, ESP_FAIL, NULL, "I2C remove device failed."), "I2C driver error."); ZH_ERROR_CHECK(i2c_master_bus_rm_device(_dev_handle) == ESP_OK, ESP_FAIL, NULL, "I2C remove device failed."), "I2C driver error.");
if ((sensor_data & 0x08) == 0) if ((sensor_data & 0x08) == 0)
{ {
ZH_ERROR_CHECK(i2c_master_transmit(_aht_handle, _aht_init_command, sizeof(_aht_init_command), 1000 / portTICK_PERIOD_MS) == ESP_OK, ESP_FAIL, ZH_ERROR_CHECK(i2c_master_transmit(_dev_handle, _aht_init_command, sizeof(_aht_init_command), 1000 / portTICK_PERIOD_MS) == ESP_OK, ESP_FAIL,
ZH_ERROR_CHECK(i2c_master_bus_rm_device(_aht_handle) == ESP_OK, ESP_FAIL, NULL, "I2C remove device failed."), "I2C driver error."); ZH_ERROR_CHECK(i2c_master_bus_rm_device(_dev_handle) == ESP_OK, ESP_FAIL, NULL, "I2C remove device failed."), "I2C driver error.");
vTaskDelay(20 / portTICK_PERIOD_MS); vTaskDelay(20 / portTICK_PERIOD_MS);
} }
handle->dev_handle = _dev_handle;
return ESP_OK; return ESP_OK;
} }