能用,自动没测,不想改了,崩溃了

This commit is contained in:
Wang Beihong
2026-03-31 21:52:29 +08:00
parent f0ac50642d
commit f8c7706c11
32 changed files with 15324 additions and 12035 deletions

View File

@@ -4,7 +4,7 @@
#include "bh1750.h"
#include "sgp30/sgp30.h"
#include "driver/i2c_master.h"
#include "vars.h"
static const char *TAG = "sensors";
@@ -112,10 +112,18 @@ void i2c0_ahtxx_task(void *pvParameters)
g_sensor_data.ahtxx_valid = true;
xSemaphoreGive(xSensorDataMutex);
}
ESP_LOGI(TAG, "AHTxx: temperature=%.1f°C, humidity=%.1f%%", temperature, humidity);
// ESP_LOGI(TAG, "AHTxx: temperature=%.1f°C, humidity=%.1f%%", temperature, humidity);
// 更新屏幕显示变量
char temp_str[16];
char humidity_str[16];
snprintf(temp_str, sizeof(temp_str), "%.1f", temperature);
snprintf(humidity_str, sizeof(humidity_str), "%.1f", humidity);
set_var_tempture(temp_str);
set_var_humity(humidity_str);
}
vTaskDelay(pdMS_TO_TICKS(5000));
}
}
@@ -141,20 +149,20 @@ void i2c0_bh1750_task(void *pvParameters)
}
}
// 设置测量模式为连续高分辨率模式
ret = bh1750_set_measure_mode(bh1750_handle, BH1750_CONTINUE_1LX_RES);
if (ret != ESP_OK)
{
ESP_LOGE(TAG, "BH1750 set measure mode failed, err=0x%x", ret);
}
// 上电
// 先上电
ret = bh1750_power_on(bh1750_handle);
if (ret != ESP_OK)
{
ESP_LOGE(TAG, "BH1750 power on failed, err=0x%x", ret);
}
// 设置测量模式为连续高分辨率模式必须在power_on之后
ret = bh1750_set_measure_mode(bh1750_handle, BH1750_CONTINUE_1LX_RES);
if (ret != ESP_OK)
{
ESP_LOGE(TAG, "BH1750 set measure mode failed, err=0x%x", ret);
}
float lux;
while (1)
{
@@ -176,10 +184,15 @@ void i2c0_bh1750_task(void *pvParameters)
g_sensor_data.bh1750_valid = true;
xSemaphoreGive(xSensorDataMutex);
}
ESP_LOGI(TAG, "BH1750: illuminance=%.1f lux", lux);
// ESP_LOGI(TAG, "BH1750: illuminance=%.1f lux", lux);
// 更新屏幕显示变量
char light_str[16];
snprintf(light_str, sizeof(light_str), "%.1f", lux);
set_var_light_value(light_str);
}
vTaskDelay(pdMS_TO_TICKS(5000));
}
}
@@ -187,13 +200,13 @@ void i2c0_bh1750_task(void *pvParameters)
void i2c0_sgp30_task(void *pvParameters)
{
(void)pvParameters;
// SGP30 配置
sgp30_config_t sgp30_config = {
.i2c_master_port = I2C_MASTER_NUM,
.i2c_address = 0x58, // SGP30 默认地址
};
// 初始化 SGP30
esp_err_t ret = sgp30_init(&sgp30_config);
if (ret != ESP_OK)
@@ -209,16 +222,16 @@ void i2c0_sgp30_task(void *pvParameters)
vTaskDelay(pdMS_TO_TICKS(5000));
}
}
ESP_LOGI(TAG, "SGP30 initialized successfully");
uint16_t co2_ppm, tvoc_ppb;
while (1)
{
ret = sgp30_read_measurements(&co2_ppm, &tvoc_ppb);
if (ret != ESP_OK)
{
ESP_LOGE(TAG, "SGP30 read failed, err=0x%x", ret);
if (xSensorDataMutex != NULL && xSemaphoreTake(xSensorDataMutex, portMAX_DELAY) == pdTRUE)
{
g_sensor_data.sgp30_valid = false;
@@ -234,10 +247,18 @@ void i2c0_sgp30_task(void *pvParameters)
g_sensor_data.sgp30_valid = true;
xSemaphoreGive(xSensorDataMutex);
}
ESP_LOGI(TAG, "SGP30: CO2=%d ppm, TVOC=%d ppb", co2_ppm, tvoc_ppb);
// ESP_LOGI(TAG, "SGP30: CO2=%d ppm, TVOC=%d ppb", co2_ppm, tvoc_ppb);
// 更新屏幕显示变量
char co2_str[16];
char voc_str[16];
snprintf(co2_str, sizeof(co2_str), "%d", co2_ppm);
snprintf(voc_str, sizeof(voc_str), "%d", tvoc_ppb);
set_var_co2_value(co2_str);
set_var_voc_value(voc_str);
}
// SGP30 建议每秒读取一次
vTaskDelay(pdMS_TO_TICKS(1000));
}
@@ -255,15 +276,6 @@ void get_sensor_data(sensor_data_t *data)
}
}
void print_sensor_data(void)
{
if (xSensorDataMutex != NULL && xSemaphoreTake(xSensorDataMutex, portMAX_DELAY) == pdTRUE)
{
// 传感器日志已移除,保持占位
xSemaphoreGive(xSensorDataMutex);
}
}
esp_err_t sensors_init(void)
{
esp_err_t err = i2c_master_init();
@@ -273,17 +285,23 @@ esp_err_t sensors_init(void)
return err;
}
BaseType_t ok = xTaskCreate(i2c0_ahtxx_task, "i2c0_ahtxx_task", 4096, NULL, 5, NULL);
ESP_LOGI(TAG, "创建传感器任务前堆: %d", heap_caps_get_free_size(MALLOC_CAP_8BIT));
/* i2c0_ahtxx_task 需要较大栈,防止栈溢出(观察到任务崩溃),恢复为 4096 */
BaseType_t ok = xTaskCreate(i2c0_ahtxx_task, "i2c0_ahtxx_task", 3048, NULL, 5, NULL);
ESP_LOGI(TAG, "创建 i2c0_ahtxx_task 后堆: %d", heap_caps_get_free_size(MALLOC_CAP_8BIT));
if (ok != pdPASS)
{
return ESP_ERR_NO_MEM;
}
ok = xTaskCreate(i2c0_bh1750_task, "i2c0_bh1750_task", 4096, NULL, 5, NULL);
// 降低光照/SGP30任务栈到 1536释放堆空间如遇溢出可再调大
ok = xTaskCreate(i2c0_bh1750_task, "i2c0_bh1750_task", 3072, NULL, 5, NULL);
ESP_LOGI(TAG, "创建 i2c0_bh1750_task 后堆: %d", heap_caps_get_free_size(MALLOC_CAP_8BIT));
if (ok != pdPASS)
{
return ESP_ERR_NO_MEM;
}
ok = xTaskCreate(i2c0_sgp30_task, "i2c0_sgp30_task", 4096, NULL, 5, NULL);
ok = xTaskCreate(i2c0_sgp30_task, "i2c0_sgp30_task", 2048, NULL, 5, NULL);
ESP_LOGI(TAG, "创建 i2c0_sgp30_task 后堆: %d", heap_caps_get_free_size(MALLOC_CAP_8BIT));
if (ok != pdPASS)
{
return ESP_ERR_NO_MEM;