Monday, June 10, 2013

Basic Tutorial for programming in #Contiki Operating System for #IoT| PART 1

During my undergrad I took a course on Operating Systems. It was pretty fascinating to know how operating systems have developed over time and how they worked. Nevertheless, I didn't find much to try to devote to this subject. I was a computer engineering major not in ICT, so my subjects varied all the way from VLSI design to Multimedia technology.

But now I have this great opportunity to learn.

For someone with limited programming experience learning to adapt to programming at this level is not easy. Here I will try to post whatever I am learning for comments, and future notes. Hope I get this all correct. I wont be using advanced technical jargon here, but just enough to make some sense. I will try to keep the description as layman as possible.

Let me start with the basic structure of any code. This code will be run on any individual node, and will have to be downloaded (installed/ported) to a node. This can be done in a simulation and then on a proper hardware device. The easiest way to get started is to download Instant Contiki and run it as a virtual machine. I used Oracle's Virtual Box myself for the virtualisation.

//First are foremost you need this header file

#include "contiki.h"

//Then just like we initialise functions, we need to
//initialize a process

PROCESS(basic, "This is just a basic template");
//The word "basic" is the name of your process which you will 
//be using in later functions. Basically
//it is the declaration of the process
//The text in "" is for a short description

AUTOSTART_PROCESSES(&basic);  //Start the process

PROCESS_THREAD(basic, ev, data) //Here goes the definition of the 

//Always try to put any code after this.

//Can initialise stuff using static keyword. 
//Will tell you why in the next block of comments.

Have to include code here what we actually want to do
This code in this portion lets the process wait for events indefinitely
by compulsorily including code for an infinite loop
This is something called as a blocking macro.
Actually, here the process returns to the kernel (to enable 
the kernel to call others) 
and process enters a blocked state. 
When an event is posted, the kernel will 
call back this process which will jump right
after it returned before. 
This may sometimes change the values of the local variables, 
that’s why we are supposed to define them as static. 

//End the process. Notice no arguments.


The structure of the blocking macro will be roughly like this:



//Wait for some external event
//Or wait for a timer to finish
//Or wait for some internal event

//And then do something..


Now the most important question is how to use this code.. If you save the above code on your desktop, and try to simulate it on Cooja on lets say a 'Z1 mote', you will get this error:

make basicTemplate_Threads.z1 TARGET=z1
make: *** No rule to make target `basicTemplate_Threads.z1'.  Stop.
Process returned error code 2

So to get your code working, just copy the source file to the Z1 folder located at
Home/contiki-2.6/examples/z1 and then use that to compile on your simulated z1 mote.

For other mote types, just copy to the corresponding folders. This is the easiest way to get it working.

Part 2 of this series can be found here.