Difference between revisions of "RSS measurement"

From Contiki
Jump to: navigation, search
(Code)
Line 71: Line 71:
 
static struct collect_conn tc;
 
static struct collect_conn tc;
  
/*---------------------------------------------------------------------------*/
 
 
PROCESS(example_collect_process, "RSS Measurement");
 
PROCESS(example_collect_process, "RSS Measurement");
 
AUTOSTART_PROCESSES(&example_collect_process);
 
AUTOSTART_PROCESSES(&example_collect_process);
/*---------------------------------------------------------------------------*/
+
 
static void
+
static void recv(const rimeaddr_t *originator, uint8_t seqno, uint8_t hops)
recv(const rimeaddr_t *originator, uint8_t seqno, uint8_t hops)
+
 
{
 
{
 
   static signed char rss;
 
   static signed char rss;
 
   static signed char rss_val;
 
   static signed char rss_val;
 
   static signed char rss_offset;
 
   static signed char rss_offset;
   printf("Sink got message from %d.%d, seqno %d, hops %d: len %d '%s'\n",
+
   printf("Sink got message from %d.%d, seqno %d, hops %d: len %d '%s'\n",originator->u8[0], originator->u8[1],seqno, hops,packetbuf_datalen(),
    originator->u8[0], originator->u8[1],
+
        (char *)packetbuf_dataptr());
    seqno, hops,
+
  rss_val = cc2420_last_rssi;
    packetbuf_datalen(),
+
  rss_offset=-45;
    (char *)packetbuf_dataptr());
+
  rss=rss_val + rss_offset;
    rss_val = cc2420_last_rssi;
+
  printf("RSSI of Last Packet Received is %d\n",rss);
    rss_offset=-45;
+
        rss=rss_val + rss_offset;
+
printf("RSSI of Last Packet Received is %d\n",rss);
+
+
 
}
 
}
/*---------------------------------------------------------------------------*/
+
 
 
static const struct collect_callbacks callbacks = { recv };
 
static const struct collect_callbacks callbacks = { recv };
/*---------------------------------------------------------------------------*/
+
 
 
PROCESS_THREAD(example_collect_process, ev, data)
 
PROCESS_THREAD(example_collect_process, ev, data)
 
