diff --git a/TC1/user_mqtt_client.c b/TC1/user_mqtt_client.c index a8e2f44..4d26083 100644 --- a/TC1/user_mqtt_client.c +++ b/TC1/user_mqtt_client.c @@ -103,68 +103,71 @@ static uint8_t status = 0; void user_mqtt_timer_func( void *arg ) { uint8_t *buf1 = NULL; - status++; - switch ( status ) + if ( mico_rtos_is_queue_empty( &mqtt_msg_send_queue ) == true ) { - case 1: - user_mqtt_hass_auto_power( ); - break; - case 2: - user_mqtt_hass_auto( 0 ); - break; - case 3: - user_mqtt_hass_auto( 1 ); - break; - case 4: - user_mqtt_hass_auto( 2 ); - break; - case 5: - user_mqtt_hass_auto( 3 ); - break; - case 6: - user_mqtt_hass_auto( 4 ); - break; - case 7: - user_mqtt_hass_auto( 5 ); - break; - case 8: - user_mqtt_hass_auto_name( 0 ); - break; - case 9: - user_mqtt_hass_auto_name( 1 ); - break; - case 10: - user_mqtt_hass_auto_name( 2 ); - break; - case 11: - user_mqtt_hass_auto_name( 3 ); - break; - case 12: - user_mqtt_hass_auto_name( 4 ); - break; - case 13: - user_mqtt_hass_auto_name( 5 ); - break; - case 14: - user_mqtt_hass_auto_power_name( ); - break; - case 15: + status++; + switch ( status ) + { + case 1: + user_mqtt_hass_auto_power( ); + break; + case 2: + user_mqtt_hass_auto( 0 ); + break; + case 3: + user_mqtt_hass_auto( 1 ); + break; + case 4: + user_mqtt_hass_auto( 2 ); + break; + case 5: + user_mqtt_hass_auto( 3 ); + break; + case 6: + user_mqtt_hass_auto( 4 ); + break; + case 7: + user_mqtt_hass_auto( 5 ); + break; + case 8: + user_mqtt_hass_auto_name( 0 ); + break; + case 9: + user_mqtt_hass_auto_name( 1 ); + break; + case 10: + user_mqtt_hass_auto_name( 2 ); + break; + case 11: + user_mqtt_hass_auto_name( 3 ); + break; + case 12: + user_mqtt_hass_auto_name( 4 ); + break; + case 13: + user_mqtt_hass_auto_name( 5 ); + break; + case 14: + user_mqtt_hass_auto_power_name( ); + break; + case 15: - buf1 = malloc( 1024 ); //idx为1位时长度为24 - if ( buf1 != NULL ) - { - sprintf( - buf1, - "{\"mac\":\"%s\",\"version\":null,\"plug_0\":{\"on\":null,\"setting\":{\"name\":null}},\"plug_1\":{\"on\":null,\"setting\":{\"name\":null}},\"plug_2\":{\"on\":null,\"setting\":{\"name\":null}},\"plug_3\":{\"on\":null,\"setting\":{\"name\":null}},\"plug_4\":{\"on\":null,\"setting\":{\"name\":null}},\"plug_5\":{\"on\":null,\"setting\":{\"name\":null}}}", - strMac ); - user_function_cmd_received( 0, buf1 ); - free( buf1 ); - } - break; - default: - mico_stop_timer( &timer_handle ); + buf1 = malloc( 1024 ); //idx为1位时长度为24 + if ( buf1 != NULL ) + { + sprintf( + buf1, + "{\"mac\":\"%s\",\"version\":null,\"plug_0\":{\"on\":null,\"setting\":{\"name\":null}},\"plug_1\":{\"on\":null,\"setting\":{\"name\":null}},\"plug_2\":{\"on\":null,\"setting\":{\"name\":null}},\"plug_3\":{\"on\":null,\"setting\":{\"name\":null}},\"plug_4\":{\"on\":null,\"setting\":{\"name\":null}},\"plug_5\":{\"on\":null,\"setting\":{\"name\":null}}}", + strMac ); + user_function_cmd_received( 0, buf1 ); + free( buf1 ); + } + break; + default: + mico_stop_timer( &timer_handle ); // mico_deinit_timer( &timer_handle ); - break; + break; + } } } @@ -398,7 +401,7 @@ void mqtt_client_thread( mico_thread_arg_t arg ) mqtt_log("Disconnect MQTT client, and reconnect after 5s, reason: mqtt_rc = %d, err = %d", rc, err ); - if (&timer_handle !=NULL && mico_rtos_is_timer_running( &timer_handle ) ) + if ( &timer_handle != NULL && mico_rtos_is_timer_running( &timer_handle ) ) { mico_stop_timer( &timer_handle ); // mico_deinit_timer( &timer_handle ); @@ -622,7 +625,7 @@ void user_mqtt_hass_auto_power_name( void ) send_buf[16] = 0xe7; send_buf[17] = 0x8e; send_buf[18] = 0x87; - user_mqtt_send_topic( topic_buf, send_buf, 1 ); + user_mqtt_send_topic( topic_buf, send_buf, 0 ); } if ( send_buf ) free( send_buf ); diff --git a/TC1/user_rtc.c b/TC1/user_rtc.c index 475de98..e911579 100644 --- a/TC1/user_rtc.c +++ b/TC1/user_rtc.c @@ -115,6 +115,7 @@ void rtc_thread( mico_thread_arg_t arg ) LinkStatusTypeDef LinkStatus; mico_rtc_time_t rtc_time; uint32_t power_last = 0xffffffff; + uint32_t total_time_last = 0xffffffff; mico_utc_time_t utc_time; mico_utc_time_t utc_time_last; @@ -139,13 +140,12 @@ void rtc_thread( mico_thread_arg_t arg ) mico_time_get_utc_time( &utc_time ); utc_time += 28800; - if(utc_time_last!=utc_time) + if ( utc_time_last != utc_time ) { - utc_time_last==utc_time; + utc_time_last == utc_time; total_time++; } - struct tm * currentTime = localtime( (const time_t *) &utc_time ); rtc_time.sec = currentTime->tm_sec; rtc_time.min = currentTime->tm_min; @@ -255,19 +255,19 @@ void rtc_thread( mico_thread_arg_t arg ) } //͹ - if ( power_last != power ) + if ( power_last != power || total_time - total_time_last > 4 ) { power_last = power; - + total_time_last = total_time; uint8_t *power_buf = NULL; - power_buf = malloc( 128 ); // + power_buf = malloc( 128 ); if ( power_buf != NULL ) { - sprintf( power_buf, "{\"mac\":\"%s\",\"power\":\"%d.%d\",\"total_time\":%d}", strMac, power/10,power%10,total_time ); + sprintf( power_buf, "{\"mac\":\"%s\",\"power\":\"%d.%d\",\"total_time\":%d}", strMac, power / 10, power % 10, total_time ); user_send( 0, power_buf ); free( power_buf ); } - user_mqtt_hass_power(); + user_mqtt_hass_power( ); } mico_rtos_thread_msleep( 900 ); diff --git a/build/TC1@MK3031@moc/binary/TC1@MK3031@moc.all.bin b/build/TC1@MK3031@moc/binary/TC1@MK3031@moc.all.bin index 71d72e6..06736e8 100644 Binary files a/build/TC1@MK3031@moc/binary/TC1@MK3031@moc.all.bin and b/build/TC1@MK3031@moc/binary/TC1@MK3031@moc.all.bin differ diff --git a/build/TC1@MK3031@moc/binary/TC1@MK3031@moc.ota.bin b/build/TC1@MK3031@moc/binary/TC1@MK3031@moc.ota.bin index 0631374..ba5cfd1 100644 Binary files a/build/TC1@MK3031@moc/binary/TC1@MK3031@moc.ota.bin and b/build/TC1@MK3031@moc/binary/TC1@MK3031@moc.ota.bin differ