Skip to content

Commit

Permalink
V1.0 局域网控制
Browse files Browse the repository at this point in the history
  • Loading branch information
linlyv committed May 1, 2019
1 parent e1b72b2 commit 914047e
Show file tree
Hide file tree
Showing 5 changed files with 146 additions and 158 deletions.
5 changes: 2 additions & 3 deletions app_main.c
Original file line number Diff line number Diff line change
Expand Up @@ -410,11 +410,10 @@ int USER_FUNC app_main (void)

hfgpio_fset_out_high(LED);
key_init();
rtctime_init();
rtctime_init();
user_udp_init( );
hfnet_get_mac_address(strMac);
u_printf("local mac:%s\n", strMac);

u_printf("local mac:%s\n", strMac);
hfgpio_fset_out_low(LED);
while(!hfnet_wifi_is_active())
{
Expand Down
12 changes: 6 additions & 6 deletions user_function.c
Original file line number Diff line number Diff line change
Expand Up @@ -101,9 +101,9 @@ void USER_FUNC user_function_cmd_received( int udp_flag, char *pusrdata )
if ( p_setting_name && cJSON_IsString( p_setting_name ) )
{
update_user_config_flag = true;
u_printf("u_config.plug.name:%s\n",u_config.plug[1].name);
//u_printf("u_config.plug.name:%s\n",u_config.plug[1].name);
sprintf( deviceid, p_setting_name->valuestring );
u_printf("p_setting_name->valuestring:%s\n",deviceid);
//u_printf("p_setting_name->valuestring:%s\n",deviceid);
//
}

Expand All @@ -128,15 +128,15 @@ void USER_FUNC user_function_cmd_received( int udp_flag, char *pusrdata )
if ( p_mqtt_user && cJSON_IsString( p_mqtt_user ) )
{
update_user_config_flag = true;
sprintf( u_config.mqtt_user, p_mqtt_user->valuestring );
sprintf( u_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 ) )
{
update_user_config_flag = true;
sprintf( u_config.mqtt_password, p_mqtt_password->valuestring );
sprintf( u_config.mqtt_password, p_mqtt_password->valuestring );
}


Expand Down Expand Up @@ -171,7 +171,7 @@ void USER_FUNC user_function_cmd_received( int udp_flag, char *pusrdata )
if ( return_flag == true )
{
char *json_str = cJSON_Print( json_send );
u_printf( "[user_function_cmd_received] pRoot: %s\r\n", json_str );
//u_printf( "[user_function_cmd_received] pRoot: %s\r\n", json_str );
user_send( udp_flag, json_str ); //发送数据
hfmem_free( (void *) json_str );
}
Expand All @@ -183,7 +183,7 @@ void USER_FUNC user_function_cmd_received( int udp_flag, char *pusrdata )
//mico_system_context_update( sys_config );
update_user_config_flag = false;
}

cJSON_Delete( pJsonRoot );

}
Expand Down
46 changes: 22 additions & 24 deletions user_gpio.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
#define TIMER1_ID (1) //定时器id


char ntpserver[]="pool.ntp.org";
char ntpserver[]="ntp1.aliyun.com";

uint8_t key_time = 0;
#define BUTTON_LONG_PRESS_TIME 10 //
Expand Down Expand Up @@ -154,20 +154,19 @@ void USER_FUNC key_short_press( void )
key_continue = tmp;
//HF_Debug(DEBUG_WARN,"key timeout,tmp=%d,,key_continue=%d,,key_time=%d\n",tmp,key_continue,key_time);

