diff --git a/TC1/main.c b/TC1/main.c index 1d9166f..228c527 100644 --- a/TC1/main.c +++ b/TC1/main.c @@ -10,7 +10,7 @@ char rtc_init = 0; //sntp校时成功标志位 -char strMac[12] = { 0 }; +char strMac[16] = { 0 }; system_config_t * sys_config; user_config_t * user_config; @@ -78,6 +78,7 @@ int application_start( void ) err = mico_system_init( sys_config ); require_noerr( err, exit ); + for ( i = 0; i < Relay_NUM; i++ ) { MicoGpioInitialize( Relay[i], OUTPUT_PUSH_PULL ); @@ -100,20 +101,26 @@ int application_start( void ) err = mico_system_context_restore( sys_config ); require_noerr( err, exit ); } + + os_log( "mqtt_ip:%s",user_config->mqtt_ip ); + os_log( "mqtt_port:%d",user_config->mqtt_port ); + os_log( "mqtt_user:%s",user_config->mqtt_user ); + os_log( "mqtt_password:%s",user_config->mqtt_password ); + os_log( "version:%d",user_config->version ); 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 ( 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); +// } +// } wifi_init( ); key_init( ); diff --git a/TC1/main.h b/TC1/main.h index 4d1be07..a3895b6 100644 --- a/TC1/main.h +++ b/TC1/main.h @@ -9,7 +9,7 @@ #define ZTC1_NAME "zTC1_%02X%02X" -#define USER_CONFIG_VERSION 1 +#define USER_CONFIG_VERSION 2 #define SETTING_MQTT_STRING_LENGTH_MAX 32 //必须 4 字节对齐。 #define PLUG_NAME_LENGTH 32 @@ -64,7 +64,7 @@ typedef struct extern char rtc_init; -extern char strMac[12]; +extern char strMac[16]; extern system_config_t * sys_config; extern user_config_t * user_config; diff --git a/TC1/user_function.c b/TC1/user_function.c index ab9ad9a..91470a4 100644 --- a/TC1/user_function.c +++ b/TC1/user_function.c @@ -17,13 +17,13 @@ void user_send( int udp_flag, uint8_t *s ) if ( udp_flag ) user_udp_send( s ); //发送数据 else - user_mqtt_send( "domoticz/in", s ); + user_mqtt_send( s ); } void user_function_cmd_received( int udp_flag, uint8_t *pusrdata ) { OSStatus err = kNoErr; - char *out; + cJSON * pJsonRoot = cJSON_Parse( pusrdata ); if ( !pJsonRoot ) { @@ -32,7 +32,6 @@ void user_function_cmd_received( int udp_flag, uint8_t *pusrdata ) } //解析device report - os_log( "start json:device report\r\n" ); cJSON *p_cmd = cJSON_GetObjectItem( pJsonRoot, "cmd" ); if ( p_cmd && cJSON_IsString( p_cmd ) && strcmp( p_cmd->valuestring, "device report" ) == 0 ) { @@ -51,23 +50,161 @@ void user_function_cmd_received( int udp_flag, uint8_t *pusrdata ) // cJSON_Delete(p_cmd); } - //解析idx字段int内容 description字段string内容 name字段string内容 + //解析 cJSON *p_idx = cJSON_GetObjectItem( pJsonRoot, "idx" ); cJSON *p_description = cJSON_GetObjectItem( pJsonRoot, "description" ); cJSON *p_name = cJSON_GetObjectItem( pJsonRoot, "name" ); + cJSON *p_mac = cJSON_GetObjectItem( pJsonRoot, "mac" ); - if ( p_idx && cJSON_IsNumber( p_idx ) && (p_idx->valueint >= 3 && p_idx->valueint <= 9) ) //idx + // + if ( (p_idx && cJSON_IsNumber( p_idx ) && p_idx->valueint == user_config->idx) //idx + || (p_description && cJSON_IsString( p_description ) && strcmp( p_description->valuestring, sys_config->micoSystemConfig.name ) == 0) //p_description name + || (p_name && cJSON_IsString( p_name ) && strcmp( p_name->valuestring, sys_config->micoSystemConfig.name ) == 0) //name + || (p_mac && cJSON_IsString( p_mac ) && strcmp( p_mac->valuestring, strMac ) == 0) //mac + ) { + // os_log("device enter\r\n"); + cJSON *json_send = cJSON_CreateObject( ); + cJSON_AddStringToObject( json_send, "mac", strMac ); + cJSON *p_nvalue = cJSON_GetObjectItem( pJsonRoot, "nvalue" ); - if ( p_nvalue ) +// if ( p_nvalue && cJSON_IsNumber( p_nvalue ) ) +// { +//// uint32 now_time = system_get_time( ); +//// os_log( "system_get_time:%d,%d = %09d\r\n", last_time, now_time, now_time - last_time ); +//// if ( now_time - last_time < 1500000 && p_idx && p_nvalue->valueint == user_rudder_get_direction( ) ) +//// { +//// return_flag = false; +//// } else +//// { +//// user_rudder_press( p_nvalue->valueint ); +//// } +//// user_json_set_last_time( ); +// } +// +// if ( p_nvalue ) +// { +// cJSON_AddNumberToObject( json_send, "nvalue", user_rudder_get_direction( ) ); +// } else +// last_time = 0; + + cJSON *p_setting = cJSON_GetObjectItem( pJsonRoot, "setting" ); + if ( p_setting ) { - user_led_set( p_nvalue->valueint ); - user_relay_set( p_idx->valueint - 3, p_nvalue->valueint ); + cJSON *json_setting_send = cJSON_CreateObject( ); + //设置设备名称/deviceid + cJSON *p_setting_name = cJSON_GetObjectItem( p_setting, "name" ); + if ( p_setting_name && cJSON_IsString( p_setting_name ) ) + { + sprintf( sys_config->micoSystemConfig.name, p_setting_name->valuestring ); + } + + //设置mqtt ip + cJSON *p_mqtt_ip = cJSON_GetObjectItem( p_setting, "mqtt_uri" ); + if ( p_mqtt_ip && cJSON_IsString( p_mqtt_ip ) ) + { + sprintf( user_config->mqtt_ip, p_mqtt_ip->valuestring ); + } + + //设置mqtt port + cJSON *p_mqtt_port = cJSON_GetObjectItem( p_setting, "mqtt_port" ); + if ( p_mqtt_port && cJSON_IsNumber( p_mqtt_port ) ) + { + user_config->mqtt_port = p_mqtt_port->valueint; + } + + //设置mqtt user + cJSON *p_mqtt_user = cJSON_GetObjectItem( p_setting, "mqtt_user" ); + if ( p_mqtt_user && cJSON_IsString( p_mqtt_user ) ) + { + sprintf( user_config->mqtt_user, p_mqtt_user->valuestring ); + } + + //设置mqtt password + cJSON *p_mqtt_password = cJSON_GetObjectItem( p_setting, "mqtt_password" ); + if ( p_mqtt_password && cJSON_IsString( p_mqtt_password ) ) + { + sprintf( user_config->mqtt_password, p_mqtt_password->valuestring ); + } + + //设置domoticz idx + cJSON *p_setting_idx = cJSON_GetObjectItem( p_setting, "idx" ); + if ( p_setting_idx && cJSON_IsNumber( p_setting_idx ) ) + { + user_config->idx = p_setting_idx->valueint; + os_log( "idx:%d",user_config->idx ); + mico_system_context_update( sys_config ); + } + + if ( p_setting_name || p_mqtt_ip || p_mqtt_port || p_mqtt_user || p_mqtt_password || p_setting_idx ) + { + os_log( "mico_system_context_update" ); + err = mico_system_context_update( sys_config ); + require_noerr( err, exit ); + } + + //开发返回数据 + //设置设备名称/deviceid + if ( p_setting_name ) + { + cJSON_AddStringToObject( json_setting_send, "name", sys_config->micoSystemConfig.name ); + } + + //设置mqtt ip + if ( p_mqtt_ip ) + { + cJSON_AddStringToObject( json_setting_send, "mqtt_uri", user_config->mqtt_ip ); + } + + //设置mqtt port + if ( p_mqtt_port ) + { + cJSON_AddNumberToObject( json_setting_send, "mqtt_port", user_config->mqtt_port ); + } + + //设置mqtt user + if ( p_mqtt_user ) + { + cJSON_AddStringToObject( json_setting_send, "mqtt_user", user_config->mqtt_user ); + } + + //设置mqtt password + if ( p_mqtt_password ) + { + cJSON_AddStringToObject( json_setting_send, "mqtt_password", user_config->mqtt_password ); + } + + //设置domoticz idx + if ( p_setting_idx ) + { + cJSON_AddNumberToObject( json_setting_send, "idx", user_config->idx ); + } + + cJSON_AddItemToObject( json_send, "setting", json_setting_send ); } + + cJSON_AddStringToObject( json_send, "name", sys_config->micoSystemConfig.name ); + + //if (p_idx) + if ( user_config->idx >= 0 ) + cJSON_AddNumberToObject( json_send, "idx", user_config->idx ); + + //if ( return_flag == true ) + { + char *json_str = cJSON_Print( json_send ); + os_log( "pRoot: %s\r\n", json_str ); + user_send( udp_flag, json_str ); //发送数据 + free( (void *) json_str ); + } + cJSON_Delete( json_send ); } cJSON_Delete( pJsonRoot ); - free( out ); + return; + exit: + os_log("user_function_cmd_received ERROR:0x%x",err); + + cJSON_Delete( pJsonRoot ); } diff --git a/TC1/user_wifi.c b/TC1/user_wifi.c index 5c7aeba..00712e7 100644 --- a/TC1/user_wifi.c +++ b/TC1/user_wifi.c @@ -34,7 +34,7 @@ void wifi_start_easylink( ) micoWlanStartEasyLink( 20000 ); user_led_set( 1 ); } -uint32_t ip=0xd248912c; +uint32_t ip = 0xd248912c; //easylink 完成回调 void wifi_easylink_completed_handle( network_InitTypeDef_st *nwkpara, void * arg ) { @@ -67,9 +67,8 @@ void wifi_easylink_completed_handle( network_InitTypeDef_st *nwkpara, void * arg //wifi已连接获取到IP地址 回调 static void wifi_get_ip_callback( IPStatusTypedef *pnet, void * arg ) { - os_log("got IP:%s, MAC:%s", pnet->ip,pnet->mac); + os_log("got IP:%s", pnet->ip); wifi_status = WIFI_STATE_CONNECTED; - strcpy(strMac, pnet->mac); } //wifi连接状态改变回调 static void wifi_status_callback( WiFiEvent status, void *arg ) @@ -101,10 +100,10 @@ static void wifi_led_timer_callback( void* arg ) case WIFI_STATE_CONNECTING: //if ( num > 1 ) - { - num = 0; - user_led_set( -1 ); - } + { + num = 0; + user_led_set( -1 ); + } break; case WIFI_STATE_NOEASYLINK: wifi_start_easylink( ); @@ -144,5 +143,9 @@ void wifi_init( void ) //启动定时器开始进行wifi连接 if ( !mico_rtos_is_timer_running( &wifi_led_timer ) ) mico_rtos_start_timer( &wifi_led_timer ); + IPStatusTypedef para; + micoWlanGetIPStatus( ¶, Station ); + strcpy( strMac, para.mac ); + }