feat: 添加 AHT30 温湿度传感器支持,优化 I2C 传感器管理逻辑

This commit is contained in:
Wang Beihong
2026-03-06 10:32:18 +08:00
parent bde2df4e13
commit d8b25ae1ac
8 changed files with 433 additions and 79 deletions

View File

@@ -16,3 +16,4 @@ dependencies:
# public: true
espressif/esp_lvgl_port: ^2.7.2
espressif/bh1750: ^2.0.0
k0i05/esp_ahtxx: ^1.2.7

View File

@@ -1,20 +1,48 @@
#include <stdio.h>
#include <inttypes.h>
#include "sdkconfig.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "esp_check.h"
#include "esp_log.h"
#include "wifi-connect.h"
#include "lvgl_st7735s_use.h"
#include "i2c_master_messager.h"
#define BOTANY_I2C_PORT I2C_NUM_0
#define BOTANY_I2C_SCL_GPIO GPIO_NUM_5
#define BOTANY_I2C_SDA_GPIO GPIO_NUM_4
#define BOTANY_READ_PERIOD_MS 1000
#ifndef CONFIG_I2C_MASTER_MESSAGER_BH1750_ENABLE
#define CONFIG_I2C_MASTER_MESSAGER_BH1750_ENABLE 0
#endif
#ifndef CONFIG_I2C_MASTER_MESSAGER_AHT30_ENABLE
#define CONFIG_I2C_MASTER_MESSAGER_AHT30_ENABLE 0
#endif
#ifndef CONFIG_I2C_MASTER_MESSAGER_BH1750_READ_PERIOD_MS
#define CONFIG_I2C_MASTER_MESSAGER_BH1750_READ_PERIOD_MS 500
#endif
#ifndef CONFIG_I2C_MASTER_MESSAGER_AHT30_READ_PERIOD_MS
#define CONFIG_I2C_MASTER_MESSAGER_AHT30_READ_PERIOD_MS 2000
#endif
#ifndef CONFIG_I2C_MASTER_MESSAGER_ENABLE_INTERNAL_PULLUP
#define CONFIG_I2C_MASTER_MESSAGER_ENABLE_INTERNAL_PULLUP 1
#endif
#define BOTANY_I2C_PORT I2C_NUM_0
#define BOTANY_I2C_SCL_GPIO GPIO_NUM_5
#define BOTANY_I2C_SDA_GPIO GPIO_NUM_4
#define BOTANY_BH1750_ENABLE CONFIG_I2C_MASTER_MESSAGER_BH1750_ENABLE
#define BOTANY_AHT30_ENABLE CONFIG_I2C_MASTER_MESSAGER_AHT30_ENABLE
#define BOTANY_BH1750_PERIOD_MS CONFIG_I2C_MASTER_MESSAGER_BH1750_READ_PERIOD_MS
#define BOTANY_AHT30_PERIOD_MS CONFIG_I2C_MASTER_MESSAGER_AHT30_READ_PERIOD_MS
#define BOTANY_I2C_INTERNAL_PULLUP CONFIG_I2C_MASTER_MESSAGER_ENABLE_INTERNAL_PULLUP
static const char *TAG = "main";
void app_main(void)
{
//初始化 Wi-Fi 配网组件,支持长按按键进入配网
// 初始化 Wi-Fi 配网组件,支持长按按键进入配网
ESP_ERROR_CHECK(wifi_connect_init());
printf("设备启动完成:长按按键进入配网模式,手机连接 ESP32-* 后访问 http://192.168.4.1\n");
@@ -25,27 +53,78 @@ void app_main(void)
.i2c_port = BOTANY_I2C_PORT,
.scl_io_num = BOTANY_I2C_SCL_GPIO,
.sda_io_num = BOTANY_I2C_SDA_GPIO,
.read_period_ms = BOTANY_READ_PERIOD_MS,
.bh1750_addr = 0x23,
.i2c_enable_internal_pullup = BOTANY_I2C_INTERNAL_PULLUP,
.bh1750_enable = BOTANY_BH1750_ENABLE,
.aht30_enable = BOTANY_AHT30_ENABLE,
.bh1750_read_period_ms = BOTANY_BH1750_PERIOD_MS,
.aht30_read_period_ms = BOTANY_AHT30_PERIOD_MS,
.bh1750_mode = BH1750_CONTINUE_1LX_RES,
};
ESP_ERROR_CHECK(i2c_master_messager_init(&i2c_cfg));
ESP_ERROR_CHECK(i2c_master_messager_start());
bool i2c_ready = false;
esp_err_t ret = i2c_master_messager_init(&i2c_cfg);
if (ret == ESP_OK)
{
ret = i2c_master_messager_start();
}
if (ret != ESP_OK)
{
ESP_LOGE(TAG, "I2C 传感器管理启动失败: %s", esp_err_to_name(ret));
ESP_LOGW(TAG, "请检查 I2C 引脚/上拉电阻/端口占用情况,系统将继续运行但不采集传感器");
ESP_ERROR_CHECK(lvgl_st7735s_set_center_text("I2C init failed"));
}
else
{
i2c_ready = true;
}
for (;;)
{
i2c_master_messager_data_t sensor_data = {0};
if (i2c_master_messager_get_data(&sensor_data) == ESP_OK && sensor_data.bh1750.valid) {
char text[32] = {0};
snprintf(text, sizeof(text), "Light: %.1f lx", sensor_data.bh1750.lux);
ESP_ERROR_CHECK(lvgl_st7735s_set_center_text(text));
printf("[BH1750] lux=%.2f update_ms=%" PRId64 "\n",
sensor_data.bh1750.lux,
sensor_data.bh1750.last_update_ms);
} else {
ESP_ERROR_CHECK(lvgl_st7735s_set_center_text("Light: waiting..."));
printf("[BH1750] waiting data, err=0x%x\n", sensor_data.bh1750.last_error);
if (i2c_ready && i2c_master_messager_get_data(&sensor_data) == ESP_OK)
{
char text[64] = {0};
if (BOTANY_BH1750_ENABLE && BOTANY_AHT30_ENABLE &&
sensor_data.bh1750.valid && sensor_data.aht30.valid)
{
snprintf(text,
sizeof(text),
"L:%.0f T:%.1fC\nH:%.1f%%",
sensor_data.bh1750.lux,
sensor_data.aht30.temperature_c,
sensor_data.aht30.humidity_rh);
ESP_ERROR_CHECK(lvgl_st7735s_set_center_text(text));
}
else if (BOTANY_BH1750_ENABLE && sensor_data.bh1750.valid)
{
snprintf(text, sizeof(text), "Light: %.1f lx", sensor_data.bh1750.lux);
ESP_ERROR_CHECK(lvgl_st7735s_set_center_text(text));
}
else if (BOTANY_AHT30_ENABLE && sensor_data.aht30.valid)
{
snprintf(text,
sizeof(text),
"T:%.1fC\nH:%.1f%%",
sensor_data.aht30.temperature_c,
sensor_data.aht30.humidity_rh);
ESP_ERROR_CHECK(lvgl_st7735s_set_center_text(text));
}
else
{
ESP_ERROR_CHECK(lvgl_st7735s_set_center_text("Sensor waiting..."));
}
}
else if (i2c_ready)
{
ESP_ERROR_CHECK(lvgl_st7735s_set_center_text("Sensor read fail"));
}
else
{
vTaskDelay(pdMS_TO_TICKS(2000));
}
vTaskDelay(pdMS_TO_TICKS(1000));