int main (int argc, char *argv[]) { char buffer[128]; int v, cnt, i, size, socket, state; uint32_t target_mac; FF_POWER_PKT my_pwr_pkt; FF_ENV_PKT my_env_pkt; PKT_T my_pkt; time_t ts; if (argc < 3) { printf ("Usage: server port [-v]\n"); exit (1); } state = 0; verbose=0; if(argc==4) { if(strcmp(argv[3],"-v")==0) { printf( "Verbose on" ); verbose=1; } } v = slipstream_open (argv[1], atoi (argv[2]), BLOCKING); v = send_ping (); cnt = 0; while (1) { v = slipstream_receive (buffer); if (v > 0) { v = buf_to_pkt (buffer, &my_pkt); if (v == 1) { // printf ("TYPE: %d\n", my_pkt.type); // printf ("SRC MAC: %d\n", my_pkt.src_mac); // printf ("DST MAC: %d\n", my_pkt.dst_mac); // printf ("PAYLOAD LEN: %d\n", my_pkt.payload_len); // printf ("PAYLOAD: ["); // for (i = 0; i < my_pkt.payload_len; i++) // printf ("%d ", my_pkt.payload[i]); // printf ("]\n"); if(my_pkt.type==APP) { ts=time(NULL); switch(my_pkt.payload[1]) { case 1: power_unpack (my_pkt.payload, &my_pwr_pkt); if(verbose) printf( "(time,mac,total_secs,freq,v-rms,i-rms,t-pwr,energy,i-rms2,t-pwr2,energy2): %u,%x,%d,",ts,my_pkt.src_mac,my_pwr_pkt.total_secs ); else printf( "P,%u,%x,%d,",ts,my_pkt.src_mac,my_pwr_pkt.total_secs ); printf( "%u,%u,%u,%u,%lu",my_pwr_pkt.freq, my_pwr_pkt.rms_voltage,my_pwr_pkt.rms_current, my_pwr_pkt.true_power,my_pwr_pkt.long_energy); printf( ",%u,%u,%lu",my_pwr_pkt.rms_current1, my_pwr_pkt.true_power1,my_pwr_pkt.long_energy1); printf( ",%u,%u",my_pwr_pkt.v_p2p_low, my_pwr_pkt.v_p2p_high); printf( ",%u,%u",my_pwr_pkt.c_p2p_low, my_pwr_pkt.c_p2p_high); printf( ",%u,%u",my_pwr_pkt.c_p2p_low2, my_pwr_pkt.c_p2p_high2); printf( ",%u,%u,%u",my_pwr_pkt.v_center, my_pwr_pkt.c_center, my_pwr_pkt.c2_center); // printf ("%d,%d)", // ((float) my_pwr_pkt.true_power) / POWER_SCALER, // my_pwr_pkt.true_power); printf( "\n" ); break; case 3: env_unpack(my_pkt.payload, &my_env_pkt ); if(verbose) printf( "(time,mac,bat,light,temp,acc_x,acc_y,acc_z,audio_p2p,digital-temp,humidity,pressure,motion,gpio_state): %u,%x,%u,%u,%u,%u,%u,%u,%u,%u,%u,%u,%u,%u",ts,my_pkt.src_mac, my_env_pkt.bat, my_env_pkt.light, my_env_pkt.temp, my_env_pkt.acc_x, my_env_pkt.acc_y, my_env_pkt.acc_z, my_env_pkt.audio_p2p, my_env_pkt.digital_temp,my_env_pkt.humidity, my_env_pkt.pressure,my_env_pkt.motion,my_env_pkt.gpio_state ); else printf( "S,%u,%x,%u,%u,%u,%u,%u,%u,%u,%u,%u,%u,%u,%u",ts,my_pkt.src_mac, my_env_pkt.bat, my_env_pkt.light, my_env_pkt.temp, my_env_pkt.acc_x, my_env_pkt.acc_y, my_env_pkt.acc_z, my_env_pkt.audio_p2p,my_env_pkt.digital_temp,my_env_pkt.humidity, my_env_pkt.pressure, my_env_pkt.motion, my_env_pkt.gpio_state ); printf( "\n" ); break; case 4: printf("(time,mac,state): %u,%u,%u\n",ts,my_pkt.src_mac,my_pkt.payload[2] ); break; default: if(verbose) printf( "unkown pkt type (%d): ",my_pkt.payload[1] ); else printf( "U,%d,",my_pkt.payload[1] ); for(i=2; i<my_pkt.payload_len; i++ ) printf( ",%d",my_pkt.payload[i] ); printf( "\n" ); } } } } else printf ("Error reading packet\n"); // Count to 5 and then send an actuate packet /* cnt++; if (cnt > 5) { target_mac=0x04; socket=0; state=1; printf ("Sending an actuate command [mac=0x%x socket %d, state %d]\n",target_mac,socket,state); // Enable outlet 0 on node 0x01 outlet_actuate (target_mac, socket, state); cnt = 0; }*/ } }
int main (int argc, char *argv[]) { char buffer[128]; int v, cnt, i, size, socket, state; uint32_t target_mac; FF_POWER_PKT my_pwr_pkt; FF_ENV_PKT my_env_pkt; FF_3PHASE_PKT my_three_phase_pkt; PKT_T my_pkt; time_t ts; uint32_t small_cnt, large_cnt; if (argc < 3) { printf ("Usage: server port [-v]\n"); exit (1); } state = 0; verbose=0; if(argc==4) { if(strcmp(argv[3],"-v")==0) { printf( "Verbose on" ); verbose=1; } } v = slipstream_open (argv[1], atoi (argv[2]), BLOCKING); v = send_ping (); cnt = 0; while (1) { v = slipstream_receive (buffer); if (v > 0) { v = buf_to_pkt (buffer, &my_pkt); //printf( "pkt: %d\n",v ); if (v == 1) { /* * printf ("TYPE: %d\n", my_pkt.type); printf ("RSSI: %u\n", my_pkt.rssi); printf ("SRC MAC: %d\n", my_pkt.src_mac); printf ("DST MAC: %d\n", my_pkt.dst_mac); printf ("PAYLOAD LEN: %d\n", my_pkt.payload_len); printf ("PAYLOAD: ["); for (i = 0; i < my_pkt.payload_len; i++) printf ("%d ", my_pkt.payload[i]); printf ("]\n"); */ if(my_pkt.type==APP) { ts=time(NULL); switch(my_pkt.payload[1]) { case 1: power_unpack (my_pkt.payload, &my_pwr_pkt); if(verbose) printf( "(time,mac,rssi,total_secs,freq,v-rms,i-rms,t-pwr,energy,i-rms2,t-pwr2,energy2): %u,%x,%u,%u,",ts,my_pkt.src_mac,my_pkt.rssi,my_pwr_pkt.total_secs ); else printf( "P,%u,%x,%u,%u,",ts,my_pkt.src_mac,my_pkt.rssi,my_pwr_pkt.total_secs ); printf( "%u,%u,%u,%u,%lu",my_pwr_pkt.freq, my_pwr_pkt.rms_voltage,my_pwr_pkt.rms_current, my_pwr_pkt.true_power,my_pwr_pkt.long_energy); printf( ",%u,%u,%lu",my_pwr_pkt.rms_current1, my_pwr_pkt.true_power1,my_pwr_pkt.long_energy1); printf( ",%u,%u",my_pwr_pkt.v_p2p_low, my_pwr_pkt.v_p2p_high); printf( ",%u,%u",my_pwr_pkt.c_p2p_low, my_pwr_pkt.c_p2p_high); printf( ",%u,%u",my_pwr_pkt.c_p2p_low2, my_pwr_pkt.c_p2p_high2); printf( ",%u,%u,%u,%u",my_pwr_pkt.v_center, my_pwr_pkt.c_center, my_pwr_pkt.c2_center, my_pwr_pkt.socket_state); // printf ("%d,%d)", // ((float) my_pwr_pkt.true_power) / POWER_SCALER, // my_pwr_pkt.true_power); printf( "\n" ); break; case 3: env_unpack(my_pkt.payload, &my_env_pkt ); if(verbose) printf( "(time,mac,rssi,bat,light,temp,acc_x,acc_y,acc_z,audio_p2p,digital-temp,humidity,pressure,motion,gpio_state): %u,%x,%u,%u,%u,%u,%u,%u,%u,%u,%u,%u,%u,%u,%u",ts,my_pkt.src_mac, my_pkt.rssi,my_env_pkt.bat, my_env_pkt.light, my_env_pkt.temp, my_env_pkt.acc_x, my_env_pkt.acc_y, my_env_pkt.acc_z, my_env_pkt.audio_p2p, my_env_pkt.digital_temp,my_env_pkt.humidity, my_env_pkt.pressure,my_env_pkt.motion,my_env_pkt.gpio_state ); else printf( "S,%u,%x,%u,%u,%u,%u,%u,%u,%u,%u,%u,%u,%u,%u,%u",ts,my_pkt.src_mac, my_pkt.rssi, my_env_pkt.bat, my_env_pkt.light, my_env_pkt.temp, my_env_pkt.acc_x, my_env_pkt.acc_y, my_env_pkt.acc_z, my_env_pkt.audio_p2p,my_env_pkt.digital_temp,my_env_pkt.humidity, my_env_pkt.pressure, my_env_pkt.motion, my_env_pkt.gpio_state ); printf( "\n" ); break; case 4: printf("(time,mac,rssi,state): %u,%x,%u,%u\n",ts,my_pkt.src_mac,my_pkt.rssi,my_pkt.payload[2] ); break; case 5: if(verbose) printf("(time,mac,rssi,gain): %u,%x,%u,%u\n",ts,my_pkt.src_mac,my_pkt.rssi, my_pkt.payload[2] ); else printf("B,%u,%u,%u,%u\n",ts,my_pkt.src_mac,my_pkt.rssi,my_pkt.payload[2] ); break; case 6: // Payload index 2 represents counts per minute if(verbose) printf("(time,mac,rssi,cpm): %u,%x,%u\n",ts,my_pkt.src_mac,my_pkt.rssi,my_pkt.payload[2] ); else printf("R,%u,%x,%u,%u\n",ts,my_pkt.src_mac,my_pkt.rssi,my_pkt.payload[2] ); break; case 7: // Payload index 2,3,4,5 represent a little endian value for small particles counted // Payload index 6,7,8,9 represent a little endian value for large particles counted small_cnt=my_pkt.payload[2] | my_pkt.payload[3]<<8 | my_pkt.payload[4]<<16 | my_pkt.payload[5]<<24; large_cnt=my_pkt.payload[6] | my_pkt.payload[7]<<8 | my_pkt.payload[8]<<16 | my_pkt.payload[9]<<24; if(verbose) printf("(time,mac,rssi,small_cnt,large_cnt): %u,%x,%u,%u,%u\n",ts,my_pkt.src_mac,my_pkt.rssi,small_cnt,large_cnt ); else printf("A,%u,%x,%u,%u,%u\n",ts,my_pkt.src_mac,my_pkt.rssi,small_cnt,large_cnt); break; case 8: three_phase_unpack (my_pkt.payload, &my_three_phase_pkt); if(verbose) printf( "3 Phase meter (time,mac,rssi,total_secs,period,awatthr,awatt,ava,avrms,airms,bwatt,bwatthr,bva,bvrms,birms,cwatt,cwatthr,cva,cvrms,cirms): %u,%x,%u,%u,",ts,my_pkt.src_mac,my_pkt.rssi,my_three_phase_pkt.total_secs ); else printf( "M,%u,%x,%u,%u,",ts,my_pkt.src_mac,my_pkt.rssi,my_three_phase_pkt.total_secs ); printf( "%u,%d,%d,%d,%d,%d,",my_three_phase_pkt.period, my_three_phase_pkt.awatthr,my_three_phase_pkt.awatt,my_three_phase_pkt.ava,my_three_phase_pkt.avrms,my_three_phase_pkt.airms ); printf( "%d,%d,%d,%d,%d,",my_three_phase_pkt.bwatthr,my_three_phase_pkt.bwatt,my_three_phase_pkt.bva,my_three_phase_pkt.bvrms,my_three_phase_pkt.birms ); printf( "%d,%d,%d,%d,%d",my_three_phase_pkt.cwatthr,my_three_phase_pkt.cwatt,my_three_phase_pkt.cva,my_three_phase_pkt.cvrms,my_three_phase_pkt.cirms ); printf( "\n" ); break; default: if(verbose) printf( "unkown pkt type (%d): ",my_pkt.payload[1] ); else printf( "U,%d",my_pkt.payload[1] ); for(i=2; i<my_pkt.payload_len; i++ ) printf( ",%d",my_pkt.payload[i] ); printf( "\n" ); } } } } else printf ("Error reading packet\n"); // Count to 5 and then send an actuate packet /* cnt++; if (cnt > 5) { target_mac=0x04; socket=0; state=1; printf ("Sending an actuate command [mac=0x%x socket %d, state %d]\n",target_mac,socket,state); // Enable outlet 0 on node 0x01 outlet_actuate (target_mac, socket, state); cnt = 0; }*/ } }