mirror of
https://github.com/oopuuu/zTC1.git
synced 2025-12-12 13:08:13 +08:00
优化HA功率
This commit is contained in:
@@ -105,7 +105,7 @@ exit:
|
||||
|
||||
static int HttpGetTc1Status(httpd_request_t *req)
|
||||
{
|
||||
const unsigned char* sockets = GetSocketStatus();
|
||||
char* sockets = GetSocketStatus();
|
||||
char* tc1_status = malloc(412);
|
||||
sprintf(tc1_status, TC1_STATUS_JSON, sockets, ip_status.mode,
|
||||
sys_config->micoSystemConfig.ssid, sys_config->micoSystemConfig.user_key,
|
||||
|
||||
20
TC1/main.c
20
TC1/main.c
@@ -11,11 +11,9 @@
|
||||
char rtc_init = 0; //sntp校时成功标志位
|
||||
uint32_t total_time = 0;
|
||||
char str_mac[16] = { 0 };
|
||||
uint32_t real_time_power = 0;
|
||||
|
||||
system_config_t* sys_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 };
|
||||
|
||||
@@ -121,23 +119,10 @@ int application_start(void)
|
||||
err = UserRtcInit();
|
||||
require_noerr(err, exit);
|
||||
PowerInit();
|
||||
|
||||
//uint32_t power_last = 0xffffffff;
|
||||
AppHttpdStart(); // start http server thread
|
||||
char* power_buf = malloc(128);
|
||||
if (!power_buf) goto exit;
|
||||
|
||||
uint32_t last_p_count = p_count;
|
||||
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);
|
||||
if (task_top && now >= task_top->prs_time)
|
||||
{
|
||||
@@ -146,16 +131,11 @@ int application_start(void)
|
||||
UserRelaySet(task_top->socket_idx, task_top->on);
|
||||
DelFirstTask();
|
||||
}
|
||||
else
|
||||
{
|
||||
//tc1_log("timed task count[%u]", task_count);
|
||||
}
|
||||
mico_thread_msleep(1000);
|
||||
}
|
||||
|
||||
exit:
|
||||
tc1_log("application_start ERROR!");
|
||||
if (power_buf) free(power_buf);
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
@@ -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.16"
|
||||
#define VERSION "v1.0.17"
|
||||
|
||||
#define TYPE 1
|
||||
#define TYPE_NAME "zTC1"
|
||||
@@ -60,10 +60,8 @@ typedef struct
|
||||
extern char rtc_init;
|
||||
extern uint32_t total_time;
|
||||
extern char str_mac[16];
|
||||
extern uint32_t real_time_power;
|
||||
extern system_config_t* sys_config;
|
||||
extern user_config_t* user_config;
|
||||
extern char socket_status[32];
|
||||
extern mico_gpio_t Relay[Relay_NUM];
|
||||
|
||||
#endif
|
||||
|
||||
@@ -24,6 +24,7 @@
|
||||
#include "mico.h"
|
||||
#include "MQTTClient.h"
|
||||
#include "user_gpio.h"
|
||||
#include "user_power.h"
|
||||
#include "user_mqtt_client.h"
|
||||
|
||||
typedef struct
|
||||
@@ -507,7 +508,7 @@ void UserMqttHassPower(void)
|
||||
if (send_buf != NULL && topic_buf != NULL)
|
||||
{
|
||||
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);
|
||||
}
|
||||
if (send_buf) free(send_buf);
|
||||
|
||||
@@ -5,6 +5,7 @@
|
||||
#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 };
|
||||
char socket_status[32] = { 0 };
|
||||
|
||||
void UserLedSet(char x)
|
||||
{
|
||||
@@ -29,7 +30,7 @@ bool RelayOut(void)
|
||||
return false;
|
||||
}
|
||||
|
||||
const unsigned char* GetSocketStatus()
|
||||
char* GetSocketStatus()
|
||||
{
|
||||
sprintf(socket_status, "%d,%d,%d,%d,%d,%d",
|
||||
user_config->socket_status[0],
|
||||
@@ -38,7 +39,7 @@ const unsigned char* GetSocketStatus()
|
||||
user_config->socket_status[3],
|
||||
user_config->socket_status[4],
|
||||
user_config->socket_status[5]);
|
||||
return (const unsigned char*)socket_status;
|
||||
return socket_status;
|
||||
}
|
||||
|
||||
void SetSocketStatus(char* socket_status)
|
||||
|
||||
@@ -1,17 +1,17 @@
|
||||
|
||||
#ifndef __USER_KEY_H_
|
||||
#define __USER_KEY_H_
|
||||
|
||||
|
||||
#include "mico.h"
|
||||
#include "micokit_ext.h"
|
||||
|
||||
extern char socket_status[32];
|
||||
|
||||
void UserLedSet(char x);
|
||||
void KeyInit(void);
|
||||
void UserRelaySet(unsigned char x,unsigned char y);
|
||||
void UserRelaySetAll(char y);
|
||||
bool RelayOut(void);
|
||||
const unsigned char* GetSocketStatus();
|
||||
char* GetSocketStatus();
|
||||
void SetSocketStatus(char* socket_status);
|
||||
|
||||
#endif
|
||||
|
||||
@@ -9,6 +9,7 @@
|
||||
uint32_t p_count = 0;
|
||||
PowerRecord power_record = { 1,{ 0 } };
|
||||
char power_record_str[1101] = { 0 };
|
||||
float real_time_power = 0;
|
||||
|
||||
void SetPowerRecord(PowerRecord* pr, uint32_t pw)
|
||||
{
|
||||
@@ -54,14 +55,16 @@ static void PowerIrqHandler(void* arg)
|
||||
|
||||
int n = (spend_ns - past_ns % NS) / NS;
|
||||
n_1s += (float)(NS - irq_old % NS) / spend_ns;
|
||||
float power2 = 17.1 * n_1s;
|
||||
SetPowerRecord(&power_record, (int)power2);
|
||||
real_time_power = 17.1 * n_1s;
|
||||
SetPowerRecord(&power_record, (int)real_time_power);
|
||||
UserMqttHassPower();
|
||||
|
||||
int i = 0;
|
||||
for (; i < n; i++)
|
||||
{
|
||||
power2 = 17.1 * NS / spend_ns;
|
||||
SetPowerRecord(&power_record, (int)power2);
|
||||
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;
|
||||
|
||||
@@ -10,6 +10,7 @@ typedef struct
|
||||
|
||||
extern PowerRecord power_record;
|
||||
extern uint32_t p_count;
|
||||
extern float real_time_power;
|
||||
|
||||
char* GetPowerRecord(int idx);
|
||||
void PowerInit(void);
|
||||
|
||||
Reference in New Issue
Block a user