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


Ashwin said...

Isn't nesC an easier way to do programming than TinyOS C?

Ashwin said...

I may be wrong, of course... I'm a noob :) and interested in learning nesC. It's just that I've had a hard time looking for a good ebook. Anyway, great initiative you've taken. Good blog! Hoping to read more with the passage of time

Gireesh said...

Hi Aswin,
It depends on the reader!!! For me, learning nesC was bit difficult because of its style!!!
TosThreads or Tinyos C is no much different from nesc in practise!!! but gives an eazy way to start programming!!
I will post the radio communication program, which I wrote with my own idea in Tosthreads!!!

If you find more simple way of explaining NesC please let me know!!!


Anonymous said...

very useful,thank you for your work!!!

Fazlullah said...

Outstanding Gireesh!
I have learn alot from this Blog.
Very much heartiest thanks to you.
Fazlullah Khan

ankur said...

Hello Girish,
I am working on WSN simulation and trying to learn tinyos and tossim . Using python i need to use variables...
but the following error occurs..
Please help me.....

>>> from tinyos.tossim.TossimApp import *
>>> n = NescApp("Unknown App", "app.xml")
Traceback (most recent call last):
File "", line 1, in
File "tinyos/tossim/", line 496, in __init__
Your nesC app cannot be imported. Be sure that you compiled with the \"nescDecls\" option.\n\n""" % xmlFile)
ERROR: cannot find file "app.xml".

Your nesC app cannot be imported. Be sure that you compiled with the "nescDecls" option.

Gireesh said...

I have not worked with TOSSIM
Also, in tosthreads, there is no nesc used. The extension is C

Anonymous said...

Hello Gireesh,
I have lately started to work on TinyOs and am interested in learning about NesC and the C-like version for programming. However I have a older copy of TinyOS installed in my PC ver - 2.0.2
This version doesnot have the TOSThread Libraries. Please let me know if I could have these libraries installed somehow in my version or how can I replace the existing version with minimal changes so that these libraries are supported.

Deepak Hemnani.

Gireesh said...

easiest is to install 2.1.0/2.1.1 tinyos