Monday, March 30, 2009

HelloWorld for TinyOS

This semester I took "Embedded Systems". I have always liked playing with the sensors and ADC kind of stuff. I checked out few Telosb motes from the lab, installed TinyOS 2.1 on my OpenSuse11.1 desktop, and tried my first "HelloWorld" program. The "HelloWorld" for TinyOS is not that straightforward. Here are the step to make the "HelloWorld" code work on your mote.

1. Install TinyOS. Please refer http://www.tinyos.net/tinyos-2.x/doc/html/install-tinyos.html
2. Make sure you have the correct version of Java on your machine. The default Java on your machine may confilct with the required one.
3. mkdir HelloWorld
4. HelloWorldAppC.nc file:
configuration HelloWorldAppC {
}
implementation {
components HelloWorldC;
components MainC,LedsC;
components new TimerMilliC() as Timer0;

HelloWorldC.Boot-> MainC.Boot;
HelloWorldC.Leds-> LedsC;
HelloWorldC.Timer0->Timer0;
}
5. HelloWorldC.nc file:
module HelloWorldC{
uses{
interface Boot;
interface Leds;
interface Timer as Timer0;
}
}

implementation{
uint16_t count = 0;
event void Boot.booted()
{
call Timer0.startPeriodic(500);
}

event void Timer0.fired()
{
count++;
if (count & 0x0004)
call Leds.led2On();
else
call Leds.led2Off();
if (count & 0x0002)
call Leds.led1On();
else
call Leds.led1Off();
if (count & 0x0001)
call Leds.led0On();
else
call Leds.led0Off();
}
}
6. The Makefile:
COMPONENT=HelloWorldAppC
include $(MAKERULES)

7. So, now you have the HelloWorldAppC.nc, HelloWorldC.nc and Makefile, 3 files in your folder.
8. compile the code: make telosb or make micaz
9. If no error messages, then upload the binary to the mote:
make telosb reinstall
10. Now, you should see the mote saying "HelloWorld" by blinking the 3 Leds.

That is it. Enjoy your TinyOS.