///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;
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
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;
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 )
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 );
}
}
}
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 );
}
}
}
}
}
-void rpt_timer_cb( uv_timer_t * handle );
-
void connect_timer_cb( uv_timer_t * handle )
{
iot_t *p_iot;
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 );
}
}
+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;
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{
}
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{
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 );
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 )
{
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 );
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;
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" );
//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" );
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);
}
//常态报文
-char* wrap_status_pkt( void )
+char* wrap_status_pkt( char *task_id )
{
cJSON *root;
cJSON *rpt;
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{
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) );
{
int fd;
int ret = -1;
- uint8_t data[8];
+ float float_data[4];
fd = open( RN8302_DEV_PATH, O_RDONLY );
if( fd < 0 ){
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{
}
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;
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 );
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);