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

3 comments:

Ashwin said...

I guess you've put in a lot of effort for this. I'm not sure what the nesC equivalent would look like, but this post does create an interest in me to learn cthreads. :)

I'll be looking forward to the next post on tracking.

Abhishek Goyal said...

hi,
i tried to run the above sender and recive codes in two different shells. i tried for 100 seconds simulation on sender as well as reciver side.But avrora does not display any packet contents as displayed in BlinkToRadio.plz help me out.How can I display packet contents while receving or sending.

Gireesan said...

@abhishek
sender and receiver codes has to be run in same shell with commadn line args in avrora
i have explained it in last post, how 2 do it
More information is there if u open the shell script "convert-avrora.sh" where i commented it as usage