添加 MQTT 定时发布传感器数据功能,支持将数据打包成 JSON 格式并发布
This commit is contained in:
@@ -28,6 +28,7 @@
|
|||||||
#include "esp_system.h"
|
#include "esp_system.h"
|
||||||
#include "esp_efuse.h"
|
#include "esp_efuse.h"
|
||||||
#include "esp_efuse_table.h"
|
#include "esp_efuse_table.h"
|
||||||
|
#include "esp_timer.h"
|
||||||
#include "sntp_time.h"
|
#include "sntp_time.h"
|
||||||
#include "bh1750_use.h"
|
#include "bh1750_use.h"
|
||||||
#include "aht30.h"
|
#include "aht30.h"
|
||||||
@@ -38,6 +39,7 @@
|
|||||||
#include "hx711.hpp"
|
#include "hx711.hpp"
|
||||||
#include "su-03t.h"
|
#include "su-03t.h"
|
||||||
#include "agri_env.h"
|
#include "agri_env.h"
|
||||||
|
#include "cJSON.h"
|
||||||
|
|
||||||
#define TAG "MAIN"
|
#define TAG "MAIN"
|
||||||
#define CO2_SPOILAGE_THRESHOLD_PPM 1500.0f
|
#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
|
/* 函数: app_main
|
||||||
* 作用: 执行模块内与函数名对应的业务逻辑。
|
* 作用: 执行模块内与函数名对应的业务逻辑。
|
||||||
* 重点: 关注输入合法性、返回码与并发安全。
|
* 重点: 关注输入合法性、返回码与并发安全。
|
||||||
@@ -630,4 +694,7 @@ extern "C" void app_main(void)
|
|||||||
|
|
||||||
// 独立任务:继电器状态同步到 UI
|
// 独立任务:继电器状态同步到 UI
|
||||||
xTaskCreate(relay_status_task, "relay_status_task", 4096, NULL, 5, NULL);
|
xTaskCreate(relay_status_task, "relay_status_task", 4096, NULL, 5, NULL);
|
||||||
|
|
||||||
|
// 独立任务:MQTT 定时发布传感器数据
|
||||||
|
xTaskCreate(mqtt_publish_task, "mqtt_publish_task", 4096 * 2, NULL, 5, NULL);
|
||||||
}
|
}
|
||||||
|
|||||||
BIN
相关资料/~$说明书 (1).docx
Normal file
BIN
相关资料/~$说明书 (1).docx
Normal file
Binary file not shown.
Reference in New Issue
Block a user