Merge branch 'oopuuu:master' into master

This commit is contained in:
wsuow
2025-03-12 08:40:13 +08:00
committed by GitHub
47 changed files with 819 additions and 699 deletions

5
.gitignore vendored
View File

@@ -2,7 +2,4 @@
~*
build
zTC1.*
*.bin
*.rar
TC1/http_server/web/assets/*
*.rar

View File

@@ -52,6 +52,7 @@ TC1 排插硬件分 A1 A2 两个版本, 本固件仅支持 **A1 版本**. A1 A2
- [x] HomeAssistant中增加总耗电量传感器今日耗电量传感器昨日耗电量传感器数据来自于插座历史统计
- [x] 可以设置mqtt数据上报频率默认2秒
- [x] 可以设置电源 led 是否打开,默认打开(系统自检以及错误指示灯仍会工作)
# 编译固件

View File

@@ -427,6 +427,7 @@ static int LedSetEnabled(httpd_request_t *req) {
} else {
UserLedSet(0);
}
UserMqttSendLedState();
mico_system_context_update(sys_config);
send_http("OK", 2, exit, &err);

View File

@@ -795,7 +795,7 @@
HttpPost("/socket", function (re) {
}, sockets_st);
HttpPost("/led", function (re) {
}, checkboxs[0].checked ? 1 : 0);
}, checkboxs[0].checked ? "1" : "0");
});
//Socket-end

File diff suppressed because it is too large Load Diff

View File

@@ -16,6 +16,7 @@
char rtc_init = 0; //sntp校时成功标志位
uint32_t total_time = 0;
char str_mac[16] = {0};
time_t last_check_time = 0;
system_config_t *sys_config;
user_config_t *user_config;
@@ -58,16 +59,31 @@ void recordDailyPCount() {
time_t now;
time(&now);
struct tm *current_time = localtime(&now);
if (current_time->tm_hour != 0 || current_time->tm_min != 0) {
return;
// 判断上次检查的时间与当前时间的日期是否不同
if (last_check_time != 0) {
struct tm *last_check_time_tm = localtime(&last_check_time);
// 如果日期发生变化(即跨天了),则进行记录
if (current_time->tm_year != last_check_time_tm->tm_year ||
current_time->tm_mon != last_check_time_tm->tm_mon ||
current_time->tm_mday != last_check_time_tm->tm_mday) {
// 记录数据
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! p_count_1_day_ago:%d p_count_2_days_ago:%d",
user_config->p_count_1_day_ago, user_config->p_count_2_days_ago);
}
}
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! p_count_1_day_ago:%d p_count_2_days_ago:%d",
user_config->p_count_1_day_ago, user_config->p_count_2_days_ago);
// 更新上次检查时间
last_check_time = now;
}
void schedule_p_count_task(mico_thread_arg_t arg) {

View File

@@ -69,9 +69,12 @@ void UserMqttTimerFunc(void *arg)
}
if (mico_rtos_is_queue_empty(&mqtt_msg_send_queue))
{
timer_status++;
switch (timer_status)
{
case 0:
UserMqttHassAutoLed();
break;
case 1:
case 2:
case 3:
@@ -87,6 +90,7 @@ void UserMqttTimerFunc(void *arg)
mico_stop_timer(&timer_handle);
break;
}
timer_status++;
}
}
@@ -245,6 +249,8 @@ void MqttClientThread(mico_thread_arg_t arg)
mqtt_log("MQTT client connect success!");
UserLedSet(RelayOut() && user_config->power_led_enabled);
/* 4. mqtt client subscribe */
rc = MQTTSubscribe(&c, topic_set, QOS0, MessageArrived);
require_noerr_string(rc, MQTT_reconnect, "ERROR: MQTT client subscribe err.");
@@ -258,6 +264,8 @@ void MqttClientThread(mico_thread_arg_t arg)
UserMqttSendSocketState(i);
}
UserMqttSendLedState();
mico_init_timer(&timer_handle, 150, UserMqttTimerFunc, &arg);
mico_start_timer(&timer_handle);
/* 5. client loop for recv msg && keepalive */
@@ -391,6 +399,19 @@ void ProcessHaCmd(char* cmd)
UserRelaySet(i, on);
UserMqttSendSocketState(i);
mico_system_context_update(sys_config);
}else if(strcmp(cmd, "set led") == ' '){
int on;
sscanf(cmd, "set led %s %d", mac, &on);
if (strcmp(mac, str_mac)) return;
mqtt_log("set led on[%d]", on);
user_config->power_led_enabled = on;
if (RelayOut() && user_config->power_led_enabled) {
UserLedSet(1);
} else {
UserLedSet(0);
}
UserMqttSendLedState();
mico_system_context_update(sys_config);
}
}
@@ -453,6 +474,23 @@ OSStatus UserMqttSendSocketState(char socket_id)
return oss_status;
}
OSStatus UserMqttSendLedState(void)
{
char *send_buf = malloc(64);
char *topic_buf = malloc(64);
OSStatus oss_status = kUnknownErr;
if (send_buf != NULL && topic_buf != NULL)
{
sprintf(topic_buf, "homeassistant/switch/%s/led/state", str_mac);
sprintf(send_buf, "set led %s %d", str_mac, (int)user_config->power_led_enabled);
oss_status = UserMqttSendTopic(topic_buf, send_buf, 1);
}
if (send_buf) free(send_buf);
if (topic_buf) free(topic_buf);
return oss_status;
}
//hass mqtt鑷姩鍙戠幇鏁版嵁寮<E5B581>鍏冲彂閫<E5BD82>
void UserMqttHassAuto(char socket_id)
{
@@ -479,6 +517,33 @@ void UserMqttHassAuto(char socket_id)
if (topic_buf)
free(topic_buf);
}
void UserMqttHassAutoLed(void)
{
char *send_buf = NULL;
char *topic_buf = NULL;
send_buf = (char *) malloc(300);
topic_buf = (char *) malloc(64);
if (send_buf != NULL && topic_buf != NULL)
{
sprintf(topic_buf, "homeassistant/switch/%s/led/config", str_mac);
sprintf(send_buf, "set led %s %d", str_mac, (int)user_config->power_led_enabled);
sprintf(send_buf,
"{\"name\":\"TC1_%s_Led\","
"\"uniq_id\":\"%s_led\","
"\"stat_t\":\"homeassistant/switch/%s/led/state\","
"\"cmd_t\":\"device/ztc1/set\","
"\"pl_on\":\"set led %s 1\","
"\"pl_off\":\"set led %s 0\"}",
str_mac+8, str_mac, str_mac, str_mac,str_mac);
UserMqttSendTopic(topic_buf, send_buf, 1);
}
if (send_buf)
free(send_buf);
if (topic_buf)
free(topic_buf);
}
//hass mqtt鑷姩鍙戠幇鏁版嵁鍔熺巼鍙戦<E98D99><E688A6>
void UserMqttHassAutoPower(void)
{

View File

@@ -29,10 +29,14 @@ extern bool UserMqttIsConnect(void);
extern OSStatus UserMqttSendSocketState(char socket_id);
extern OSStatus UserMqttSendLedState(void);
extern void UserMqttHassAuto(char socket_id);
extern void UserMqttHassPower(void);
extern void UserMqttHassAutoPower(void);
extern void UserMqttHassAutoLed(void);
#endif

View File

@@ -124,7 +124,7 @@ static void WifiLedTimerCallback(void* arg)
case WIFI_STATE_CONNECTED:
UserLedSet(0);
mico_rtos_stop_timer(&wifi_led_timer);
if (RelayOut())
if (RelayOut()&&user_config->power_led_enabled)
UserLedSet(1);
else
UserLedSet(0);

View File

@@ -1,8 +1,11 @@
cd ./TC1/http_server
python ./test.py > web_data.c
cd ../..
mico make clean
mico make TC1@MK3031@moc
mico make TC1@MK3031@moc total
mico make TC1@MK3031@moc download run
mico make TC1@MK3031@moc total download run
cp ./build/TC1\@MK3031\@moc/binary/TC1\@MK3031\@moc.ota.bin ./build/TC1\@MK3031\@moc/binary/ota.bin

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.