Using SerialForwarder Interface of TinyOS with Avrora

Dear reader,
Special thanks for the feedback and comments. Please continue supporting this small effort of mine to tinyos community.
As I promised in last post, here is the first step to analyze the RSSI values from a mote. In order to understand concept of base station and how things are done in real world, the following tutorial may help you.
As usual, sample code is provided at the end

Assumptions:
1) You have done up to the previous post, radio communication using cthreads. If not, please have a look at it. We are using the same program here for further analysis with a base station.
2) The username is "test"
3) You have eclipse installed and running.
4) You know how to add CLASSPATH for custom jar library

Before we begin, a small note on base station. Base station in real time can be a sophisticated node with high transmission range or a normal mote connected to computer. In either way, it act as a sink for data collection. That means, every application you develop using WSN needs to run the base station.

With this, here we begin.
Part 1: Making program ready for SerialForwarder
  1. Copy the folder BaseStation from /opt/tinyos-2.1.0/apps to /home/test/Serial
  2. Take shell and navigate to ~/Serial
  3. Compile the program. "make mica2"
  4. Convert the main program to avrora compatible format convert-avrora build/mica2/main.exe base.od
  5. Move the base.od to parent folder. mv base.od ../
  6. Copy the sender.od from previous example to Serial folder
  7. Instead of our previous post step, command for avrora is different here. " avrora -simulation=sensor-network -seconds=160.0 -monitors=serial,real-time -platform=mica2 -nodecount=1,1 base.od sender.od"
  8. This will give a display "Waiting for serial connection on port 2390..."
If everything is fine, the O/P will be similar to the following


Part 2: Opening a new project in Eclipse and adding TinyOS library
  1. Open a new java project in Eclipse
  2. Copy the following code

Running Program
  1. Add java library path for suport for TinyOS serialforwarder
  2. Compile.
  3. Run the serialforwarder
  4. Modify the entry in serialforwarder to "network@localhost:2390"
  5. Start Server
  6. Now the counter will be running for "packets received"
  7. With out closing the serialforwarder, go back to eclipse and run the Listen
  8. Observe the output.

For those who are done with this.
Please have a look at Octopus project
If you are able to run the code from Octopus in avrora, it's a good sign!!!
Cheers

Download java code
Links you may be interested in
  1. Radio Communication using cthreads(tosthreads library)
  2. Running cthreads (tosthreads) program in Avrora
  3. BlinkToRadio in Avrora
  4. Running TinyOS programs using Avrora

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