Compare commits
4 Commits
feature/we
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
81da463124 | ||
|
|
981dc2b47c | ||
|
|
7accf1279e | ||
|
|
5980e171c4 |
252
README.md
252
README.md
@@ -1,92 +1,198 @@
|
||||
# BotanicalBuddy
|
||||
|
||||
需求:
|
||||
智能盆栽管理系统
|
||||
1. 环境全维度监测:实时、同步监测土壤湿度、环境温湿度、光照强度。
|
||||
2. 智能预警通知:当任何监测数据超出用户设定的阈值时,系统自动向手机App推送报警信息。
|
||||
3. 双向远程控制:
|
||||
· 手动控制:用户通过手机App远程手动控制水泵浇水、补光灯开关。
|
||||
· 自动控制:系统根据预设阈值(如土壤过干)自动执行浇水或补光。
|
||||
4. 双模式人机交互:
|
||||
· 远程交互:通过手机App查看实时数据、历史曲线和进行控制。
|
||||
· 本地交互:通过LCD屏幕现场查看系统状态与关键数据。
|
||||
基于 ESP-IDF 的智能盆栽系统固件项目(ESP32-C3)。
|
||||
|
||||
基于 ESP-IDF 的植物助手项目,当前已集成:
|
||||
当前结论:单片机端核心功能已完成,可直接联调 App/小程序侧。
|
||||
|
||||
- **Wi-Fi 配网组件(wifi-connect)**:手机连接设备热点后通过网页完成路由器配置
|
||||
- **LCD 显示组件(lvgl_st7735s_use)**:基于 LVGL 驱动 ST77xx SPI 屏并显示界面
|
||||
- **I2C 传感器组件(i2c_master_messager)**:统一读取 BH1750 / AHT30 数据
|
||||
- **IO 外设控制组件(io_device_control)**:控制水泵与光照开关(高电平有效)
|
||||
## 固件完成度
|
||||
|
||||
## 功能特性
|
||||
- 环境采集:土壤湿度、空气温湿度、光照强度
|
||||
- 本地显示:LCD + LVGL 多页面轮播
|
||||
- 设备控制:水泵、补光灯(高电平有效)
|
||||
- 自动控制:阈值 + 回差控制
|
||||
- 手动控制:MQTT 远程开关泵/灯
|
||||
- 模式切换:`auto` / `manual`
|
||||
- 告警推送:超阈值边沿事件上报
|
||||
- 状态上报:周期性遥测(含模式与执行器状态)
|
||||
- Wi-Fi 配网:SoftAP + Captive Portal
|
||||
- 状态网页:独立 HTTP 状态看板与 JSON API(端口 8080)
|
||||
|
||||
- 长按按键进入配网模式
|
||||
- 支持两种配网策略:按键触发 / 常驻配网
|
||||
- 设备开启 SoftAP(`ESP32-xxxxxx`)+ Captive Portal
|
||||
- 手机访问 `http://192.168.4.1` 完成 Wi-Fi 配置
|
||||
- 支持清除已保存 Wi-Fi 参数并重新配网
|
||||
- 串口中文状态日志,便于调试和现场维护
|
||||
- 支持 ST77xx SPI LCD 显示(LVGL)
|
||||
- 支持方向/偏移参数化配置,便于后续适配不同屏幕
|
||||
- 支持水泵(GPIO1)与光照(GPIO10)控制接口
|
||||
## 系统架构
|
||||
|
||||
## 目录结构
|
||||
- `main/`:业务编排、控制循环、MQTT 回调对接
|
||||
- `components/wifi-connect/`:配网与路由连接
|
||||
- `components/lvgl_st7735s_use/`:LCD 与 LVGL 端口
|
||||
- `components/ui/`:界面对象与变量绑定
|
||||
- `components/i2c_master_messager/`:AHT30、BH1750 采集
|
||||
- `components/capactive_soil_moisture_sensor_V2.0/`:土壤湿度采集
|
||||
- `components/io_device_control/`:水泵/补光灯 GPIO 控制
|
||||
- `components/mqtt_control/`:MQTT 连接、发布、控制指令解析
|
||||
- `main/auto_ctrl_thresholds.*`:阈值存取与校验
|
||||
- `main/auto_alerts.*`:告警判定与回调分发
|
||||
- `main/status_web.*`:独立状态网页服务(HTTP 8080)
|
||||
|
||||
- `main/`:应用入口(`app_main`)
|
||||
- `components/wifi-connect/`:配网组件实现与文档
|
||||
- `README.md`:组件说明
|
||||
- `USER_GUIDE.md`:用户操作手册
|
||||
- `QUICK_POSTER.md`:张贴版快速指引
|
||||
- `BLOG.md`:博客草稿
|
||||
- `components/lvgl_st7735s_use/`:LCD 显示组件(LVGL + ST77xx)
|
||||
- `README.md`:组件说明与调参指南
|
||||
- `components/i2c_master_messager/`:I2C 传感器管理组件
|
||||
- `README.md`:传感器采集与配置说明
|
||||
- `components/io_device_control/`:IO 外设控制组件
|
||||
- `README.md`:水泵/光照控制接口说明
|
||||
## 状态网页(独立于配网页)
|
||||
|
||||
- 配网页面:`http://192.168.4.1`(仅 SoftAP 配网阶段)
|
||||
- 状态页面:`http://<设备STA_IP>:8080/`
|
||||
- 状态 API:`http://<设备STA_IP>:8080/api/status`
|
||||
|
||||
说明:
|
||||
- 两个网页服务独立运行,端口不同、职责不同。
|
||||
- 状态页用于运行态观测,不承载 Wi-Fi 配网流程。
|
||||
|
||||
`/api/status` 当前主要字段:
|
||||
- `temp`、`hum`、`soil`、`lux`:传感器字符串值
|
||||
- `pump`、`light`:执行器状态(`on/off`)
|
||||
- `mode`:控制模式(`auto/manual`)
|
||||
- `soil_on`、`soil_off`、`light_on`、`light_off`:自动控制阈值
|
||||
- `wifi_status`:Wi-Fi 状态(`idle/provisioning/connecting/connected/failed/timeout`)
|
||||
- `sta_ip`:STA 当前 IP
|
||||
- `mqtt_connected`:MQTT 连接状态(布尔)
|
||||
- `i2c_ready`、`soil_sensor_ready`:关键外设初始化状态(布尔)
|
||||
- `loop_counter`:主循环计数
|
||||
- `uptime_ms`:设备运行时长(毫秒)
|
||||
- `free_heap`、`min_free_heap`、`largest_block`:堆内存指标
|
||||
- `app_version`:固件版本字符串
|
||||
- `snapshot_update_ms`、`snapshot_update_count`、`snapshot_age_ms`:状态快照时间与更新统计
|
||||
|
||||
## 运行逻辑
|
||||
|
||||
1. 上电初始化 Wi-Fi、LCD、传感器、IO。
|
||||
2. Wi-Fi 连通后启动 MQTT 与 Console。
|
||||
3. 主循环每 1s 执行:
|
||||
- 采集传感器并刷新 UI 数据。
|
||||
- 若 `mode=auto`,按阈值进行泵灯自动控制。
|
||||
- 进行告警边沿判定并发布告警消息。
|
||||
- 每 5s 发布一次状态遥测消息。
|
||||
4. 收到 MQTT 控制消息时:
|
||||
- 可切模式(`auto/manual`)。
|
||||
- 可更新阈值(四个阈值需同条下发)。
|
||||
- 可手动控制泵灯开关。
|
||||
|
||||
## 开发环境
|
||||
|
||||
- Linux
|
||||
- ESP-IDF `v5.5.2`(建议)
|
||||
- Python 与 ESP-IDF 工具链按官方方式安装
|
||||
- ESP-IDF `v5.5.2`
|
||||
- 目标芯片:`esp32c3`
|
||||
|
||||
## 快速开始
|
||||
## 编译与烧录
|
||||
|
||||
1. 配置并编译
|
||||
- `idf.py set-target esp32`
|
||||
- `idf.py build`
|
||||
2. 烧录并查看日志
|
||||
- `idf.py -p /dev/ttyUSB0 flash monitor`
|
||||
3. 显示初始化
|
||||
- 在 `app_main` 中调用:`ESP_ERROR_CHECK(start_lvgl_demo());`
|
||||
- 可选:`ESP_ERROR_CHECK(lvgl_st7735s_set_center_text("BotanicalBuddy"));`
|
||||
4. 配网
|
||||
- 按键触发模式:长按设备按键进入配网模式
|
||||
- 常驻配网模式:上电自动进入配网模式
|
||||
- 手机连接 `ESP32-xxxxxx`
|
||||
- 打开 `http://192.168.4.1`
|
||||
- 选择路由器并输入密码提交
|
||||
1. 配置环境变量
|
||||
```bash
|
||||
export IDF_PATH=/home/beihong/esp/v5.5.2/esp-idf
|
||||
source $IDF_PATH/export.sh
|
||||
```
|
||||
|
||||
## 调试建议
|
||||
2. 构建
|
||||
```bash
|
||||
idf.py set-target esp32c3
|
||||
idf.py build
|
||||
```
|
||||
|
||||
- 若出现“按键未按下却进入配网”,通常是按键引脚与 LCD/外设复用导致电平抖动。
|
||||
- 可在 `WiFi Connect` 配置中调大:
|
||||
- `WIFI_CONNECT_BUTTON_STARTUP_GUARD_MS`(建议 8000~10000)
|
||||
- `WIFI_CONNECT_BUTTON_RELEASE_ARM_MS`(建议 300~500)
|
||||
- 若硬件允许,优先给配网按键使用独立 GPIO,避免与屏幕复位脚复用。
|
||||
- 若使用常驻配网模式,可不依赖按键触发(适合按键与 LCD 复位脚复用场景)。
|
||||
3. 烧录并监视日志
|
||||
```bash
|
||||
idf.py -p /dev/ttyACM0 flash monitor
|
||||
```
|
||||
|
||||
## 当前状态
|
||||
## MQTT 协议
|
||||
|
||||
项目已完成第一版配网闭环:
|
||||
- 配网入口
|
||||
- 路由连接
|
||||
- 状态显示
|
||||
- 清除配置
|
||||
- 中文日志与文档
|
||||
### ESP32 -> WEX
|
||||
|
||||
1. 告警消息主题:`topic/alert/esp32_iothome_001`
|
||||
|
||||
```json
|
||||
{
|
||||
"metric": "light",
|
||||
"state": "alarm"
|
||||
}
|
||||
```
|
||||
|
||||
字段:
|
||||
- `metric`:`soil` 或 `light`
|
||||
- `state`:`normal` 或 `alarm`
|
||||
|
||||
2. 状态消息主题:`topic/sensor/esp32_BotanicalBuddy_001`
|
||||
|
||||
```json
|
||||
{
|
||||
"temp": "34.3",
|
||||
"hum": "30.5",
|
||||
"soil": "0",
|
||||
"lux": "40",
|
||||
"pump": "on",
|
||||
"light": "off",
|
||||
"mode": "auto",
|
||||
"soil_on": 35.0,
|
||||
"soil_off": 45.0,
|
||||
"light_on": 100.0,
|
||||
"light_off": 350.0
|
||||
}
|
||||
```
|
||||
|
||||
字段:
|
||||
- `pump`:`on/off`
|
||||
- `light`:`on/off`
|
||||
- `mode`:`auto/manual`
|
||||
- `soil_on`:土壤湿度低阈值(低于该值自动开泵)
|
||||
- `soil_off`:土壤湿度高阈值(高于该值自动关泵)
|
||||
- `light_on`:光照低阈值(低于该值自动开灯)
|
||||
- `light_off`:光照高阈值(高于该值自动关灯)
|
||||
|
||||
### WEX -> ESP32
|
||||
|
||||
控制主题:`topic/control/esp32_BotanicalBuddy_001`
|
||||
|
||||
1. 切换模式
|
||||
```json
|
||||
{ "mode": "manual" }
|
||||
```
|
||||
```json
|
||||
{ "mode": "auto" }
|
||||
```
|
||||
|
||||
2. 手动控制(建议先切到 `manual`)
|
||||
```json
|
||||
{ "pump": "on", "light": "off" }
|
||||
```
|
||||
|
||||
3. 更新自动阈值(四个字段需同时下发)
|
||||
```json
|
||||
{
|
||||
"soil_on": 35,
|
||||
"soil_off": 45,
|
||||
"light_on": 100,
|
||||
"light_off": 350
|
||||
}
|
||||
```
|
||||
|
||||
4. 混合下发(同一条消息可同时包含模式、阈值、手动开关)
|
||||
```json
|
||||
{
|
||||
"mode": "auto",
|
||||
"soil_on": 35,
|
||||
"soil_off": 45,
|
||||
"light_on": 100,
|
||||
"light_off": 350,
|
||||
"pump": "off",
|
||||
"light": "on"
|
||||
}
|
||||
```
|
||||
|
||||
兼容输入:
|
||||
- `pump/light` 支持 `on/off`、`true/false`、`1/0`
|
||||
- `mode` 支持 `auto/manual`,也兼容 `true/false`、`1/0`(`true/1=auto`)
|
||||
|
||||
## 联调建议
|
||||
|
||||
1. 先下发 `{"mode":"manual"}`,验证手动泵灯控制。
|
||||
2. 再下发阈值并切 `{"mode":"auto"}`,观察自动控制接管。
|
||||
3. 注意阈值含回差:
|
||||
- 土壤:`soil_on` 开泵,`soil_off` 关泵
|
||||
- 光照:`light_on` 开灯,`light_off` 关灯
|
||||
|
||||
## 说明
|
||||
|
||||
- 当前 README 聚焦单片机固件能力与联调协议。
|
||||
- App/小程序页面与云端业务可按本协议直接对接。
|
||||
|
||||
并完成 LCD 显示链路:
|
||||
- SPI 屏初始化
|
||||
- LVGL 显示注册
|
||||
- 方向/偏移可配置
|
||||
3
components/mqtt_control/CMakeLists.txt
Normal file
3
components/mqtt_control/CMakeLists.txt
Normal file
@@ -0,0 +1,3 @@
|
||||
idf_component_register(SRCS "mqtt_control.c"
|
||||
INCLUDE_DIRS "include"
|
||||
REQUIRES mqtt cjson)
|
||||
49
components/mqtt_control/include/mqtt_control.h
Normal file
49
components/mqtt_control/include/mqtt_control.h
Normal file
@@ -0,0 +1,49 @@
|
||||
#pragma once
|
||||
|
||||
#include <stdbool.h>
|
||||
|
||||
#include "esp_err.h"
|
||||
#include "mqtt_client.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
typedef struct {
|
||||
bool has_mode;
|
||||
bool auto_mode;
|
||||
|
||||
bool has_thresholds;
|
||||
float soil_on_pct;
|
||||
float soil_off_pct;
|
||||
float light_on_lux;
|
||||
float light_off_lux;
|
||||
|
||||
bool has_pump;
|
||||
bool pump_on;
|
||||
|
||||
bool has_light;
|
||||
bool light_on;
|
||||
} mqtt_control_command_t;
|
||||
|
||||
typedef esp_err_t (*mqtt_control_command_handler_t)(const mqtt_control_command_t *cmd, void *user_ctx);
|
||||
|
||||
esp_err_t mqtt_control_start(void);
|
||||
esp_err_t mqtt_control_stop(void);
|
||||
|
||||
esp_err_t mqtt_control_register_command_handler(mqtt_control_command_handler_t handler, void *user_ctx);
|
||||
|
||||
bool mqtt_control_is_connected(void);
|
||||
|
||||
// Generic publish API for any topic.
|
||||
esp_err_t mqtt_control_publish(const char *topic,
|
||||
const char *payload,
|
||||
int qos,
|
||||
int retain);
|
||||
|
||||
// Publish telemetry payload to default sensor topic.
|
||||
esp_err_t mqtt_control_publish_sensor(const char *payload, int qos, int retain);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
377
components/mqtt_control/mqtt_control.c
Normal file
377
components/mqtt_control/mqtt_control.c
Normal file
@@ -0,0 +1,377 @@
|
||||
#include <inttypes.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <strings.h>
|
||||
|
||||
#include "cJSON.h"
|
||||
#include "esp_check.h"
|
||||
#include "esp_log.h"
|
||||
#include "esp_mac.h"
|
||||
|
||||
#include "mqtt_control.h"
|
||||
|
||||
// MQTT 服务器地址(协议+域名+端口)
|
||||
#define MQTT_BROKER_URL "mqtt://beihong.wang:1883"
|
||||
// MQTT 用户名
|
||||
#define MQTT_USERNAME "BotanicalBuddy"
|
||||
// MQTT 密码
|
||||
#define MQTT_PASSWORD "YTGui8979HI"
|
||||
// 传感器数据发布主题
|
||||
#define MQTT_SENSOR_TOPIC "topic/sensor/esp32_BotanicalBuddy_001"
|
||||
// 控制指令订阅主题
|
||||
#define MQTT_CONTROL_TOPIC "topic/control/esp32_BotanicalBuddy_001"
|
||||
|
||||
|
||||
static const char *TAG = "mqtt_control"; // 日志标签
|
||||
|
||||
static esp_mqtt_client_handle_t g_mqtt_client = NULL; // 全局 MQTT 客户端句柄
|
||||
static bool g_mqtt_connected = false; // MQTT 连接状态标志
|
||||
static mqtt_control_command_handler_t g_cmd_handler = NULL;
|
||||
static void *g_cmd_user_ctx = NULL;
|
||||
|
||||
static bool json_read_bool(cJSON *root, const char *key, bool *out)
|
||||
{
|
||||
cJSON *item = cJSON_GetObjectItemCaseSensitive(root, key);
|
||||
if (item == NULL)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
if (cJSON_IsBool(item))
|
||||
{
|
||||
*out = cJSON_IsTrue(item);
|
||||
return true;
|
||||
}
|
||||
if (cJSON_IsNumber(item))
|
||||
{
|
||||
*out = (item->valuedouble != 0.0);
|
||||
return true;
|
||||
}
|
||||
if (cJSON_IsString(item) && item->valuestring != NULL)
|
||||
{
|
||||
if (strcasecmp(item->valuestring, "on") == 0 ||
|
||||
strcasecmp(item->valuestring, "true") == 0 ||
|
||||
strcmp(item->valuestring, "1") == 0)
|
||||
{
|
||||
*out = true;
|
||||
return true;
|
||||
}
|
||||
if (strcasecmp(item->valuestring, "off") == 0 ||
|
||||
strcasecmp(item->valuestring, "false") == 0 ||
|
||||
strcmp(item->valuestring, "0") == 0)
|
||||
{
|
||||
*out = false;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
static bool json_read_float(cJSON *root, const char *key, float *out)
|
||||
{
|
||||
cJSON *item = cJSON_GetObjectItemCaseSensitive(root, key);
|
||||
if (!cJSON_IsNumber(item))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
*out = (float)item->valuedouble;
|
||||
return true;
|
||||
}
|
||||
|
||||
static bool json_read_mode_auto(cJSON *root, const char *key, bool *out_auto)
|
||||
{
|
||||
cJSON *item = cJSON_GetObjectItemCaseSensitive(root, key);
|
||||
if (item == NULL)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
if (cJSON_IsString(item) && item->valuestring != NULL)
|
||||
{
|
||||
if (strcasecmp(item->valuestring, "auto") == 0)
|
||||
{
|
||||
*out_auto = true;
|
||||
return true;
|
||||
}
|
||||
if (strcasecmp(item->valuestring, "manual") == 0)
|
||||
{
|
||||
*out_auto = false;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
if (cJSON_IsBool(item))
|
||||
{
|
||||
*out_auto = cJSON_IsTrue(item);
|
||||
return true;
|
||||
}
|
||||
|
||||
if (cJSON_IsNumber(item))
|
||||
{
|
||||
*out_auto = (item->valuedouble != 0.0);
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
static esp_err_t mqtt_parse_control_command(const char *data, int data_len, mqtt_control_command_t *out_cmd)
|
||||
{
|
||||
ESP_RETURN_ON_FALSE(data != NULL && data_len > 0, ESP_ERR_INVALID_ARG, TAG, "invalid mqtt data");
|
||||
ESP_RETURN_ON_FALSE(out_cmd != NULL, ESP_ERR_INVALID_ARG, TAG, "out_cmd is null");
|
||||
|
||||
memset(out_cmd, 0, sizeof(*out_cmd));
|
||||
|
||||
cJSON *root = cJSON_ParseWithLength(data, (size_t)data_len);
|
||||
ESP_RETURN_ON_FALSE(root != NULL, ESP_ERR_INVALID_ARG, TAG, "control json parse failed");
|
||||
|
||||
float soil_on = 0.0f;
|
||||
float soil_off = 0.0f;
|
||||
float light_on_lux = 0.0f;
|
||||
float light_off_lux = 0.0f;
|
||||
|
||||
bool has_soil_on = json_read_float(root, "soil_on", &soil_on);
|
||||
bool has_soil_off = json_read_float(root, "soil_off", &soil_off);
|
||||
bool has_light_on = json_read_float(root, "light_on", &light_on_lux);
|
||||
bool has_light_off = json_read_float(root, "light_off", &light_off_lux);
|
||||
|
||||
out_cmd->has_mode = json_read_mode_auto(root, "mode", &out_cmd->auto_mode);
|
||||
|
||||
if (has_soil_on && has_soil_off && has_light_on && has_light_off)
|
||||
{
|
||||
out_cmd->has_thresholds = true;
|
||||
out_cmd->soil_on_pct = soil_on;
|
||||
out_cmd->soil_off_pct = soil_off;
|
||||
out_cmd->light_on_lux = light_on_lux;
|
||||
out_cmd->light_off_lux = light_off_lux;
|
||||
}
|
||||
|
||||
out_cmd->has_pump = json_read_bool(root, "pump", &out_cmd->pump_on);
|
||||
out_cmd->has_light = json_read_bool(root, "light", &out_cmd->light_on);
|
||||
|
||||
cJSON_Delete(root);
|
||||
|
||||
ESP_RETURN_ON_FALSE(out_cmd->has_mode || out_cmd->has_thresholds || out_cmd->has_pump || out_cmd->has_light,
|
||||
ESP_ERR_INVALID_ARG,
|
||||
TAG,
|
||||
"no valid control fields in payload");
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief 判断接收到的 MQTT 主题是否与预期主题匹配
|
||||
*
|
||||
* @param event_topic 事件中的主题字符串
|
||||
* @param event_topic_len 事件中主题的长度
|
||||
* @param expected 预期的主题字符串
|
||||
* @return true 匹配成功;false 匹配失败
|
||||
*/
|
||||
static bool mqtt_topic_match(const char *event_topic, int event_topic_len, const char *expected)
|
||||
{
|
||||
size_t expected_len = strlen(expected);
|
||||
return expected_len == (size_t)event_topic_len && strncmp(event_topic, expected, expected_len) == 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief MQTT 事件处理回调函数
|
||||
*
|
||||
* 处理连接、断开、订阅、数据接收等事件。
|
||||
*
|
||||
* @param handler_args 用户传入的参数(未使用)
|
||||
* @param base 事件基类型(ESP-MQTT)
|
||||
* @param event_id 具体事件 ID
|
||||
* @param event_data 事件数据指针
|
||||
*/
|
||||
static void mqtt_event_handler(void *handler_args, esp_event_base_t base, int32_t event_id, void *event_data)
|
||||
{
|
||||
(void)handler_args;
|
||||
ESP_LOGD(TAG, "event base=%s id=%" PRIi32, base, event_id);
|
||||
|
||||
esp_mqtt_event_handle_t event = (esp_mqtt_event_handle_t)event_data;
|
||||
esp_mqtt_client_handle_t client = event->client;
|
||||
|
||||
switch ((esp_mqtt_event_id_t)event_id)
|
||||
{
|
||||
case MQTT_EVENT_CONNECTED: {
|
||||
g_mqtt_connected = true;
|
||||
ESP_LOGI(TAG, "MQTT connected");
|
||||
// 连接成功后订阅控制主题
|
||||
int msg_id = esp_mqtt_client_subscribe(client, MQTT_CONTROL_TOPIC, 1);
|
||||
ESP_LOGI(TAG, "subscribe topic=%s msg_id=%d", MQTT_CONTROL_TOPIC, msg_id);
|
||||
break;
|
||||
}
|
||||
|
||||
case MQTT_EVENT_DISCONNECTED:
|
||||
g_mqtt_connected = false;
|
||||
ESP_LOGW(TAG, "MQTT disconnected");
|
||||
break;
|
||||
|
||||
case MQTT_EVENT_SUBSCRIBED:
|
||||
ESP_LOGI(TAG, "MQTT subscribed msg_id=%d", event->msg_id);
|
||||
break;
|
||||
|
||||
case MQTT_EVENT_DATA:
|
||||
ESP_LOGI(TAG, "MQTT data topic=%.*s data=%.*s",
|
||||
event->topic_len,
|
||||
event->topic,
|
||||
event->data_len,
|
||||
event->data);
|
||||
|
||||
// 如果是控制主题的数据,则解析控制命令(待实现)
|
||||
if (mqtt_topic_match(event->topic, event->topic_len, MQTT_CONTROL_TOPIC))
|
||||
{
|
||||
mqtt_control_command_t cmd = {0};
|
||||
esp_err_t parse_ret = mqtt_parse_control_command(event->data, event->data_len, &cmd);
|
||||
if (parse_ret != ESP_OK)
|
||||
{
|
||||
ESP_LOGW(TAG, "控制命令解析失败: %s", esp_err_to_name(parse_ret));
|
||||
break;
|
||||
}
|
||||
|
||||
if (g_cmd_handler != NULL)
|
||||
{
|
||||
esp_err_t handle_ret = g_cmd_handler(&cmd, g_cmd_user_ctx);
|
||||
if (handle_ret != ESP_OK)
|
||||
{
|
||||
ESP_LOGW(TAG, "控制命令处理失败: %s", esp_err_to_name(handle_ret));
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
ESP_LOGW(TAG, "未注册控制命令处理器,忽略控制消息");
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
||||
case MQTT_EVENT_ERROR:
|
||||
ESP_LOGE(TAG, "MQTT error type=%d", event->error_handle ? event->error_handle->error_type : -1);
|
||||
break;
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief 启动 MQTT 客户端
|
||||
*
|
||||
* 初始化客户端、注册事件回调、启动连接。
|
||||
*
|
||||
* @return esp_err_t 启动结果,ESP_OK 表示成功
|
||||
*/
|
||||
esp_err_t mqtt_control_start(void)
|
||||
{
|
||||
if (g_mqtt_client != NULL)
|
||||
{
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
// 生成基于 MAC 地址后三字节的唯一客户端 ID
|
||||
char client_id[32] = {0};
|
||||
uint8_t mac[6] = {0};
|
||||
ESP_RETURN_ON_ERROR(esp_read_mac(mac, ESP_MAC_WIFI_STA), TAG, "read mac failed");
|
||||
snprintf(client_id, sizeof(client_id), "esp32_%02x%02x%02x", mac[3], mac[4], mac[5]);
|
||||
|
||||
// 配置 MQTT 客户端参数
|
||||
esp_mqtt_client_config_t mqtt_cfg = {
|
||||
.broker.address.uri = MQTT_BROKER_URL,
|
||||
.credentials.username = MQTT_USERNAME,
|
||||
.credentials.client_id = client_id,
|
||||
.credentials.authentication.password = MQTT_PASSWORD,
|
||||
};
|
||||
|
||||
g_mqtt_client = esp_mqtt_client_init(&mqtt_cfg);
|
||||
ESP_RETURN_ON_FALSE(g_mqtt_client != NULL, ESP_FAIL, TAG, "mqtt client init failed");
|
||||
|
||||
ESP_RETURN_ON_ERROR(esp_mqtt_client_register_event(g_mqtt_client,
|
||||
ESP_EVENT_ANY_ID,
|
||||
mqtt_event_handler,
|
||||
NULL),
|
||||
TAG,
|
||||
"register event failed");
|
||||
|
||||
ESP_RETURN_ON_ERROR(esp_mqtt_client_start(g_mqtt_client), TAG, "start mqtt client failed");
|
||||
ESP_LOGI(TAG, "MQTT started with client_id=%s", client_id);
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
esp_err_t mqtt_control_register_command_handler(mqtt_control_command_handler_t handler, void *user_ctx)
|
||||
{
|
||||
g_cmd_handler = handler;
|
||||
g_cmd_user_ctx = user_ctx;
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief 停止并销毁 MQTT 客户端
|
||||
*
|
||||
* @return esp_err_t 停止结果,ESP_OK 表示成功
|
||||
*/
|
||||
esp_err_t mqtt_control_stop(void)
|
||||
{
|
||||
if (g_mqtt_client == NULL)
|
||||
{
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
esp_err_t ret = esp_mqtt_client_stop(g_mqtt_client);
|
||||
if (ret != ESP_OK)
|
||||
{
|
||||
return ret;
|
||||
}
|
||||
|
||||
ret = esp_mqtt_client_destroy(g_mqtt_client);
|
||||
if (ret != ESP_OK)
|
||||
{
|
||||
return ret;
|
||||
}
|
||||
|
||||
g_mqtt_client = NULL;
|
||||
g_mqtt_connected = false;
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief 查询 MQTT 当前连接状态
|
||||
*
|
||||
* @return true 已连接;false 未连接
|
||||
*/
|
||||
bool mqtt_control_is_connected(void)
|
||||
{
|
||||
return g_mqtt_connected;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief 发布 MQTT 消息到指定主题
|
||||
*
|
||||
* @param topic 目标主题
|
||||
* @param payload 消息载荷
|
||||
* @param qos 服务质量等级(0,1,2)
|
||||
* @param retain 是否保留消息
|
||||
* @return esp_err_t 发布结果
|
||||
*/
|
||||
esp_err_t mqtt_control_publish(const char *topic,
|
||||
const char *payload,
|
||||
int qos,
|
||||
int retain)
|
||||
{
|
||||
ESP_RETURN_ON_FALSE(topic != NULL, ESP_ERR_INVALID_ARG, TAG, "topic is null");
|
||||
ESP_RETURN_ON_FALSE(payload != NULL, ESP_ERR_INVALID_ARG, TAG, "payload is null");
|
||||
ESP_RETURN_ON_FALSE(g_mqtt_client != NULL, ESP_ERR_INVALID_STATE, TAG, "mqtt not started");
|
||||
|
||||
int msg_id = esp_mqtt_client_publish(g_mqtt_client, topic, payload, 0, qos, retain);
|
||||
ESP_RETURN_ON_FALSE(msg_id >= 0, ESP_FAIL, TAG, "publish failed");
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief 发布传感器数据到预定义的传感器主题
|
||||
*
|
||||
* @param payload 传感器数据字符串
|
||||
* @param qos 服务质量
|
||||
* @param retain 是否保留消息
|
||||
* @return esp_err_t 发布结果
|
||||
*/
|
||||
esp_err_t mqtt_control_publish_sensor(const char *payload, int qos, int retain)
|
||||
{
|
||||
return mqtt_control_publish(MQTT_SENSOR_TOPIC, payload, qos, retain);
|
||||
}
|
||||
@@ -9,6 +9,16 @@ dependencies:
|
||||
registry_url: https://components.espressif.com/
|
||||
type: service
|
||||
version: 2.0.0
|
||||
espressif/cjson:
|
||||
component_hash: 002c6d1872ee4c97d333938ebe107a29841cc847f9de89e676714bd2844057ea
|
||||
dependencies:
|
||||
- name: idf
|
||||
require: private
|
||||
version: '>=5.0'
|
||||
source:
|
||||
registry_url: https://components.espressif.com/
|
||||
type: service
|
||||
version: 1.7.19~1
|
||||
espressif/console_simple_init:
|
||||
component_hash: b488b12318f3cb6e0b55b034bd12956926d45f0e1396442e820f8ece4776c306
|
||||
dependencies:
|
||||
@@ -33,6 +43,16 @@ dependencies:
|
||||
registry_url: https://components.espressif.com/
|
||||
type: service
|
||||
version: 2.7.2
|
||||
espressif/mqtt:
|
||||
component_hash: ffdad5659706b4dc14bc63f8eb73ef765efa015bf7e9adf71c813d52a2dc9342
|
||||
dependencies:
|
||||
- name: idf
|
||||
require: private
|
||||
version: '>=5.3'
|
||||
source:
|
||||
registry_url: https://components.espressif.com/
|
||||
type: service
|
||||
version: 1.0.0
|
||||
idf:
|
||||
source:
|
||||
type: idf
|
||||
@@ -70,10 +90,12 @@ dependencies:
|
||||
version: 9.5.0
|
||||
direct_dependencies:
|
||||
- espressif/bh1750
|
||||
- espressif/cjson
|
||||
- espressif/console_simple_init
|
||||
- espressif/esp_lvgl_port
|
||||
- espressif/mqtt
|
||||
- idf
|
||||
- k0i05/esp_ahtxx
|
||||
manifest_hash: 876b8b787041413cd7d3f71227f1618dceac35f343e17a5874d56c77837d0705
|
||||
manifest_hash: 718977b7c70d2e199530b4f98a537ecc03c07999f59c844987823a832f51b9b0
|
||||
target: esp32c3
|
||||
version: 2.0.0
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
idf_component_register(SRCS "main.c"
|
||||
idf_component_register(SRCS "main.c" "auto_ctrl_thresholds.c" "auto_alerts.c" "status_web.c"
|
||||
INCLUDE_DIRS "."
|
||||
REQUIRES wifi-connect esp_lvgl_port lvgl_st7735s_use i2c_master_messager io_device_control console_simple_init console console_user_cmds capactive_soil_moisture_sensor_V2.0 ui
|
||||
REQUIRES wifi-connect mqtt_control esp_lvgl_port lvgl_st7735s_use i2c_master_messager io_device_control console_simple_init console console_user_cmds capactive_soil_moisture_sensor_V2.0 ui esp_app_format
|
||||
)
|
||||
|
||||
188
main/auto_alerts.c
Normal file
188
main/auto_alerts.c
Normal file
@@ -0,0 +1,188 @@
|
||||
#include "auto_alerts.h"
|
||||
|
||||
#include "esp_check.h"
|
||||
#include "esp_log.h"
|
||||
#include "esp_timer.h"
|
||||
#include "freertos/FreeRTOS.h"
|
||||
|
||||
static const char *TAG = "auto_alerts"; // 日志标签
|
||||
|
||||
// 用于保护全局状态的自旋锁(临界区)
|
||||
static portMUX_TYPE s_alerts_lock = portMUX_INITIALIZER_UNLOCKED;
|
||||
// 用户注册的回调函数
|
||||
static auto_alert_callback_t s_callback = NULL;
|
||||
// 回调函数的用户上下文指针
|
||||
static void *s_user_ctx = NULL;
|
||||
|
||||
// 土壤湿度告警是否已激活
|
||||
static bool s_soil_alarm_active = false;
|
||||
// 光照强度告警是否已激活
|
||||
static bool s_light_alarm_active = false;
|
||||
|
||||
/**
|
||||
* @brief 发送自动告警事件
|
||||
*
|
||||
* @param metric 告警指标类型(如土壤湿度、光照强度)
|
||||
* @param state 告警状态(告警或恢复正常)
|
||||
* @param value 当前测量值
|
||||
* @param threshold 触发告警的阈值
|
||||
*/
|
||||
static void auto_alerts_emit(auto_alert_metric_t metric,
|
||||
auto_alert_state_t state,
|
||||
float value,
|
||||
float threshold)
|
||||
{
|
||||
auto_alert_event_t event = {
|
||||
.metric = metric,
|
||||
.state = state,
|
||||
.value = value,
|
||||
.threshold = threshold,
|
||||
.timestamp_ms = esp_timer_get_time() / 1000, // 转换为毫秒时间戳
|
||||
};
|
||||
|
||||
auto_alert_callback_t callback = NULL;
|
||||
void *user_ctx = NULL;
|
||||
|
||||
// 进入临界区,安全读取回调和上下文
|
||||
taskENTER_CRITICAL(&s_alerts_lock);
|
||||
callback = s_callback;
|
||||
user_ctx = s_user_ctx;
|
||||
taskEXIT_CRITICAL(&s_alerts_lock);
|
||||
|
||||
if (callback != NULL)
|
||||
{
|
||||
callback(&event, user_ctx); // 调用用户注册的回调函数
|
||||
}
|
||||
|
||||
// 打印日志信息
|
||||
ESP_LOGI(TAG,
|
||||
"alert metric=%d state=%d value=%.1f threshold=%.1f",
|
||||
(int)event.metric,
|
||||
(int)event.state,
|
||||
event.value,
|
||||
event.threshold);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief 初始化自动告警模块
|
||||
*
|
||||
* 将所有告警状态重置为未激活。
|
||||
*/
|
||||
void auto_alerts_init(void)
|
||||
{
|
||||
taskENTER_CRITICAL(&s_alerts_lock);
|
||||
s_soil_alarm_active = false;
|
||||
s_light_alarm_active = false;
|
||||
taskEXIT_CRITICAL(&s_alerts_lock);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief 注册自动告警回调函数
|
||||
*
|
||||
* @param callback 用户定义的回调函数
|
||||
* @param user_ctx 用户上下文指针
|
||||
* @return esp_err_t 总是返回 ESP_OK
|
||||
*/
|
||||
esp_err_t auto_alerts_register_callback(auto_alert_callback_t callback, void *user_ctx)
|
||||
{
|
||||
taskENTER_CRITICAL(&s_alerts_lock);
|
||||
s_callback = callback;
|
||||
s_user_ctx = user_ctx;
|
||||
taskEXIT_CRITICAL(&s_alerts_lock);
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief 根据当前传感器数据和阈值评估是否触发或解除告警
|
||||
*
|
||||
* @param soil_valid 土壤湿度数据是否有效
|
||||
* @param soil_moisture_pct 当前土壤湿度百分比
|
||||
* @param light_valid 光照数据是否有效
|
||||
* @param light_lux 当前光照强度(单位:lux)
|
||||
* @param thresholds 自动控制阈值配置结构体指针
|
||||
*/
|
||||
void auto_alerts_evaluate(bool soil_valid,
|
||||
float soil_moisture_pct,
|
||||
bool light_valid,
|
||||
float light_lux,
|
||||
const auto_ctrl_thresholds_t *thresholds)
|
||||
{
|
||||
if (thresholds == NULL)
|
||||
{
|
||||
return; // 阈值为空,直接返回
|
||||
}
|
||||
|
||||
// 处理土壤湿度告警逻辑
|
||||
if (soil_valid)
|
||||
{
|
||||
bool emit_alarm = false; // 是否需要触发告警
|
||||
bool emit_recover = false; // 是否需要恢复通知
|
||||
|
||||
taskENTER_CRITICAL(&s_alerts_lock);
|
||||
// 如果当前未告警,且土壤湿度低于启动水泵的阈值,则触发告警
|
||||
if (!s_soil_alarm_active && soil_moisture_pct < thresholds->pump_on_soil_below_pct)
|
||||
{
|
||||
s_soil_alarm_active = true;
|
||||
emit_alarm = true;
|
||||
}
|
||||
// 如果当前处于告警状态,且土壤湿度高于关闭水泵的阈值,则恢复
|
||||
else if (s_soil_alarm_active && soil_moisture_pct > thresholds->pump_off_soil_above_pct)
|
||||
{
|
||||
s_soil_alarm_active = false;
|
||||
emit_recover = true;
|
||||
}
|
||||
taskEXIT_CRITICAL(&s_alerts_lock);
|
||||
|
||||
if (emit_alarm)
|
||||
{
|
||||
auto_alerts_emit(AUTO_ALERT_METRIC_SOIL_MOISTURE,
|
||||
AUTO_ALERT_STATE_ALARM,
|
||||
soil_moisture_pct,
|
||||
thresholds->pump_on_soil_below_pct);
|
||||
}
|
||||
if (emit_recover)
|
||||
{
|
||||
auto_alerts_emit(AUTO_ALERT_METRIC_SOIL_MOISTURE,
|
||||
AUTO_ALERT_STATE_NORMAL,
|
||||
soil_moisture_pct,
|
||||
thresholds->pump_off_soil_above_pct);
|
||||
}
|
||||
}
|
||||
|
||||
// 处理光照强度告警逻辑
|
||||
if (light_valid)
|
||||
{
|
||||
bool emit_alarm = false; // 是否需要触发告警
|
||||
bool emit_recover = false; // 是否需要恢复通知
|
||||
|
||||
taskENTER_CRITICAL(&s_alerts_lock);
|
||||
// 如果当前未告警,且光照强度低于开启补光灯的阈值,则触发告警
|
||||
if (!s_light_alarm_active && light_lux < thresholds->light_on_lux_below)
|
||||
{
|
||||
s_light_alarm_active = true;
|
||||
emit_alarm = true;
|
||||
}
|
||||
// 如果当前处于告警状态,且光照强度高于关闭补光灯的阈值,则恢复
|
||||
else if (s_light_alarm_active && light_lux > thresholds->light_off_lux_above)
|
||||
{
|
||||
s_light_alarm_active = false;
|
||||
emit_recover = true;
|
||||
}
|
||||
taskEXIT_CRITICAL(&s_alerts_lock);
|
||||
|
||||
if (emit_alarm)
|
||||
{
|
||||
auto_alerts_emit(AUTO_ALERT_METRIC_LIGHT_INTENSITY,
|
||||
AUTO_ALERT_STATE_ALARM,
|
||||
light_lux,
|
||||
thresholds->light_on_lux_below);
|
||||
}
|
||||
if (emit_recover)
|
||||
{
|
||||
auto_alerts_emit(AUTO_ALERT_METRIC_LIGHT_INTENSITY,
|
||||
AUTO_ALERT_STATE_NORMAL,
|
||||
light_lux,
|
||||
thresholds->light_off_lux_above);
|
||||
}
|
||||
}
|
||||
}
|
||||
48
main/auto_alerts.h
Normal file
48
main/auto_alerts.h
Normal file
@@ -0,0 +1,48 @@
|
||||
#pragma once
|
||||
|
||||
#include <stdbool.h>
|
||||
#include <stdint.h>
|
||||
|
||||
#include "auto_ctrl_thresholds.h"
|
||||
#include "esp_err.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
typedef enum {
|
||||
AUTO_ALERT_METRIC_SOIL_MOISTURE = 1,
|
||||
AUTO_ALERT_METRIC_LIGHT_INTENSITY = 2,
|
||||
} auto_alert_metric_t;
|
||||
|
||||
typedef enum {
|
||||
AUTO_ALERT_STATE_NORMAL = 0,
|
||||
AUTO_ALERT_STATE_ALARM = 1,
|
||||
} auto_alert_state_t;
|
||||
|
||||
typedef struct {
|
||||
auto_alert_metric_t metric;
|
||||
auto_alert_state_t state;
|
||||
float value;
|
||||
float threshold;
|
||||
int64_t timestamp_ms;
|
||||
} auto_alert_event_t;
|
||||
|
||||
typedef void (*auto_alert_callback_t)(const auto_alert_event_t *event, void *user_ctx);
|
||||
|
||||
// Reset internal state at boot.
|
||||
void auto_alerts_init(void);
|
||||
|
||||
// Register callback sink (e.g. MQTT publisher). Passing NULL clears callback.
|
||||
esp_err_t auto_alerts_register_callback(auto_alert_callback_t callback, void *user_ctx);
|
||||
|
||||
// Evaluate current sensor values and emit edge-triggered alert events.
|
||||
void auto_alerts_evaluate(bool soil_valid,
|
||||
float soil_moisture_pct,
|
||||
bool light_valid,
|
||||
float light_lux,
|
||||
const auto_ctrl_thresholds_t *thresholds);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
146
main/auto_ctrl_thresholds.c
Normal file
146
main/auto_ctrl_thresholds.c
Normal file
@@ -0,0 +1,146 @@
|
||||
#include "auto_ctrl_thresholds.h"
|
||||
|
||||
#include "freertos/FreeRTOS.h"
|
||||
#include "esp_check.h"
|
||||
|
||||
// 默认土壤湿度低于此百分比时启动水泵(单位:%)
|
||||
#define DEFAULT_PUMP_ON_SOIL_BELOW_PCT 35.0f
|
||||
// 默认土壤湿度高于此百分比时关闭水泵(单位:%)
|
||||
#define DEFAULT_PUMP_OFF_SOIL_ABOVE_PCT 45.0f
|
||||
// 默认光照强度低于此值时开启补光灯(单位:lux)
|
||||
#define DEFAULT_LIGHT_ON_LUX_BELOW 200.0f
|
||||
// 默认光照强度高于此值时关闭补光灯(单位:lux)
|
||||
#define DEFAULT_LIGHT_OFF_LUX_ABOVE 350.0f
|
||||
|
||||
// 用于保护阈值数据的自旋锁(临界区)
|
||||
static portMUX_TYPE s_thresholds_lock = portMUX_INITIALIZER_UNLOCKED;
|
||||
|
||||
// 全局阈值配置结构体,初始化为默认值
|
||||
static auto_ctrl_thresholds_t s_thresholds = {
|
||||
.pump_on_soil_below_pct = DEFAULT_PUMP_ON_SOIL_BELOW_PCT,
|
||||
.pump_off_soil_above_pct = DEFAULT_PUMP_OFF_SOIL_ABOVE_PCT,
|
||||
.light_on_lux_below = DEFAULT_LIGHT_ON_LUX_BELOW,
|
||||
.light_off_lux_above = DEFAULT_LIGHT_OFF_LUX_ABOVE,
|
||||
};
|
||||
|
||||
/**
|
||||
* @brief 验证自动控制阈值配置的有效性
|
||||
*
|
||||
* 检查指针非空、数值范围合法、启停阈值满足 on < off 等条件。
|
||||
*
|
||||
* @param cfg 待验证的阈值配置指针
|
||||
* @return esp_err_t 验证结果,ESP_OK 表示有效
|
||||
*/
|
||||
static esp_err_t auto_ctrl_thresholds_validate(const auto_ctrl_thresholds_t *cfg)
|
||||
{
|
||||
ESP_RETURN_ON_FALSE(cfg != NULL, ESP_ERR_INVALID_ARG, "auto_ctrl_thresholds", "cfg is null");
|
||||
|
||||
ESP_RETURN_ON_FALSE(cfg->pump_on_soil_below_pct >= 0.0f && cfg->pump_on_soil_below_pct <= 100.0f,
|
||||
ESP_ERR_INVALID_ARG,
|
||||
"auto_ctrl_thresholds",
|
||||
"pump_on_soil_below_pct out of range");
|
||||
ESP_RETURN_ON_FALSE(cfg->pump_off_soil_above_pct >= 0.0f && cfg->pump_off_soil_above_pct <= 100.0f,
|
||||
ESP_ERR_INVALID_ARG,
|
||||
"auto_ctrl_thresholds",
|
||||
"pump_off_soil_above_pct out of range");
|
||||
ESP_RETURN_ON_FALSE(cfg->pump_on_soil_below_pct < cfg->pump_off_soil_above_pct,
|
||||
ESP_ERR_INVALID_ARG,
|
||||
"auto_ctrl_thresholds",
|
||||
"pump thresholds must satisfy on < off");
|
||||
|
||||
ESP_RETURN_ON_FALSE(cfg->light_on_lux_below >= 0.0f,
|
||||
ESP_ERR_INVALID_ARG,
|
||||
"auto_ctrl_thresholds",
|
||||
"light_on_lux_below out of range");
|
||||
ESP_RETURN_ON_FALSE(cfg->light_off_lux_above >= 0.0f,
|
||||
ESP_ERR_INVALID_ARG,
|
||||
"auto_ctrl_thresholds",
|
||||
"light_off_lux_above out of range");
|
||||
ESP_RETURN_ON_FALSE(cfg->light_on_lux_below < cfg->light_off_lux_above,
|
||||
ESP_ERR_INVALID_ARG,
|
||||
"auto_ctrl_thresholds",
|
||||
"light thresholds must satisfy on < off");
|
||||
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief 初始化阈值为默认值
|
||||
*
|
||||
* 将全局阈值结构体重置为预设的默认配置。
|
||||
*/
|
||||
void auto_ctrl_thresholds_init_defaults(void)
|
||||
{
|
||||
const auto_ctrl_thresholds_t defaults = {
|
||||
.pump_on_soil_below_pct = DEFAULT_PUMP_ON_SOIL_BELOW_PCT,
|
||||
.pump_off_soil_above_pct = DEFAULT_PUMP_OFF_SOIL_ABOVE_PCT,
|
||||
.light_on_lux_below = DEFAULT_LIGHT_ON_LUX_BELOW,
|
||||
.light_off_lux_above = DEFAULT_LIGHT_OFF_LUX_ABOVE,
|
||||
};
|
||||
|
||||
taskENTER_CRITICAL(&s_thresholds_lock);
|
||||
s_thresholds = defaults;
|
||||
taskEXIT_CRITICAL(&s_thresholds_lock);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief 获取当前阈值配置
|
||||
*
|
||||
* 安全地复制当前阈值到输出参数中。
|
||||
*
|
||||
* @param out 输出参数,指向接收阈值的结构体
|
||||
*/
|
||||
void auto_ctrl_thresholds_get(auto_ctrl_thresholds_t *out)
|
||||
{
|
||||
if (out == NULL) {
|
||||
return;
|
||||
}
|
||||
|
||||
taskENTER_CRITICAL(&s_thresholds_lock);
|
||||
*out = s_thresholds;
|
||||
taskEXIT_CRITICAL(&s_thresholds_lock);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief 设置新的阈值配置
|
||||
*
|
||||
* 验证输入配置有效性后,安全更新全局阈值。
|
||||
*
|
||||
* @param cfg 新的阈值配置指针
|
||||
* @return esp_err_t 设置结果,ESP_OK 表示成功
|
||||
*/
|
||||
esp_err_t auto_ctrl_thresholds_set(const auto_ctrl_thresholds_t *cfg)
|
||||
{
|
||||
ESP_RETURN_ON_ERROR(auto_ctrl_thresholds_validate(cfg), "auto_ctrl_thresholds", "invalid thresholds");
|
||||
|
||||
taskENTER_CRITICAL(&s_thresholds_lock);
|
||||
s_thresholds = *cfg;
|
||||
taskEXIT_CRITICAL(&s_thresholds_lock);
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief 通过独立参数设置阈值
|
||||
*
|
||||
* 提供一种更便捷的阈值设置方式,内部封装为结构体后调用 set 接口。
|
||||
*
|
||||
* @param pump_on_soil_below_pct 水泵启动土壤湿度阈值(%)
|
||||
* @param pump_off_soil_above_pct 水泵关闭土壤湿度阈值(%)
|
||||
* @param light_on_lux_below 补光灯开启光照阈值(lux)
|
||||
* @param light_off_lux_above 补光灯关闭光照阈值(lux)
|
||||
* @return esp_err_t 设置结果
|
||||
*/
|
||||
esp_err_t auto_ctrl_thresholds_set_values(float pump_on_soil_below_pct,
|
||||
float pump_off_soil_above_pct,
|
||||
float light_on_lux_below,
|
||||
float light_off_lux_above)
|
||||
{
|
||||
const auto_ctrl_thresholds_t cfg = {
|
||||
.pump_on_soil_below_pct = pump_on_soil_below_pct,
|
||||
.pump_off_soil_above_pct = pump_off_soil_above_pct,
|
||||
.light_on_lux_below = light_on_lux_below,
|
||||
.light_off_lux_above = light_off_lux_above,
|
||||
};
|
||||
|
||||
return auto_ctrl_thresholds_set(&cfg);
|
||||
}
|
||||
33
main/auto_ctrl_thresholds.h
Normal file
33
main/auto_ctrl_thresholds.h
Normal file
@@ -0,0 +1,33 @@
|
||||
#pragma once
|
||||
|
||||
#include "esp_err.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
typedef struct {
|
||||
float pump_on_soil_below_pct;
|
||||
float pump_off_soil_above_pct;
|
||||
float light_on_lux_below;
|
||||
float light_off_lux_above;
|
||||
} auto_ctrl_thresholds_t;
|
||||
|
||||
// Initializes default thresholds once at boot.
|
||||
void auto_ctrl_thresholds_init_defaults(void);
|
||||
|
||||
// Thread-safe snapshot read, intended for control loop usage.
|
||||
void auto_ctrl_thresholds_get(auto_ctrl_thresholds_t *out);
|
||||
|
||||
// Thread-safe full update with range/order validation.
|
||||
esp_err_t auto_ctrl_thresholds_set(const auto_ctrl_thresholds_t *cfg);
|
||||
|
||||
// Convenience API for MQTT callback usage.
|
||||
esp_err_t auto_ctrl_thresholds_set_values(float pump_on_soil_below_pct,
|
||||
float pump_off_soil_above_pct,
|
||||
float light_on_lux_below,
|
||||
float light_off_lux_above);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
@@ -18,3 +18,6 @@ dependencies:
|
||||
espressif/bh1750: ^2.0.0
|
||||
k0i05/esp_ahtxx: ^1.2.7
|
||||
espressif/console_simple_init: ^1.1.0
|
||||
|
||||
espressif/mqtt: ^1.0.0
|
||||
espressif/cjson: ^1.7.19
|
||||
|
||||
447
main/main.c
447
main/main.c
@@ -15,43 +15,410 @@
|
||||
#include "ui.h" // 使用EEZStudio提供的ui组件,便于后续扩展
|
||||
#include "esp_lvgl_port.h"
|
||||
#include "vars.h" // 定义全局变量接口
|
||||
#include "auto_ctrl_thresholds.h"
|
||||
#include "auto_alerts.h"
|
||||
#include "mqtt_control.h" // MQTT 控制接口
|
||||
#include "status_web.h"
|
||||
|
||||
// 配置宏定义:BH1750 光照传感器是否启用(默认禁用)
|
||||
#ifndef CONFIG_I2C_MASTER_MESSAGER_BH1750_ENABLE
|
||||
#define CONFIG_I2C_MASTER_MESSAGER_BH1750_ENABLE 0
|
||||
#endif
|
||||
|
||||
// 配置宏定义:AHT30 温湿度传感器是否启用(默认禁用)
|
||||
#ifndef CONFIG_I2C_MASTER_MESSAGER_AHT30_ENABLE
|
||||
#define CONFIG_I2C_MASTER_MESSAGER_AHT30_ENABLE 0
|
||||
#endif
|
||||
|
||||
// 配置宏定义:BH1750 读取周期(毫秒,默认500ms)
|
||||
#ifndef CONFIG_I2C_MASTER_MESSAGER_BH1750_READ_PERIOD_MS
|
||||
#define CONFIG_I2C_MASTER_MESSAGER_BH1750_READ_PERIOD_MS 500
|
||||
#endif
|
||||
|
||||
// 配置宏定义:AHT30 读取周期(毫秒,默认2000ms)
|
||||
#ifndef CONFIG_I2C_MASTER_MESSAGER_AHT30_READ_PERIOD_MS
|
||||
#define CONFIG_I2C_MASTER_MESSAGER_AHT30_READ_PERIOD_MS 2000
|
||||
#endif
|
||||
|
||||
// 配置宏定义:I2C 是否启用内部上拉电阻(默认启用)
|
||||
#ifndef CONFIG_I2C_MASTER_MESSAGER_ENABLE_INTERNAL_PULLUP
|
||||
#define CONFIG_I2C_MASTER_MESSAGER_ENABLE_INTERNAL_PULLUP 1
|
||||
#endif
|
||||
|
||||
// I2C 端口配置
|
||||
#define BOTANY_I2C_PORT I2C_NUM_0
|
||||
// I2C SCL 引脚
|
||||
#define BOTANY_I2C_SCL_GPIO GPIO_NUM_5
|
||||
// I2C SDA 引脚
|
||||
#define BOTANY_I2C_SDA_GPIO GPIO_NUM_4
|
||||
// BH1750 使能标志
|
||||
#define BOTANY_BH1750_ENABLE CONFIG_I2C_MASTER_MESSAGER_BH1750_ENABLE
|
||||
// AHT30 使能标志
|
||||
#define BOTANY_AHT30_ENABLE CONFIG_I2C_MASTER_MESSAGER_AHT30_ENABLE
|
||||
// BH1750 读取周期
|
||||
#define BOTANY_BH1750_PERIOD_MS CONFIG_I2C_MASTER_MESSAGER_BH1750_READ_PERIOD_MS
|
||||
// AHT30 读取周期
|
||||
#define BOTANY_AHT30_PERIOD_MS CONFIG_I2C_MASTER_MESSAGER_AHT30_READ_PERIOD_MS
|
||||
// I2C 内部上拉使能
|
||||
#define BOTANY_I2C_INTERNAL_PULLUP CONFIG_I2C_MASTER_MESSAGER_ENABLE_INTERNAL_PULLUP
|
||||
// MQTT 告警主题
|
||||
#define BOTANY_MQTT_ALERT_TOPIC "topic/alert/esp32_iothome_001"
|
||||
// MQTT 遥测数据上报周期(毫秒)
|
||||
#define BOTANY_MQTT_TELEMETRY_PERIOD_MS 5000
|
||||
#define BOTANY_STATUS_WEB_PORT 8080
|
||||
|
||||
// 日志标签
|
||||
static const char *TAG = "main";
|
||||
|
||||
// 全局变量:存储空气温度字符串
|
||||
static char s_air_temp[16];
|
||||
// 全局变量:存储空气湿度字符串
|
||||
static char s_air_hum[16];
|
||||
// 全局变量:存储土壤湿度字符串
|
||||
static char s_soil[16];
|
||||
// 全局变量:存储光照强度字符串
|
||||
static char s_lux[16];
|
||||
// 全局变量:水泵状态(true=开启,false=关闭)
|
||||
static bool s_pump_on = false;
|
||||
// 全局变量:补光灯状态(true=开启,false=关闭)
|
||||
static bool s_light_on = false;
|
||||
// 全局变量:自动控制模式使能(true=自动,false=手动)
|
||||
static bool s_auto_control_enabled = true;
|
||||
static bool s_i2c_ready = false;
|
||||
static bool s_soil_sensor_ready = false;
|
||||
static uint32_t s_main_loop_counter = 0;
|
||||
|
||||
/**
|
||||
* @brief 发布当前完整状态快照(含阈值)到传感器主题
|
||||
*/
|
||||
static esp_err_t publish_telemetry_snapshot(void)
|
||||
{
|
||||
if (!mqtt_control_is_connected())
|
||||
{
|
||||
return ESP_ERR_INVALID_STATE;
|
||||
}
|
||||
|
||||
auto_ctrl_thresholds_t thresholds = {0};
|
||||
auto_ctrl_thresholds_get(&thresholds);
|
||||
|
||||
char telemetry_payload[256] = {0};
|
||||
int len = snprintf(telemetry_payload,
|
||||
sizeof(telemetry_payload),
|
||||
"{\"temp\":\"%s\",\"hum\":\"%s\",\"soil\":\"%s\",\"lux\":\"%s\",\"pump\":\"%s\",\"light\":\"%s\",\"mode\":\"%s\",\"soil_on\":%.1f,\"soil_off\":%.1f,\"light_on\":%.1f,\"light_off\":%.1f}",
|
||||
s_air_temp,
|
||||
s_air_hum,
|
||||
s_soil,
|
||||
s_lux,
|
||||
s_pump_on ? "on" : "off",
|
||||
s_light_on ? "on" : "off",
|
||||
s_auto_control_enabled ? "auto" : "manual",
|
||||
thresholds.pump_on_soil_below_pct,
|
||||
thresholds.pump_off_soil_above_pct,
|
||||
thresholds.light_on_lux_below,
|
||||
thresholds.light_off_lux_above);
|
||||
if (len <= 0 || len >= (int)sizeof(telemetry_payload))
|
||||
{
|
||||
return ESP_ERR_INVALID_SIZE;
|
||||
}
|
||||
|
||||
return mqtt_control_publish_sensor(telemetry_payload, 0, 0);
|
||||
}
|
||||
|
||||
static void update_status_web_snapshot(void)
|
||||
{
|
||||
status_web_snapshot_t snap = {0};
|
||||
snprintf(snap.temp, sizeof(snap.temp), "%s", s_air_temp[0] ? s_air_temp : "--");
|
||||
snprintf(snap.hum, sizeof(snap.hum), "%s", s_air_hum[0] ? s_air_hum : "--");
|
||||
snprintf(snap.soil, sizeof(snap.soil), "%s", s_soil[0] ? s_soil : "--");
|
||||
snprintf(snap.lux, sizeof(snap.lux), "%s", s_lux[0] ? s_lux : "--");
|
||||
snap.pump_on = s_pump_on;
|
||||
snap.light_on = s_light_on;
|
||||
snap.auto_mode = s_auto_control_enabled;
|
||||
|
||||
auto_ctrl_thresholds_t thresholds = {0};
|
||||
auto_ctrl_thresholds_get(&thresholds);
|
||||
snap.soil_on_threshold = thresholds.pump_on_soil_below_pct;
|
||||
snap.soil_off_threshold = thresholds.pump_off_soil_above_pct;
|
||||
snap.light_on_threshold = thresholds.light_on_lux_below;
|
||||
snap.light_off_threshold = thresholds.light_off_lux_above;
|
||||
snap.i2c_ready = s_i2c_ready;
|
||||
snap.soil_sensor_ready = s_soil_sensor_ready;
|
||||
snap.loop_counter = s_main_loop_counter;
|
||||
|
||||
esp_err_t ret = status_web_update(&snap);
|
||||
if (ret != ESP_OK && ret != ESP_ERR_INVALID_STATE)
|
||||
{
|
||||
ESP_LOGW(TAG, "status web update failed: %s", esp_err_to_name(ret));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief MQTT 控制命令处理函数
|
||||
*
|
||||
* 处理来自 MQTT 的控制命令,包括模式切换、阈值更新、水泵和补光灯控制。
|
||||
*
|
||||
* @param cmd 指向 MQTT 控制命令结构体的指针
|
||||
* @param user_ctx 用户上下文(未使用)
|
||||
* @return esp_err_t 处理结果
|
||||
*/
|
||||
static esp_err_t mqtt_control_command_handler(const mqtt_control_command_t *cmd, void *user_ctx)
|
||||
{
|
||||
(void)user_ctx;
|
||||
ESP_RETURN_ON_FALSE(cmd != NULL, ESP_ERR_INVALID_ARG, TAG, "cmd is null");
|
||||
esp_err_t final_ret = ESP_OK;
|
||||
bool has_any_control = cmd->has_mode || cmd->has_thresholds || cmd->has_pump || cmd->has_light;
|
||||
|
||||
// 处理模式切换命令
|
||||
if (cmd->has_mode)
|
||||
{
|
||||
s_auto_control_enabled = cmd->auto_mode;
|
||||
ESP_LOGI(TAG, "MQTT 控制模式切换: %s", s_auto_control_enabled ? "auto" : "manual");
|
||||
}
|
||||
|
||||
// 处理阈值更新命令
|
||||
if (cmd->has_thresholds)
|
||||
{
|
||||
esp_err_t ret = auto_ctrl_thresholds_set_values(cmd->soil_on_pct,
|
||||
cmd->soil_off_pct,
|
||||
cmd->light_on_lux,
|
||||
cmd->light_off_lux);
|
||||
if (ret == ESP_OK)
|
||||
{
|
||||
ESP_LOGI(TAG,
|
||||
"MQTT 更新阈值: soil_on=%.1f soil_off=%.1f light_on=%.1f light_off=%.1f",
|
||||
cmd->soil_on_pct,
|
||||
cmd->soil_off_pct,
|
||||
cmd->light_on_lux,
|
||||
cmd->light_off_lux);
|
||||
}
|
||||
else
|
||||
{
|
||||
ESP_LOGE(TAG, "设置阈值失败: %s", esp_err_to_name(ret));
|
||||
final_ret = ret;
|
||||
}
|
||||
}
|
||||
|
||||
// 处理水泵控制命令
|
||||
if (cmd->has_pump)
|
||||
{
|
||||
esp_err_t ret = io_device_control_set_pump(cmd->pump_on);
|
||||
if (ret == ESP_OK)
|
||||
{
|
||||
s_pump_on = cmd->pump_on;
|
||||
ESP_LOGI(TAG, "MQTT 控制水泵: %s", cmd->pump_on ? "on" : "off");
|
||||
}
|
||||
else
|
||||
{
|
||||
ESP_LOGE(TAG, "MQTT 控制水泵失败: %s", esp_err_to_name(ret));
|
||||
final_ret = ret;
|
||||
}
|
||||
}
|
||||
|
||||
// 处理补光灯控制命令
|
||||
if (cmd->has_light)
|
||||
{
|
||||
esp_err_t ret = io_device_control_set_light(cmd->light_on);
|
||||
if (ret == ESP_OK)
|
||||
{
|
||||
s_light_on = cmd->light_on;
|
||||
ESP_LOGI(TAG, "MQTT 控制补光灯: %s", cmd->light_on ? "on" : "off");
|
||||
}
|
||||
else
|
||||
{
|
||||
ESP_LOGE(TAG, "MQTT 控制补光灯失败: %s", esp_err_to_name(ret));
|
||||
final_ret = ret;
|
||||
}
|
||||
}
|
||||
|
||||
// 任何控制指令处理后都立即上报最新状态(含阈值)作为回复。
|
||||
if (has_any_control)
|
||||
{
|
||||
esp_err_t pub_ret = publish_telemetry_snapshot();
|
||||
if (pub_ret != ESP_OK)
|
||||
{
|
||||
ESP_LOGW(TAG, "控制后立即上报失败: %s", esp_err_to_name(pub_ret));
|
||||
if (final_ret == ESP_OK)
|
||||
{
|
||||
final_ret = pub_ret;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return final_ret;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief 将告警指标类型转换为字符串
|
||||
*
|
||||
* @param metric 告警指标类型
|
||||
* @return const char* 对应的字符串表示
|
||||
*/
|
||||
static const char *alert_metric_text(auto_alert_metric_t metric)
|
||||
{
|
||||
switch (metric)
|
||||
{
|
||||
case AUTO_ALERT_METRIC_SOIL_MOISTURE:
|
||||
return "soil";
|
||||
case AUTO_ALERT_METRIC_LIGHT_INTENSITY:
|
||||
return "light";
|
||||
default:
|
||||
return "unknown";
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief 将告警状态转换为字符串
|
||||
*
|
||||
* @param state 告警状态
|
||||
* @return const char* 对应的字符串表示
|
||||
*/
|
||||
static const char *alert_state_text(auto_alert_state_t state)
|
||||
{
|
||||
switch (state)
|
||||
{
|
||||
case AUTO_ALERT_STATE_NORMAL:
|
||||
return "normal";
|
||||
case AUTO_ALERT_STATE_ALARM:
|
||||
return "alarm";
|
||||
default:
|
||||
return "unknown";
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief 自动告警 MQTT 回调函数
|
||||
*
|
||||
* 当自动告警模块触发事件时,通过此函数将告警信息以 JSON 格式发布到 MQTT。
|
||||
*
|
||||
* @param event 指向告警事件结构体的指针
|
||||
* @param user_ctx 用户上下文(未使用)
|
||||
*/
|
||||
static void auto_alert_mqtt_callback(const auto_alert_event_t *event, void *user_ctx)
|
||||
{
|
||||
(void)user_ctx;
|
||||
if (event == NULL)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
// 使用明文发送报警简单的 JSON 字符串,格式示例:{"metric":"soil","state":"alarm"}
|
||||
char payload[64] = {0};
|
||||
int len = snprintf(payload,
|
||||
sizeof(payload),
|
||||
"{\"metric\":\"%s\",\"state\":\"%s\"}",
|
||||
alert_metric_text(event->metric),
|
||||
alert_state_text(event->state));
|
||||
if (len <= 0 || len >= (int)sizeof(payload))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (!mqtt_control_is_connected())
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
esp_err_t ret = mqtt_control_publish(BOTANY_MQTT_ALERT_TOPIC, payload, 1, 0);
|
||||
if (ret != ESP_OK)
|
||||
{
|
||||
ESP_LOGE(TAG, "告警 MQTT 发布失败: %s", esp_err_to_name(ret));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief 自动控制逻辑更新函数
|
||||
*
|
||||
* 根据当前传感器数据和阈值,决定是否需要开启或关闭水泵和补光灯。
|
||||
*
|
||||
* @param soil_valid 土壤湿度数据是否有效
|
||||
* @param soil_moisture_pct 当前土壤湿度百分比
|
||||
* @param light_valid 光照数据是否有效
|
||||
* @param light_lux 当前光照强度(lux)
|
||||
* @param thresholds 指向阈值配置结构体的指针
|
||||
* @param pump_on 指向当前水泵状态的指针(输入/输出)
|
||||
* @param light_on 指向当前补光灯状态的指针(输入/输出)
|
||||
*/
|
||||
static void auto_control_update(bool soil_valid,
|
||||
float soil_moisture_pct,
|
||||
bool light_valid,
|
||||
float light_lux,
|
||||
const auto_ctrl_thresholds_t *thresholds,
|
||||
bool *pump_on,
|
||||
bool *light_on)
|
||||
{
|
||||
bool desired_pump = *pump_on;
|
||||
bool desired_light = *light_on;
|
||||
|
||||
// 根据土壤湿度决定水泵状态
|
||||
if (soil_valid)
|
||||
{
|
||||
if (!desired_pump && soil_moisture_pct < thresholds->pump_on_soil_below_pct)
|
||||
{
|
||||
desired_pump = true;
|
||||
}
|
||||
else if (desired_pump && soil_moisture_pct > thresholds->pump_off_soil_above_pct)
|
||||
{
|
||||
desired_pump = false;
|
||||
}
|
||||
}
|
||||
|
||||
// 根据光照强度决定补光灯状态
|
||||
if (light_valid)
|
||||
{
|
||||
if (!desired_light && light_lux < thresholds->light_on_lux_below)
|
||||
{
|
||||
desired_light = true;
|
||||
}
|
||||
else if (desired_light && light_lux > thresholds->light_off_lux_above)
|
||||
{
|
||||
desired_light = false;
|
||||
}
|
||||
}
|
||||
|
||||
// 如果水泵状态需要改变,则执行控制
|
||||
if (desired_pump != *pump_on)
|
||||
{
|
||||
esp_err_t ret = io_device_control_set_pump(desired_pump);
|
||||
if (ret == ESP_OK)
|
||||
{
|
||||
*pump_on = desired_pump;
|
||||
ESP_LOGI(TAG,
|
||||
"自动控制: 水泵%s (土壤湿度=%.1f%%)",
|
||||
desired_pump ? "开启" : "关闭",
|
||||
soil_moisture_pct);
|
||||
}
|
||||
else
|
||||
{
|
||||
ESP_LOGE(TAG, "自动控制: 水泵控制失败: %s", esp_err_to_name(ret));
|
||||
}
|
||||
}
|
||||
|
||||
// 如果补光灯状态需要改变,则执行控制
|
||||
if (desired_light != *light_on)
|
||||
{
|
||||
esp_err_t ret = io_device_control_set_light(desired_light);
|
||||
if (ret == ESP_OK)
|
||||
{
|
||||
*light_on = desired_light;
|
||||
ESP_LOGI(TAG,
|
||||
"自动控制: 补光灯%s (光照=%.1f lux)",
|
||||
desired_light ? "开启" : "关闭",
|
||||
light_lux);
|
||||
}
|
||||
else
|
||||
{
|
||||
ESP_LOGE(TAG, "自动控制: 补光灯控制失败: %s", esp_err_to_name(ret));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief UI 任务函数
|
||||
*
|
||||
* 负责定期切换显示页面(每3秒切换一次)。
|
||||
*
|
||||
* @param arg 任务参数(未使用)
|
||||
*/
|
||||
static void ui_task(void *arg)
|
||||
{
|
||||
(void)arg;
|
||||
@@ -84,6 +451,11 @@ static void ui_task(void *arg)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief 等待 Wi-Fi 连接成功
|
||||
*
|
||||
* 在初始化 console 之前,确保 Wi-Fi 已连接成功,最多等待120秒。
|
||||
*/
|
||||
static void wait_for_wifi_connected(void)
|
||||
{
|
||||
const uint32_t timeout_s = 120;
|
||||
@@ -111,6 +483,11 @@ static void wait_for_wifi_connected(void)
|
||||
ESP_LOGI(TAG, "Wi-Fi 已连接,开始初始化 console");
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief 主函数
|
||||
*
|
||||
* 系统启动入口,初始化所有组件并进入主循环。
|
||||
*/
|
||||
void app_main(void)
|
||||
{
|
||||
// 初始化 Wi-Fi 配网组件,支持长按按键进入配网
|
||||
@@ -160,6 +537,7 @@ void app_main(void)
|
||||
else
|
||||
{
|
||||
i2c_ready = true;
|
||||
s_i2c_ready = true;
|
||||
}
|
||||
|
||||
// 初始化电容式土壤湿度传感器(GPIO0 / ADC1_CH0)。
|
||||
@@ -181,26 +559,60 @@ void app_main(void)
|
||||
else
|
||||
{
|
||||
soil_ready = true;
|
||||
s_soil_sensor_ready = true;
|
||||
}
|
||||
|
||||
// 按需求:仅在 Wi-Fi 确认连通后再初始化 console。
|
||||
// 按需求:仅在 Wi-Fi 确认连通后再初始化 MQTT和console。
|
||||
wait_for_wifi_connected();
|
||||
|
||||
// 独立状态网页(端口 8080),与配网页面(端口 80)互不干扰。
|
||||
ESP_ERROR_CHECK(status_web_start(BOTANY_STATUS_WEB_PORT));
|
||||
|
||||
ESP_ERROR_CHECK(mqtt_control_register_command_handler(mqtt_control_command_handler, NULL));
|
||||
ESP_ERROR_CHECK(mqtt_control_start()); // 启动 MQTT 客户端
|
||||
|
||||
ESP_ERROR_CHECK(console_cmd_init());
|
||||
ESP_ERROR_CHECK(console_user_cmds_register());
|
||||
ESP_ERROR_CHECK(console_cmd_all_register()); // 可选:自动注册插件命令
|
||||
ESP_ERROR_CHECK(console_cmd_start());
|
||||
|
||||
auto_ctrl_thresholds_init_defaults();
|
||||
auto_alerts_init();
|
||||
ESP_ERROR_CHECK(auto_alerts_register_callback(auto_alert_mqtt_callback, NULL));
|
||||
auto_ctrl_thresholds_t thresholds = {0};
|
||||
auto_ctrl_thresholds_get(&thresholds);
|
||||
|
||||
uint32_t telemetry_elapsed_ms = 0;
|
||||
ESP_LOGI(TAG,
|
||||
"自动控制阈值: pump_on<%.1f%%, pump_off>%.1f%%, light_on<%.1flux, light_off>%.1flux",
|
||||
thresholds.pump_on_soil_below_pct,
|
||||
thresholds.pump_off_soil_above_pct,
|
||||
thresholds.light_on_lux_below,
|
||||
thresholds.light_off_lux_above);
|
||||
|
||||
for (;;)
|
||||
{
|
||||
s_main_loop_counter++;
|
||||
|
||||
// 预留给 MQTT 回调动态更新阈值:每个周期读取最新配置。
|
||||
auto_ctrl_thresholds_get(&thresholds);
|
||||
|
||||
bool soil_valid = false;
|
||||
float soil_moisture_pct = 0.0f;
|
||||
|
||||
cap_soil_sensor_data_t soil_data = {0};
|
||||
if (soil_ready && cap_soil_sensor_read(&soil_data) == ESP_OK)
|
||||
{
|
||||
// 读取成功
|
||||
soil_valid = true;
|
||||
soil_moisture_pct = soil_data.moisture_percent;
|
||||
snprintf(s_soil, sizeof(s_soil), "%.0f", soil_data.moisture_percent);
|
||||
set_var_soil_moisture(s_soil);
|
||||
}
|
||||
|
||||
bool light_valid = false;
|
||||
float light_lux = 0.0f;
|
||||
|
||||
i2c_master_messager_data_t sensor_data = {0};
|
||||
if (i2c_ready && i2c_master_messager_get_data(&sensor_data) == ESP_OK)
|
||||
{
|
||||
@@ -215,11 +627,44 @@ void app_main(void)
|
||||
}
|
||||
if (sensor_data.bh1750.valid)
|
||||
{
|
||||
light_valid = true;
|
||||
light_lux = sensor_data.bh1750.lux;
|
||||
snprintf(s_lux, sizeof(s_lux), "%.0f", sensor_data.bh1750.lux);
|
||||
set_var_light_intensity(s_lux);
|
||||
}
|
||||
}
|
||||
|
||||
if (s_auto_control_enabled)
|
||||
{
|
||||
auto_control_update(soil_valid,
|
||||
soil_moisture_pct,
|
||||
light_valid,
|
||||
light_lux,
|
||||
&thresholds,
|
||||
&s_pump_on,
|
||||
&s_light_on);
|
||||
}
|
||||
|
||||
// 预留给 MQTT:回调注册后可在此处收到边沿告警事件并发布。
|
||||
auto_alerts_evaluate(soil_valid,
|
||||
soil_moisture_pct,
|
||||
light_valid,
|
||||
light_lux,
|
||||
&thresholds);
|
||||
|
||||
update_status_web_snapshot();
|
||||
|
||||
telemetry_elapsed_ms += 1000;
|
||||
if (telemetry_elapsed_ms >= BOTANY_MQTT_TELEMETRY_PERIOD_MS)
|
||||
{
|
||||
telemetry_elapsed_ms = 0;
|
||||
esp_err_t pub_ret = publish_telemetry_snapshot();
|
||||
if (pub_ret != ESP_OK && pub_ret != ESP_ERR_INVALID_STATE)
|
||||
{
|
||||
ESP_LOGW(TAG, "周期状态上报失败: %s", esp_err_to_name(pub_ret));
|
||||
}
|
||||
}
|
||||
|
||||
vTaskDelay(pdMS_TO_TICKS(1000));
|
||||
}
|
||||
}
|
||||
256
main/status_web.c
Normal file
256
main/status_web.c
Normal file
@@ -0,0 +1,256 @@
|
||||
#include "status_web.h"
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "esp_app_desc.h"
|
||||
#include "esp_check.h"
|
||||
#include "esp_heap_caps.h"
|
||||
#include "esp_http_server.h"
|
||||
#include "esp_log.h"
|
||||
#include "esp_netif.h"
|
||||
#include "esp_netif_ip_addr.h"
|
||||
#include "esp_timer.h"
|
||||
#include "freertos/FreeRTOS.h"
|
||||
#include "freertos/semphr.h"
|
||||
#include "mqtt_control.h"
|
||||
#include "wifi-connect.h"
|
||||
|
||||
static const char *TAG = "status_web";
|
||||
|
||||
static httpd_handle_t s_server = NULL;
|
||||
static SemaphoreHandle_t s_lock = NULL;
|
||||
static uint64_t s_snapshot_update_ms = 0;
|
||||
static uint32_t s_snapshot_update_count = 0;
|
||||
static status_web_snapshot_t s_snapshot = {
|
||||
.temp = "--",
|
||||
.hum = "--",
|
||||
.soil = "--",
|
||||
.lux = "--",
|
||||
.pump_on = false,
|
||||
.light_on = false,
|
||||
.auto_mode = true,
|
||||
.soil_on_threshold = 35.0f,
|
||||
.soil_off_threshold = 45.0f,
|
||||
.light_on_threshold = 100.0f,
|
||||
.light_off_threshold = 350.0f,
|
||||
.i2c_ready = false,
|
||||
.soil_sensor_ready = false,
|
||||
.loop_counter = 0,
|
||||
};
|
||||
|
||||
static const char *wifi_status_text(wifi_connect_status_t status)
|
||||
{
|
||||
switch (status) {
|
||||
case WIFI_CONNECT_STATUS_IDLE: return "idle";
|
||||
case WIFI_CONNECT_STATUS_PROVISIONING: return "provisioning";
|
||||
case WIFI_CONNECT_STATUS_CONNECTING: return "connecting";
|
||||
case WIFI_CONNECT_STATUS_CONNECTED: return "connected";
|
||||
case WIFI_CONNECT_STATUS_FAILED: return "failed";
|
||||
case WIFI_CONNECT_STATUS_TIMEOUT: return "timeout";
|
||||
default: return "unknown";
|
||||
}
|
||||
}
|
||||
|
||||
static void get_sta_ip_text(char *out, size_t out_size)
|
||||
{
|
||||
if (out == NULL || out_size == 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
snprintf(out, out_size, "--");
|
||||
esp_netif_t *sta = esp_netif_get_handle_from_ifkey("WIFI_STA_DEF");
|
||||
if (sta == NULL) {
|
||||
return;
|
||||
}
|
||||
|
||||
esp_netif_ip_info_t ip_info;
|
||||
if (esp_netif_get_ip_info(sta, &ip_info) == ESP_OK) {
|
||||
snprintf(out,
|
||||
out_size,
|
||||
IPSTR,
|
||||
IP2STR(&ip_info.ip));
|
||||
}
|
||||
}
|
||||
|
||||
static const char *s_page_html =
|
||||
"<!doctype html><html><head><meta charset='utf-8'/>"
|
||||
"<meta name='viewport' content='width=device-width, initial-scale=1'/>"
|
||||
"<title>BotanicalBuddy Status</title>"
|
||||
"<style>"
|
||||
"body{font-family:system-ui,-apple-system,Segoe UI,Roboto,sans-serif;background:#eef2f7;margin:0;padding:14px;color:#111827;}"
|
||||
".wrap{max-width:1080px;margin:0 auto;background:#fff;border-radius:14px;padding:14px;box-shadow:0 8px 24px rgba(0,0,0,.08);}"
|
||||
"h1{font-size:20px;margin:0 0 10px;}"
|
||||
".meta{font-size:12px;color:#6b7280;margin-bottom:10px;}"
|
||||
".sec{margin-top:10px;}"
|
||||
".sec h2{font-size:14px;margin:0 0 6px;color:#374151;}"
|
||||
".grid{display:grid;grid-template-columns:repeat(3,minmax(0,1fr));gap:8px;}"
|
||||
"@media(max-width:900px){.grid{grid-template-columns:1fr 1fr;}}"
|
||||
"@media(max-width:560px){.grid{grid-template-columns:1fr;}}"
|
||||
".card{padding:10px;border:1px solid #e5e7eb;border-radius:10px;background:#fafafa;}"
|
||||
".k{font-size:12px;color:#6b7280;}"
|
||||
".v{font-size:17px;font-weight:600;margin-top:2px;word-break:break-all;}"
|
||||
"#raw{font-family:ui-monospace,Menlo,Consolas,monospace;font-size:12px;background:#0f172a;color:#e2e8f0;padding:10px;border-radius:10px;overflow:auto;max-height:260px;}"
|
||||
"button{margin-top:10px;border:none;border-radius:8px;background:#1d4ed8;color:#fff;padding:8px 12px;cursor:pointer;}"
|
||||
"</style></head><body><div class='wrap'>"
|
||||
"<h1>BotanicalBuddy 设备状态总览</h1>"
|
||||
"<div class='meta'>独立状态服务(port 8080),每2秒自动刷新</div>"
|
||||
"<div class='sec'><h2>传感与控制</h2><div class='grid'>"
|
||||
"<div class='card'><div class='k'>空气温度</div><div id='temp' class='v'>--</div></div>"
|
||||
"<div class='card'><div class='k'>空气湿度</div><div id='hum' class='v'>--</div></div>"
|
||||
"<div class='card'><div class='k'>土壤湿度</div><div id='soil' class='v'>--</div></div>"
|
||||
"<div class='card'><div class='k'>光照强度</div><div id='lux' class='v'>--</div></div>"
|
||||
"<div class='card'><div class='k'>水泵</div><div id='pump' class='v'>--</div></div>"
|
||||
"<div class='card'><div class='k'>补光灯</div><div id='light' class='v'>--</div></div>"
|
||||
"<div class='card'><div class='k'>控制模式</div><div id='mode' class='v'>--</div></div>"
|
||||
"<div class='card'><div class='k'>soil_on/off</div><div id='soil_th' class='v'>--</div></div>"
|
||||
"<div class='card'><div class='k'>light_on/off</div><div id='light_th' class='v'>--</div></div>"
|
||||
"</div></div>"
|
||||
"<div class='sec'><h2>连接与系统</h2><div class='grid'>"
|
||||
"<div class='card'><div class='k'>Wi-Fi 状态</div><div id='wifi' class='v'>--</div></div>"
|
||||
"<div class='card'><div class='k'>STA IP</div><div id='ip' class='v'>--</div></div>"
|
||||
"<div class='card'><div class='k'>MQTT 连接</div><div id='mqtt' class='v'>--</div></div>"
|
||||
"<div class='card'><div class='k'>I2C Ready</div><div id='i2c' class='v'>--</div></div>"
|
||||
"<div class='card'><div class='k'>Soil Sensor Ready</div><div id='soil_ready' class='v'>--</div></div>"
|
||||
"<div class='card'><div class='k'>主循环计数</div><div id='loop' class='v'>--</div></div>"
|
||||
"<div class='card'><div class='k'>运行时长</div><div id='uptime' class='v'>--</div></div>"
|
||||
"<div class='card'><div class='k'>空闲堆/最小堆</div><div id='heap' class='v'>--</div></div>"
|
||||
"<div class='card'><div class='k'>固件版本</div><div id='ver' class='v'>--</div></div>"
|
||||
"<div class='card'><div class='k'>快照更新时间</div><div id='snap' class='v'>--</div></div>"
|
||||
"<div class='card'><div class='k'>快照序号</div><div id='seq' class='v'>--</div></div>"
|
||||
"<div class='card'><div class='k'>快照年龄</div><div id='age' class='v'>--</div></div>"
|
||||
"</div></div>"
|
||||
"<div class='sec'><h2>原始 JSON</h2><pre id='raw'>{}</pre></div>"
|
||||
"<button onclick='loadStatus()'>立即刷新</button>"
|
||||
"</div><script>"
|
||||
"function onoff(v){return v?'on':'off';}"
|
||||
"function yn(v){return v?'yes':'no';}"
|
||||
"function fmtMs(ms){if(ms<1000)return ms+' ms';const s=Math.floor(ms/1000);if(s<60)return s+' s';const m=Math.floor(s/60);const rs=s%60;if(m<60)return m+'m '+rs+'s';const h=Math.floor(m/60);return h+'h '+(m%60)+'m';}"
|
||||
"async function loadStatus(){try{const r=await fetch('/api/status');const d=await r.json();"
|
||||
"temp.textContent=d.temp;hum.textContent=d.hum;soil.textContent=d.soil;lux.textContent=d.lux;"
|
||||
"pump.textContent=d.pump;light.textContent=d.light;mode.textContent=d.mode;"
|
||||
"soil_th.textContent=`${d.soil_on}/${d.soil_off}`;light_th.textContent=`${d.light_on}/${d.light_off}`;"
|
||||
"wifi.textContent=d.wifi_status;ip.textContent=d.sta_ip;mqtt.textContent=onoff(d.mqtt_connected);"
|
||||
"i2c.textContent=yn(d.i2c_ready);soil_ready.textContent=yn(d.soil_sensor_ready);"
|
||||
"loop.textContent=d.loop_counter;uptime.textContent=fmtMs(d.uptime_ms);"
|
||||
"heap.textContent=`${d.free_heap}/${d.min_free_heap}`;ver.textContent=d.app_version;"
|
||||
"snap.textContent=d.snapshot_update_ms;seq.textContent=d.snapshot_update_count;age.textContent=fmtMs(d.snapshot_age_ms);"
|
||||
"raw.textContent=JSON.stringify(d,null,2);"
|
||||
"}catch(e){raw.textContent='读取失败: '+e;}}"
|
||||
"setInterval(loadStatus,2000);loadStatus();"
|
||||
"</script></body></html>";
|
||||
|
||||
static esp_err_t status_root_handler(httpd_req_t *req)
|
||||
{
|
||||
httpd_resp_set_type(req, "text/html");
|
||||
return httpd_resp_send(req, s_page_html, HTTPD_RESP_USE_STRLEN);
|
||||
}
|
||||
|
||||
static esp_err_t status_api_handler(httpd_req_t *req)
|
||||
{
|
||||
status_web_snapshot_t snap;
|
||||
uint64_t snapshot_update_ms = 0;
|
||||
uint32_t snapshot_update_count = 0;
|
||||
xSemaphoreTake(s_lock, portMAX_DELAY);
|
||||
snap = s_snapshot;
|
||||
snapshot_update_ms = s_snapshot_update_ms;
|
||||
snapshot_update_count = s_snapshot_update_count;
|
||||
xSemaphoreGive(s_lock);
|
||||
|
||||
const esp_app_desc_t *app_desc = esp_app_get_description();
|
||||
uint64_t now_ms = (uint64_t)(esp_timer_get_time() / 1000);
|
||||
uint64_t snapshot_age_ms = (snapshot_update_ms > 0 && now_ms >= snapshot_update_ms) ? (now_ms - snapshot_update_ms) : 0;
|
||||
const uint32_t free_heap = esp_get_free_heap_size();
|
||||
const uint32_t min_free_heap = esp_get_minimum_free_heap_size();
|
||||
const uint32_t largest_block = heap_caps_get_largest_free_block(MALLOC_CAP_8BIT);
|
||||
const bool mqtt_connected = mqtt_control_is_connected();
|
||||
const wifi_connect_status_t wifi_status = wifi_connect_get_status();
|
||||
char ip_text[16] = {0};
|
||||
get_sta_ip_text(ip_text, sizeof(ip_text));
|
||||
|
||||
char json[640];
|
||||
int len = snprintf(json,
|
||||
sizeof(json),
|
||||
"{\"temp\":\"%s\",\"hum\":\"%s\",\"soil\":\"%s\",\"lux\":\"%s\",\"pump\":\"%s\",\"light\":\"%s\",\"mode\":\"%s\",\"soil_on\":%.1f,\"soil_off\":%.1f,\"light_on\":%.1f,\"light_off\":%.1f,\"wifi_status\":\"%s\",\"sta_ip\":\"%s\",\"mqtt_connected\":%s,\"i2c_ready\":%s,\"soil_sensor_ready\":%s,\"loop_counter\":%lu,\"uptime_ms\":%llu,\"free_heap\":%lu,\"min_free_heap\":%lu,\"largest_block\":%lu,\"app_version\":\"%s\",\"snapshot_update_ms\":%llu,\"snapshot_update_count\":%lu,\"snapshot_age_ms\":%llu}",
|
||||
snap.temp,
|
||||
snap.hum,
|
||||
snap.soil,
|
||||
snap.lux,
|
||||
snap.pump_on ? "on" : "off",
|
||||
snap.light_on ? "on" : "off",
|
||||
snap.auto_mode ? "auto" : "manual",
|
||||
snap.soil_on_threshold,
|
||||
snap.soil_off_threshold,
|
||||
snap.light_on_threshold,
|
||||
snap.light_off_threshold,
|
||||
wifi_status_text(wifi_status),
|
||||
ip_text,
|
||||
mqtt_connected ? "true" : "false",
|
||||
snap.i2c_ready ? "true" : "false",
|
||||
snap.soil_sensor_ready ? "true" : "false",
|
||||
(unsigned long)snap.loop_counter,
|
||||
(unsigned long long)now_ms,
|
||||
(unsigned long)free_heap,
|
||||
(unsigned long)min_free_heap,
|
||||
(unsigned long)largest_block,
|
||||
app_desc ? app_desc->version : "unknown",
|
||||
(unsigned long long)snapshot_update_ms,
|
||||
(unsigned long)snapshot_update_count,
|
||||
(unsigned long long)snapshot_age_ms);
|
||||
if (len <= 0 || len >= (int)sizeof(json)) {
|
||||
return ESP_FAIL;
|
||||
}
|
||||
|
||||
httpd_resp_set_type(req, "application/json");
|
||||
return httpd_resp_sendstr(req, json);
|
||||
}
|
||||
|
||||
esp_err_t status_web_start(uint16_t port)
|
||||
{
|
||||
if (s_server != NULL) {
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
if (s_lock == NULL) {
|
||||
s_lock = xSemaphoreCreateMutex();
|
||||
ESP_RETURN_ON_FALSE(s_lock != NULL, ESP_ERR_NO_MEM, TAG, "create mutex failed");
|
||||
}
|
||||
|
||||
httpd_config_t config = HTTPD_DEFAULT_CONFIG();
|
||||
config.server_port = port;
|
||||
config.ctrl_port = (uint16_t)(port + 1);
|
||||
|
||||
ESP_RETURN_ON_ERROR(httpd_start(&s_server, &config), TAG, "httpd_start failed");
|
||||
|
||||
const httpd_uri_t root = {
|
||||
.uri = "/",
|
||||
.method = HTTP_GET,
|
||||
.handler = status_root_handler,
|
||||
.user_ctx = NULL,
|
||||
};
|
||||
const httpd_uri_t api = {
|
||||
.uri = "/api/status",
|
||||
.method = HTTP_GET,
|
||||
.handler = status_api_handler,
|
||||
.user_ctx = NULL,
|
||||
};
|
||||
|
||||
ESP_RETURN_ON_ERROR(httpd_register_uri_handler(s_server, &root), TAG, "register root failed");
|
||||
ESP_RETURN_ON_ERROR(httpd_register_uri_handler(s_server, &api), TAG, "register api failed");
|
||||
|
||||
ESP_LOGI(TAG, "status web started at port %u", (unsigned)port);
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
esp_err_t status_web_update(const status_web_snapshot_t *snapshot)
|
||||
{
|
||||
ESP_RETURN_ON_FALSE(snapshot != NULL, ESP_ERR_INVALID_ARG, TAG, "snapshot is null");
|
||||
ESP_RETURN_ON_FALSE(s_lock != NULL, ESP_ERR_INVALID_STATE, TAG, "status web not started");
|
||||
|
||||
xSemaphoreTake(s_lock, portMAX_DELAY);
|
||||
s_snapshot = *snapshot;
|
||||
s_snapshot_update_ms = (uint64_t)(esp_timer_get_time() / 1000);
|
||||
s_snapshot_update_count++;
|
||||
xSemaphoreGive(s_lock);
|
||||
return ESP_OK;
|
||||
}
|
||||
34
main/status_web.h
Normal file
34
main/status_web.h
Normal file
@@ -0,0 +1,34 @@
|
||||
#pragma once
|
||||
|
||||
#include <stdbool.h>
|
||||
#include <stdint.h>
|
||||
|
||||
#include "esp_err.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
typedef struct {
|
||||
char temp[16];
|
||||
char hum[16];
|
||||
char soil[16];
|
||||
char lux[16];
|
||||
bool pump_on;
|
||||
bool light_on;
|
||||
bool auto_mode;
|
||||
float soil_on_threshold;
|
||||
float soil_off_threshold;
|
||||
float light_on_threshold;
|
||||
float light_off_threshold;
|
||||
bool i2c_ready;
|
||||
bool soil_sensor_ready;
|
||||
uint32_t loop_counter;
|
||||
} status_web_snapshot_t;
|
||||
|
||||
esp_err_t status_web_start(uint16_t port);
|
||||
esp_err_t status_web_update(const status_web_snapshot_t *snapshot);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
4
partitions.csv
Normal file
4
partitions.csv
Normal file
@@ -0,0 +1,4 @@
|
||||
# Name, Type, SubType, Offset, Size, Flags
|
||||
nvs, data, nvs, 0x9000, 0x6000,
|
||||
phy_init, data, phy, 0xf000, 0x1000,
|
||||
factory, app, factory, 0x10000, 0x200000,
|
||||
|
Reference in New Issue
Block a user