Radio Communication Simplified Using TOSTHREADS

Dear reader,
Welcome back again with another post on tosthreads or cthreads in TinyOS. As usual my assumption is that you have followed up to my last post on cthreads. If not, it's beter to do it now before continuing!!!
If you have visited tinyos wiki, you may wonder how complex the radio communication code for BlinkToRadio. All that we are trying is to send a simple msg and it needs lot of files. Till now I have not seen a better way of doing that communication. (I am also learning things in NesC)
What we are trying to do is , to use 2 nodes "a sender and a receiver". Sender will send the data and receiver will receive it and blink the leds. We will write the program and will test it using Avrora as usual.

Algorithm Sender
  1. Initialise radio
  2. Create packet
  3. Set count
  4. Send the data
  5. Increment count and repeat 4
Here, we need to create a nx_struct as a wrapper. Those who tried to implement BlinkToRadio will see that this part is same
After that create a thread which will continuously send the data.
#include "tosthread.h"
#include "tosthread_amradio.h"
#include "tosthread_leds.h"
typedef nx_struct RadioMsg {
nx_uint8_t counter;
} RadioMsg;
//Initialize variables associated with each thread
tosthread_t radio_thread;
void radio_thread_foo(void* arg);
message_t send_msg;
RadioMsg* rdata; //pointer into message structure
//Initialize messages for sending out over the radio


void tosthread_main(void* arg)
{
while( amRadioStart() != SUCCESS ); //wait till radio is ready
tosthread_create(&radio_thread, radio_thread_foo, NULL, 200);
}
void radio_thread_foo(void* arg)
{
uint8_t count;
rdata=radioGetPayload(&send_msg, sizeof(RadioMsg));

for(;;)
{
count++;
rdata->counter=count;
if(amRadioSend(AM_BROADCAST_ADDR,&send_msg, sizeof(RadioMsg), 2) == SUCCESS)
{

setLeds(count);
tosthread_sleep(1000);
}

}
}

The code is self explanatory!!!!

Algorithm Receiver
  1. Initialise the radio
  2. Wait for a packet
  3. Get the data
  4. Blink LED s
#include "tosthread.h"
#include "tosthread_amradio.h"
#include "tosthread_leds.h"
typedef nx_struct RadioMsg {
nx_uint8_t counter;
} RadioMsg;
//Initialize variables associated with each thread
tosthread_t radio_thread;
void radio_thread_foo(void* arg);

//Initialize messages for sending out over the radio
message_t radiomsg;

void tosthread_main(void* arg)
{
while( amRadioStart() != SUCCESS ); //wait till radio is ready
tosthread_create(&radio_thread, radio_thread_foo, &radiomsg, 200);
}
void radio_thread_foo(void* arg)
{
message_t* m = (message_t*)arg;
RadioMsg *rm;
uint8_t count=0;
for(;;)
{
if((amRadioReceive(m, 50, 2))== SUCCESS)
{

// if(radioGetPayloadLength(m)==sizeof(RadioMsg))
{
rm=radioGetPayload(m,sizeof(RadioMsg));
setLeds(rm->counter);
tosthread_sleep(1000);

}
}
}
}


Create makefile for each and compile the code using "make mica2 cthreads"
Follow the steps in last post, you can run it using avrora.

Download Code

Links you may be interested in
  1. Working with serialforwarder in Avrora
  2. Running cthreads (tosthreads) program in Avrora
  3. BlinkToRadio in Avrora
  4. Running TinyOS programs using Avrora