Ejemplo n.º 1
0
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);
		}
	}
}
Ejemplo n.º 2
0
void rx_task ()
{
    nrk_time_t t;
    uint16_t cnt;
    int8_t v;
    uint8_t len, i;
    uint8_t chan;


    cnt = 0;
    nrk_kprintf (PSTR ("Nano-RK Version "));
    printf ("%d\r\n", NRK_VERSION);


    nrk_kprintf( PSTR( "RX Task PID=" ));
    printf ("%u\r\n", nrk_get_pid ());
    t.secs = 5;
    t.nano_secs = 0;

    while (cal_done==0)
        nrk_wait_until_next_period ();


    chan = RADIO_CHANNEL;
    if (SET_MAC == 0x00) {

        v = read_eeprom_mac_address (&mac_address);
        if (v == NRK_OK) {
            v = read_eeprom_channel (&chan);
            v = read_eeprom_aes_key(aes_key);
        }
        else {
            while (1) {
                nrk_kprintf (PSTR
                             ("* ERROR reading MAC address, run eeprom-set utility\r\n"));
                nrk_wait_until_next_period ();
            }
        }
    }
    else
        mac_address = SET_MAC;

    printf ("MAC ADDR: %x\r\n", mac_address & 0xffff);
    printf ("chan = %d\r\n", chan);
    len=0;
    for(i=0; i<16; i++ ) {
        len+=aes_key[i];
    }
    printf ("AES checksum = %d\r\n", len);



    tdma_init (TDMA_CLIENT, chan, (mac_address));

    tdma_aes_setkey(aes_key);
    tdma_aes_enable();

    while (!tdma_started ())
        nrk_wait_until_next_period ();

    v = tdma_tx_slot_add (mac_address&0xff);

    if (v != NRK_OK)
        nrk_kprintf (PSTR ("Could not add slot!\r\n"));

    // setup a software watch dog timer
    t.secs=30;
    t.nano_secs=0;
    nrk_sw_wdt_init(0, &t, NULL);
    nrk_sw_wdt_start(0);
    while (1) {
        // Update watchdog timer
        nrk_sw_wdt_update(0);
        v = tdma_recv (&rx_tdma_fd, &rx_buf, &len, TDMA_BLOCKING);
        if (v == NRK_OK) {
            // printf ("src: %u\r\nrssi: %d\r\n", rx_tdma_fd.src, rx_tdma_fd.rssi);
            // printf ("slot: %u\r\n", rx_tdma_fd.slot);
            // printf ("cycle len: %u\r\n", rx_tdma_fd.cycle_size);
            v=buf_to_pkt(&rx_buf, &rx_pkt);
            if(v==NRK_OK)
            {
                if(((rx_pkt.dst_mac&0xff) == (mac_address&0xff)) || ((rx_pkt.dst_mac&0xff)==0xff))
                {
                    if(rx_pkt.type==PING)
                    {
                        send_ack=1;
                        nrk_led_clr(0);
                        nrk_led_clr(1);
                        if(rx_pkt.payload[0]==PING_1)
                        {
                            nrk_led_set(0);
                            nrk_wait_until_next_period();
                            nrk_wait_until_next_period();
                            nrk_wait_until_next_period();
                            nrk_led_clr(0);
                        }
                        if(rx_pkt.payload[0]==PING_2)
                        {
                            nrk_led_set(1);
                            nrk_wait_until_next_period();
                            nrk_wait_until_next_period();
                            nrk_wait_until_next_period();
                            nrk_led_clr(1);
                        }
                        if(rx_pkt.payload[0]==PING_PERSIST)
                        {
                            nrk_led_set(0);
                        }


                    }

                    if(rx_pkt.type==APP)
                    {
                        // payload 1: Key
                        if(rx_pkt.payload[1]==2)
                        {
                            send_ack=1;
                            // payload 2: Outlet Number
                            // payload 3: On/Off
                            if(rx_pkt.payload[3]==0) {
                                power_socket_disable(rx_pkt.payload[2]);
                                plug_led_green_clr();
                                //printf( "Disable %d\r\n", rx_pkt.payload[2] );
                            }
                            if(rx_pkt.payload[3]==1) {
                                power_socket_enable(rx_pkt.payload[2]);
                                //printf( "Enable %d\r\n", rx_pkt.payload[2] );
                                plug_led_green_set();
                            }
                        }

                        // payload 1: Key
                        if(rx_pkt.payload[1]==3)
                        {
                            send_ack=1;
                            true_power_thresh=((uint32_t)rx_pkt.payload[3])<<16 | ((uint32_t)rx_pkt.payload[4])<<8 | (uint32_t)rx_pkt.payload[5];
                            set_power_thresh(true_power_thresh);
                        }




                    }

                }

            }
            /*      printf ("len: %u\r\npayload: ", len);
            for (i = 0; i < len; i++)
              printf ("%d ", rx_buf[i]);
            printf ("\r\n");

            if(rx_buf[0]==(mac_address&0xff))
            {
            if(rx_buf[2]==0) {
            	power_socket_disable(rx_buf[1]);
            	printf( "Disable %d\r\n", rx_buf[1] );
            }
            if(rx_buf[2]==1) {
            	power_socket_enable(rx_buf[1]);
            	printf( "Enable %d\r\n", rx_buf[1] );
            }
                 }
                 */

        }

        tdma_rx_pkt_release();
        //  nrk_wait_until_next_period();
    }

}
Ejemplo n.º 3
0
void rx_task ()
{
    nrk_time_t t;
    uint16_t cnt;
    int8_t v,fd;
    uint8_t len, i;
    uint8_t chan;


    cnt = 0;
    nrk_kprintf (PSTR ("Nano-RK Version "));
    printf ("%d\r\n", NRK_VERSION);


    printf ("RX Task PID=%u\r\n", nrk_get_pid ());
    t.secs = 5;
    t.nano_secs = 0;

    chan = CHAN;
    if (SET_MAC == 0x00) {

        v = read_eeprom_mac_address (&mac_address);
        if (v == NRK_OK) {
            v = read_eeprom_channel (&chan);
        }
        else {
            while (1) {
                nrk_kprintf (PSTR
                             ("* ERROR reading MAC address, run eeprom-set utility\r\n"));
                nrk_wait_until_next_period ();
            }
        }
    }
    else
        mac_address = SET_MAC;

    printf ("MAC ADDR: %x\r\n", mac_address & 0xffff);
    printf ("chan = %d\r\n", chan);



    tdma_init (TDMA_CLIENT, chan, mac_address);


    while (!tdma_started ())
        nrk_wait_until_next_period ();

    // Set TDMA slot to lower byte of MAC address
    v = tdma_tx_slot_add (mac_address & 0xff);

    if (v != NRK_OK)
        nrk_kprintf (PSTR ("Could not add slot!\r\n"));

    while (1) {
        // Update watchdog timer
        // nrk_sw_wdt_update(0);
        v = tdma_recv (&rx_tdma_fd, &rx_buf, &len, TDMA_BLOCKING);
        if (v == NRK_OK) {
            // printf ("src: %u\r\nrssi: %d\r\n", rx_tdma_fd.src, rx_tdma_fd.rssi);
            // printf ("slot: %u\r\n", rx_tdma_fd.slot);
            // printf ("cycle len: %u\r\n", rx_tdma_fd.cycle_size);
            v=buf_to_pkt(&rx_buf, &rx_pkt);
            tdma_rx_pkt_release();
            /*if((rx_pkt.dst_mac&0xff) == (mac_address&0xff))
            {
            printf ("len: %u\r\npayload: ", len);
            for (i = 0; i < len; i++)
              printf ("%d ", rx_buf[i]);
            printf ("\r\n");




            }*/


        }

        //  nrk_wait_until_next_period();
    }

}
Ejemplo n.º 4
0
void rx_task ()
{
    nrk_time_t t;
    uint16_t cnt;
    int8_t v;
    uint8_t len, i;
    uint8_t chan;


    cnt = 0;
    nrk_kprintf (PSTR ("Nano-RK Version "));
    printf ("%d\r\n", NRK_VERSION);


    printf ("RX Task PID=%u\r\n", nrk_get_pid ());
    t.secs = 5;
    t.nano_secs = 0;

    chan = CHAN;
    if (SET_MAC == 0x00) {

        v = read_eeprom_mac_address (&mac_address);
        if (v == NRK_OK) {
            v = read_eeprom_channel (&chan);
            v=read_eeprom_aes_key(aes_key);
        }
        else {
            while (1) {
                nrk_kprintf (PSTR
                             ("* ERROR reading MAC address, run eeprom-set utility\r\n"));
                nrk_wait_until_next_period ();
            }
        }
    }
    else
        mac_address = SET_MAC;

    printf ("MAC ADDR: %x\r\n", mac_address & 0xffff);
    printf ("chan = %d\r\n", chan);
    len=0;
    for(i=0; i<16; i++ ) {
        len+=aes_key[i];
    }
    printf ("AES checksum = %d\r\n", len);



    tdma_init (TDMA_CLIENT, chan, mac_address);

    tdma_aes_setkey(aes_key);
    tdma_aes_enable();


    while (!tdma_started ())
        nrk_wait_until_next_period ();

    // Mask off lower byte of MAC address for TDMA slot
    // FIXME: This should eventually be lower 2 bytes for larger TDMA cycles
    v = tdma_tx_slot_add (mac_address&0xFF);

    if (v != NRK_OK)
        nrk_kprintf (PSTR ("Could not add slot!\r\n"));

    while (1) {
        // Update watchdog timer
        // nrk_sw_wdt_update(0);
        v = tdma_recv (&rx_tdma_fd, &rx_buf, &len, TDMA_BLOCKING);
        if (v == NRK_OK) {
            // printf ("src: %u\r\nrssi: %d\r\n", rx_tdma_fd.src, rx_tdma_fd.rssi);
            // printf ("slot: %u\r\n", rx_tdma_fd.slot);
            // printf ("cycle len: %u\r\n", rx_tdma_fd.cycle_size);
            v=buf_to_pkt(&rx_buf, &rx_pkt);

            /*
                  printf ("raw len: %u\r\nraw buf: ", len);
                  for (i = 0; i < len; i++)
                    printf ("%d ", rx_buf[i]);
                  printf ("\r\n");
            */
            if(rx_pkt.type==PING && (((rx_pkt.dst_mac&0xff) == (mac_address&0xff)) || ((rx_pkt.dst_mac&0xff)==0xff)))
            {
                send_ack=1;
                nrk_led_clr(0);
                nrk_led_clr(1);
                if(rx_pkt.payload[0]==PING_1)
                {
                    nrk_led_set(0);
                    nrk_wait_until_next_period();
                    nrk_wait_until_next_period();
                    nrk_wait_until_next_period();
                    nrk_led_clr(0);
                }
                if(rx_pkt.payload[0]==PING_2)
                {
                    nrk_led_set(1);
                    nrk_wait_until_next_period();
                    nrk_wait_until_next_period();
                    nrk_wait_until_next_period();
                    nrk_led_clr(1);
                }
                if(rx_pkt.payload[0]==PING_PERSIST)
                {
                    nrk_led_set(0);
                }


            }
            if(rx_pkt.type==NW_CONFIG && (((rx_pkt.dst_mac&0xff) == (mac_address&0xff)) || (rx_pkt.dst_mac==0xff)))
            {

                /*      printf ("payload len: %u\r\npayload: ", rx_pkt.payload_len);
                      for (i = 0; i < rx_pkt.payload_len; i++)
                        printf ("%d ", rx_pkt.payload[i]);
                      printf ("\r\n");
                */
                // Sleep control packet
                if(rx_pkt.payload[1]==NW_CONFIG_SLEEP)
                {
                    // sleep mode off
                    if(rx_pkt.payload[2]==0 )
                    {
                        // Enter Deep Sleep
                        deep_sleep_button();

                    }
                    if(rx_pkt.payload[2]==2 )
                    {
                        // Enter snooze mode
                        tdma_disable();
                        nrk_wait_until_next_period();
                        tdma_snooze();
                        tdma_enable();

                    }


                }

            }

        }

        //  nrk_wait_until_next_period();
    }

}