diff --git a/TC1/main.c b/TC1/main.c index 1b53f46..0ce1e8c 100644 --- a/TC1/main.c +++ b/TC1/main.c @@ -13,6 +13,7 @@ char rtc_init = 0; //sntp校时成功标志位 char strMac[16] = { 0 }; +uint32_t power=0; system_config_t * sys_config; user_config_t * user_config; @@ -151,7 +152,7 @@ int application_start( void ) require_noerr( err, exit ); err = user_rtc_init( ); require_noerr( err, exit ); -// user_power_init(); + user_power_init(); while ( 1 ) { // mico_thread_msleep(500); diff --git a/TC1/main.h b/TC1/main.h index 98c4906..b315ca7 100644 --- a/TC1/main.h +++ b/TC1/main.h @@ -4,7 +4,7 @@ #include "mico.h" #include "MiCOKit_EXT.h" -#define VERSION "v0.6" +#define VERSION "v0.7" #define TYPE 1 #define TYPE_NAME "zTC1" @@ -66,6 +66,7 @@ typedef struct extern char rtc_init; extern char strMac[16]; +extern uint32_t power; extern system_config_t * sys_config; extern user_config_t * user_config; diff --git a/TC1/user_mqtt_client.c b/TC1/user_mqtt_client.c index a01e162..62b8e2b 100644 --- a/TC1/user_mqtt_client.c +++ b/TC1/user_mqtt_client.c @@ -111,6 +111,7 @@ OSStatus user_recv_handler( void *arg ); OSStatus user_mqtt_send_plug_state( char plug_id ); void user_mqtt_hass_auto( char plug_id ); +void user_mqtt_hass_auto_power( void ); /****************************************************** * Variables Definitions @@ -264,7 +265,7 @@ void mqtt_client_thread( mico_thread_arg_t arg ) { isconnect = false; mico_rtos_thread_sleep( 3 ); - if ( MQTT_SERVER[0] < 0x20 || MQTT_SERVER[0] > 0x7f || MQTT_SERVER_PORT < 1 ) continue; //未配置mqtt服务器时不连接 + if ( MQTT_SERVER[0] < 0x20 || MQTT_SERVER[0] > 0x7f || MQTT_SERVER_PORT < 1 ) continue; //鏈厤缃甿qtt鏈嶅姟鍣ㄦ椂涓嶈繛鎺 micoWlanGetLinkStatus( &LinkStatus ); if ( LinkStatus.is_connected != 1 ) @@ -307,10 +308,10 @@ void mqtt_client_thread( mico_thread_arg_t arg ) rc = MQTTSubscribe( &c, topic_set, QOS0, messageArrived ); require_noerr_string( rc, MQTT_reconnect, "ERROR: MQTT client subscribe err." ); mqtt_log("MQTT client subscribe success! recv_topic=[%s].", topic_set); - /*4.1 连接成功后先更新发送一次数据*/ + /*4.1 杩炴帴鎴愬姛鍚庡厛鏇存柊鍙戦佷竴娆℃暟鎹*/ isconnect = true; uint8_t *buf1 = NULL; - buf1 = malloc( 1024 ); //idx为1位时长度为24 + buf1 = malloc( 1024 ); //idx涓1浣嶆椂闀垮害涓24 if ( buf1 != NULL ) { sprintf( @@ -339,11 +340,12 @@ void mqtt_client_thread( mico_thread_arg_t arg ) no_mqtt_msg_exchange = false; } - if ( hass_mqtt_flag>0 ) + if ( hass_mqtt_flag > 0 ) { - hass_mqtt_flag --; - user_mqtt_hass_auto(hass_mqtt_flag); - user_mqtt_send_plug_state(hass_mqtt_flag); + if ( hass_mqtt_flag == PLUG_NUM ) user_mqtt_hass_auto_power( ); + hass_mqtt_flag--; + user_mqtt_hass_auto( hass_mqtt_flag ); + user_mqtt_send_plug_state( hass_mqtt_flag ); } /* recv msg from user worker thread to be sent to server */ if ( FD_ISSET( msg_send_event_fd, &readfds ) ) @@ -481,7 +483,7 @@ OSStatus user_mqtt_send( char *arg ) return user_mqtt_send_topic( topic_state, arg, 0 ); } -//更新ha开关状态 +//鏇存柊ha寮鍏崇姸鎬 OSStatus user_mqtt_send_plug_state( char plug_id ) { @@ -499,7 +501,7 @@ OSStatus user_mqtt_send_plug_state( char plug_id ) if ( topic_buf ) free( topic_buf ); } -//hass mqtt自动发现数据发送 +//hass mqtt鑷姩鍙戠幇鏁版嵁寮鍏冲彂閫 void user_mqtt_hass_auto( char plug_id ) { uint8_t i; @@ -519,15 +521,60 @@ void user_mqtt_hass_auto( char plug_id ) "\"payload_off\":\"{\\\"mac\\\":\\\"%s\\\",\\\"plug_%d\\\":{\\\"on\\\":0}}\"" "}", user_config->plug[plug_id].name, - strMac, plug_id, - strMac, plug_id, - strMac, plug_id ); + strMac, + plug_id, + strMac, + plug_id, + strMac, + plug_id ); user_mqtt_send_topic( topic_buf, send_buf, 1 ); } if ( send_buf ) free( send_buf ); if ( topic_buf ) free( topic_buf ); } +//hass mqtt鑷姩鍙戠幇鏁版嵁鍔熺巼鍙戦 +void user_mqtt_hass_auto_power( void ) +{ + uint8_t i; + uint8_t *send_buf = NULL; + uint8_t *topic_buf = NULL; + send_buf = malloc( 512 ); // + topic_buf = malloc( 128 ); // + if ( send_buf != NULL && topic_buf != NULL ) + { + sprintf( topic_buf, "homeassistant/sensor/%s/power/config", strMac ); + sprintf( send_buf, + "{" + "\"name\":\"鍔熺巼\"," + "\"state_topic\":\"homeassistant/sensor/%s/power/state\"," + "\"unit_of_measurement\":\"W\"," + "\"icon\":\"mdi:gauge\"," + "\"value_template\":\"{{ value_json.power }}\"" + "}", + strMac ); + user_mqtt_send_topic( topic_buf, send_buf, 1 ); + } + if ( send_buf ) free( send_buf ); + if ( topic_buf ) free( topic_buf ); +} + +void user_mqtt_hass_power( void ) +{ + uint8_t i; + uint8_t *send_buf = NULL; + uint8_t *topic_buf = NULL; + send_buf = malloc( 512 ); // + topic_buf = malloc( 128 ); // + if ( send_buf != NULL && topic_buf != NULL ) + { + sprintf( topic_buf, "homeassistant/sensor/%s/power/state", strMac ); + sprintf( send_buf, "{\"power\":\"%d.%d\"}", power/10,power%10 ); + user_mqtt_send_topic( topic_buf, send_buf, 0 ); + } + if ( send_buf ) free( send_buf ); + if ( topic_buf ) free( topic_buf ); +} bool user_mqtt_isconnect( ) { diff --git a/TC1/user_mqtt_client.h b/TC1/user_mqtt_client.h index f02e945..8fef47f 100644 --- a/TC1/user_mqtt_client.h +++ b/TC1/user_mqtt_client.h @@ -11,4 +11,5 @@ extern OSStatus user_mqtt_send( char *arg ); extern bool user_mqtt_isconnect(void); extern OSStatus user_mqtt_send_plug_state( char plug_id ); extern void user_mqtt_hass_auto( char plug_id ); +extern void user_mqtt_hass_power( void ); #endif diff --git a/TC1/user_ota.c b/TC1/user_ota.c index 6f33de3..d5491f9 100644 --- a/TC1/user_ota.c +++ b/TC1/user_ota.c @@ -14,8 +14,8 @@ static void ota_server_status_handler( OTA_STATE_E state, float progress ) { case OTA_LOADING: os_log("ota server is loading, progress %.2f%%", progress); -// if ( (int) progress == progress ) -// sprintf( str, "{\"mac\":\"%s\",\"ota_progress\":%d}", strMac,(int) progress ); + if ( ((int) progress)%10 == 1 ) + sprintf( str, "{\"mac\":\"%s\",\"ota_progress\":%d}", strMac,((int) progress) ); break; case OTA_SUCCE: os_log("ota server daemons success"); diff --git a/TC1/user_power.c b/TC1/user_power.c index 253bba6..3f34a07 100644 --- a/TC1/user_power.c +++ b/TC1/user_power.c @@ -17,11 +17,14 @@ static void power_timer_handler( void* arg ) { // uint8_t pin_input = MicoGpioInputGet( POWER ); - if ( timer_count_last != timer_count ) { - os_log("power_irq_handler:%u-%u=%u",timer_count,timer_count_last,timer); +// os_log("power_irq_handler:%u-%u=%u",timer_count,timer_count_last,timer); timer_count_last = timer_count; + + timer=timer/1000; + power=9500000/timer; + os_log("power_irq_handler:%u,%07u",power,timer); } // if(timer_count==0) os_log("power_timer_handler Hight:%d",timer_count_last); // timer_count++; diff --git a/TC1/user_rtc.c b/TC1/user_rtc.c index c8e2bff..39efc45 100644 --- a/TC1/user_rtc.c +++ b/TC1/user_rtc.c @@ -114,6 +114,7 @@ void rtc_thread( mico_thread_arg_t arg ) OSStatus err = kUnknownErr; LinkStatusTypeDef LinkStatus; mico_rtc_time_t rtc_time; + uint32_t power_last = 0xffffffff; mico_utc_time_t utc_time; @@ -171,7 +172,7 @@ void rtc_thread( mico_thread_arg_t arg ) { user_relay_set( i, user_config->plug[i].task[j].action ); update_user_config_flag = 1; - user_mqtt_send_plug_state(i); + user_mqtt_send_plug_state( i ); } if ( repeat == 0x00 ) { @@ -218,13 +219,13 @@ void rtc_thread( mico_thread_arg_t arg ) cJSON_AddItemToObject( json_send_plug, "setting", json_send_plug_setting ); - task_flag[i]=-1; + task_flag[i] = -1; } cJSON_AddItemToObject( json_send, strTemp1, json_send_plug ); } char *json_str = cJSON_Print( json_send ); - user_send(false,json_str);//发送数据 + user_send( false, json_str ); //发送数据 free( json_str ); cJSON_Delete( json_send ); @@ -245,6 +246,22 @@ void rtc_thread( mico_thread_arg_t arg ) } } + //发送功率数据 + if ( power_last != power ) + { + power_last = power; + + uint8_t *power_buf = NULL; + power_buf = malloc( 128 ); // + if ( power_buf != NULL ) + { + sprintf( power_buf, "{\"mac\":\"%s\",\"power\":\"%d.%d\"}", strMac, power/10,power%10 ); + user_send( 0, power_buf ); + free( power_buf ); + } + 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 db50a15..1cd3c73 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 a93454f..5edf256 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