diff --git a/TC1/main.c b/TC1/main.c index a4dd859..dea0914 100644 --- a/TC1/main.c +++ b/TC1/main.c @@ -123,6 +123,8 @@ int application_start(void) while (1) { + UserMqttHassPower(); + time_t now = time(NULL); if (task_top && now >= task_top->prs_time) { diff --git a/TC1/main.h b/TC1/main.h index 1dd3a6d..df37ea2 100644 --- a/TC1/main.h +++ b/TC1/main.h @@ -14,7 +14,7 @@ #define wifi_log(M, ...) do { custom_log("WIFI", M, ##__VA_ARGS__); web_log("WIFI", M, ##__VA_ARGS__) } while(0) #define power_log(M, ...) do { custom_log("POWER", M, ##__VA_ARGS__); web_log("POWER", M, ##__VA_ARGS__) } while(0) -#define VERSION "v1.0.17" +#define VERSION "v1.0.18" #define TYPE 1 #define TYPE_NAME "zTC1" diff --git a/TC1/mqtt_server/user_mqtt_client.c b/TC1/mqtt_server/user_mqtt_client.c index 9e178b6..83250ea 100644 --- a/TC1/mqtt_server/user_mqtt_client.c +++ b/TC1/mqtt_server/user_mqtt_client.c @@ -499,20 +499,13 @@ void UserMqttHassAutoPower(void) if (topic_buf) free(topic_buf); } +char topic_buf[64] = { 0 }; +char send_buf[32] = { 0 }; void UserMqttHassPower(void) { - char *send_buf = NULL; - char *topic_buf = NULL; - send_buf = malloc(512); // - topic_buf = malloc(128); // - if (send_buf != NULL && topic_buf != NULL) - { - sprintf(topic_buf, "homeassistant/sensor/%s/power/state", str_mac); - sprintf(send_buf, "{\"power\":\"%.3f\"}", real_time_power); - UserMqttSendTopic(topic_buf, send_buf, 0); - } - if (send_buf) free(send_buf); - if (topic_buf) free(topic_buf); + sprintf(topic_buf, "homeassistant/sensor/%s/power/state", str_mac); + sprintf(send_buf, "{\"power\":\"%.3f\"}", real_time_power/10); + UserMqttSendTopic(topic_buf, send_buf, 0); } bool UserMqttIsConnect() diff --git a/TC1/user_power.c b/TC1/user_power.c index a18b2fa..6632055 100644 --- a/TC1/user_power.c +++ b/TC1/user_power.c @@ -40,6 +40,8 @@ uint64_t irq_old = 0; //上次中断的时间(纳秒) static void PowerIrqHandler(void* arg) { + //警告! 不能在此函数里调用任何有关moloc()的操作 + p_count++; //mico_time_get_time(&past_ns); //系统运行毫秒数 @@ -57,14 +59,12 @@ static void PowerIrqHandler(void* arg) n_1s += (float)(NS - irq_old % NS) / spend_ns; real_time_power = 17.1 * n_1s; SetPowerRecord(&power_record, (int)real_time_power); - UserMqttHassPower(); int i = 0; for (; i < n; i++) { real_time_power = 17.1 * NS / spend_ns; SetPowerRecord(&power_record, (int)real_time_power); - UserMqttHassPower(); } irq_old = past_ns; n_1s = (float)(past_ns % NS) / spend_ns;