diff --git a/TC1/main.c b/TC1/main.c index 1a76379..1d9166f 100644 --- a/TC1/main.c +++ b/TC1/main.c @@ -8,7 +8,7 @@ #define os_log(format, ...) custom_log("TC1", format, ##__VA_ARGS__) -char rtc_init=0; //sntp校时成功标志位 +char rtc_init = 0; //sntp校时成功标志位 char strMac[12] = { 0 }; @@ -23,21 +23,39 @@ 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; - userConfigDefault->version=USER_CONFIG_VERSION; - for(i=0;iplug[i].idx=-1; - sprintf( userConfigDefault->plug[i].name, "插座%d",i ); - for(j=0;jmicoSystemConfig.name, ZTC1_NAME,mac1,mac2 ); + user_config_t* userConfigDefault = user_config_data; + + userConfigDefault->mqtt_ip[0] = 0; + userConfigDefault->mqtt_port = 0; + userConfigDefault->mqtt_user[0] = 0; + userConfigDefault->mqtt_password[0] = 0; + + userConfigDefault->idx = -1; + userConfigDefault->version = USER_CONFIG_VERSION; + for ( i = 0; i < PLUG_NUM; i++ ) + { + userConfigDefault->plug[i].idx = -1; + sprintf( userConfigDefault->plug[i].name, "插座%d", i ); + for ( j = 0; j < PLUG_TIME_TASK_NUM; j++ ) { - userConfigDefault->plug[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; - userConfigDefault->plug[i].task[j].action=1; + userConfigDefault->plug[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; + userConfigDefault->plug[i].task[j].action = 1; } } @@ -76,7 +94,7 @@ int application_start( void ) MicoGpioInitialize( (mico_gpio_t) MICO_GPIO_5, OUTPUT_PUSH_PULL ); user_led_set( 0 ); - if (user_config->version!=USER_CONFIG_VERSION || user_config->plug[0].task[0].hour < 0 || user_config->plug[0].task[0].hour > 23 ) + if ( user_config->version != USER_CONFIG_VERSION || 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 ); @@ -97,17 +115,14 @@ int application_start( void ) } } - - wifi_init( ); key_init( ); err = user_mqtt_init( ); require_noerr( err, exit ); - err = user_rtc_init(); + err = user_rtc_init( ); require_noerr( err, exit ); - - err = user_udp_init(); + err = user_udp_init( ); while ( 1 ) { // mico_thread_msleep(500); diff --git a/TC1/main.h b/TC1/main.h index 4e5923b..4d1be07 100644 --- a/TC1/main.h +++ b/TC1/main.h @@ -4,16 +4,18 @@ #include "mico.h" #include "MiCOKit_EXT.h" -#define ZTC1_NAME "zTC1" -#define USER_CONFIG_VERSION 1 +#define TYPE 1 +#define TYPE_NAME "zTC1" +#define ZTC1_NAME "zTC1_%02X%02X" + +#define USER_CONFIG_VERSION 1 +#define SETTING_MQTT_STRING_LENGTH_MAX 32 //必须 4 字节对齐。 #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 @@ -28,10 +30,8 @@ #define Relay_5 MICO_GPIO_18 #define Relay_NUM PLUG_NUM - - - -typedef struct { +typedef struct +{ char hour; //小时 char minute; //分钟 uint8_t repeat; //bit7:一次 bit6-0:周日-周一 @@ -40,7 +40,8 @@ typedef struct { } user_plug_task_config_t; -typedef struct { +typedef struct +{ char name[PLUG_NAME_LENGTH]; char idx; user_plug_task_config_t task[PLUG_TIME_TASK_NUM]; @@ -48,13 +49,19 @@ typedef struct { } user_plug_config_t; //用户保存参数结构体 -typedef struct { +typedef struct +{ + char mqtt_ip[SETTING_MQTT_STRING_LENGTH_MAX]; //mqtt service ip + int mqtt_port; //mqtt service port + char mqtt_user[SETTING_MQTT_STRING_LENGTH_MAX]; //mqtt service user + char mqtt_password[SETTING_MQTT_STRING_LENGTH_MAX]; //mqtt service user +// char mqtt_device_id[SETTING_MQTT_STRING_LENGTH_MAX]; //mqtt service user device name + char version; char idx; user_plug_config_t plug[PLUG_NUM]; } user_config_t; - extern char rtc_init; extern char strMac[12]; diff --git a/TC1/user_function.c b/TC1/user_function.c index 92026fe..ab9ad9a 100644 --- a/TC1/user_function.c +++ b/TC1/user_function.c @@ -12,7 +12,15 @@ typedef struct _user_json_context_t int8_t val; } user_json_context_t; -void user_function_cmd_received(struct sockaddr *addr, uint8_t *pusrdata ) +void user_send( int udp_flag, uint8_t *s ) +{ + if ( udp_flag ) + user_udp_send( s ); //发送数据 + else + user_mqtt_send( "domoticz/in", s ); +} + +void user_function_cmd_received( int udp_flag, uint8_t *pusrdata ) { OSStatus err = kNoErr; char *out; @@ -23,40 +31,25 @@ void user_function_cmd_received(struct sockaddr *addr, uint8_t *pusrdata ) return; } -// //解析device report -// os_printf( "start json:device report\r\n" ); -// cJSON *p_cmd = cJSON_GetObjectItem( pJsonRoot, "cmd" ); -// if ( p_cmd && cJSON_IsString( p_cmd ) && os_strcmp( p_cmd->valuestring, "device report" ) == 0 ) -// { -// -// cJSON *pRoot = cJSON_CreateObject( ); -// cJSON_AddStringToObject( pRoot, "name", mqtt_device_id ); -// cJSON_AddStringToObject( pRoot, "mac", strMac ); -// cJSON_AddNumberToObject( pRoot, "type", TYPE ); -// cJSON_AddStringToObject( pRoot, "type_name", TYPE_NAME ); -// char *s = cJSON_Print( pRoot ); -// os_printf( "pRoot: %s\r\n", s ); -// -// if ( addr ) -// { -// if ( addr && pesp_conn->type == ESPCONN_UDP ) -// { -// pesp_conn->type = ESPCONN_UDP; -// pesp_conn->proto.udp->remote_port = 10181; //获取端口 -// pesp_conn->proto.udp->remote_ip[0] = 255; //获取IP地址 -// pesp_conn->proto.udp->remote_ip[1] = 255; -// pesp_conn->proto.udp->remote_ip[2] = 255; -// pesp_conn->proto.udp->remote_ip[3] = 255; -// } -// espconn_send( pesp_conn, s, os_strlen( s ) ); //发送数据 -// } else -// { -// user_mqtt_send( "domoticz/in", s ); -// } -// cJSON_free( (void *) s ); -// cJSON_Delete( pRoot ); -// // cJSON_Delete(p_cmd); -// } + //解析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 ) + { + + cJSON *pRoot = cJSON_CreateObject( ); + cJSON_AddStringToObject( pRoot, "name", sys_config->micoSystemConfig.name ); + cJSON_AddStringToObject( pRoot, "mac", strMac ); + cJSON_AddNumberToObject( pRoot, "type", TYPE ); + cJSON_AddStringToObject( pRoot, "type_name", TYPE_NAME ); + char *s = cJSON_Print( pRoot ); + os_log( "pRoot: %s\r\n", s ); + + user_send( udp_flag, s ); //发送数据 + free( (void *) s ); + cJSON_Delete( pRoot ); + // cJSON_Delete(p_cmd); + } //解析idx字段int内容 description字段string内容 name字段string内容 cJSON *p_idx = cJSON_GetObjectItem( pJsonRoot, "idx" ); @@ -73,18 +66,32 @@ void user_function_cmd_received(struct sockaddr *addr, uint8_t *pusrdata ) } } - /* - if ( - (p_idx && cJSON_IsNumber( p_idx ) && p_idx->valueint == 2) //idx - || (p_description && cJSON_IsString( p_description ) && strcmp( p_description->valuestring, "123" ) == 0) //description mqttid - || (p_name && cJSON_IsString( p_name ) && strcmp( p_name->valuestring, sys_config->micoSystemConfig.name ) == 0) //name - ) - { - cJSON *p_nvalue = cJSON_GetObjectItem( pJsonRoot, "nvalue" ); - if ( p_nvalue ) led( p_nvalue->valueint ); - } - */ cJSON_Delete( pJsonRoot ); free( out ); } + +unsigned char strtohex( char a, char b ) +{ + if ( a >= 0x30 && a <= 0x39 ) + a -= 0x30; + else if ( a >= 0x41 && a <= 0x46 ) + { + a = a + 10 - 0x41; + } else if ( a >= 0x61 && a <= 0x66 ) + { + a = a + 10 - 0x61; + } + + if ( b >= 0x30 && b <= 0x39 ) + b -= 0x30; + else if ( b >= 0x41 && b <= 0x46 ) + { + b = b + 10 - 0x41; + } else if ( b >= 0x61 && b <= 0x66 ) + { + b = b + 10 - 0x61; + } + + return a * 16 + b; +} diff --git a/TC1/user_function.h b/TC1/user_function.h index 7024c96..04693f1 100644 --- a/TC1/user_function.h +++ b/TC1/user_function.h @@ -6,8 +6,8 @@ #include "mico.h" #include "MiCOKit_EXT.h" -void user_function_cmd_received(struct sockaddr *addr,uint8_t *pusrdata); - +void user_function_cmd_received(int udp_flag,uint8_t *pusrdata); +unsigned char strtohex(char a, char b); #endif diff --git a/TC1/user_mqtt_client.c b/TC1/user_mqtt_client.c index 72345f0..9b651bd 100644 --- a/TC1/user_mqtt_client.c +++ b/TC1/user_mqtt_client.c @@ -33,13 +33,12 @@ * Constants ******************************************************/ -#define MQTT_CLIENT_ID "MQTT_Client_zTC1" -#define MQTT_CLIENT_USERNAME "z" -#define MQTT_CLIENT_PASSWORD "2633063" +#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 "test/mico/test0" // loop msg -#define MQTT_CLIENT_SUB_TOPIC2 "domoticz/out" // loop msg -#define MQTT_CLIENT_PUB_TOPIC "test/mico/test1" +#define MQTT_CLIENT_SUB_TOPIC1 "domoticz/out" // loop msg +#define MQTT_CLIENT_PUB_TOPIC "domoticz/in" #define MQTT_CMD_TIMEOUT 5000 // 5s #define MQTT_YIELD_TMIE 5000 // 5s //#define MQTT_CLIENT_SSL_ENABLE // ssl @@ -74,8 +73,8 @@ REyPOFdGdhBY2P1FNRy0MDr6xr+D2ZOwxs63dG1nnAnWZg7qwoLgpZ4fESPD3PkA\r\n\ #else // ! MQTT_CLIENT_SSL_ENABLE -#define MQTT_SERVER "47.112.16.98" -#define MQTT_SERVER_PORT 1883 +#define MQTT_SERVER user_config->mqtt_ip +#define MQTT_SERVER_PORT user_config->mqtt_port #endif // MQTT_CLIENT_SSL_ENABLE @@ -247,6 +246,8 @@ void mqtt_client_thread( mico_thread_arg_t arg ) LinkStatusTypeDef LinkStatus; while ( 1 ) { + mico_rtos_thread_sleep( 3 ); + if(MQTT_SERVER[0]<0x20 ||MQTT_SERVER[0]>0x7f ||MQTT_SERVER_PORT<1) continue; //未配置mqtt服务器时不连接 micoWlanGetLinkStatus( &LinkStatus ); if ( LinkStatus.is_connected != 1 ) @@ -259,7 +260,7 @@ void mqtt_client_thread( mico_thread_arg_t arg ) 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); - mico_rtos_thread_sleep( 3 ); + } mqtt_log("MQTT network connection success!"); @@ -290,10 +291,6 @@ void mqtt_client_thread( mico_thread_arg_t arg ) require_noerr_string( rc, MQTT_reconnect, "ERROR: MQTT client subscribe err." ); mqtt_log("MQTT client subscribe success! recv_topic=[%s].", MQTT_CLIENT_SUB_TOPIC1); - rc = MQTTSubscribe( &c, MQTT_CLIENT_SUB_TOPIC2, QOS0, messageArrived ); - require_noerr_string( rc, MQTT_reconnect, "ERROR: MQTT client subscribe err." ); - mqtt_log("MQTT client subscribe success! recv_topic=[%s].", MQTT_CLIENT_SUB_TOPIC2); - /* 5. client loop for recv msg && keepalive */ while ( 1 ) { @@ -394,7 +391,7 @@ OSStatus user_recv_handler( void *arg ) require( p_recv_msg, exit ); app_log("user get data success! from_topic=[%s], msg=[%ld][%s].\r\n", p_recv_msg->topic, p_recv_msg->datalen, p_recv_msg->data); - user_function_cmd_received(NULL, p_recv_msg->data ); + user_function_cmd_received(0, p_recv_msg->data ); free( p_recv_msg ); exit: diff --git a/TC1/user_udp.c b/TC1/user_udp.c index 93ea29b..10b2728 100644 --- a/TC1/user_udp.c +++ b/TC1/user_udp.c @@ -1,6 +1,7 @@ #define os_log(format, ...) custom_log("UDP", format, ##__VA_ARGS__) #include "main.h" +#include "user_function.h" #define LOCAL_UDP_PORT 10182 #define REMOTE_UDP_PORT 10181 @@ -42,7 +43,7 @@ void udp_thread( void *arg ) OSStatus err; struct sockaddr_in addr; fd_set readfds; - struct timeval t = { 0, 5000 * 1000 }; //5s + socklen_t addrLen = sizeof(addr); int udp_fd = -1, len; p_udp_send_msg_t p_send_msg = NULL; @@ -87,8 +88,10 @@ void udp_thread( void *arg ) require_action( len >= 0, exit, err = kConnectionErr ); strcpy( ip_address, inet_ntoa( addr.sin_addr ) ); + if(len<1024) buf[len]=0; os_log( "udp recv from %s:%d, len:%d :%s", ip_address,addr.sin_port, len ,buf); - sendto( udp_fd, buf, len, 0, (struct sockaddr *) &addr, sizeof(struct sockaddr_in) ); + user_function_cmd_received(1,buf); +// sendto( udp_fd, buf, len, 0, (struct sockaddr *) &addr, sizeof(struct sockaddr_in) ); } /* recv msg from user worker thread to be sent to server */