From: kyz Date: Sat, 24 May 2025 10:03:07 +0000 (+0800) Subject: modify iot data X-Git-Url: http://kyz.pub/gitweb/?a=commitdiff_plain;h=b933f12b1b827590300215d9c340e31b9a8a5308;p=yn%2Falarmmonitor.git modify iot data --- diff --git a/app/src/am_main.c b/app/src/am_main.c index d451338..025b6a4 100644 --- a/app/src/am_main.c +++ b/app/src/am_main.c @@ -166,9 +166,15 @@ int32_t device_lcd_refresh( am_dev_t *dev ) ///lcd_write_page( dev->disp.page ); lcd_write_alarm( dev->disp.alarm, dev->disp.alarm_time ); lcd_write_power_info( dev->disp.pwr[0], dev->disp.pwr[1] ); - lcd_write_sensor_info( 7, dev->disp.sensor[7] ); - lcd_write_sensor_info( 8, dev->disp.sensor[8] ); - lcd_write_sensor_info( 9, dev->disp.sensor[9] ); + lcd_write_sensor_info( 1, dev->sensor_data.ac_vol[0] ); + lcd_write_sensor_info( 2, dev->sensor_data.ac_curr[0] ); + lcd_write_sensor_info( 3, dev->sensor_data.ac_vol[0]*dev->sensor_data.ac_curr[0] ); + lcd_write_sensor_info( 4, dev->sensor_data.ac_vol[1] ); + lcd_write_sensor_info( 5, dev->sensor_data.ac_curr[1] ); + lcd_write_sensor_info( 6, dev->sensor_data.ac_vol[1]*dev->sensor_data.ac_curr[1] ); + lcd_write_sensor_info( 7, g_dev.sensor_data.temperature ); + lcd_write_sensor_info( 8, g_dev.sensor_data.humidity); + lcd_write_sensor_info( 9, dev->sensor_data.sound_pressure); //noise dB lcd_write_sound_type( dev->disp.sound_type ); return 0; @@ -347,7 +353,7 @@ int main( int argc, char ** argv ) device_button_init( dev ); device_led_init( dev ); device_lcd_init( dev ); - ///device_sensor_init( dev ); + device_sensor_init( dev ); sensor_board_init( dev ); //ec200 ACM diff --git a/app/src/iot.c b/app/src/iot.c index 57447aa..98b8f52 100644 --- a/app/src/iot.c +++ b/app/src/iot.c @@ -13,6 +13,7 @@ typedef struct{ typedef struct{ iot_info_t info; uv_timer_t iot_timer; + uv_timer_t heartbeat_timer; am_dev_t *dev; tcp_cli_t tcp_client; @@ -36,12 +37,16 @@ static uint16_t pkt_sn = 0; extern char * handle_iot_rx_data( char * data, int sz ); extern uint8_t * iot_wrap_button_event_pkt( int button ); +extern uint8_t * iot_wrap_heartbeat_pkt( void ); -extern char* wrap_status_pkt( void ); +extern char* wrap_status_pkt( char *task_id ); extern char* wrap_connect_pkt( void ); extern uint8_t * iot_wrap_alarm_event_pkt( void ); +void rpt_timer_cb( uv_timer_t * handle ); +void heartbeat_timer_cb( uv_timer_t * handle ); + void iot_tx_data( uint8_t *data, uint32_t sz ); void iot_rpt_alarm_event( void ) @@ -57,6 +62,9 @@ void iot_rpt_alarm_event( void ) if( pkt_str ){ iot_tx_data( pkt_str, strlen( pkt_str ) ); free( pkt_str ); + + uv_timer_stop( &s_iot.heartbeat_timer); + uv_timer_start( &s_iot.heartbeat_timer, heartbeat_timer_cb, 30*1000, 30*1000 ); } } } @@ -75,6 +83,9 @@ void iot_rpt_button_event( int button ) if( pkt_str ){ iot_tx_data( pkt_str, strlen( pkt_str ) ); free( pkt_str ); + + uv_timer_stop( &s_iot.heartbeat_timer); + uv_timer_start( &s_iot.heartbeat_timer, heartbeat_timer_cb, 30*1000, 30*1000 ); } } } @@ -100,8 +111,6 @@ void on_connect_resp( uint8_t *rx_data, uint32_t rx_sz ) } } -void rpt_timer_cb( uv_timer_t * handle ); - void connect_timer_cb( uv_timer_t * handle ) { iot_t *p_iot; @@ -133,6 +142,8 @@ void connect_timer_cb( uv_timer_t * handle ) p_iot->connected = 2; //tx connect req again //rpt start uv_timer_start( &s_iot.iot_timer, rpt_timer_cb, 100, 5000*3 ); + uv_timer_stop( &s_iot.heartbeat_timer); + uv_timer_start( &s_iot.heartbeat_timer, heartbeat_timer_cb, 30*1000, 30*1000 ); }else{ p_iot->connected = 0; //tx connect req again uv_timer_start( &s_iot.iot_timer, connect_timer_cb, 2000, 0 ); @@ -148,6 +159,27 @@ void connect_timer_cb( uv_timer_t * handle ) } } +void heartbeat_timer_cb( uv_timer_t * handle ) +{ + iot_t *p_iot; + uv_buf_t tx_buf; + char *rpt_str; + + p_iot = (iot_t *)handle->data; + + if( p_iot->tcp_client.connected ){ + + if( p_iot->connected ){ + rpt_str = iot_wrap_heartbeat_pkt( ); +printf( "----------HeartBeatPkt:[%s]\n", rpt_str ); + iot_tx_data( rpt_str, strlen( rpt_str ) ); + free( rpt_str ); + }else{ + + } + } +} + void rpt_timer_cb( uv_timer_t * handle ) { iot_t *p_iot; @@ -160,9 +192,13 @@ void rpt_timer_cb( uv_timer_t * handle ) if( p_iot->connected ){ - rpt_str = wrap_status_pkt(); + rpt_str = wrap_status_pkt( NULL ); iot_tx_data( rpt_str, strlen( rpt_str ) ); free( rpt_str ); + + uv_timer_stop( &s_iot.heartbeat_timer); + uv_timer_start( &s_iot.heartbeat_timer, heartbeat_timer_cb, 30*1000, 30*1000 ); + }else{ } @@ -200,6 +236,7 @@ void iot_tcp_conn_cb( tcp_cli_t *client, int connected ) printf( "------------------IOT TCP CONNECTED\n" ); s_iot.connected = 0; uv_timer_stop( &s_iot.iot_timer ); + uv_timer_stop( &s_iot.heartbeat_timer); uv_timer_start( &s_iot.iot_timer, connect_timer_cb, 100, 0 ); }else{ @@ -269,6 +306,9 @@ void iot_init( am_dev_t *dev ) s_iot.iot_timer.data = &s_iot; uv_timer_start( &s_iot.iot_timer, connect_timer_cb, 100, 0 ); + uv_timer_init( dev->loop, &s_iot.heartbeat_timer ); + s_iot.heartbeat_timer.data = &s_iot; + s_iot.tcp_client.data = &s_iot; AM_LOGI( "IOT: target %s:%d\n", dev->iot.srv_addr, dev->iot.srv_port ); diff --git a/app/src/iot_report.c b/app/src/iot_report.c index f5c7d78..2a194bd 100644 --- a/app/src/iot_report.c +++ b/app/src/iot_report.c @@ -6,6 +6,7 @@ extern void aes_decrypt(const unsigned char *ciphertext, unsigned char *plaintex extern void aes_encrypt(const unsigned char *plaintext, unsigned char *ciphertext, const unsigned char *key, int len, char *ivec ); cJSON* rpt_pkt_encrypted( int secret, cJSON * item ); +char* wrap_status_pkt( char *task_id ); char * time_str( char * str, time_t ts ) { @@ -495,6 +496,23 @@ uint8_t * iot_wrap_button_event_pkt( int button ) return pkt_data; } +/*------------------------------------------------------------------------------------------- +//heartbeat pkt +//-------------------------------------------------------------------------------------------*/ +uint8_t * iot_wrap_heartbeat_pkt( void ) +{ + uint8_t *pkt_data = NULL; + time_t ts; + char now_str[32]; + char pkt_buf[100]; + + time_str( now_str, time(NULL) ); + + sprintf( pkt_buf, "{ \"type\":\"e1002\", \"time\":\"%s\" }", now_str ); + + return strdup(pkt_buf); +} + extern void dev_wh_create( char * whid ); extern void dev_wh_delete( char * whid ); extern void remote_jbkzq_power( int on ); @@ -515,10 +533,11 @@ char* handle_iot_rx_data( char * data, int sz ) cJSON *item_type; cJSON *item_time; - cJSON *item_devices; - cJSON *item_devices_device; - cJSON *item_devices_tagcode; - cJSON *item_devices_value; + cJSON *item_devices = NULL; + cJSON *item_devices_device = NULL; + cJSON *item_devices_tagcode = NULL; + cJSON *item_devices_value = NULL; + cJSON *item_devices_type = NULL; char * plaintext = NULL; char * ciphertext= NULL; @@ -577,19 +596,22 @@ char* handle_iot_rx_data( char * data, int sz ) item_devices_value = cJSON_GetObjectItem( item_devices, "value" ); } - printf( "ccode: %s\n", item_ccode->valuestring); - printf( "time: %s\n", item_time->valuestring); - printf( "type: %s\n", item_type->valuestring); + printf( "ccode: %s\n", (item_ccode&&cJSON_IsString(item_ccode))?item_ccode->valuestring:"null"); + printf( "time: %s\n", (item_time&&cJSON_IsString(item_time))?item_time->valuestring:"null"); + printf( "type: %s\n", (item_type&&cJSON_IsString(item_type))?item_type->valuestring:"null"); printf( "device:\n"); - printf( "\tdevice: %s\n", item_devices_device->valuestring); - printf( "\ttagcode: %s\n", item_devices_tagcode->valuestring); - printf( "\tvalue: %s\n", item_devices_value->valuestring); - - if( item_ccode && item_devices && item_time && item_type && item_devices_device && item_type && strcmp( item_type->valuestring, "write")== 0){ + printf( "\tdevice: %s\n", (item_devices_device&&cJSON_IsString(item_devices_device))?item_devices_device->valuestring:"null"); + printf( "\ttagcode: %s\n", (item_devices_tagcode&&cJSON_IsString(item_devices_tagcode))?item_devices_tagcode->valuestring:"null"); + printf( "\tvalue: %s\n", (item_devices_value&&cJSON_IsString(item_devices_value))?item_devices_value->valuestring:"null"); + + if( item_ccode && item_devices && item_time && item_type + && item_devices_device && cJSON_IsString(item_devices_device ) + && item_devices tagcode && cJSON_IsString(item_devices_tagcode ) + && strcmp( item_type->valuestring, "write")== 0){ if( strcmp(item_devices_device->valuestring, "gddykz" )==0 ){ //power ctrl - if( strcmp( item_devices_tagcode->valuestring, "jbq" )== 0 ){ + if( strcmp(item_devices_tagcode->valuestring, "jbq" )== 0 ){ if( strcmp( item_devices_value->valuestring, "0" )== 0 ){ //JBQ PowerOff printf( "JBQ PowerOff .....\n" ); @@ -627,12 +649,21 @@ char* handle_iot_rx_data( char * data, int sz ) //xj printf( "\n\n---------XJ, ID:%s, start:%s\n", item_devices_tagcode->valuestring, item_devices_value->valuestring ); - if( strcmp( item_devices_value->valuestring, "1") == 0 ){ + item_devices_type = cJSON_GetObjectItem( item_devices, "checkType" ); + if( item_devices_type && cJSON_IsString( item_devices_type) + && strcmp( item_devices_type->valuestring, "remote") == 0 ){ + //remote read status pkt + AM_LOGI( "remote request status data, taskId:%s", item_devices_tagcode->valuestring ); + char *rpt_str = wrap_status_pkt( item_devices_tagcode->valuestring ); + iot_tx_data( rpt_str, strlen( rpt_str ) ); + free( rpt_str ); + }else if( strcmp( item_devices_value->valuestring, "1") == 0 ){ dev_wh_create( item_devices_tagcode->valuestring ); + resp = wrap_ctrl_resp_json( item_ccode->valuestring, item_devices_device->valuestring, "true", item_devices_tagcode->valuestring, item_devices_value->valuestring, NULL, "write" ); }else if( strcmp( item_devices_value->valuestring, "0") == 0 ){ dev_wh_delete( item_devices_tagcode->valuestring ); + resp = wrap_ctrl_resp_json( item_ccode->valuestring, item_devices_device->valuestring, "true", item_devices_tagcode->valuestring, item_devices_value->valuestring, NULL, "write" ); } - resp = wrap_ctrl_resp_json( item_ccode->valuestring, item_devices_device->valuestring, "true", item_devices_tagcode->valuestring, item_devices_value->valuestring, NULL, "write" ); }else if( strcmp(item_devices_device->valuestring, "lock" )==0 ){ // remote lock/unlock printf( "remote %s !\n", strcmp(item_devices_value->valuestring,"1" )==0?"Lock":"Unlock" ); @@ -730,7 +761,8 @@ char* wrap_connect_pkt( void ) return NULL; } - cJSON_AddStringToObject( root, "sn", g_dev.uuid ); + //cJSON_AddStringToObject( root, "sn", g_dev.uuid ); + cJSON_AddStringToObject( root, "sn", "SN1234" ); cJSON_AddStringToObject( root, "code", "yinuo" ); s_data = cJSON_Print(root); @@ -740,7 +772,7 @@ char* wrap_connect_pkt( void ) } //常态报文 -char* wrap_status_pkt( void ) +char* wrap_status_pkt( char *task_id ) { cJSON *root; cJSON *rpt; @@ -772,6 +804,8 @@ char* wrap_status_pkt( void ) root = rpt_pkt_encrypted( 1, p_data ); if( root ){ + if( task_id ) + cJSON_AddStringToObject( root, "taskId", task_id ); s_pkt = cJSON_Print( root ); cJSON_Delete(root ); }else{ diff --git a/app/src/lcd.c b/app/src/lcd.c index eb0c0d5..9718ffd 100644 --- a/app/src/lcd.c +++ b/app/src/lcd.c @@ -379,7 +379,19 @@ int32_t lcd_write_date_time( void ) int32_t lcd_write_sensor_info( int sensor, float data ) { char cmd[100]; - if( sensor == 7 ){ + if( sensor == 1 ){ + sprintf( cmd, "main.t12.txt=\"%0.1f\"\xFF\xFF\xFF", data ); lcd_write( &s_lcd, cmd, strlen(cmd) ); + }else if( sensor == 2 ){ + sprintf( cmd, "main.t13.txt=\"%0.1f\"\xFF\xFF\xFF", data ); lcd_write( &s_lcd, cmd, strlen(cmd) ); + }else if( sensor == 3 ){ + sprintf( cmd, "main.t14.txt=\"%0.1f\"\xFF\xFF\xFF", data ); lcd_write( &s_lcd, cmd, strlen(cmd) ); + }else if( sensor == 4 ){ + sprintf( cmd, "main.t22.txt=\"%0.1f\"\xFF\xFF\xFF", data ); lcd_write( &s_lcd, cmd, strlen(cmd) ); + }else if( sensor == 5 ){ + sprintf( cmd, "main.t23.txt=\"%0.1f\"\xFF\xFF\xFF", data ); lcd_write( &s_lcd, cmd, strlen(cmd) ); + }else if( sensor == 6 ){ + sprintf( cmd, "main.t24.txt=\"%0.1f\"\xFF\xFF\xFF", data ); lcd_write( &s_lcd, cmd, strlen(cmd) ); + }else if( sensor == 7 ){ sprintf( cmd, "main.t31.txt=\"%0.1f\"\xFF\xFF\xFF", data ); lcd_write( &s_lcd, cmd, strlen(cmd) ); }else if( sensor == 8 ){ sprintf( cmd, "main.t32.txt=\"%0.1f\"\xFF\xFF\xFF", data ); lcd_write( &s_lcd, cmd, strlen(cmd) ); diff --git a/app/src/sensor.c b/app/src/sensor.c index 7a6fa38..0922e74 100644 --- a/app/src/sensor.c +++ b/app/src/sensor.c @@ -41,7 +41,7 @@ int read_ac_info( float vol[2], float curr[2] ) { int fd; int ret = -1; - uint8_t data[8]; + float float_data[4]; fd = open( RN8302_DEV_PATH, O_RDONLY ); if( fd < 0 ){ @@ -49,11 +49,11 @@ int read_ac_info( float vol[2], float curr[2] ) return -1; } - if( read( fd, &data[0], 8 ) == 8 ){ - vol[0] = (data[0]<<8)|data[1]; - vol[1] = (data[2]<<8)|data[3]; - curr[0] = (data[4]<<8)|data[5]; - curr[1] = (data[6]<<8)|data[7]; + if( read( fd, &float_data[0], sizeof(float_data) ) == sizeof(float_data) ){ + vol[0] = float_data[0]; + vol[1] = float_data[1]; + curr[0] = float_data[2]; + curr[1] = float_data[3]; ret = 0; //AM_LOGD( "read IR:%d\n", data ); }else{ @@ -109,9 +109,19 @@ int read_temperature( float *temperature, float*humidity ) } if( read( fd, data, 4 ) == 4 ){ + + h = (float)data[0]; + p = (uint8_t)data[1]; + if(p>100 ) + h += p/1000.0f; + else if( p>10 ) + h += p/100.0f; + else + h += p/10.0f; + //8位整数+1位符号位+7位小数位 - t = (float)data[0]; - p = (uint8_t)data[1]&0x7f; + t = (float)data[2]; + p = (uint8_t)data[3]&0x7f; if(p>100 ) t += p/1000.0f; @@ -120,18 +130,9 @@ int read_temperature( float *temperature, float*humidity ) else t += p/10.0f; - if( data[1]&0x80 ) + if( data[3]&0x80 ) t = -t; - h = (float)data[2]; - p = (uint8_t)data[3]; - if(p>100 ) - h += p/1000.0f; - else if( p>10 ) - h += p/100.0f; - else - h += p/10.0f; - if( temperature ) *temperature = t; if( humidity ) *humidity = h; AM_LOGD( "TEM:%.2f, %.2f", t, h ); @@ -149,6 +150,12 @@ void sensor_read_cb( uv_work_t *req, int status ) if( s_sensor.data_ready ){ g_dev.sensor_data.temperature = s_sensor.tem; g_dev.sensor_data.humidity= s_sensor.humidity; + g_dev.sensor_data.humidity= s_sensor.humidity; + g_dev.sensor_data.ac_curr[0] = s_sensor.ac_curr[0]; + g_dev.sensor_data.ac_curr[1] = s_sensor.ac_curr[1]; + g_dev.sensor_data.ac_vol[0] = s_sensor.ac_vol[0]; + g_dev.sensor_data.ac_vol[1] = s_sensor.ac_vol[1]; + s_sensor.data_ready = 0; if( g_dev.sensor_data.ir != s_sensor.ir){ AM_LOGI( "IR -> %d", s_sensor.ir);