if(!hfgpio_fpin_is_high( KEY )){

if(!hfgpio_fpin_is_high( KEY )){
//any button pressed
key_time++;
key_time++;
//if(key_time%5==0)u_printf("button long pressed:%d",key_time);
if ( key_time < 10 ){
key_last = key_continue;
key_last = key_continue;
//u_printf("button short pressed:%d,key_last=%d",key_time,key_last);
}
else
{
if ( key_time == 50 )
{
key_long_press( );
key_long_press( );
printtime();//打印时间
//u_printf("hfuflash_size:%d\n",hfuflash_size());
}
Expand All @@ -177,22 +176,22 @@ void USER_FUNC key_short_press( void )
}
else if ( key_time ==180 ){
user_function_cmd_received(1,"{\"cmd\":\"device report\"}");
user_led_set( 0 );
user_led_set( 0 );
}
else if ( key_time ==190 ){
user_led_set( 1 );
user_led_set( 1 );
}
else if ( key_time == 203 )
else if ( key_time == 203 )
{
user_led_set( 0 );
u_printf("SmartLink_start > >>");
//user_function_cmd_received(1,"{\"cmd\":\"device report\"}");
hfsmtlk_start();
user_led_set( 0 );
//u_printf("SmartLink_start > >>");
user_function_cmd_received(1,"{\"cmd\":\"device report\"}");
hfsmtlk_start();
//key_time = 201;
}
}
if(key_time>204){
key_time=189;
key_time=189;
}

}else{
Expand All @@ -204,24 +203,23 @@ void USER_FUNC key_short_press( void )


void USER_FUNC key_rising_irq_handler ( uint32_t arg1,uint32_t arg2 ){
if(key_time<2){
if(key_time<2){
}
else if(key_time<20){
else if(key_time<20){
if(hfgpio_fpin_is_high( KEY )){
HF_Debug(DEBUG_WARN,"short press\n");
key_short_press();
//hfconfig_file_data_read(2,config_data,100);
HF_Debug(DEBUG_WARN,"short press\n");
key_short_press();
//hfconfig_file_data_read(2,config_data,100);

}
}
else if(key_time<80){
HF_Debug(DEBUG_WARN,"4s press\n");
} else if(key_time<80){
HF_Debug(DEBUG_WARN,"4s press\n");
}
else if(key_time<150){
HF_Debug(DEBUG_WARN,"5s press\n");
HF_Debug(DEBUG_WARN,"5s press\n");
}
else if (key_time<204){
HF_Debug(DEBUG_WARN,"10s press\n");
HF_Debug(DEBUG_WARN,"10s press\n");
}

key_time=0;
Expand Down
137 changes: 67 additions & 70 deletions user_rtc.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@ int rtctimes;
USER_FUNC void rtc_thread_func(void * arg){

int i, j;
char task_flag[PLUG_NUM] = { -1, -1, -1 ,-1,-1}; //记录要哪个插座要返回数据

char task_flag[PLUG_NUM] = { -1, -1, -1 ,-1,-1}; //记录要哪个插座要返回数据
bool ntptime_succeed;
ntptime_succeed=0;
struct tm * t_t;
Expand All @@ -31,33 +30,32 @@ USER_FUNC void rtc_thread_func(void * arg){
//u_printf("now time:hour:%d,min:%d,sec:%d\n",t_t->tm_hour,t_t->tm_min,t_t->tm_sec);
u_printf("rtc_thread started.");
while(1){
//msleep(50);
char update_user_config_flag = 0;
if(t_t->tm_min%10==0){ //
if(!ntptime_succeed){
if(update_rtctime()){
//msleep(50);
char update_user_config_flag = 0;
if(t_t->tm_min%10==0){ //
if(!ntptime_succeed){
if(update_rtctime()){
ntptime_succeed=1;
//t_t=user_time_t;
u_printf("now time is:%d : %d\n",t_t->tm_hour,t_t->tm_min);
u_printf("now time is:%d : %d\n",t_t->tm_hour,t_t->tm_min);
}else{
ntptime_succeed=0;
}
}
else{
//u_printf("update time succeed\n");
}
}
else{
//u_printf("update time succeed\n");
}

}else{
ntptime_succeed=0;
}

for ( i = 0; i < PLUG_NUM; i++ )
{
for ( j = 0; j < PLUG_TIME_TASK_NUM; j++ )
{
if ( u_config.plug[i].task[j].on != 0 )
{

}else{
ntptime_succeed=0;
}

for ( i = 0; i < PLUG_NUM; i++ )
{
for ( j = 0; j < PLUG_TIME_TASK_NUM; j++ )
{
if ( u_config.plug[i].task[j].on != 0 )
{
uint8_t repeat = u_config.plug[i].task[j].repeat;
if (
t_t->tm_sec == 0 && t_t->tm_min == u_config.plug[i].task[j].minute && t_t->tm_hour == u_config.plug[i].task[j].hour
Expand All @@ -81,54 +79,52 @@ USER_FUNC void rtc_thread_func(void * arg){
// u_printf("repeat:%d\n",repeat);
}
}
}
}
}
}

if ( update_user_config_flag == 1 )
{
update_user_config_flag = 0;
if ( update_user_config_flag == 1 )
{
update_user_config_flag = 0;

cJSON *json_send = cJSON_CreateObject( );
cJSON_AddStringToObject( json_send, "mac", strMac );
cJSON *json_send = cJSON_CreateObject( );
cJSON_AddStringToObject( json_send, "mac", strMac );

for ( i = 0; i < PLUG_NUM; i++ )
{
char strTemp1[] = "plug_X";
strTemp1[5] = i + '0';
cJSON *json_send_plug = cJSON_CreateObject( );
cJSON_AddNumberToObject( json_send_plug, "on", u_config.plug[i].on );

if ( task_flag[i] >= 0 )
{
cJSON *json_send_plug_setting = cJSON_CreateObject( );

j = task_flag[i];
char strTemp2[] = "task_X";
strTemp2[5] = j + '0';
//u_printf("[strTemp2] task_X=>%s\n",strTemp2);
cJSON *json_send_plug_task = cJSON_CreateObject( );
cJSON_AddNumberToObject( json_send_plug_task, "hour", u_config.plug[i].task[j].hour );
cJSON_AddNumberToObject( json_send_plug_task, "minute", u_config.plug[i].task[j].minute );
cJSON_AddNumberToObject( json_send_plug_task, "repeat", u_config.plug[i].task[j].repeat );
cJSON_AddNumberToObject( json_send_plug_task, "action", u_config.plug[i].task[j].action );
cJSON_AddNumberToObject( json_send_plug_task, "on", u_config.plug[i].task[j].on );
cJSON_AddItemToObject( json_send_plug_setting, strTemp2, json_send_plug_task );

cJSON_AddItemToObject( json_send_plug, "setting", json_send_plug_setting );

task_flag[i] = -1;
}
cJSON_AddItemToObject( json_send, strTemp1, json_send_plug );
}

char *json_str = cJSON_Print( json_send );
u_printf("****rtc thread ********>>> json_send:\n %d bytes\n",sizeof(json_str));
user_send( false, json_str ); //发送数据,

hfmem_free( json_str );
json_str=NULL;
cJSON_Delete( json_send );
}
for ( i = 0; i < PLUG_NUM; i++ )
{
char strTemp1[] = "plug_X";
strTemp1[5] = i + '0';
cJSON *json_send_plug = cJSON_CreateObject( );
cJSON_AddNumberToObject( json_send_plug, "on", u_config.plug[i].on );

if ( task_flag[i] >= 0 )
{
cJSON *json_send_plug_setting = cJSON_CreateObject( );

j = task_flag[i];
char strTemp2[] = "task_X";
strTemp2[5] = j + '0';
//u_printf("[strTemp2] task_X=>%s\n",strTemp2);
cJSON *json_send_plug_task = cJSON_CreateObject( );
cJSON_AddNumberToObject( json_send_plug_task, "hour", u_config.plug[i].task[j].hour );
cJSON_AddNumberToObject( json_send_plug_task, "minute", u_config.plug[i].task[j].minute );
cJSON_AddNumberToObject( json_send_plug_task, "repeat", u_config.plug[i].task[j].repeat );
cJSON_AddNumberToObject( json_send_plug_task, "action", u_config.plug[i].task[j].action );
cJSON_AddNumberToObject( json_send_plug_task, "on", u_config.plug[i].task[j].on );
cJSON_AddItemToObject( json_send_plug_setting, strTemp2, json_send_plug_task );
cJSON_AddItemToObject( json_send_plug, "setting", json_send_plug_setting );

task_flag[i] = -1;
}
cJSON_AddItemToObject( json_send, strTemp1, json_send_plug );
}

char *json_str = cJSON_Print( json_send );
u_printf("****rtc thread ********>>> json_send:\n %d bytes\n",sizeof(json_str));
user_send( false, json_str ); //发送数据,
hfmem_free( json_str );
//json_str=NULL;
cJSON_Delete( json_send );
}

}
u_printf("rtc_thread_func exit..");
Expand Down Expand Up @@ -158,10 +154,11 @@ int update_rtctime(void){
int time_mm;
time_mm=hfntp_get_time(ntpserver,123,200);
if(time_mm<0){

return 0;
}
rtctimes=time_mm;
u_printf("*****ntp time got.*****\n ");
//u_printf("*****ntp time got.*****\n ");
return 1;
}

Expand All @@ -173,7 +170,7 @@ int printtime()
t=rtctimes+8*3600; //东八区
user_time_t=gmtime(&t); //时间结构体
u_printf("hour:%d,min:%d\n",user_time_t->tm_hour,user_time_t->tm_min);
char s[100];
char s[30];
strftime(s, sizeof(s), "%Y-%m-%d %H:%M:%S", user_time_t);
u_printf("%d: %s\n", (int)t, s);

Expand Down
Loading

0 comments on commit 914047e

Please sign in to comment.