在hass mqtt中增加LED的开关

This commit is contained in:
Your Name
2025-03-11 23:26:59 +08:00
parent a493b83c4e
commit ad5f3ecf96
4 changed files with 73 additions and 5 deletions

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

@@ -1,5 +1,5 @@
const unsigned char js_pack[0xd2be] = {
0x1f,0x8b,0x08,0x00,0x67,0x25,0xd0,0x67,0x02,0xff,0xd4,0xbd,0xfb,0x72,0xdb,0x46,
0x1f,0x8b,0x08,0x00,0xa0,0x55,0xd0,0x67,0x02,0xff,0xd4,0xbd,0xfb,0x72,0xdb,0x46,
0xd2,0x38,0xfa,0xff,0x56,0xed,0x3b,0x88,0x48,0x96,0x01,0xc4,0x21,0x45,0xca,0xb1,
0x13,0x83,0x1e,0xb1,0x1c,0xdb,0x49,0xbc,0x5f,0x9c,0x64,0xe3,0x5c,0x97,0x62,0x52,
0xb8,0x92,0xa0,0x48,0x82,0xe2,0xc5,0x12,0x23,0x72,0xdf,0xe7,0xf7,0x1a,0xe7,0xc9,
@@ -3372,7 +3372,7 @@ const unsigned char js_pack[0xd2be] = {
0x02,0x0f,0xb7,0x98,0x0c,0x59,0x7f,0x97,0xf2,0x25,0x55,0x7c,0x45,0xad,0x25,0x8e,
0x0f,0x58,0x4f,0xcc,0x9f,0x00,0x32,0xd2,0xf4,0xa5,0xf0,0xee,0x02,0x00};
const unsigned char css_pack[0x6217] = {
0x1f,0x8b,0x08,0x00,0x67,0x25,0xd0,0x67,0x02,0xff,0xed,0xbd,0x79,0x73,0xdb,0xc8,
0x1f,0x8b,0x08,0x00,0xa0,0x55,0xd0,0x67,0x02,0xff,0xed,0xbd,0x79,0x73,0xdb,0xc8,
0xf5,0x28,0xfa,0x7f,0xaa,0xf2,0x1d,0x70,0x9d,0x9a,0x1a,0x6b,0x42,0xc8,0xd8,0x49,
0x4a,0x95,0x54,0xbc,0x8f,0x26,0x96,0x9c,0x78,0x19,0x8f,0x9d,0x5f,0x9e,0x0b,0x04,
0x9a,0x24,0x24,0x90,0x60,0x00,0x50,0x1b,0x4b,0xb7,0xee,0x07,0xb9,0xef,0xcb,0xbd,
@@ -4943,7 +4943,7 @@ const unsigned char css_pack[0x6217] = {
0xe6,0x54,0x09,0x49,0xbc,0xd0,0xc2,0x1c,0x55,0x23,0x76,0x55,0x21,0xff,0x3f,0x3f,
0xef,0x85,0xce,0x3d,0xbc,0x02,0x00};
const unsigned char web_index_html[0x2c41] = {
0x1f,0x8b,0x08,0x00,0x67,0x25,0xd0,0x67,0x02,0xff,0xed,0x7d,0x6b,0x73,0xdc,0xc6,
0x1f,0x8b,0x08,0x00,0xa0,0x55,0xd0,0x67,0x02,0xff,0xed,0x7d,0x6b,0x73,0xdc,0xc6,
0x95,0xe8,0xf7,0x5b,0xb5,0xff,0xa1,0x0d,0xa7,0x96,0xc3,0xe5,0x00,0x83,0xc7,0x00,
0x33,0x43,0x91,0xdc,0xd2,0xd2,0x4a,0xe4,0x0d,0x15,0x39,0xa6,0xae,0x7c,0x77,0x1d,
0xd7,0x14,0x38,0x83,0xe1,0xc0,0xc2,0x0c,0xc6,0x00,0x86,0x14,0x95,0xa8,0xca,0xca,

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,7 +249,7 @@ void MqttClientThread(mico_thread_arg_t arg)
mqtt_log("MQTT client connect success!");
UserLedSet(user_config->power_led_enabled);
UserLedSet(RelayOut() && user_config->power_led_enabled);
/* 4. mqtt client subscribe */
rc = MQTTSubscribe(&c, topic_set, QOS0, MessageArrived);
@@ -260,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 */
@@ -393,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);
}
}
@@ -455,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)
{
@@ -481,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