From 0d117d9d479b415554186c0e292d1012ae86dd23 Mon Sep 17 00:00:00 2001 From: Wang Beihong Date: Wed, 22 Apr 2026 16:30:02 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=20MQTT=20=E5=AE=9A=E6=97=B6?= =?UTF-8?q?=E5=8F=91=E5=B8=83=E4=BC=A0=E6=84=9F=E5=99=A8=E6=95=B0=E6=8D=AE?= =?UTF-8?q?=E5=8A=9F=E8=83=BD=EF=BC=8C=E6=94=AF=E6=8C=81=E5=B0=86=E6=95=B0?= =?UTF-8?q?=E6=8D=AE=E6=89=93=E5=8C=85=E6=88=90=20JSON=20=E6=A0=BC?= =?UTF-8?q?=E5=BC=8F=E5=B9=B6=E5=8F=91=E5=B8=83?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- main/main.cpp | 67 +++++++++++++++++++++++++++++++++++++ 相关资料/~$说明书 (1).docx | Bin 0 -> 162 bytes 2 files changed, 67 insertions(+) create mode 100644 相关资料/~$说明书 (1).docx diff --git a/main/main.cpp b/main/main.cpp index 2afd424..c0ab490 100644 --- a/main/main.cpp +++ b/main/main.cpp @@ -28,6 +28,7 @@ #include "esp_system.h" #include "esp_efuse.h" #include "esp_efuse_table.h" +#include "esp_timer.h" #include "sntp_time.h" #include "bh1750_use.h" #include "aht30.h" @@ -38,6 +39,7 @@ #include "hx711.hpp" #include "su-03t.h" #include "agri_env.h" +#include "cJSON.h" #define TAG "MAIN" #define CO2_SPOILAGE_THRESHOLD_PPM 1500.0f @@ -459,6 +461,68 @@ static void hx711_task(void *arg) } } +/* 函数: mqtt_publish_task + * 作用: 定时将传感器数据打包成JSON并发布到MQTT + */ +static void mqtt_publish_task(void *arg) +{ + (void)arg; + for (;;) + { + if (agri_env_mqtt_is_connected()) + { + env_data_t local_data; + if (s_env_data_lock) + { + xSemaphoreTake(s_env_data_lock, portMAX_DELAY); + local_data = s_env_data; + xSemaphoreGive(s_env_data_lock); + + cJSON *root = cJSON_CreateObject(); + if (root) + { + cJSON_AddStringToObject(root, "time", local_data.time_str); + cJSON_AddNumberToObject(root, "lux", local_data.lux); + cJSON_AddNumberToObject(root, "temp", local_data.temp); + cJSON_AddNumberToObject(root, "humidity", local_data.humidity); + cJSON_AddNumberToObject(root, "gas_percent", local_data.gas_percent); + cJSON_AddNumberToObject(root, "tvoc", local_data.tvoc); + cJSON_AddNumberToObject(root, "hcho", local_data.hcho); + cJSON_AddNumberToObject(root, "co2", local_data.co2); + cJSON_AddNumberToObject(root, "ice_weight", local_data.ice_weight); + cJSON_AddNumberToObject(root, "fire_percent", local_data.fire_percent); + + cJSON_AddBoolToObject(root, "fire_danger", local_data.fire_danger); + cJSON_AddBoolToObject(root, "human_present", local_data.human_present); + cJSON_AddBoolToObject(root, "door_closed", local_data.door_closed); + cJSON_AddBoolToObject(root, "fan_on", local_data.fan_on); + cJSON_AddBoolToObject(root, "light_on", local_data.light_on); + cJSON_AddBoolToObject(root, "cool_on", local_data.cool_on); + cJSON_AddBoolToObject(root, "hot_on", local_data.hot_on); + + cJSON_AddNumberToObject(root, "su03t_last_msgno", local_data.su03t_last_msgno); + cJSON_AddNumberToObject(root, "su03t_rx_count", local_data.su03t_rx_count); + + // 补充系统及其他分析状态数据 + cJSON_AddStringToObject(root, "ip_address", wifi_connect_get_ip()); + cJSON_AddStringToObject(root, "food_status", local_data.co2 >= CO2_SPOILAGE_THRESHOLD_PPM ? "spoilage" : "good"); + cJSON_AddNumberToObject(root, "uptime_s", esp_timer_get_time() / 1000000ULL); + cJSON_AddNumberToObject(root, "free_heap_kb", esp_get_free_heap_size() / 1024); + + char *json_str = cJSON_PrintUnformatted(root); + if (json_str) + { + agri_env_mqtt_publish(CONFIG_AGRI_ENV_MQTT_PUBLISH_TOPIC, json_str, 0, 0); + free(json_str); // 注意 cJSON_PrintUnformatted 使用 malloc 分配内存 + } + cJSON_Delete(root); + } + } + } + vTaskDelay(pdMS_TO_TICKS(5000)); // 每5秒发布一次 + } +} + /* 函数: app_main * 作用: 执行模块内与函数名对应的业务逻辑。 * 重点: 关注输入合法性、返回码与并发安全。 @@ -630,4 +694,7 @@ extern "C" void app_main(void) // 独立任务:继电器状态同步到 UI xTaskCreate(relay_status_task, "relay_status_task", 4096, NULL, 5, NULL); + + // 独立任务:MQTT 定时发布传感器数据 + xTaskCreate(mqtt_publish_task, "mqtt_publish_task", 4096 * 2, NULL, 5, NULL); } diff --git a/相关资料/~$说明书 (1).docx b/相关资料/~$说明书 (1).docx new file mode 100644 index 0000000000000000000000000000000000000000..6015944bf51cd86074fa48c2dab0cd3c23972940 GIT binary patch literal 162 vcmd;fO3lp3&r1(a%u8n=8gMf>F{CnNGGs91GvqO(GlVlFf@z`+AVDDjLW2mY literal 0 HcmV?d00001