优化HA功率

This commit is contained in:
zogodo
2020-02-18 09:51:04 +08:00
parent 22dc3bc1df
commit 73a60d3e04
9 changed files with 18 additions and 35 deletions

View File

@@ -105,7 +105,7 @@ exit:
static int HttpGetTc1Status(httpd_request_t *req) static int HttpGetTc1Status(httpd_request_t *req)
{ {
const unsigned char* sockets = GetSocketStatus(); char* sockets = GetSocketStatus();
char* tc1_status = malloc(412); char* tc1_status = malloc(412);
sprintf(tc1_status, TC1_STATUS_JSON, sockets, ip_status.mode, sprintf(tc1_status, TC1_STATUS_JSON, sockets, ip_status.mode,
sys_config->micoSystemConfig.ssid, sys_config->micoSystemConfig.user_key, sys_config->micoSystemConfig.ssid, sys_config->micoSystemConfig.user_key,

View File

@@ -11,11 +11,9 @@
char rtc_init = 0; //sntp校时成功标志位 char rtc_init = 0; //sntp校时成功标志位
uint32_t total_time = 0; uint32_t total_time = 0;
char str_mac[16] = { 0 }; char str_mac[16] = { 0 };
uint32_t real_time_power = 0;
system_config_t* sys_config; system_config_t* sys_config;
user_config_t* user_config; user_config_t* user_config;
char socket_status[32] = { 0 };
mico_gpio_t Relay[Relay_NUM] = { Relay_0, Relay_1, Relay_2, Relay_3, Relay_4, Relay_5 }; mico_gpio_t Relay[Relay_NUM] = { Relay_0, Relay_1, Relay_2, Relay_3, Relay_4, Relay_5 };
@@ -121,23 +119,10 @@ int application_start(void)
err = UserRtcInit(); err = UserRtcInit();
require_noerr(err, exit); require_noerr(err, exit);
PowerInit(); PowerInit();
//uint32_t power_last = 0xffffffff;
AppHttpdStart(); // start http server thread AppHttpdStart(); // start http server thread
char* power_buf = malloc(128);
if (!power_buf) goto exit;
uint32_t last_p_count = p_count;
while (1) while (1)
{ {
//发送功率数据
real_time_power = 171 * (p_count - last_p_count) / 10;
last_p_count = p_count;
//SetPowerRecord(&power_record, real_time_power);
sprintf(power_buf, "{\"mac\":\"%s\",\"power\":\"%u.%u\",\"total_time\":%u}",
str_mac, (unsigned int)(real_time_power/10), (unsigned int)(real_time_power%10), (unsigned int)total_time);
UserMqttHassPower();
time_t now = time(NULL); time_t now = time(NULL);
if (task_top && now >= task_top->prs_time) if (task_top && now >= task_top->prs_time)
{ {
@@ -146,16 +131,11 @@ int application_start(void)
UserRelaySet(task_top->socket_idx, task_top->on); UserRelaySet(task_top->socket_idx, task_top->on);
DelFirstTask(); DelFirstTask();
} }
else
{
//tc1_log("timed task count[%u]", task_count);
}
mico_thread_msleep(1000); mico_thread_msleep(1000);
} }
exit: exit:
tc1_log("application_start ERROR!"); tc1_log("application_start ERROR!");
if (power_buf) free(power_buf);
return 0; return 0;
} }

View File

@@ -14,7 +14,7 @@
#define wifi_log(M, ...) do { custom_log("WIFI", M, ##__VA_ARGS__); web_log("WIFI", M, ##__VA_ARGS__) } while(0) #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 power_log(M, ...) do { custom_log("POWER", M, ##__VA_ARGS__); web_log("POWER", M, ##__VA_ARGS__) } while(0)
#define VERSION "v1.0.16" #define VERSION "v1.0.17"
#define TYPE 1 #define TYPE 1
#define TYPE_NAME "zTC1" #define TYPE_NAME "zTC1"
@@ -60,10 +60,8 @@ typedef struct
extern char rtc_init; extern char rtc_init;
extern uint32_t total_time; extern uint32_t total_time;
extern char str_mac[16]; extern char str_mac[16];
extern uint32_t real_time_power;
extern system_config_t* sys_config; extern system_config_t* sys_config;
extern user_config_t* user_config; extern user_config_t* user_config;
extern char socket_status[32];
extern mico_gpio_t Relay[Relay_NUM]; extern mico_gpio_t Relay[Relay_NUM];
#endif #endif

View File

@@ -24,6 +24,7 @@
#include "mico.h" #include "mico.h"
#include "MQTTClient.h" #include "MQTTClient.h"
#include "user_gpio.h" #include "user_gpio.h"
#include "user_power.h"
#include "user_mqtt_client.h" #include "user_mqtt_client.h"
typedef struct typedef struct
@@ -507,7 +508,7 @@ void UserMqttHassPower(void)
if (send_buf != NULL && topic_buf != NULL) if (send_buf != NULL && topic_buf != NULL)
{ {
sprintf(topic_buf, "homeassistant/sensor/%s/power/state", str_mac); sprintf(topic_buf, "homeassistant/sensor/%s/power/state", str_mac);
sprintf(send_buf, "{\"power\":\"%d.%d\"}", (int)(real_time_power/10), (int)(real_time_power%10)); sprintf(send_buf, "{\"power\":\"%.3f\"}", real_time_power);
UserMqttSendTopic(topic_buf, send_buf, 0); UserMqttSendTopic(topic_buf, send_buf, 0);
} }
if (send_buf) free(send_buf); if (send_buf) free(send_buf);

View File

@@ -5,6 +5,7 @@
#include "mqtt_server/user_mqtt_client.h" #include "mqtt_server/user_mqtt_client.h"
mico_gpio_t relay[Relay_NUM] = { Relay_0, Relay_1, Relay_2, Relay_3, Relay_4, Relay_5 }; mico_gpio_t relay[Relay_NUM] = { Relay_0, Relay_1, Relay_2, Relay_3, Relay_4, Relay_5 };
char socket_status[32] = { 0 };
void UserLedSet(char x) void UserLedSet(char x)
{ {
@@ -29,7 +30,7 @@ bool RelayOut(void)
return false; return false;
} }
const unsigned char* GetSocketStatus() char* GetSocketStatus()
{ {
sprintf(socket_status, "%d,%d,%d,%d,%d,%d", sprintf(socket_status, "%d,%d,%d,%d,%d,%d",
user_config->socket_status[0], user_config->socket_status[0],
@@ -38,7 +39,7 @@ const unsigned char* GetSocketStatus()
user_config->socket_status[3], user_config->socket_status[3],
user_config->socket_status[4], user_config->socket_status[4],
user_config->socket_status[5]); user_config->socket_status[5]);
return (const unsigned char*)socket_status; return socket_status;
} }
void SetSocketStatus(char* socket_status) void SetSocketStatus(char* socket_status)

View File

@@ -1,17 +1,17 @@
#ifndef __USER_KEY_H_ #ifndef __USER_KEY_H_
#define __USER_KEY_H_ #define __USER_KEY_H_
#include "mico.h" #include "mico.h"
#include "micokit_ext.h" #include "micokit_ext.h"
extern char socket_status[32];
void UserLedSet(char x); void UserLedSet(char x);
void KeyInit(void); void KeyInit(void);
void UserRelaySet(unsigned char x,unsigned char y); void UserRelaySet(unsigned char x,unsigned char y);
void UserRelaySetAll(char y); void UserRelaySetAll(char y);
bool RelayOut(void); bool RelayOut(void);
const unsigned char* GetSocketStatus(); char* GetSocketStatus();
void SetSocketStatus(char* socket_status); void SetSocketStatus(char* socket_status);
#endif #endif

View File

@@ -9,6 +9,7 @@
uint32_t p_count = 0; uint32_t p_count = 0;
PowerRecord power_record = { 1,{ 0 } }; PowerRecord power_record = { 1,{ 0 } };
char power_record_str[1101] = { 0 }; char power_record_str[1101] = { 0 };
float real_time_power = 0;
void SetPowerRecord(PowerRecord* pr, uint32_t pw) void SetPowerRecord(PowerRecord* pr, uint32_t pw)
{ {
@@ -54,14 +55,16 @@ static void PowerIrqHandler(void* arg)
int n = (spend_ns - past_ns % NS) / NS; int n = (spend_ns - past_ns % NS) / NS;
n_1s += (float)(NS - irq_old % NS) / spend_ns; n_1s += (float)(NS - irq_old % NS) / spend_ns;
float power2 = 17.1 * n_1s; real_time_power = 17.1 * n_1s;
SetPowerRecord(&power_record, (int)power2); SetPowerRecord(&power_record, (int)real_time_power);
UserMqttHassPower();
int i = 0; int i = 0;
for (; i < n; i++) for (; i < n; i++)
{ {
power2 = 17.1 * NS / spend_ns; real_time_power = 17.1 * NS / spend_ns;
SetPowerRecord(&power_record, (int)power2); SetPowerRecord(&power_record, (int)real_time_power);
UserMqttHassPower();
} }
irq_old = past_ns; irq_old = past_ns;
n_1s = (float)(past_ns % NS) / spend_ns; n_1s = (float)(past_ns % NS) / spend_ns;

View File

@@ -10,6 +10,7 @@ typedef struct
extern PowerRecord power_record; extern PowerRecord power_record;
extern uint32_t p_count; extern uint32_t p_count;
extern float real_time_power;
char* GetPowerRecord(int idx); char* GetPowerRecord(int idx);
void PowerInit(void); void PowerInit(void);

View File

@@ -2,4 +2,3 @@
1. 登录 1. 登录
2. 删除定时任务 2. 删除定时任务
3. 重复定时任务 3. 重复定时任务
4. 优化HA功率