mirror of
https://github.com/oopuuu/zTC1.git
synced 2025-12-12 21:18:13 +08:00
add:功率功能
add:尝试增加ota进度功能
This commit is contained in:
@@ -13,6 +13,7 @@
|
|||||||
char rtc_init = 0; //sntpУʱ<D0A3>ɹ<EFBFBD><C9B9><EFBFBD>־λ
|
char rtc_init = 0; //sntpУʱ<D0A3>ɹ<EFBFBD><C9B9><EFBFBD>־λ
|
||||||
|
|
||||||
char strMac[16] = { 0 };
|
char strMac[16] = { 0 };
|
||||||
|
uint32_t power=0;
|
||||||
|
|
||||||
system_config_t * sys_config;
|
system_config_t * sys_config;
|
||||||
user_config_t * user_config;
|
user_config_t * user_config;
|
||||||
@@ -151,7 +152,7 @@ int application_start( void )
|
|||||||
require_noerr( err, exit );
|
require_noerr( err, exit );
|
||||||
err = user_rtc_init( );
|
err = user_rtc_init( );
|
||||||
require_noerr( err, exit );
|
require_noerr( err, exit );
|
||||||
// user_power_init();
|
user_power_init();
|
||||||
while ( 1 )
|
while ( 1 )
|
||||||
{
|
{
|
||||||
// mico_thread_msleep(500);
|
// mico_thread_msleep(500);
|
||||||
|
|||||||
@@ -4,7 +4,7 @@
|
|||||||
#include "mico.h"
|
#include "mico.h"
|
||||||
#include "MiCOKit_EXT.h"
|
#include "MiCOKit_EXT.h"
|
||||||
|
|
||||||
#define VERSION "v0.6"
|
#define VERSION "v0.7"
|
||||||
|
|
||||||
#define TYPE 1
|
#define TYPE 1
|
||||||
#define TYPE_NAME "zTC1"
|
#define TYPE_NAME "zTC1"
|
||||||
@@ -66,6 +66,7 @@ typedef struct
|
|||||||
extern char rtc_init;
|
extern char rtc_init;
|
||||||
|
|
||||||
extern char strMac[16];
|
extern char strMac[16];
|
||||||
|
extern uint32_t 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;
|
||||||
|
|
||||||
|
|||||||
@@ -111,6 +111,7 @@ 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 );
|
||||||
|
|
||||||
/******************************************************
|
/******************************************************
|
||||||
* Variables Definitions
|
* Variables Definitions
|
||||||
@@ -264,7 +265,7 @@ void mqtt_client_thread( mico_thread_arg_t arg )
|
|||||||
{
|
{
|
||||||
isconnect = false;
|
isconnect = false;
|
||||||
mico_rtos_thread_sleep( 3 );
|
mico_rtos_thread_sleep( 3 );
|
||||||
if ( MQTT_SERVER[0] < 0x20 || MQTT_SERVER[0] > 0x7f || MQTT_SERVER_PORT < 1 ) continue; //δ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>mqtt<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
if ( MQTT_SERVER[0] < 0x20 || MQTT_SERVER[0] > 0x7f || MQTT_SERVER_PORT < 1 ) continue; //未配置mqtt服务器时不连接
|
||||||
|
|
||||||
micoWlanGetLinkStatus( &LinkStatus );
|
micoWlanGetLinkStatus( &LinkStatus );
|
||||||
if ( LinkStatus.is_connected != 1 )
|
if ( LinkStatus.is_connected != 1 )
|
||||||
@@ -307,10 +308,10 @@ void mqtt_client_thread( mico_thread_arg_t arg )
|
|||||||
rc = MQTTSubscribe( &c, topic_set, QOS0, messageArrived );
|
rc = MQTTSubscribe( &c, topic_set, QOS0, messageArrived );
|
||||||
require_noerr_string( rc, MQTT_reconnect, "ERROR: MQTT client subscribe err." );
|
require_noerr_string( rc, MQTT_reconnect, "ERROR: MQTT client subscribe err." );
|
||||||
mqtt_log("MQTT client subscribe success! recv_topic=[%s].", topic_set);
|
mqtt_log("MQTT client subscribe success! recv_topic=[%s].", topic_set);
|
||||||
/*4.1 <EFBFBD><EFBFBD><EFBFBD>ӳɹ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ȸ<EFBFBD><EFBFBD>·<EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>*/
|
/*4.1 连接成功后先更新发送一次数据*/
|
||||||
isconnect = true;
|
isconnect = true;
|
||||||
uint8_t *buf1 = NULL;
|
uint8_t *buf1 = NULL;
|
||||||
buf1 = malloc( 1024 ); //idxΪ1λʱ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϊ24
|
buf1 = malloc( 1024 ); //idx为1位时长度为24
|
||||||
if ( buf1 != NULL )
|
if ( buf1 != NULL )
|
||||||
{
|
{
|
||||||
sprintf(
|
sprintf(
|
||||||
@@ -339,11 +340,12 @@ 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 > 0 )
|
||||||
{
|
{
|
||||||
hass_mqtt_flag --;
|
if ( hass_mqtt_flag == PLUG_NUM ) user_mqtt_hass_auto_power( );
|
||||||
user_mqtt_hass_auto(hass_mqtt_flag);
|
hass_mqtt_flag--;
|
||||||
user_mqtt_send_plug_state(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 ) )
|
||||||
@@ -481,7 +483,7 @@ OSStatus user_mqtt_send( char *arg )
|
|||||||
return user_mqtt_send_topic( topic_state, arg, 0 );
|
return user_mqtt_send_topic( topic_state, arg, 0 );
|
||||||
}
|
}
|
||||||
|
|
||||||
//<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ha<EFBFBD><EFBFBD><EFBFBD><EFBFBD>״̬
|
//更新ha开关状态
|
||||||
OSStatus user_mqtt_send_plug_state( char plug_id )
|
OSStatus user_mqtt_send_plug_state( char plug_id )
|
||||||
{
|
{
|
||||||
|
|
||||||
@@ -499,7 +501,7 @@ OSStatus user_mqtt_send_plug_state( char plug_id )
|
|||||||
if ( topic_buf ) free( topic_buf );
|
if ( topic_buf ) free( topic_buf );
|
||||||
}
|
}
|
||||||
|
|
||||||
//hass mqtt<EFBFBD>Զ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݷ<EFBFBD><EFBFBD><EFBFBD>
|
//hass mqtt自动发现数据开关发送
|
||||||
void user_mqtt_hass_auto( char plug_id )
|
void user_mqtt_hass_auto( char plug_id )
|
||||||
{
|
{
|
||||||
uint8_t i;
|
uint8_t i;
|
||||||
@@ -519,15 +521,60 @@ void user_mqtt_hass_auto( char plug_id )
|
|||||||
"\"payload_off\":\"{\\\"mac\\\":\\\"%s\\\",\\\"plug_%d\\\":{\\\"on\\\":0}}\""
|
"\"payload_off\":\"{\\\"mac\\\":\\\"%s\\\",\\\"plug_%d\\\":{\\\"on\\\":0}}\""
|
||||||
"}",
|
"}",
|
||||||
user_config->plug[plug_id].name,
|
user_config->plug[plug_id].name,
|
||||||
strMac, plug_id,
|
strMac,
|
||||||
strMac, plug_id,
|
plug_id,
|
||||||
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 );
|
||||||
|
|
||||||
}
|
}
|
||||||
|
//hass mqtt自动发现数据功率发送
|
||||||
|
void user_mqtt_hass_auto_power( void )
|
||||||
|
{
|
||||||
|
uint8_t i;
|
||||||
|
uint8_t *send_buf = NULL;
|
||||||
|
uint8_t *topic_buf = NULL;
|
||||||
|
send_buf = malloc( 512 ); //
|
||||||
|
topic_buf = malloc( 128 ); //
|
||||||
|
if ( send_buf != NULL && topic_buf != NULL )
|
||||||
|
{
|
||||||
|
sprintf( topic_buf, "homeassistant/sensor/%s/power/config", strMac );
|
||||||
|
sprintf( send_buf,
|
||||||
|
"{"
|
||||||
|
"\"name\":\"功率\","
|
||||||
|
"\"state_topic\":\"homeassistant/sensor/%s/power/state\","
|
||||||
|
"\"unit_of_measurement\":\"W\","
|
||||||
|
"\"icon\":\"mdi:gauge\","
|
||||||
|
"\"value_template\":\"{{ value_json.power }}\""
|
||||||
|
"}",
|
||||||
|
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_power( void )
|
||||||
|
{
|
||||||
|
uint8_t i;
|
||||||
|
uint8_t *send_buf = NULL;
|
||||||
|
uint8_t *topic_buf = NULL;
|
||||||
|
send_buf = malloc( 512 ); //
|
||||||
|
topic_buf = malloc( 128 ); //
|
||||||
|
if ( send_buf != NULL && topic_buf != NULL )
|
||||||
|
{
|
||||||
|
sprintf( topic_buf, "homeassistant/sensor/%s/power/state", strMac );
|
||||||
|
sprintf( send_buf, "{\"power\":\"%d.%d\"}", power/10,power%10 );
|
||||||
|
user_mqtt_send_topic( topic_buf, send_buf, 0 );
|
||||||
|
}
|
||||||
|
if ( send_buf ) free( send_buf );
|
||||||
|
if ( topic_buf ) free( topic_buf );
|
||||||
|
}
|
||||||
|
|
||||||
bool user_mqtt_isconnect( )
|
bool user_mqtt_isconnect( )
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -11,4 +11,5 @@ 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_power( void );
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@@ -14,8 +14,8 @@ static void ota_server_status_handler( OTA_STATE_E state, float progress )
|
|||||||
{
|
{
|
||||||
case OTA_LOADING:
|
case OTA_LOADING:
|
||||||
os_log("ota server is loading, progress %.2f%%", progress);
|
os_log("ota server is loading, progress %.2f%%", progress);
|
||||||
// if ( (int) progress == progress )
|
if ( ((int) progress)%10 == 1 )
|
||||||
// sprintf( str, "{\"mac\":\"%s\",\"ota_progress\":%d}", strMac,(int) progress );
|
sprintf( str, "{\"mac\":\"%s\",\"ota_progress\":%d}", strMac,((int) progress) );
|
||||||
break;
|
break;
|
||||||
case OTA_SUCCE:
|
case OTA_SUCCE:
|
||||||
os_log("ota server daemons success");
|
os_log("ota server daemons success");
|
||||||
|
|||||||
@@ -17,11 +17,14 @@ static void power_timer_handler( void* arg )
|
|||||||
{
|
{
|
||||||
|
|
||||||
// uint8_t pin_input = MicoGpioInputGet( POWER );
|
// uint8_t pin_input = MicoGpioInputGet( POWER );
|
||||||
|
|
||||||
if ( timer_count_last != timer_count )
|
if ( timer_count_last != timer_count )
|
||||||
{
|
{
|
||||||
os_log("power_irq_handler:%u-%u=%u",timer_count,timer_count_last,timer);
|
// os_log("power_irq_handler:%u-%u=%u",timer_count,timer_count_last,timer);
|
||||||
timer_count_last = timer_count;
|
timer_count_last = timer_count;
|
||||||
|
|
||||||
|
timer=timer/1000;
|
||||||
|
power=9500000/timer;
|
||||||
|
os_log("power_irq_handler:%u,%07u",power,timer);
|
||||||
}
|
}
|
||||||
// if(timer_count==0) os_log("power_timer_handler Hight:%d",timer_count_last);
|
// if(timer_count==0) os_log("power_timer_handler Hight:%d",timer_count_last);
|
||||||
// timer_count++;
|
// timer_count++;
|
||||||
|
|||||||
@@ -114,6 +114,7 @@ void rtc_thread( mico_thread_arg_t arg )
|
|||||||
OSStatus err = kUnknownErr;
|
OSStatus err = kUnknownErr;
|
||||||
LinkStatusTypeDef LinkStatus;
|
LinkStatusTypeDef LinkStatus;
|
||||||
mico_rtc_time_t rtc_time;
|
mico_rtc_time_t rtc_time;
|
||||||
|
uint32_t power_last = 0xffffffff;
|
||||||
|
|
||||||
mico_utc_time_t utc_time;
|
mico_utc_time_t utc_time;
|
||||||
|
|
||||||
@@ -171,7 +172,7 @@ void rtc_thread( mico_thread_arg_t arg )
|
|||||||
{
|
{
|
||||||
user_relay_set( i, user_config->plug[i].task[j].action );
|
user_relay_set( i, user_config->plug[i].task[j].action );
|
||||||
update_user_config_flag = 1;
|
update_user_config_flag = 1;
|
||||||
user_mqtt_send_plug_state(i);
|
user_mqtt_send_plug_state( i );
|
||||||
}
|
}
|
||||||
if ( repeat == 0x00 )
|
if ( repeat == 0x00 )
|
||||||
{
|
{
|
||||||
@@ -218,13 +219,13 @@ void rtc_thread( mico_thread_arg_t arg )
|
|||||||
|
|
||||||
cJSON_AddItemToObject( json_send_plug, "setting", json_send_plug_setting );
|
cJSON_AddItemToObject( json_send_plug, "setting", json_send_plug_setting );
|
||||||
|
|
||||||
task_flag[i]=-1;
|
task_flag[i] = -1;
|
||||||
}
|
}
|
||||||
cJSON_AddItemToObject( json_send, strTemp1, json_send_plug );
|
cJSON_AddItemToObject( json_send, strTemp1, json_send_plug );
|
||||||
}
|
}
|
||||||
|
|
||||||
char *json_str = cJSON_Print( json_send );
|
char *json_str = cJSON_Print( json_send );
|
||||||
user_send(false,json_str);//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
user_send( false, json_str ); //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||||
|
|
||||||
free( json_str );
|
free( json_str );
|
||||||
cJSON_Delete( json_send );
|
cJSON_Delete( json_send );
|
||||||
@@ -245,6 +246,22 @@ void rtc_thread( mico_thread_arg_t arg )
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//<2F><><EFBFBD><EFBFBD><CDB9><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||||
|
if ( power_last != power )
|
||||||
|
{
|
||||||
|
power_last = power;
|
||||||
|
|
||||||
|
uint8_t *power_buf = NULL;
|
||||||
|
power_buf = malloc( 128 ); //
|
||||||
|
if ( power_buf != NULL )
|
||||||
|
{
|
||||||
|
sprintf( power_buf, "{\"mac\":\"%s\",\"power\":\"%d.%d\"}", strMac, power/10,power%10 );
|
||||||
|
user_send( 0, power_buf );
|
||||||
|
free( power_buf );
|
||||||
|
}
|
||||||
|
user_mqtt_hass_power();
|
||||||
|
}
|
||||||
|
|
||||||
mico_rtos_thread_msleep( 900 );
|
mico_rtos_thread_msleep( 900 );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Binary file not shown.
Binary file not shown.
Reference in New Issue
Block a user