diff --git a/TC1/main.c b/TC1/main.c index 29328c1..877cef1 100644 --- a/TC1/main.c +++ b/TC1/main.c @@ -11,9 +11,34 @@ user_config_t * user_config; mico_gpio_t Relay[Relay_NUM] = { Relay_0, Relay_1, Relay_2, Relay_3, Relay_4, Relay_5 }; +/* MICO system callback: Restore default configuration provided by application */ +void appRestoreDefault_callback( void * const user_config_data, uint32_t size ) +{ + int i, j; + UNUSED_PARAMETER( size ); + + sprintf( mico_system_context_get( )->micoSystemConfig.name, ZTC1_NAME ); + user_config_t* userConfigDefault = user_config_data; + userConfigDefault->idx = -1; + + for(i=0;iplug[i].idx=-1; + sprintf( userConfigDefault->plug[i].name, "插座%d",i ); + for(j=0;jplug[i].task[j].hour=0; + userConfigDefault->plug[i].task[j].minute=0; + userConfigDefault->plug[i].task[j].repeat=0x80; + userConfigDefault->plug[i].task[j].on=0; + } + } + +} + int application_start( void ) { - + int i, j; os_log( "Start" ); OSStatus err = kNoErr; @@ -22,12 +47,25 @@ int application_start( void ) sys_config = mico_system_context_init( sizeof(user_config_t) ); user_config = ((system_context_t *) sys_config)->user_config_data; require_action( user_config, exit, err = kNoMemoryErr ); - os_log( "user config:%d",user_config->val ); + err = mico_system_init( sys_config ); require_noerr( err, exit ); + os_log( "idx:%d",user_config->idx ); + for ( i = 0; i < PLUG_NUM; i++ ) + { + os_log("plug_%d:",i); + os_log("\tname:%s:",user_config->plug[i].name); + os_log("\tidx:%d:",user_config->plug[i].idx); + for ( j = 0; j < PLUG_TIME_TASK_NUM; j++ ) + { + os_log("\t\ton:%d\t %02d:%02d repeat:0x%X",user_config->plug[i].task[j].on, + user_config->plug[i].task[j].hour,user_config->plug[i].task[j].minute, + user_config->plug[i].task[j].repeat); + } + } - for ( int i = 0; i < Relay_NUM; i++ ) + for ( i = 0; i < Relay_NUM; i++ ) { MicoGpioInitialize( Relay[i], OUTPUT_PUSH_PULL ); //MicoGpioOutputHigh(Relay[i]); @@ -43,10 +81,17 @@ int application_start( void ) MicoGpioInitialize( (mico_gpio_t) MICO_GPIO_5, OUTPUT_PUSH_PULL ); led( 0 ); + if ( user_config->plug[0].task[0].hour < 0 || user_config->plug[0].task[0].hour > 23 ) + { + os_log( "WARNGIN: user params restored!" ); + err = mico_system_context_restore( sys_config ); + require_noerr( err, exit ); + } + wifi_init( ); key_init( ); - user_mqtt_init(); -// wifi_start_easylink(); + user_mqtt_init( ); + while ( 1 ) { // mico_thread_msleep(500); diff --git a/TC1/main.h b/TC1/main.h index 9db1f43..2d6c35b 100644 --- a/TC1/main.h +++ b/TC1/main.h @@ -4,6 +4,14 @@ #include "mico.h" #include "MiCOKit_EXT.h" +#define ZTC1_NAME "zTC1" + +#define PLUG_NAME_LENGTH 32 +#define PLUG_NUM 6 //插座数量 +#define PLUG_TIME_TASK_NUM 5 //每个插座最多5组定时任务 + + + #define Led MICO_GPIO_5 #define Button MICO_GPIO_23 @@ -13,12 +21,30 @@ #define Relay_3 MICO_GPIO_9 #define Relay_4 MICO_GPIO_10 #define Relay_5 MICO_GPIO_18 -#define Relay_NUM 6 +#define Relay_NUM PLUG_NUM + + +typedef struct { + char hour; //小时 + char minute; //分钟 + uint8_t repeat; //bit7:一次 bit6-0:周日-周一 + char on; //开关 + +} user_plug_task_config_t; + +typedef struct { + char name[PLUG_NAME_LENGTH]; + char idx; + user_plug_task_config_t task[PLUG_TIME_TASK_NUM]; + +} user_plug_config_t; + //用户保存参数结构体 typedef struct { - char val; + char idx; + user_plug_config_t plug[PLUG_NUM]; } user_config_t; extern system_config_t * sys_config; diff --git a/TC1/user_mqtt_client.c b/TC1/user_mqtt_client.c index d2f3531..b5f2423 100644 --- a/TC1/user_mqtt_client.c +++ b/TC1/user_mqtt_client.c @@ -240,8 +240,16 @@ MQTT_start: #else ssl_settings.ssl_enable = false; #endif - + LinkStatusTypeDef LinkStatus; while( 1 ){ + + micoWlanGetLinkStatus(&LinkStatus); + if(LinkStatus.is_connected!=1){ + mqtt_log("ERROR:WIFI not connection , waiting 3s for connecting and then connecting MQTT ", rc); + mico_rtos_thread_sleep( 3 ); + continue; + } + rc = NewNetwork( &n, MQTT_SERVER, MQTT_SERVER_PORT, ssl_settings ); if( rc == MQTT_SUCCESS ) break; mqtt_log("ERROR: MQTT network connection err=%d, reconnect after 3s...", rc);