From 2b25b497e9022cb4d30b2520e23c610a2dc8535a Mon Sep 17 00:00:00 2001 From: Your Name Date: Sun, 9 Mar 2025 00:24:27 +0800 Subject: [PATCH] =?UTF-8?q?hamqtt=E4=B8=AD=E5=A2=9E=E5=8A=A0=E4=BB=8A?= =?UTF-8?q?=E6=97=A5=E8=80=97=E7=94=B5=E9=87=8F=EF=BC=8C=E6=98=A8=E6=97=A5?= =?UTF-8?q?=E8=80=97=E7=94=B5=E9=87=8F=E4=B8=A4=E4=B8=AA=E4=BC=A0=E6=84=9F?= =?UTF-8?q?=E5=99=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- TC1/main.c | 79 +++++++++++++++++++++++++++--- TC1/main.h | 2 + TC1/mqtt_server/user_mqtt_client.c | 34 ++++++++++++- 3 files changed, 105 insertions(+), 10 deletions(-) diff --git a/TC1/main.c b/TC1/main.c index e403050..a0ffc04 100644 --- a/TC1/main.c +++ b/TC1/main.c @@ -1,4 +1,8 @@ #include "main.h" +#include "stdio.h" +#include "stdlib.h" +#include "time.h" +#include "unistd.h" #include "user_gpio.h" #include "user_wifi.h" @@ -34,6 +38,8 @@ void appRestoreDefault_callback(void * const user_config_data, uint32_t size) userConfigDefault->task_top = NULL; userConfigDefault->task_count = 0; userConfigDefault->mqtt_report_freq = 2; + userConfigDefault->p_count_2_days_ago = 0; + userConfigDefault->p_count_1_day_ago = 0; userConfigDefault->version = USER_CONFIG_VERSION; int i; @@ -48,6 +54,59 @@ void appRestoreDefault_callback(void * const user_config_data, uint32_t size) //mico_system_context_update(sys_config); } +void recordDailyPcount(){ + if(user_config->p_count_1_day_ago != 0){ + user_config->p_count_2_days_ago = user_config->p_count_1_day_ago; + } + user_config->p_count_1_day_ago = p_count; + mico_system_context_update(sys_config); + tc1_log("WARNGIN: p_count record!"); +} + +void schedule_p_count_task(mico_thread_arg_t arg){ + mico_rtos_thread_sleep(20); + tc1_log("WARNGIN: p_count timer thread created!"); + while (1) { + // 获取当前时间 + time_t now; + struct tm next_run; + time(&now); + struct tm *current_time = localtime(&now); + // 计算下次执行时间,目标是 0 点 0 分 + next_run = *current_time; + next_run.tm_hour = 0; + next_run.tm_min = 0; + next_run.tm_sec = 0; + // 如果当前时间已经过了 0 点 0 分,则设置为第二天 + if (current_time->tm_hour >= 0 && current_time->tm_min > 0) { + next_run.tm_mday += 1; // 第二天 + } + // 计算时间间隔(秒数) + time_t next_time = mktime(&next_run); + double seconds_until_next_run = difftime(next_time, now); + if (seconds_until_next_run > 0) { + // 休眠直到目标时间 + mico_rtos_thread_sleep(seconds_until_next_run); + } + // 执行任务 + recordDailyPcount(); + } +} + +void reportMqttPowerInfoThread(){ + while (1) + { + UserMqttHassPower(); + int freq = user_config->mqtt_report_freq; + + if(freq == 0){ + freq = 2; + } + + mico_thread_msleep(1000*freq); + } +} + int application_start(void) { int i; @@ -128,22 +187,26 @@ int application_start(void) PowerInit(); AppHttpdStart(); // start http server thread + err = mico_rtos_create_thread(NULL, MICO_APPLICATION_PRIORITY, "p_count", + (mico_thread_function_t) schedule_p_count_task, + 0x2000, 0); + require_noerr_string(err, exit, "ERROR: Unable to start the p_count thread."); + + err = mico_rtos_create_thread(NULL, MICO_APPLICATION_PRIORITY, "mqtt_power_report", + (mico_thread_function_t) reportMqttPowerInfoThread, + 0x2000, 0); + require_noerr_string(err, exit, "ERROR: Unable to start the mqtt_power_report thread."); + + while (1) { - UserMqttHassPower(); - time_t now = time(NULL); if (user_config->task_top && now >= user_config->task_top->prs_time) { ProcessTask(); } - int freq = user_config->mqtt_report_freq; - if(freq == 0){ - freq = 2; - } - - mico_thread_msleep(1000*freq); + mico_thread_msleep(1000); } exit: diff --git a/TC1/main.h b/TC1/main.h index 2e11d03..f18a99f 100644 --- a/TC1/main.h +++ b/TC1/main.h @@ -61,6 +61,8 @@ typedef struct char ap_name[32]; char ap_key[32]; int task_count; + uint32_t p_count_2_days_ago; + uint32_t p_count_1_day_ago; pTimedTask task_top; struct TimedTask timed_tasks[MAX_TASK_NUM]; } user_config_t; diff --git a/TC1/mqtt_server/user_mqtt_client.c b/TC1/mqtt_server/user_mqtt_client.c index 92be9a4..dc20f8d 100644 --- a/TC1/mqtt_server/user_mqtt_client.c +++ b/TC1/mqtt_server/user_mqtt_client.c @@ -507,13 +507,35 @@ void UserMqttHassAutoPower(void) "\"value_template\":\"{{ value_json.powerConsumption }}\"}", str_mac+8, str_mac, str_mac); UserMqttSendTopic(topic_buf, send_buf, 1); + + sprintf(topic_buf, "homeassistant/sensor/%s/powerConsumptionToday/config", str_mac); + sprintf(send_buf, + "{\"name\":\"TC1_%s_powerConsumptionToday\"," + "\"uniq_id\":\"%s_pc_today\"," + "\"state_topic\":\"homeassistant/sensor/%s/powerConsumptionToday/state\"," + "\"unit_of_measurement\":\"kWh\"," + "\"icon\":\"mdi:fence-electric\"," + "\"value_template\":\"{{ value_json.powerConsumptionToday }}\"}", + str_mac+8, str_mac, str_mac); + UserMqttSendTopic(topic_buf, send_buf, 1); + + sprintf(topic_buf, "homeassistant/sensor/%s/powerConsumptionYesterday/config", str_mac); + sprintf(send_buf, + "{\"name\":\"TC1_%s_powerConsumptionYesterday\"," + "\"uniq_id\":\"%s_pc_yesterday\"," + "\"state_topic\":\"homeassistant/sensor/%s/powerConsumptionYesterday/state\"," + "\"unit_of_measurement\":\"kWh\"," + "\"icon\":\"mdi:fence-electric\"," + "\"value_template\":\"{{ value_json.powerConsumptionYesterday }}\"}", + str_mac+8, str_mac, str_mac); + UserMqttSendTopic(topic_buf, send_buf, 1); } if (send_buf) free(send_buf); if (topic_buf) free(topic_buf); } -char topic_buf[64] = { 0 }; -char send_buf[32] = { 0 }; +char topic_buf[128] = { 0 }; +char send_buf[128] = { 0 }; void UserMqttHassPower(void) { sprintf(topic_buf, "homeassistant/sensor/%s/power/state", str_mac); @@ -523,6 +545,14 @@ void UserMqttHassPower(void) sprintf(topic_buf, "homeassistant/sensor/%s/powerConsumption/state", str_mac); sprintf(send_buf, "{\"powerConsumption\":\"%.3f\"}", (17.1 * p_count) / 1000 / 36000); UserMqttSendTopic(topic_buf, send_buf, 0); + + sprintf(topic_buf, "homeassistant/sensor/%s/powerConsumptionToday/state", str_mac); + sprintf(send_buf, "{\"powerConsumptionToday\":\"%.3f\"}", (17.1 * ((p_count-user_config->p_count_1_day_ago)<0?0:(p_count-user_config->p_count_1_day_ago))) / 1000 / 36000); + UserMqttSendTopic(topic_buf, send_buf, 0); + + sprintf(topic_buf, "homeassistant/sensor/%s/powerConsumptionYesterday/state", str_mac); + sprintf(send_buf, "{\"powerConsumptionYesterday\":\"%.3f\"}", (17.1 * ((user_config->p_count_1_day_ago-user_config->p_count_2_days_ago)<0?0:(user_config->p_count_1_day_ago-user_config->p_count_2_days_ago))) / 1000 / 36000); + UserMqttSendTopic(topic_buf, send_buf, 0); } bool UserMqttIsConnect()