mirror of
https://github.com/oopuuu/zTC1.git
synced 2025-12-12 13:08:13 +08:00
fix:多个ztc1加入hass 实体id重复的问题
add:增加当前启动时间(仅app获取,hass无法获取)
This commit is contained in:
@@ -10,8 +10,10 @@
|
|||||||
|
|
||||||
#define os_log(format, ...) custom_log("TC1", format, ##__VA_ARGS__)
|
#define os_log(format, ...) custom_log("TC1", format, ##__VA_ARGS__)
|
||||||
|
|
||||||
char rtc_init = 0; //sntpУʱ<D0A3>ɹ<EFBFBD><C9B9><EFBFBD>־λ
|
|
||||||
|
|
||||||
|
|
||||||
|
char rtc_init = 0; //sntpУʱ<D0A3>ɹ<EFBFBD><C9B9><EFBFBD>־λ
|
||||||
|
uint32_t total_time=0;
|
||||||
char strMac[16] = { 0 };
|
char strMac[16] = { 0 };
|
||||||
uint32_t power=0;
|
uint32_t power=0;
|
||||||
|
|
||||||
|
|||||||
@@ -4,7 +4,7 @@
|
|||||||
#include "mico.h"
|
#include "mico.h"
|
||||||
#include "MiCOKit_EXT.h"
|
#include "MiCOKit_EXT.h"
|
||||||
|
|
||||||
#define VERSION "v0.8"
|
#define VERSION "v0.9"
|
||||||
|
|
||||||
#define TYPE 1
|
#define TYPE 1
|
||||||
#define TYPE_NAME "zTC1"
|
#define TYPE_NAME "zTC1"
|
||||||
@@ -65,6 +65,8 @@ typedef struct
|
|||||||
|
|
||||||
extern char rtc_init;
|
extern char rtc_init;
|
||||||
|
|
||||||
|
|
||||||
|
extern uint32_t total_time;
|
||||||
extern char strMac[16];
|
extern char strMac[16];
|
||||||
extern uint32_t power;
|
extern uint32_t power;
|
||||||
extern system_config_t * sys_config;
|
extern system_config_t * sys_config;
|
||||||
|
|||||||
@@ -18,6 +18,9 @@
|
|||||||
* <h2><center>© COPYRIGHT 2014 MXCHIP Inc.</center></h2>
|
* <h2><center>© COPYRIGHT 2014 MXCHIP Inc.</center></h2>
|
||||||
******************************************************************************
|
******************************************************************************
|
||||||
*/
|
*/
|
||||||
|
#define app_log(M, ...) custom_log("APP", M, ##__VA_ARGS__)
|
||||||
|
#define mqtt_log(M, ...) custom_log("MQTT", M, ##__VA_ARGS__)
|
||||||
|
|
||||||
#include "main.h"
|
#include "main.h"
|
||||||
#include "mico.h"
|
#include "mico.h"
|
||||||
#include "MQTTClient.h"
|
#include "MQTTClient.h"
|
||||||
@@ -26,25 +29,6 @@
|
|||||||
#include "user_mqtt_client.h"
|
#include "user_mqtt_client.h"
|
||||||
#include "cJSON/cJSON.h"
|
#include "cJSON/cJSON.h"
|
||||||
|
|
||||||
/******************************************************
|
|
||||||
* Macros
|
|
||||||
******************************************************/
|
|
||||||
|
|
||||||
#define app_log(M, ...) custom_log("APP", M, ##__VA_ARGS__)
|
|
||||||
#define mqtt_log(M, ...) custom_log("MQTT", M, ##__VA_ARGS__)
|
|
||||||
|
|
||||||
/******************************************************
|
|
||||||
* Constants
|
|
||||||
******************************************************/
|
|
||||||
|
|
||||||
#define MQTT_CLIENT_ID sys_config->micoSystemConfig.name
|
|
||||||
#define MQTT_CLIENT_USERNAME user_config->mqtt_user
|
|
||||||
#define MQTT_CLIENT_PASSWORD user_config->mqtt_password
|
|
||||||
#define MQTT_CLIENT_KEEPALIVE 30
|
|
||||||
#define MQTT_CLIENT_SUB_TOPIC1 "device/ztc1/set" // loop msg
|
|
||||||
#define MQTT_CLIENT_PUB_TOPIC "device/ztc1/%s/state"
|
|
||||||
#define MQTT_CMD_TIMEOUT 5000 // 5s
|
|
||||||
#define MQTT_YIELD_TMIE 5000 // 5s
|
|
||||||
//#define MQTT_CLIENT_SSL_ENABLE // ssl
|
//#define MQTT_CLIENT_SSL_ENABLE // ssl
|
||||||
|
|
||||||
#define MAX_MQTT_TOPIC_SIZE (256)
|
#define MAX_MQTT_TOPIC_SIZE (256)
|
||||||
@@ -82,10 +66,6 @@ REyPOFdGdhBY2P1FNRy0MDr6xr+D2ZOwxs63dG1nnAnWZg7qwoLgpZ4fESPD3PkA\r\n\
|
|||||||
|
|
||||||
#endif // MQTT_CLIENT_SSL_ENABLE
|
#endif // MQTT_CLIENT_SSL_ENABLE
|
||||||
|
|
||||||
/******************************************************
|
|
||||||
* Structures
|
|
||||||
******************************************************/
|
|
||||||
|
|
||||||
typedef struct
|
typedef struct
|
||||||
{
|
{
|
||||||
char topic[MAX_MQTT_TOPIC_SIZE];
|
char topic[MAX_MQTT_TOPIC_SIZE];
|
||||||
@@ -96,26 +76,16 @@ typedef struct
|
|||||||
uint32_t datalen;
|
uint32_t datalen;
|
||||||
} mqtt_recv_msg_t, *p_mqtt_recv_msg_t, mqtt_send_msg_t, *p_mqtt_send_msg_t;
|
} mqtt_recv_msg_t, *p_mqtt_recv_msg_t, mqtt_send_msg_t, *p_mqtt_send_msg_t;
|
||||||
|
|
||||||
/******************************************************
|
|
||||||
* Function Declarations
|
|
||||||
******************************************************/
|
|
||||||
|
|
||||||
static void mqtt_client_thread( mico_thread_arg_t arg );
|
static void mqtt_client_thread( mico_thread_arg_t arg );
|
||||||
static void messageArrived( MessageData* md );
|
static void messageArrived( MessageData* md );
|
||||||
static OSStatus mqtt_msg_publish( Client *c, const char* topic, char qos, char retained,
|
static OSStatus mqtt_msg_publish( Client *c, const char* topic, char qos, char retained, const unsigned char* msg, uint32_t msg_len );
|
||||||
const unsigned char* msg,
|
|
||||||
uint32_t msg_len );
|
|
||||||
|
|
||||||
OSStatus user_send_handler( void *arg );
|
|
||||||
OSStatus user_recv_handler( void *arg );
|
OSStatus user_recv_handler( void *arg );
|
||||||
|
|
||||||
OSStatus user_mqtt_send_plug_state( char plug_id );
|
OSStatus user_mqtt_send_plug_state( char plug_id );
|
||||||
void user_mqtt_hass_auto( char plug_id );
|
void user_mqtt_hass_auto( char plug_id );
|
||||||
void user_mqtt_hass_auto_power( void );
|
void user_mqtt_hass_auto_power( void );
|
||||||
|
|
||||||
/******************************************************
|
|
||||||
* Variables Definitions
|
|
||||||
******************************************************/
|
|
||||||
bool isconnect = false;
|
bool isconnect = false;
|
||||||
mico_queue_t mqtt_msg_send_queue = NULL;
|
mico_queue_t mqtt_msg_send_queue = NULL;
|
||||||
|
|
||||||
@@ -127,17 +97,83 @@ static mico_timed_event_t mqtt_client_send_event;
|
|||||||
|
|
||||||
char topic_state[MAX_MQTT_TOPIC_SIZE];
|
char topic_state[MAX_MQTT_TOPIC_SIZE];
|
||||||
char topic_set[MAX_MQTT_TOPIC_SIZE];
|
char topic_set[MAX_MQTT_TOPIC_SIZE];
|
||||||
char hass_mqtt_flag;
|
|
||||||
/******************************************************
|
mico_timer_t timer_handle;
|
||||||
* Function Definitions
|
static uint8_t status = 0;
|
||||||
******************************************************/
|
void user_mqtt_timer_func( void *arg )
|
||||||
|
{
|
||||||
|
uint8_t *buf1 = NULL;
|
||||||
|
status++;
|
||||||
|
switch ( status )
|
||||||
|
{
|
||||||
|
case 1:
|
||||||
|
user_mqtt_hass_auto_power( );
|
||||||
|
break;
|
||||||
|
case 2:
|
||||||
|
user_mqtt_hass_auto( 0 );
|
||||||
|
break;
|
||||||
|
case 3:
|
||||||
|
user_mqtt_hass_auto( 1 );
|
||||||
|
break;
|
||||||
|
case 4:
|
||||||
|
user_mqtt_hass_auto( 2 );
|
||||||
|
break;
|
||||||
|
case 5:
|
||||||
|
user_mqtt_hass_auto( 3 );
|
||||||
|
break;
|
||||||
|
case 6:
|
||||||
|
user_mqtt_hass_auto( 4 );
|
||||||
|
break;
|
||||||
|
case 7:
|
||||||
|
user_mqtt_hass_auto( 5 );
|
||||||
|
break;
|
||||||
|
case 8:
|
||||||
|
user_mqtt_hass_auto_name( 0 );
|
||||||
|
break;
|
||||||
|
case 9:
|
||||||
|
user_mqtt_hass_auto_name( 1 );
|
||||||
|
break;
|
||||||
|
case 10:
|
||||||
|
user_mqtt_hass_auto_name( 2 );
|
||||||
|
break;
|
||||||
|
case 11:
|
||||||
|
user_mqtt_hass_auto_name( 3 );
|
||||||
|
break;
|
||||||
|
case 12:
|
||||||
|
user_mqtt_hass_auto_name( 4 );
|
||||||
|
break;
|
||||||
|
case 13:
|
||||||
|
user_mqtt_hass_auto_name( 5 );
|
||||||
|
break;
|
||||||
|
case 14:
|
||||||
|
user_mqtt_hass_auto_power_name( );
|
||||||
|
break;
|
||||||
|
case 15:
|
||||||
|
|
||||||
|
buf1 = malloc( 1024 ); //idx为1位时长度为24
|
||||||
|
if ( buf1 != NULL )
|
||||||
|
{
|
||||||
|
sprintf(
|
||||||
|
buf1,
|
||||||
|
"{\"mac\":\"%s\",\"version\":null,\"plug_0\":{\"on\":null,\"setting\":{\"name\":null}},\"plug_1\":{\"on\":null,\"setting\":{\"name\":null}},\"plug_2\":{\"on\":null,\"setting\":{\"name\":null}},\"plug_3\":{\"on\":null,\"setting\":{\"name\":null}},\"plug_4\":{\"on\":null,\"setting\":{\"name\":null}},\"plug_5\":{\"on\":null,\"setting\":{\"name\":null}}}",
|
||||||
|
strMac );
|
||||||
|
user_function_cmd_received( 0, buf1 );
|
||||||
|
free( buf1 );
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
mico_stop_timer( &timer_handle );
|
||||||
|
// mico_deinit_timer( &timer_handle );
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/* Application entrance */
|
/* Application entrance */
|
||||||
OSStatus user_mqtt_init( void )
|
OSStatus user_mqtt_init( void )
|
||||||
{
|
{
|
||||||
OSStatus err = kNoErr;
|
OSStatus err = kNoErr;
|
||||||
|
|
||||||
sprintf( topic_set, MQTT_CLIENT_SUB_TOPIC1, strMac );
|
sprintf( topic_set, MQTT_CLIENT_SUB_TOPIC1 );
|
||||||
sprintf( topic_state, MQTT_CLIENT_PUB_TOPIC, strMac );
|
sprintf( topic_state, MQTT_CLIENT_PUB_TOPIC, strMac );
|
||||||
|
|
||||||
#ifdef MQTT_CLIENT_SSL_ENABLE
|
#ifdef MQTT_CLIENT_SSL_ENABLE
|
||||||
@@ -165,9 +201,6 @@ OSStatus user_mqtt_init( void )
|
|||||||
err = mico_rtos_create_worker_thread( &mqtt_client_worker_thread, MICO_APPLICATION_PRIORITY, 0x800, 5 );
|
err = mico_rtos_create_worker_thread( &mqtt_client_worker_thread, MICO_APPLICATION_PRIORITY, 0x800, 5 );
|
||||||
require_noerr_string( err, exit, "ERROR: Unable to start the mqtt client worker thread." );
|
require_noerr_string( err, exit, "ERROR: Unable to start the mqtt client worker thread." );
|
||||||
|
|
||||||
// /* Trigger a period send event */
|
|
||||||
// mico_rtos_register_timed_event( &mqtt_client_send_event, &mqtt_client_worker_thread, user_send_handler, 2000, NULL );
|
|
||||||
|
|
||||||
exit:
|
exit:
|
||||||
if ( kNoErr != err ) app_log("ERROR, app thread exit err: %d", err);
|
if ( kNoErr != err ) app_log("ERROR, app thread exit err: %d", err);
|
||||||
return err;
|
return err;
|
||||||
@@ -280,7 +313,6 @@ void mqtt_client_thread( mico_thread_arg_t arg )
|
|||||||
mqtt_log("ERROR: MQTT network connection err=%d, reconnect after 3s...", rc);
|
mqtt_log("ERROR: MQTT network connection err=%d, reconnect after 3s...", rc);
|
||||||
|
|
||||||
}
|
}
|
||||||
hass_mqtt_flag = PLUG_NUM;
|
|
||||||
mqtt_log("MQTT network connection success!");
|
mqtt_log("MQTT network connection success!");
|
||||||
|
|
||||||
/* 2. init mqtt client */
|
/* 2. init mqtt client */
|
||||||
@@ -293,9 +325,9 @@ void mqtt_client_thread( mico_thread_arg_t arg )
|
|||||||
/* 3. create mqtt client connection */
|
/* 3. create mqtt client connection */
|
||||||
connectData.willFlag = 0;
|
connectData.willFlag = 0;
|
||||||
connectData.MQTTVersion = 4; // 3: 3.1, 4: v3.1.1
|
connectData.MQTTVersion = 4; // 3: 3.1, 4: v3.1.1
|
||||||
connectData.clientID.cstring = MQTT_CLIENT_ID;
|
connectData.clientID.cstring = strMac;
|
||||||
connectData.username.cstring = MQTT_CLIENT_USERNAME;
|
connectData.username.cstring = user_config->mqtt_user;
|
||||||
connectData.password.cstring = MQTT_CLIENT_PASSWORD;
|
connectData.password.cstring = user_config->mqtt_password;
|
||||||
connectData.keepAliveInterval = MQTT_CLIENT_KEEPALIVE;
|
connectData.keepAliveInterval = MQTT_CLIENT_KEEPALIVE;
|
||||||
connectData.cleansession = 1;
|
connectData.cleansession = 1;
|
||||||
|
|
||||||
@@ -310,18 +342,9 @@ void mqtt_client_thread( mico_thread_arg_t arg )
|
|||||||
mqtt_log("MQTT client subscribe success! recv_topic=[%s].", topic_set);
|
mqtt_log("MQTT client subscribe success! recv_topic=[%s].", topic_set);
|
||||||
/*4.1 连接成功后先更新发送一次数据*/
|
/*4.1 连接成功后先更新发送一次数据*/
|
||||||
isconnect = true;
|
isconnect = true;
|
||||||
uint8_t *buf1 = NULL;
|
|
||||||
buf1 = malloc( 1024 ); //idx为1位时长度为24
|
|
||||||
if ( buf1 != NULL )
|
|
||||||
{
|
|
||||||
sprintf(
|
|
||||||
buf1,
|
|
||||||
"{\"mac\":\"%s\",\"version\":null,\"plug_0\":{\"on\":null,\"setting\":{\"name\":null}},\"plug_1\":{\"on\":null,\"setting\":{\"name\":null}},\"plug_2\":{\"on\":null,\"setting\":{\"name\":null}},\"plug_3\":{\"on\":null,\"setting\":{\"name\":null}},\"plug_4\":{\"on\":null,\"setting\":{\"name\":null}},\"plug_5\":{\"on\":null,\"setting\":{\"name\":null}}}",
|
|
||||||
strMac );
|
|
||||||
user_function_cmd_received( 0, buf1 );
|
|
||||||
free( buf1 );
|
|
||||||
}
|
|
||||||
|
|
||||||
|
mico_init_timer( &timer_handle, 150, user_mqtt_timer_func, &arg );
|
||||||
|
mico_start_timer( &timer_handle );
|
||||||
/* 5. client loop for recv msg && keepalive */
|
/* 5. client loop for recv msg && keepalive */
|
||||||
while ( 1 )
|
while ( 1 )
|
||||||
{
|
{
|
||||||
@@ -340,13 +363,6 @@ void mqtt_client_thread( mico_thread_arg_t arg )
|
|||||||
no_mqtt_msg_exchange = false;
|
no_mqtt_msg_exchange = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if ( hass_mqtt_flag > 0 )
|
|
||||||
{
|
|
||||||
if ( hass_mqtt_flag == PLUG_NUM ) user_mqtt_hass_auto_power( );
|
|
||||||
hass_mqtt_flag--;
|
|
||||||
user_mqtt_hass_auto( hass_mqtt_flag );
|
|
||||||
user_mqtt_send_plug_state( hass_mqtt_flag );
|
|
||||||
}
|
|
||||||
/* recv msg from user worker thread to be sent to server */
|
/* recv msg from user worker thread to be sent to server */
|
||||||
if ( FD_ISSET( msg_send_event_fd, &readfds ) )
|
if ( FD_ISSET( msg_send_event_fd, &readfds ) )
|
||||||
{
|
{
|
||||||
@@ -379,7 +395,15 @@ void mqtt_client_thread( mico_thread_arg_t arg )
|
|||||||
}
|
}
|
||||||
|
|
||||||
MQTT_reconnect:
|
MQTT_reconnect:
|
||||||
|
|
||||||
mqtt_log("Disconnect MQTT client, and reconnect after 5s, reason: mqtt_rc = %d, err = %d", rc, err );
|
mqtt_log("Disconnect MQTT client, and reconnect after 5s, reason: mqtt_rc = %d, err = %d", rc, err );
|
||||||
|
|
||||||
|
if (&timer_handle !=NULL && mico_rtos_is_timer_running( &timer_handle ) )
|
||||||
|
{
|
||||||
|
mico_stop_timer( &timer_handle );
|
||||||
|
// mico_deinit_timer( &timer_handle );
|
||||||
|
}
|
||||||
|
|
||||||
mqtt_client_release( &c, &n );
|
mqtt_client_release( &c, &n );
|
||||||
isconnect = false;
|
isconnect = false;
|
||||||
user_led_set( -1 );
|
user_led_set( -1 );
|
||||||
@@ -512,27 +536,44 @@ void user_mqtt_hass_auto( char plug_id )
|
|||||||
if ( send_buf != NULL && topic_buf != NULL )
|
if ( send_buf != NULL && topic_buf != NULL )
|
||||||
{
|
{
|
||||||
sprintf( topic_buf, "homeassistant/switch/%s/plug_%d/config", strMac, plug_id );
|
sprintf( topic_buf, "homeassistant/switch/%s/plug_%d/config", strMac, plug_id );
|
||||||
sprintf( send_buf,
|
sprintf( send_buf, "{"
|
||||||
"{"
|
"\"name\":\"zTC1_plug%d_%s\","
|
||||||
"\"name\":\"%s\","
|
"\"stat_t\":\"homeassistant/switch/%s/plug_%d/state\","
|
||||||
"\"state_topic\":\"homeassistant/switch/%s/plug_%d/state\","
|
"\"cmd_t\":\"device/ztc1/set\","
|
||||||
"\"command_topic\":\"device/ztc1/set\","
|
"\"pl_on\":\"{\\\"mac\\\":\\\"%s\\\",\\\"plug_%d\\\":{\\\"on\\\":1}}\","
|
||||||
"\"payload_on\":\"{\\\"mac\\\":\\\"%s\\\",\\\"plug_%d\\\":{\\\"on\\\":1}}\","
|
"\"pl_off\":\"{\\\"mac\\\":\\\"%s\\\",\\\"plug_%d\\\":{\\\"on\\\":0}}\""
|
||||||
"\"payload_off\":\"{\\\"mac\\\":\\\"%s\\\",\\\"plug_%d\\\":{\\\"on\\\":0}}\""
|
"}\0",
|
||||||
"}",
|
plug_id, strMac + 8, strMac, plug_id, strMac, plug_id, strMac, plug_id );
|
||||||
user_config->plug[plug_id].name,
|
|
||||||
strMac,
|
|
||||||
plug_id,
|
|
||||||
strMac,
|
|
||||||
plug_id,
|
|
||||||
strMac,
|
|
||||||
plug_id );
|
|
||||||
user_mqtt_send_topic( topic_buf, send_buf, 1 );
|
user_mqtt_send_topic( topic_buf, send_buf, 1 );
|
||||||
}
|
}
|
||||||
if ( send_buf ) free( send_buf );
|
if ( send_buf ) free( send_buf );
|
||||||
if ( topic_buf ) free( topic_buf );
|
if ( topic_buf ) free( topic_buf );
|
||||||
|
|
||||||
}
|
}
|
||||||
|
void user_mqtt_hass_auto_name( char plug_id )
|
||||||
|
{
|
||||||
|
uint8_t *send_buf = NULL;
|
||||||
|
uint8_t *topic_buf = NULL;
|
||||||
|
send_buf = (uint8_t *) malloc( 300 );
|
||||||
|
topic_buf = (uint8_t *) malloc( 64 );
|
||||||
|
if ( send_buf != NULL && topic_buf != NULL )
|
||||||
|
{
|
||||||
|
sprintf( topic_buf, "homeassistant/switch/%s/plug_%d/config", strMac, plug_id );
|
||||||
|
sprintf( send_buf, "{"
|
||||||
|
"\"name\":\"%s\","
|
||||||
|
"\"stat_t\":\"homeassistant/switch/%s/plug_%d/state\","
|
||||||
|
"\"cmd_t\":\"device/ztc1/set\","
|
||||||
|
"\"pl_on\":\"{\\\"mac\\\":\\\"%s\\\",\\\"plug_%d\\\":{\\\"on\\\":1}}\","
|
||||||
|
"\"pl_off\":\"{\\\"mac\\\":\\\"%s\\\",\\\"plug_%d\\\":{\\\"on\\\":0}}\""
|
||||||
|
"}\0",
|
||||||
|
user_config->plug[plug_id].name, strMac, plug_id, strMac, plug_id, strMac, plug_id );
|
||||||
|
user_mqtt_send_topic( topic_buf, send_buf, 0 );
|
||||||
|
}
|
||||||
|
if ( send_buf )
|
||||||
|
free( send_buf );
|
||||||
|
if ( topic_buf )
|
||||||
|
free( topic_buf );
|
||||||
|
}
|
||||||
//hass mqtt自动发现数据功率发送
|
//hass mqtt自动发现数据功率发送
|
||||||
void user_mqtt_hass_auto_power( void )
|
void user_mqtt_hass_auto_power( void )
|
||||||
{
|
{
|
||||||
@@ -544,19 +585,49 @@ void user_mqtt_hass_auto_power( void )
|
|||||||
if ( send_buf != NULL && topic_buf != NULL )
|
if ( send_buf != NULL && topic_buf != NULL )
|
||||||
{
|
{
|
||||||
sprintf( topic_buf, "homeassistant/sensor/%s/power/config", strMac );
|
sprintf( topic_buf, "homeassistant/sensor/%s/power/config", strMac );
|
||||||
sprintf( send_buf,
|
sprintf( send_buf, "{"
|
||||||
"{"
|
"\"name\":\"zTC1_power_%s\","
|
||||||
"\"name\":\"功率\","
|
"\"state_topic\":\"homeassistant/sensor/%s/power/state\","
|
||||||
|
"\"unit_of_measurement\":\"W\","
|
||||||
|
"\"icon\":\"mdi:gauge\","
|
||||||
|
"\"value_template\":\"{{ value_json.power }}\""
|
||||||
|
"}",
|
||||||
|
strMac + 8, strMac );
|
||||||
|
|
||||||
|
user_mqtt_send_topic( topic_buf, send_buf, 1 );
|
||||||
|
}
|
||||||
|
if ( send_buf ) free( send_buf );
|
||||||
|
if ( topic_buf ) free( topic_buf );
|
||||||
|
}
|
||||||
|
void user_mqtt_hass_auto_power_name( void )
|
||||||
|
{
|
||||||
|
uint8_t *send_buf = NULL;
|
||||||
|
uint8_t *topic_buf = NULL;
|
||||||
|
send_buf = (uint8_t *) malloc( 300 ); //
|
||||||
|
topic_buf = (uint8_t *) malloc( 64 ); //
|
||||||
|
if ( send_buf != NULL && topic_buf != NULL )
|
||||||
|
{
|
||||||
|
sprintf( topic_buf, "homeassistant/sensor/%s/power/config", strMac );
|
||||||
|
sprintf( send_buf, "{"
|
||||||
|
"\"name\":\"zTC1xxxxxx\","
|
||||||
"\"state_topic\":\"homeassistant/sensor/%s/power/state\","
|
"\"state_topic\":\"homeassistant/sensor/%s/power/state\","
|
||||||
"\"unit_of_measurement\":\"W\","
|
"\"unit_of_measurement\":\"W\","
|
||||||
"\"icon\":\"mdi:gauge\","
|
"\"icon\":\"mdi:gauge\","
|
||||||
"\"value_template\":\"{{ value_json.power }}\""
|
"\"value_template\":\"{{ value_json.power }}\""
|
||||||
"}",
|
"}",
|
||||||
strMac );
|
strMac );
|
||||||
|
send_buf[13] = 0xe5;
|
||||||
|
send_buf[14] = 0x8a;
|
||||||
|
send_buf[15] = 0x9f;
|
||||||
|
send_buf[16] = 0xe7;
|
||||||
|
send_buf[17] = 0x8e;
|
||||||
|
send_buf[18] = 0x87;
|
||||||
user_mqtt_send_topic( topic_buf, send_buf, 1 );
|
user_mqtt_send_topic( topic_buf, send_buf, 1 );
|
||||||
}
|
}
|
||||||
if ( send_buf ) free( send_buf );
|
if ( send_buf )
|
||||||
if ( topic_buf ) free( topic_buf );
|
free( send_buf );
|
||||||
|
if ( topic_buf )
|
||||||
|
free( topic_buf );
|
||||||
}
|
}
|
||||||
|
|
||||||
void user_mqtt_hass_power( void )
|
void user_mqtt_hass_power( void )
|
||||||
|
|||||||
@@ -4,6 +4,11 @@
|
|||||||
|
|
||||||
#include "mico.h"
|
#include "mico.h"
|
||||||
|
|
||||||
|
#define MQTT_CLIENT_KEEPALIVE 30
|
||||||
|
#define MQTT_CLIENT_SUB_TOPIC1 "device/ztc1/set"
|
||||||
|
#define MQTT_CLIENT_PUB_TOPIC "device/ztc1/%s/state"
|
||||||
|
#define MQTT_CMD_TIMEOUT 5000 // 5s
|
||||||
|
#define MQTT_YIELD_TMIE 5000 // 5s
|
||||||
|
|
||||||
|
|
||||||
extern OSStatus user_mqtt_init(void);
|
extern OSStatus user_mqtt_init(void);
|
||||||
@@ -11,5 +16,8 @@ extern OSStatus user_mqtt_send( char *arg );
|
|||||||
extern bool user_mqtt_isconnect(void);
|
extern bool user_mqtt_isconnect(void);
|
||||||
extern OSStatus user_mqtt_send_plug_state( char plug_id );
|
extern OSStatus user_mqtt_send_plug_state( char plug_id );
|
||||||
extern void user_mqtt_hass_auto( char plug_id );
|
extern void user_mqtt_hass_auto( char plug_id );
|
||||||
|
extern void user_mqtt_hass_auto_name(char plug_id);
|
||||||
extern void user_mqtt_hass_power( void );
|
extern void user_mqtt_hass_power( void );
|
||||||
|
extern void user_mqtt_hass_auto_power( void );
|
||||||
|
extern void user_mqtt_hass_auto_power_name(void);
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@@ -117,7 +117,7 @@ void rtc_thread( mico_thread_arg_t arg )
|
|||||||
uint32_t power_last = 0xffffffff;
|
uint32_t power_last = 0xffffffff;
|
||||||
|
|
||||||
mico_utc_time_t utc_time;
|
mico_utc_time_t utc_time;
|
||||||
|
mico_utc_time_t utc_time_last;
|
||||||
while ( 1 )
|
while ( 1 )
|
||||||
{ //<2F>ϵ<EFBFBD><CFB5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>wifi<66>ſ<EFBFBD>ʼ<EFBFBD><CABC>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD><EFBFBD>ȴ<EFBFBD><C8B4><EFBFBD><EFBFBD><EFBFBD>
|
{ //<2F>ϵ<EFBFBD><CFB5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>wifi<66>ſ<EFBFBD>ʼ<EFBFBD><CABC>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD><EFBFBD>ȴ<EFBFBD><C8B4><EFBFBD><EFBFBD><EFBFBD>
|
||||||
micoWlanGetLinkStatus( &LinkStatus );
|
micoWlanGetLinkStatus( &LinkStatus );
|
||||||
@@ -138,6 +138,14 @@ void rtc_thread( mico_thread_arg_t arg )
|
|||||||
{
|
{
|
||||||
mico_time_get_utc_time( &utc_time );
|
mico_time_get_utc_time( &utc_time );
|
||||||
utc_time += 28800;
|
utc_time += 28800;
|
||||||
|
|
||||||
|
if(utc_time_last!=utc_time)
|
||||||
|
{
|
||||||
|
utc_time_last==utc_time;
|
||||||
|
total_time++;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
struct tm * currentTime = localtime( (const time_t *) &utc_time );
|
struct tm * currentTime = localtime( (const time_t *) &utc_time );
|
||||||
rtc_time.sec = currentTime->tm_sec;
|
rtc_time.sec = currentTime->tm_sec;
|
||||||
rtc_time.min = currentTime->tm_min;
|
rtc_time.min = currentTime->tm_min;
|
||||||
@@ -255,7 +263,7 @@ void rtc_thread( mico_thread_arg_t arg )
|
|||||||
power_buf = malloc( 128 ); //
|
power_buf = malloc( 128 ); //
|
||||||
if ( power_buf != NULL )
|
if ( power_buf != NULL )
|
||||||
{
|
{
|
||||||
sprintf( power_buf, "{\"mac\":\"%s\",\"power\":\"%d.%d\"}", strMac, power/10,power%10 );
|
sprintf( power_buf, "{\"mac\":\"%s\",\"power\":\"%d.%d\",\"total_time\":%d}", strMac, power/10,power%10,total_time );
|
||||||
user_send( 0, power_buf );
|
user_send( 0, power_buf );
|
||||||
free( power_buf );
|
free( power_buf );
|
||||||
}
|
}
|
||||||
|
|||||||
Binary file not shown.
Binary file not shown.
Reference in New Issue
Block a user