Working with motes using TOSTHREADS... An easier way to do TinyOS programming

Dear reader,
Thanks a lot for responses and comments to make my small efforts on TinyOS programming a success. So here is another good news for C programmers who wants to work on TinyOS. No more NesC codes and hurdles of it.
TinyOS 2.10 comes with integrated library for tinythreads. The library examples can be found in "/opt/tinyos-2.1.0/apps/tosthreads" for a default installation.
The directory under it, "capps" is specially interesting to us.

Here I assume that you have done TinyOS installation and Avrora configuration from my past posts and enjoyed testing the blink application and blink2radio application.Also you have done programming in Threads using C.

What is so exciting in this folder? Well we are back to our favourite language "C". Let's rewrite the program using C and tosthreads

Quick refresh on Posix standard Thread in C

  • Declare thread_t instances
  • Define functions of void* foo(void*)
  • Create thread using pthread_create()
TOSTHREADS programming
Revisiting Blink

#include "tosthread.h"
#include "tosthread_leds.h"
tosthread_t blink;

void blink_thread(void* arg);
void tosthread_main(void*arg)


void blink_thread(void*arg)
uint8_t counter;
setLeds(counter) ; tosthread_sleep(200);

Save the above code as Blink.c
Making the program : Makefile

include $(MAKERULES)
Save above code as Makefile

Compiling the program
Open a shell in the same folder where Makefile and Blink.c are stored. Type
make mica2 cthreads
If everything goes fine you will get output similar to

Running the program using Avrora
From shell change directory to "mica2/build"
cd mica2/build
Convert the main.exe to blink.od
convert-avrora main.exe blink.od
Run the simulation
avrora -platform=mica2 -seconds=3 blink.od
Output will be similar to the following

Tip: Modify the program to make the thread running for ever instead of 8 counts.
Keep reading!!!!
I will be back with more programs.
Meanwhile if you have any simple code, please send it or add as comments so that others will be benefited.

Links you might be interested

Running TinyOS Programs using Avrora

Running multiple node simulation using Avrora: Example BlinkToRadio

Installation of TinyOS 2.10 in Ubuntu

WSN: A layman's view

Running BlinkToRadio using Avrora

Dear reader,
So it's time to test something more interesting. Here I assume that you have already installed tinyos2.10 and Avrora using my previous posts and you have gone through the TinyOS wiki about BlinkToRadio. The tinyos install directory contains app/tutorials folder where you can find implemented code of all these programs.

I am not explaing the code, but just try to demonstrate how to run the code using Avrora

Build the program using "make mica2"

Step 2
Convert the main.exe file to radio.od
"convert-avrora main.exe radio.od"

Step 3
Run the program using sensor network mode of avrora
"avrora -simulation=sensor-network -seconds=2.0 -nodecount=2 radio.od"

If things work fine, output will be similar to the following

Play around with the avrora monitor options to see more options
Have fun!!!!!!

Running TinyOS programs using Avrora

The readers may be wondering, the post for installation of TinyOS does not have TOSSIM installation steps. It's because I have found Avrora emulator more easier than TOSSIM scripts. Avrora is emulator/simulator for wireless sensor networks, written in Java by UCLA group. It takes an object dump of tinyos programs over AVR platforms ( mica2/micaz) and is capable of single node emulation for verification of the program as well as multiple node simulation. The gui provided with Avrora is not functional when this document is written.

Here I assume that username is "test", tinyos 2.10 and java run time environent are installed. The code used for illustration along with one more application is given for download at end of tutorial

Download the Avrora [Beta 1.7.105] from the site.

Setting up the environment
  1. Copy the avrora jar file in a directory say "home/test/avrora/avrora.jar"
  2. Download the
  3. Extract the tar file and get the
  4. Assuming that path for is "home/test/avrora/" copy the following code to ".bashrc"

  5. alias avrora='java -jar /home/test/avrora/avrora.jar'
    alias convert-avrora='sh /home/test/avrora/'
  6. Close all the shells opened
  7. Take a new shell and type avrora /convert-avrora, it should produce the following output

Writing our first program: Blink
Please note that this is not blink program described in TinyOS wiki. This is much more simpler version of it.
This consist of following steps
  1. Interfaces : Boot, Leds
  2. Implementation file BlinkC
  3. Define Wiring
  4. BlinkC -> MainC.Boot
  5. BlinkC.Leds -> LedsC ( 3-5 steps is in BlinkAppC )
  6. Write the implementation file
  7. Write the Makefile
  8. Compiling
  9. Running the application using Avrora
Save all the following codes in "blink" folder ( say /home/test/tinyospgs/blink)

#include "Timer.h" module BlinkC { uses interface Leds; uses interface Boot; } implementation { event void Boot.booted() { call Leds.set(1) ; call Leds.set(2); call Leds.set(3); call Leds.set(4); call Leds.set(5); call Leds.set(6); call Leds.set(7); } }
Save the above code as ""
configuration BlinkAppC
components MainC, BlinkC, LedsC;
BlinkC -> MainC.Boot;
BlinkC.Leds -> LedsC;
Save above code as
include $(MAKERULES)

Save above as Makefile

Now take a shell, navigate to this folder
Type "make mica2 "
If everything is correct, it will create build/mica2 folder
Navigate to mica2 folder in shell (cd build/mica2)
Type "convert-avrora main.exe blink.od"

Run the simulation by "avrora -seconds=5.0 -platform=mica2 blink.od"
Output will be similar to the following

You are successfully emulated the first program
Note that only AVR platforms such as Mica2/Micaz is supported by Avrora

Links you might be interested in
Code Download this code and blink with timer here

Radio communication simulation simulation using Avrora

TinyOS programming using C an eazier way : tosthreads simulation using Avrora