{
 
{
Line 104: Line 98:
 
   collect_open(&tc, 130, COLLECT_ROUTER, &callbacks);
 
   collect_open(&tc, 130, COLLECT_ROUTER, &callbacks);
  
   if(rimeaddr_node_addr.u8[0] == 1 &&
+
   if(rimeaddr_node_addr.u8[0] == 1 && rimeaddr_node_addr.u8[1] == 0)  
    rimeaddr_node_addr.u8[1] == 0) {
+
  {
 
     printf("I am sink\n");
 
     printf("I am sink\n");
 
     collect_set_sink(&tc, 1);
 
     collect_set_sink(&tc, 1);
Line 114: Line 108:
 
   PROCESS_WAIT_UNTIL(etimer_expired(&et));
 
   PROCESS_WAIT_UNTIL(etimer_expired(&et));
  
   while(1) {
+
   while(1)  
 +
  {
  
 
     /* Send a packet every 1 seconds. */
 
     /* Send a packet every 1 seconds. */
     if(etimer_expired(&periodic)) {
+
     if(etimer_expired(&periodic))  
 +
    {
 
       etimer_set(&periodic, CLOCK_SECOND * 1 );
 
       etimer_set(&periodic, CLOCK_SECOND * 1 );
 
       etimer_set(&et, random_rand() % (CLOCK_SECOND * 1));
 
       etimer_set(&et, random_rand() % (CLOCK_SECOND * 1));
Line 125: Line 121:
  
  
     if(etimer_expired(&et)) {
+
     if(etimer_expired(&et))  
 +
    {
 
       static rimeaddr_t oldparent;
 
       static rimeaddr_t oldparent;
 
       const rimeaddr_t *parent;
 
       const rimeaddr_t *parent;
    if(rimeaddr_node_addr.u8[0] != 1 ) {
+
      if(rimeaddr_node_addr.u8[0] != 1 )
      printf("Sending\n");
+
      {
      packetbuf_clear();
+
        printf("Sending\n");
      packetbuf_set_datalen(sprintf(packetbuf_dataptr(),"%s", "Fight On") + 1);
+
        packetbuf_clear();
      collect_send(&tc, 15);
+
        packetbuf_set_datalen(sprintf(packetbuf_dataptr(),"%s", "Fight On") + 1);
 +
        collect_send(&tc, 15);
  
      parent = collect_parent(&tc);
+
        parent = collect_parent(&tc);
      if(!rimeaddr_cmp(parent, &oldparent)) {
+
        if(!rimeaddr_cmp(parent, &oldparent))  
         if(!rimeaddr_cmp(&oldparent, &rimeaddr_null)) {
+
         {
          printf("#L %d 0\n", oldparent.u8[0]);
+
          if(!rimeaddr_cmp(&oldparent, &rimeaddr_null))
        }
+
          {
        if(!rimeaddr_cmp(parent, &rimeaddr_null)) {
+
              printf("#L %d 0\n", oldparent.u8[0]);
          printf("#L %d 1\n", parent->u8[0]);
+
          }
 +
          if(!rimeaddr_cmp(parent, &rimeaddr_null))  
 +
          {
 +
              printf("#L %d 1\n", parent->u8[0]);
 +
          }
 +
         
 +
          rimeaddr_copy(&oldparent, parent);
 
         }
 
         }
        rimeaddr_copy(&oldparent, parent);
 
      }
 
 
       }
 
       }
 
     }
 
     }

Revision as of 02:50, 14 November 2014

Back to Contiki Tutorials

Introduction

This tutorial helps you to understand how to measure Radio Signal Strength (RSS) received by a Tmote Sky.


RSS Measurement Program on Cooja

Step 1

Create New Simulation on Cooja. Let's say we call our simulation as "RSS".


RSS 1.png


Step 2

Create Motes.


RSS 2.png


Step 3

Browse the firmware. You can find the code later in this tutorial.


RSS 3.png


Step 4

Compile the firmware.


RSS 4.png


Step 5

Select number of new motes=2. Note: We are uploading same firmware on both the motes. One will act as Sender and another will act as Receiver.


RSS 5.png


Step 6

Start the simulation.


RSS 6.png


Step 7

You can see the RSSI value in the mote output window.


RSS 7.png

Code



static struct collect_conn tc;

PROCESS(example_collect_process, "RSS Measurement");
AUTOSTART_PROCESSES(&example_collect_process);

static void recv(const rimeaddr_t *originator, uint8_t seqno, uint8_t hops)
{
  static signed char rss;
  static signed char rss_val;
  static signed char rss_offset;
  printf("Sink got message from %d.%d, seqno %d, hops %d: len %d '%s'\n",originator->u8[0], originator->u8[1],seqno, hops,packetbuf_datalen(),
         (char *)packetbuf_dataptr());
  rss_val = cc2420_last_rssi;
  rss_offset=-45;
  rss=rss_val + rss_offset;
  printf("RSSI of Last Packet Received is %d\n",rss);
}

static const struct collect_callbacks callbacks = { recv };

PROCESS_THREAD(example_collect_process, ev, data)
{
  static struct etimer periodic;
  static struct etimer et;
  
  PROCESS_BEGIN();

  collect_open(&tc, 130, COLLECT_ROUTER, &callbacks);

  if(rimeaddr_node_addr.u8[0] == 1 && rimeaddr_node_addr.u8[1] == 0) 
  {
    printf("I am sink\n");
    collect_set_sink(&tc, 1);
  }

  /* Allow some time for the network to settle. */
  etimer_set(&et, 120 * CLOCK_SECOND);
  PROCESS_WAIT_UNTIL(etimer_expired(&et));

  while(1) 
  {

    /* Send a packet every 1 seconds. */
    if(etimer_expired(&periodic)) 
    {
      etimer_set(&periodic, CLOCK_SECOND * 1 );
      etimer_set(&et, random_rand() % (CLOCK_SECOND * 1));
    }

    PROCESS_WAIT_EVENT();


    if(etimer_expired(&et)) 
    {
      static rimeaddr_t oldparent;
      const rimeaddr_t *parent;
      if(rimeaddr_node_addr.u8[0] != 1 )
      {
        printf("Sending\n");
        packetbuf_clear();
        packetbuf_set_datalen(sprintf(packetbuf_dataptr(),"%s", "Fight On") + 1);
        collect_send(&tc, 15);

        parent = collect_parent(&tc);
        if(!rimeaddr_cmp(parent, &oldparent)) 
        {
           if(!rimeaddr_cmp(&oldparent, &rimeaddr_null))
           {
              printf("#L %d 0\n", oldparent.u8[0]);
           }
           if(!rimeaddr_cmp(parent, &rimeaddr_null)) 
           {
              printf("#L %d 1\n", parent->u8[0]);
           }
           
           rimeaddr_copy(&oldparent, parent);
        }
      }
    }

  } //end of while

  PROCESS_END();
} //end of process thread
 

Understanding the Code

etimer_set(&et, CLOCK_SECOND * 2);

This will set the timer to repeat the iterations every 2 seconds.


SENSORS_ACTIVATE(light_sensor);
SENSORS_ACTIVATE(sht11_sensor);

We need to activate light_sensor for measuring the light intensity and sht11_sensor for the measurement of temperature and humidity.


val = sht11_sensor.value(SHT11_SENSOR_TEMP);

Here we are capturing the actual sensor value.


s = ((0.01*val) - 39.60);

We need to calibrate the sensor values by doing some calculations. You can find the calculations here: http://tinyos.stanford.edu/tinyos-wiki/index.php/Boomerang_ADC_Example


SENSORS_DEACTIVATE(light_sensor);
SENSORS_DEACTIVATE(sht11_sensor);

After we are done with the calculations, we need to deactivate the sensors.


Back to Contiki Tutorials


Edited by : Nitin http://web.stanford.edu/class/cs244e/papers/cc2420.pdf