void DataInitiate() { uint8_t i,j; uint8_t buf[2],fd; for(i=0; i<MaxHopsPossible; i++) { if(cache[i] == Gateway) { break; } } uint8_t temp[szMax]; temp[0]=TTL+1; for(j=1; j<i+2; j++) { temp[j]=cache[j-1]; } fd=nrk_open(FIREFLY_SENSOR_BASIC,READ); //wait_ms(1); nrk_set_status(fd, SENSOR_SELECT, TEMP); nrk_read(fd, &buf[0], 2); temp[j++] = buf[1]; temp[j++] = buf[0]; nrk_set_status(fd, SENSOR_SELECT, LIGHT); nrk_read(fd, &buf[0], 2); temp[j++] = buf[1]; temp[j] = buf[0]; nrk_close(fd); DataTx(temp,i+6,0); }
uint16_t get_eeg_val() { uint8_t eeg_val[2]; uint8_t retval; retval=nrk_read(eeg_adc_fd,&eeg_val,2); //TODO: ensure correct byte ordering if(retval==NRK_ERROR) nrk_kprintf("Failed to read ADC for EEG\r\n"); printf("get_eeg_val: %d\r\n", eeg_val); return eeg_val; }
void tx_task() { int8_t v,fd; uint8_t len,cnt,chan; printf( "Tx Task PID=%u\r\n",nrk_get_pid()); while(!tdma_started()) nrk_wait_until_next_period(); v=tdma_tx_slot_add(mac_address); cnt=0; while(1) { // Open ADC device as read fd = nrk_open (FIREFLY_SENSOR_BASIC, READ); nrk_wait_until_next_period(); // allow sensor to warm up if (fd == NRK_ERROR) nrk_kprintf (PSTR ("Failed to open sensor driver\r\n")); v = nrk_set_status (fd, SENSOR_SELECT, BAT); v = nrk_read (fd, &bat, 2); v = nrk_set_status (fd, SENSOR_SELECT, LIGHT); v = nrk_read (fd, &light, 2); v = nrk_set_status (fd, SENSOR_SELECT, TEMP); v = nrk_read (fd, &temp, 2); v = nrk_set_status (fd, SENSOR_SELECT, ACC_X); v = nrk_read (fd, &adxl_x, 2); v = nrk_set_status (fd, SENSOR_SELECT, ACC_Y); v = nrk_read (fd, &adxl_y, 2); v = nrk_set_status (fd, SENSOR_SELECT, ACC_Z); v = nrk_read (fd, &adxl_z, 2); v = nrk_set_status (fd, SENSOR_SELECT, AUDIO_P2P); v = nrk_read (fd, &mic, 2); nrk_close(fd); // Build a sensor packet sprintf (tx_buf, "S MAC: %u bat: %u light: %u temp: %u audio: %u adxl: %u %u %u\r\n", (uint16_t) (mac_address & 0xffff), bat, light, temp, mic, adxl_x, adxl_y, adxl_z); len=strlen(tx_buf); v=tdma_send(&tx_tdma_fd, &tx_buf, len, TDMA_BLOCKING ); if(v==NRK_OK) { printf ("%s", tx_buf); } } }
void Task1() { uint16_t cnt; int8_t i,fd,val; uint16_t buf; printf( "My node's address is %d\r\n",NODE_ADDR ); printf( "Task1 PID=%d\r\n",nrk_get_pid()); cnt=0; while(1) { // Open ADC device as read fd=nrk_open(FIREFLY_SENSOR_BASIC,READ); if(fd==NRK_ERROR) nrk_kprintf(PSTR("Failed to open sensor driver\r\n")); nrk_led_toggle(BLUE_LED); // Example of setting a sensor val=nrk_set_status(fd,SENSOR_SELECT,BAT); // Read battery first while other sensors warm up val=nrk_read(fd,&buf,2); printf( "Task bat=%d",buf); val=nrk_set_status(fd,SENSOR_SELECT,LIGHT); val=nrk_read(fd,&buf,2); printf( " light=%d",buf); val=nrk_set_status(fd,SENSOR_SELECT,TEMP); val=nrk_read(fd,&buf,2); printf( " temp=%d",buf); val=nrk_set_status(fd,SENSOR_SELECT,ACC_X); val=nrk_read(fd,&buf,2); printf( " acc_x=%d",buf); val=nrk_set_status(fd,SENSOR_SELECT,ACC_Y); val=nrk_read(fd,&buf,2); printf( " acc_y=%d",buf); val=nrk_set_status(fd,SENSOR_SELECT,ACC_Z); val=nrk_read(fd,&buf,2); printf( " acc_z=%d",buf); val=nrk_set_status(fd,SENSOR_SELECT,AUDIO_P2P); nrk_spin_wait_us(60000); val=nrk_read(fd,&buf,2); printf( " audio=%d\r\n",buf); nrk_close(fd); nrk_wait_until_next_period(); cnt++; } }
void Task1() { uint16_t cnt; int8_t fd,val,chan; uint16_t buf[8]; uint16_t i, val100; printf( "My node's address is %d\r\n",NODE_ADDR ); printf( "Task1 PID=%d\r\n",nrk_get_pid()); // Open ADC device as read fd=nrk_open(ADC_DEV_MANAGER,READ); if(fd==NRK_ERROR) nrk_kprintf( PSTR("Failed to open ADC driver\r\n")); cnt=0; chan=0; i = 0; DDRF = (1 << 3); while(1) { //if((++i%16) == 0) // PORTF ^= (1 << 3); val=nrk_set_status(fd,ADC_CHAN,0); if(val==NRK_ERROR) nrk_kprintf( PSTR("Failed to set ADC status\r\n" )); val=nrk_read(fd,&buf[0],2); if(val==NRK_ERROR) nrk_kprintf( PSTR("Failed to read ADC\r\n" )); val100 = (buf[0]*63)/358; //printf("chan:%d=%d | %d.%d\r\n",chan,val100, val100/100, val100%100); printf("%d\r\n", val100); if(val100 > 90) nrk_led_set(GREEN_LED); else nrk_led_clr(GREEN_LED); nrk_wait_until_next_period(); cnt++; } }
void Task1() { nrk_led_clr(ORANGE_LED); nrk_led_clr(BLUE_LED); nrk_led_clr(GREEN_LED); nrk_led_clr(RED_LED); while(1) { nrk_led_toggle(ORANGE_LED); fd=nrk_open(FIREFLY_SENSOR_BASIC,READ); wait_ms(1); val = nrk_set_status(fd, SENSOR_SELECT, TEMP); val = nrk_read(fd, &buf[0], 2); adc_int = buf[0] + (buf[1]<<8); printf(" temp: %d\r\n", adc_int); nrk_close(fd); nrk_wait_until_next_period(); } }
void collect_Audio_Data(){ printf( "Task PID=%u\r\n",nrk_get_pid()); int8_t fd, val, index; uint16_t buf; // Open ADC device as read fd=nrk_open(FIREFLY_3_SENSOR_BASIC,READ); if(fd==NRK_ERROR) nrk_kprintf(PSTR("Failed to open sensor driver\r\n")); while(1) { val=nrk_set_status(fd,SENSOR_SELECT,AUDIO); //8 KHZ sensor nrk_spin_wait_us(125); val=nrk_read(fd,&buf,2); // printf( " audio=%d\r\n", buf); nrk_led_clr(BLUE_LED); nrk_led_clr(RED_LED); nrk_led_toggle(RED_LED); audio_data[index] = buf; index++; if(index == audio_data_size) { index =0; calculate_rms(audio_data, audio_data_size); // nrk_wait_until_next_period(); } } }
void tx_task () { uint8_t j, i, val, cnt; int8_t len; int8_t v,fd; uint8_t buf[2]; nrk_sig_mask_t ret; nrk_time_t t; // Set Port D.0 as input // On the FireFly nodes we use this for motion or syntonistor input // It can be interrupt driven, but we don't do this with TDMA since updates are so fast anyway // DDRD &= ~(0x1); printf ("tx_task PID=%d\r\n", nrk_get_pid ()); // setup a software watch dog timer t.secs=10; t.nano_secs=0; nrk_sw_wdt_init(0, &t, NULL); nrk_sw_wdt_start(0); while (!tdma_started ()) nrk_wait_until_next_period (); // set port G as input for gpio DDRG=0; while (1) { nrk_led_clr(RED_LED); tx_pkt.payload[0] = 1; // ELEMENTS tx_pkt.payload[1] = 3; // Key fd=nrk_open(FIREFLY_3_SENSOR_BASIC,READ); if(fd==NRK_ERROR) nrk_kprintf(PSTR("Failed to open sensor driver\r\n")); val=nrk_set_status(fd,SENSOR_SELECT,MOTION); val=nrk_read(fd,&buf,2); tx_pkt.payload[28]=buf[1]; tx_pkt.payload[29]=buf[0]; val=nrk_set_status(fd,SENSOR_SELECT,BAT); val=nrk_read(fd,&buf,2); tx_pkt.payload[2]=buf[1]; tx_pkt.payload[3]=buf[0]; //printf( "Task bat=%d",buf); // tx_pkt.payload[2]=0; // tx_pkt.payload[3]=0; val=nrk_set_status(fd,SENSOR_SELECT,LIGHT); val=nrk_read(fd,&buf,2); tx_pkt.payload[4]=buf[1]; tx_pkt.payload[5]=buf[0]; //printf( " light=%d",buf); val=nrk_set_status(fd,SENSOR_SELECT,TEMP); val=nrk_read(fd,&buf,2); tx_pkt.payload[6]=buf[1]; tx_pkt.payload[7]=buf[0]; //printf( " temp=%d",buf); val=nrk_set_status(fd,SENSOR_SELECT,ACC_X); val=nrk_read(fd,&buf,2); tx_pkt.payload[8]=buf[1]; tx_pkt.payload[9]=buf[0]; //printf( " acc_x=%d",buf); val=nrk_set_status(fd,SENSOR_SELECT,ACC_Y); val=nrk_read(fd,&buf,2); tx_pkt.payload[10]=buf[1]; tx_pkt.payload[11]=buf[0]; //printf( " acc_y=%d",buf); val=nrk_set_status(fd,SENSOR_SELECT,ACC_Z); val=nrk_read(fd,&buf,2); tx_pkt.payload[12]=buf[1]; tx_pkt.payload[13]=buf[0]; //printf( " acc_z=%d",buf); val=nrk_set_status(fd,SENSOR_SELECT,HUMIDITY); val=nrk_read(fd,&buf,4); tx_pkt.payload[16]=buf[3]; tx_pkt.payload[17]=buf[2]; tx_pkt.payload[18]=buf[1]; tx_pkt.payload[19]=buf[0]; val=nrk_set_status(fd,SENSOR_SELECT,TEMP2); val=nrk_read(fd,&buf,4); tx_pkt.payload[20]=buf[3]; tx_pkt.payload[21]=buf[2]; tx_pkt.payload[22]=buf[1]; tx_pkt.payload[23]=buf[0]; val=nrk_set_status(fd,SENSOR_SELECT,PRESS); val=nrk_read(fd,&buf,4); tx_pkt.payload[24]=buf[3]; tx_pkt.payload[25]=buf[2]; tx_pkt.payload[26]=buf[1]; tx_pkt.payload[27]=buf[0]; val=nrk_set_status(fd,SENSOR_SELECT,AUDIO_P2P); val=nrk_read(fd,&buf,2); tx_pkt.payload[14]=buf[1]; tx_pkt.payload[15]=buf[0]; // GPIO data // gpio_pin = !!(PINE & 0x4); gpio_pin = PING & 0x1; tx_pkt.payload[30]=gpio_pin; //printf( " audio=%d\r\n",buf); nrk_close(fd); tx_pkt.payload_len=31; tx_pkt.src_mac=mac_address; tx_pkt.dst_mac=0; tx_pkt.type=APP; len=pkt_to_buf(&tx_pkt,&tx_buf ); if(len>0) { v = tdma_send (&tx_tdma_fd, &tx_buf, len, TDMA_BLOCKING); if (v == NRK_OK) { } } else { nrk_kprintf(PSTR("Pkt tx error\r\n")); nrk_wait_until_next_period(); } nrk_sw_wdt_update(0); } }
void act_recog () { uint16_t cnt; int8_t i,fd,val, flag, sit_count, stand_still, walk_p_count, walk_n_count, run_p_count, run_n_count; uint16_t buf; uint64_t bbuf; int16_t y_axis[100]; i = 0; printf( "log:act_recog PID=%d\r\n",nrk_get_pid()); // Open ADC device as read fd=nrk_open(FIREFLY_3_SENSOR_BASIC,READ); if(fd==NRK_ERROR) nrk_kprintf(PSTR("Failed to open sensor driver\r\n")); flag = 0; cnt=0; sit_count = 0; stand_still = 0; walk_p_count = 0; run_p_count = 0; walk_n_count = 0; run_n_count = 0; while(1) { // Example of setting a sensor val=nrk_set_status(fd,SENSOR_SELECT,ACC_Y); val=nrk_read(fd,&buf,2); y_axis[i] = buf - 430; if (y_axis[i] <= 40 && y_axis[i] > -20) stand_still++; if (y_axis[i] <= 90 && y_axis[i] > 40) sit_count++; else if (y_axis[i] >= 30 && y_axis[i] <= 125) walk_p_count++; else if (y_axis[i] >= 150 && y_axis[i] <= 200) run_p_count++; else if (y_axis[i] >= -250 && y_axis[i] <= -175) walk_n_count++; else if (y_axis[i] >= -350 && y_axis[i] <= -300) run_n_count++; if (i == 99) { if (sit_count > 75 && stand_still < 45 && walk_p_count < 3 && run_p_count < 3) flag = 1; else if (stand_still >= 45 && walk_p_count < 3 && run_p_count < 3) flag = 2; else if (((walk_p_count + walk_n_count) >= 3) && ((run_p_count + run_n_count) <= 3) && (sit_count < 40)) flag = 3; else if ((run_p_count + run_n_count) > 3) flag = 4; if (log_g) { if (flag == 3) printf("ACT:%d:%d\r\n", flag, walk_p_count); else if (flag == 4) printf("ACT:%d:%d\r\n", flag, run_p_count+run_n_count); else printf("ACT:%d\r\n", flag); } flag = 0; sit_count = 0; stand_still = 0; walk_p_count = 0; run_p_count = 0; walk_n_count = 0; run_n_count = 0; i = 0; } i++; //nrk_close(fd); nrk_wait_until_next_period(); cnt++; } }
void tx_task () { uint8_t j, i, val, cnt; int8_t len; int8_t v,fd; uint8_t buf[2]; nrk_sig_t tx_done_signal; nrk_sig_mask_t ret; nrk_time_t r_period; // Set Port D.0 as input // On the FireFly nodes we use this for motion or syntonistor input // It can be interrupt driven, but we don't do this with TDMA since updates are so fast anyway DDRD &= ~(0x1); printf ("tx_task PID=%d\r\n", nrk_get_pid ()); // Wait until the tx_task starts up bmac // This should be called by all tasks using bmac that while (!tdma_started ()) nrk_wait_until_next_period (); while (1) { fd=nrk_open(FIREFLY_SENSOR_BASIC,READ); if(fd==NRK_ERROR) nrk_kprintf(PSTR("Failed to open sensor driver\r\n")); tx_pkt.payload[0] = 1; // ELEMENTS tx_pkt.payload[1] = 3; // Key val=nrk_set_status(fd,SENSOR_SELECT,BAT); val=nrk_read(fd,&buf,2); tx_pkt.payload[2]=buf[1]; tx_pkt.payload[3]=buf[0]; //printf( "Task bat=%d",buf); val=nrk_set_status(fd,SENSOR_SELECT,LIGHT); val=nrk_read(fd,&buf,2); tx_pkt.payload[4]=buf[1]; tx_pkt.payload[5]=buf[0]; //printf( " light=%d",buf); val=nrk_set_status(fd,SENSOR_SELECT,TEMP); val=nrk_read(fd,&buf,2); tx_pkt.payload[6]=buf[1]; tx_pkt.payload[7]=buf[0]; //printf( " temp=%d",buf); val=nrk_set_status(fd,SENSOR_SELECT,ACC_X); val=nrk_read(fd,&buf,2); tx_pkt.payload[8]=buf[1]; tx_pkt.payload[9]=buf[0]; //printf( " acc_x=%d",buf); val=nrk_set_status(fd,SENSOR_SELECT,ACC_Y); val=nrk_read(fd,&buf,2); tx_pkt.payload[10]=buf[1]; tx_pkt.payload[11]=buf[0]; //printf( " acc_y=%d",buf); val=nrk_set_status(fd,SENSOR_SELECT,ACC_Z); val=nrk_read(fd,&buf,2); tx_pkt.payload[12]=buf[1]; tx_pkt.payload[13]=buf[0]; //printf( " acc_z=%d",buf); val=nrk_set_status(fd,SENSOR_SELECT,AUDIO_P2P); nrk_spin_wait_us(60000); val=nrk_read(fd,&buf,2); tx_pkt.payload[14]=buf[1]; tx_pkt.payload[15]=buf[0]; // GPIO data gpio_pin = (PIND & 0x1); tx_pkt.payload[16]=gpio_pin; //printf( " audio=%d\r\n",buf); tx_pkt.payload_len=17; nrk_close(fd); tx_pkt.src_mac=mac_address; tx_pkt.dst_mac=0; tx_pkt.type=APP; len=pkt_to_buf(&tx_pkt,&tx_buf ); if(len>0) { v = tdma_send (&tx_tdma_fd, &tx_buf, len, TDMA_BLOCKING); if (v == NRK_OK) { //nrk_kprintf (PSTR ("App Packet Sent\n")); } } else nrk_wait_until_next_period(); } }
int8_t transducer_handler(TRANSDUCER_MSG_T *in_msg, TRANSDUCER_MSG_T *out_msg) { int8_t fd,val,i; uint8_t value; if(in_msg->type==TRAN_LED_BLINK) { value=in_msg->payload[0]; if((value&TRAN_RED_LED_MASK)!=0) nrk_led_set(RED_LED); if((value&TRAN_GREEN_LED_MASK)!=0) nrk_led_set(GREEN_LED); if((value&TRAN_BLUE_LED_MASK)!=0) nrk_led_set(BLUE_LED); if((value&TRAN_ORANGE_LED_MASK)!=0) nrk_led_set(ORANGE_LED); nrk_wait_until_ticks(50); nrk_led_clr(RED_LED); nrk_led_clr(GREEN_LED); nrk_led_clr(BLUE_LED); nrk_led_clr(ORANGE_LED); out_msg->type=TRAN_ACK; return 1; } else if(in_msg->type==TRAN_FF_BASIC_SHORT) { FF_SENSOR_SHORT_PKT_T s; debug_stats.sensor_samples++; // Open ADC device as read fd=nrk_open(FIREFLY_SENSOR_BASIC,READ); if(fd==NRK_ERROR) nrk_kprintf(PSTR("Failed to open sensor driver\r\n")); val=nrk_set_status(fd,SENSOR_SELECT,BAT); val=nrk_read(fd,&tmp,2); s.battery=(uint8_t)(tmp-100); // subtract 1 volt to fit in 8 bits // Wait for Audio sensor to stabalize nrk_wait_ticks(100); val=nrk_set_status(fd,SENSOR_SELECT,LIGHT); val=nrk_read(fd,&(s.light),1); val=nrk_set_status(fd,SENSOR_SELECT,TEMP); val=nrk_read(fd,&tmp,2); if(tmp<TEMPERATURE_OFFSET) tmp=0; else tmp-=TEMPERATURE_OFFSET; // subtract offset and divide by 2 #ifndef FIREFLY_2_3 tmp=tmp>>1; #endif if(tmp>255) tmp=255; s.temperature=tmp; max_delta=0; val=nrk_set_status(fd,SENSOR_SELECT,ACC_X); val=nrk_read(fd,&buf,2); if(buf> adxl_prev_x ) tmp=buf-adxl_prev_x; else tmp=adxl_prev_x-buf; adxl_prev_x=buf; if(tmp>max_delta) max_delta=tmp; val=nrk_set_status(fd,SENSOR_SELECT,ACC_Y); val=nrk_read(fd,&buf,2); if(buf> adxl_prev_y ) tmp=buf-adxl_prev_y; else tmp=adxl_prev_y-buf; adxl_prev_y=buf; if(tmp>max_delta) max_delta=tmp; val=nrk_set_status(fd,SENSOR_SELECT,ACC_Z); val=nrk_read(fd,&buf,2); if(buf> adxl_prev_z ) tmp=buf-adxl_prev_z; else tmp=adxl_prev_z-buf; adxl_prev_z=buf; if(tmp>max_delta) max_delta=tmp; if(max_delta>255) s.acceleration=255; else s.acceleration=(uint8_t)(max_delta & 0xFF); val=nrk_set_status(fd,SENSOR_SELECT,AUDIO_P2P); val=nrk_read(fd,&tmp,2); if(tmp>255) tmp=255; s.sound_level=tmp; nrk_close(fd); // Pack up the data ff_basic_sensor_short_pack( out_msg, &s ); return 1; }
void whacky_task () { uint8_t i, len, fd; int8_t rssi, val; uint8_t *local_buf; uint16_t light, node_id, got_poll; uint8_t pos; printf ("whacky_task PID=%d\r\n", nrk_get_pid ()); // Open ADC device as read fd=nrk_open(FIREFLY_3_SENSOR_BASIC,READ); if(fd==NRK_ERROR) nrk_kprintf(PSTR("Failed to open sensor driver\r\n")); printf(PSTR("Sensor opened\r\n")); // init bmac on channel 15 bmac_init (15); printf(PSTR("bmac init\r\n")); srand(1); // This sets the next RX buffer. // This can be called at anytime before releasing the packet // if you wish to do a zero-copy buffer switch bmac_rx_pkt_set_buffer (rx_buf, RF_MAX_PAYLOAD_SIZE); printf(PSTR("RX buffer set\r\n")); while (1) { node_id = 0; got_poll = 0; printf(PSTR("Waiting for a Packet\r\n")); // Get the RX packet nrk_led_set (ORANGE_LED); // Wait until an RX packet is received if(!bmac_rx_pkt_ready()) { val = bmac_wait_until_rx_pkt (); } local_buf = bmac_rx_pkt_get (&len, &rssi); printf ("Got RX packet len=%d RSSI=%d [%s]\r\n", len, rssi, local_buf); // Check for a poll packet if(len>5 && local_buf[0] == 'P' && local_buf[1] == 'O' && local_buf[2] == 'L' && local_buf[3] == 'L' && local_buf[4] == ':') { // Assume that there is a space after POLL pos = 6; while(pos < len && local_buf[pos] != '\0' && local_buf[pos] >='0' && local_buf[pos]<='9') { node_id *= 10; node_id += (local_buf[pos]-'0'); pos++; } if(pos > 6) { got_poll = 1; } } nrk_led_clr (ORANGE_LED); // Release the RX buffer so future packets can arrive bmac_rx_pkt_release (); if(got_poll == 1 && node_id == MAC_ADDR) { val=nrk_set_status(fd,SENSOR_SELECT,LIGHT); val=nrk_read(fd,&light,2); sprintf (tx_buf, "Node %d Status %u", MAC_ADDR, light); nrk_led_set (BLUE_LED); val=bmac_tx_pkt(tx_buf, strlen(tx_buf)+1); if(val != NRK_OK) { nrk_kprintf(PSTR("Could not Transmit!\r\n")); } // Task gets control again after TX complete nrk_kprintf (PSTR ("Tx task sent data!\r\n")); printf("%s\r\n", tx_buf); nrk_led_clr (BLUE_LED); } } }
// sample_task - sample sensors void sample_task() { // local variable instantiation packet tx_packet; packet hello_packet; volatile int8_t val; volatile int8_t pwr_back; volatile uint8_t hw_rev; volatile uint8_t local_network_joined = FALSE; volatile uint8_t pwr_period_count = 0; volatile uint8_t temp_period_count = 0; volatile uint8_t light_period_count = 0; volatile uint8_t pwr_rcvd[3]; volatile uint8_t sensor_sampled = FALSE; volatile uint16_t local_pwr_val = 0; volatile uint16_t local_temp_val = 0; volatile uint16_t local_light_val = 0; volatile uint16_t adc_buf[2]; // print task pid printf("sample_task PID: %d.\r\n", nrk_get_pid()); // initialize sensor packet g_sensor_pkt.pwr_val = local_pwr_val; g_sensor_pkt.temp_val = local_temp_val; g_sensor_pkt.light_val = local_light_val; // initialize tx_packet tx_packet.source_id = MAC_ADDR; tx_packet.type = MSG_DATA; tx_packet.num_hops = 0; // initialize hello packet hello_packet.source_id = MAC_ADDR; hello_packet.type = MSG_HAND; hello_packet.num_hops = 0; hello_packet.payload[0] = (HARDWARE_REV >> 24) & 0xff; hello_packet.payload[1] = (HARDWARE_REV >> 16) & 0xff; hello_packet.payload[2] = (HARDWARE_REV >> 8) & 0xff; hello_packet.payload[3] = (HARDWARE_REV) & 0xff; // get the hardware rev of this node hw_rev = GET_REV(HARDWARE_REV); // Open the ATMEGA ADC device as read g_atmega_adc_fd = nrk_open(ADC_DEV_MANAGER,READ); if(NRK_ERROR == g_atmega_adc_fd) { nrk_kprintf(PSTR("Failed to open ADC driver\r\n")); } // loop forever - run th task while (1) { // check if the network has been joined local_network_joined = atomic_network_joined(); // if the network has been joined then start sampling sensors if(TRUE == local_network_joined) { // update period counts pwr_period_count++; temp_period_count++; light_period_count++; pwr_period_count %= g_pwr_period; temp_period_count %= g_temp_period; light_period_count %= g_light_period; sensor_sampled = FALSE; // sample power sensor if appropriate if((SAMPLE_SENSOR == pwr_period_count) && (HW_REV0 == hw_rev)) { // read power pwr_read(WATT, (uint8_t *)&pwr_rcvd); pwr_back = pwr_rcvd[2]; if (pwr_back < 0) { local_pwr_val = (~pwr_back) + 1; } else { local_pwr_val = pwr_back; } // // pull out dinner location // local_pwr_val = (pwr_rcvd[0] << 8) | pwr_rcvd[1]; // printf("val1: %x:%x:%x\r\n", pwr_rcvd[0], pwr_rcvd[1], pwr_rcvd[2]); // local_pwr_val = transform_pwr(local_pwr_val); // printf("val2: %d\r\n", local_pwr_val); g_sensor_pkt.pwr_val = local_pwr_val; sensor_sampled = TRUE; } // sample temperature sensor if appropriate if(SAMPLE_SENSOR == temp_period_count) { // sample analog temperature sensor via Atmega ADC if(HW_REV0 == hw_rev) { val = nrk_set_status(g_atmega_adc_fd, ADC_CHAN, CHAN_6); } else if(HW_REV1 == hw_rev) { val = nrk_set_status(g_atmega_adc_fd, ADC_CHAN, CHAN_4); } if(NRK_ERROR == val) { nrk_kprintf(PSTR("Failed to set ADC status\r\n")); } else { val = nrk_read(g_atmega_adc_fd, (uint8_t *)&adc_buf[0],2); if(NRK_ERROR == val) { nrk_kprintf(PSTR("Failed to read ADC\r\n")); } else { local_temp_val = (uint16_t)adc_buf[0]; local_temp_val = transform_temp(local_temp_val); g_sensor_pkt.temp_val = local_temp_val; sensor_sampled = TRUE; } } } // sample light sensor if appropriate if((SAMPLE_SENSOR == light_period_count) && (HW_REV1 == hw_rev)) { val = nrk_set_status(g_atmega_adc_fd, ADC_CHAN, CHAN_2); if(NRK_ERROR == val) { nrk_kprintf(PSTR("Failed to set ADC status\r\n")); } else { val = nrk_read(g_atmega_adc_fd, (uint8_t *)&adc_buf[0],2); if(NRK_ERROR == val) { nrk_kprintf(PSTR("Failed to read ADC\r\n")); } else { local_light_val = (uint16_t)adc_buf[0]; g_sensor_pkt.light_val = local_light_val; sensor_sampled = TRUE; } } } // if a sensor has been sampled, send a packet out if(TRUE == sensor_sampled) { // update sequence number tx_packet.seq_num = atomic_increment_seq_num(); // add data values to sensor packet tx_packet.payload[DATA_PWR_INDEX] = (uint8_t)((g_sensor_pkt.pwr_val >> 8) & 0xFF); tx_packet.payload[DATA_PWR_INDEX + 1] = (uint8_t)(g_sensor_pkt.pwr_val& 0xFF); tx_packet.payload[DATA_TEMP_INDEX] = (uint8_t)((g_sensor_pkt.temp_val >> 8) & 0xFF); tx_packet.payload[DATA_TEMP_INDEX + 1] = (uint8_t)(g_sensor_pkt.temp_val & 0xFF); tx_packet.payload[DATA_LIGHT_INDEX] = (uint8_t)((g_sensor_pkt.light_val >> 8) & 0xFF); tx_packet.payload[DATA_LIGHT_INDEX + 1] = (uint8_t)(g_sensor_pkt.light_val & 0xFF); tx_packet.payload[DATA_STATE_INDEX] = atomic_outlet_state(); // print the sensor info if(TRUE == g_verbose) { printf("P: %d, T: %d, L: %d\r\n", g_sensor_pkt.pwr_val, g_sensor_pkt.temp_val, g_sensor_pkt.light_val); } // add packet to data queue atomic_push(&g_data_tx_queue, &tx_packet, g_data_tx_queue_mux); } }
void print_sensor_readings() { uint16_t cnt; int8_t i,fd,val; uint16_t buf; uint16_t buf_last = 0; uint16_t motions[] = {0,0}; uint16_t index = 0; uint64_t bbuf; printf( "My node's address is %d\r\n",NODE_ADDR ); printf( "Task1 PID=%d\r\n",nrk_get_pid()); // Open ADC device as read fd=nrk_open(FIREFLY_3_SENSOR_BASIC,READ); if(fd==NRK_ERROR) nrk_kprintf(PSTR("Failed to open sensor driver\r\n")); cnt=0; while(1) { // humidity = 37% temperature(A) = 59.7F temperature(D) = 77.1F pressure= 29.85inHg light=259lm acc_x=510 acc_y=490 motion=detected audio=30 // Example of setting a sensor val=nrk_set_status(fd,SENSOR_SELECT,HUMIDITY); val=nrk_read(fd,&bbuf,4); //percent humidity // Relative Humidity (%) linear | 1 | 0 | 0 | 100 | printf( "Percent humidity (in %%)= %lu, ",bbuf); // | digital_temp | Temperature (digital sensor, F) | linear | 0.18 | 32 | 40 | 90 val=nrk_set_status(fd,SENSOR_SELECT,TEMP2); val=nrk_read(fd,&bbuf,4); uint16_t temp_converted = ((9*bbuf)/5)+ 32; printf( " Digital Temperature (in F)= %d, ", temp_converted/10); val=nrk_set_status(fd,SENSOR_SELECT,LIGHT); val=nrk_read(fd,&buf,2); // light | Incident Illumination (lumens) | linear | -1 | 1024 | 0 | 1024 char *light_type; if(buf > 1000){ light_type= "dark \r"; } else { if(buf < 580){ light_type= "daylight \r"; } else { light_type= "room-light \r"; } } printf( " Ambient light (in lumens)= %d, ",-1*buf + 1024); printf( " Light type detected as %s\r\n", light_type); val=nrk_set_status(fd,SENSOR_SELECT,PRESS); val=nrk_read(fd,&bbuf,4); // pressure | Barometric Pressure (in. Hg) | linear | 0.000295780903035 | 0 | 28 | 31 | printf( "Barometric Pressure (in. Hg)= %d, ",295*(bbuf)/1000000); // | acc_x | Acceleration - X (ft / sec^2) | linear | 1 | 0 | 0 | 50 | // | acc_y | Acceleration - Y (ft / sec^2) | linear | 1 | 0 | 0 | 50 | // | acc_z | Acceleration - Z (ft / sec^2) | linear | 1 | 0 | 0 | 50 val=nrk_set_status(fd,SENSOR_SELECT, ACC_X); val=nrk_read(fd,&buf,2); printf( " Acceleration - X (ft / sec^2) =%d, ",buf); val=nrk_set_status(fd,SENSOR_SELECT, ACC_Y); val=nrk_read(fd,&buf,2); printf( " Acceleration - Y (ft / sec^2) =%d\r\n",buf); // motion | Motion Detected (binary) | threshhold | 950 | 0 | 0 | 1 val=nrk_set_status(fd,SENSOR_SELECT, MOTION); val=nrk_read(fd,&buf,2); // printf( " Motion value =%d\r\n",buf); if(buf> 990){ printf( "Motion based on Infrared = Yes "); } else { printf( "Motion based on Infrared = No "); } uint16_t sum = 0; int size = sizeof(motions)/sizeof(motions[0]); for ( i = 0; i < size; i++ ) { sum = sum + motions[i]; } uint16_t avg = sum/size; nrk_led_clr(RED_LED); nrk_led_clr(GREEN_LED); printf( ":"); if( abs(buf - avg) >10) //if( abs(buf - buf_last) >2) { printf(" Motion based on light change = Yes \r\n" ); nrk_led_toggle(RED_LED); } else { printf(" Motion based on light change = No \r\n" ); nrk_led_toggle(GREEN_LED); } motions[index] = buf; buf_last = buf; nrk_wait_until_next_period(); cnt++; index++; if(index == size) index =0; } }