Friday, July 22, 2011

Writing Contiki Programs

Here I write down some simple instructions on how to program WSN motes on the Contiki operating system platform. If you are familier with C programming even for a little bit, you can write programs for Contiki operating system easily after learning the basics which are specific to Contiki operating system. After writing the very first Contiki program, most of the convensions that we have to follow will be clear.

As I have mentioned in a previous post, the most exiting thing we can find as a WSN programmer is the proto-threads in Contiki. Proto-threads allow us to write multi-threaded applications on top of the Contiki operating system. Therefore we can get rid of writing codes to implement state machines to run on the event driven kernel. The below program is the most simplest Contiki program you can write which contains most of the necessary components that should be included in any Contiki program.
1:  #include "contiki.h"  
2:  #include <stdio.h>   
3:  PROCESS(my_first_process, "Hello World Process");  
4:  AUTOSTART_PROCESSES(&my_first_process);  
5:  PROCESS_THREAD(my_first_process, ev, data)  
6:  {  
7:   PROCESS_BEGIN();  
8:   printf("Hello WSN World!\n");  
9:   PROCESS_END();  
10:  }   

Save this file as hello_world.c
The header file "contiki.h" which is included at the very first contains all the declarations of the Contiki operating systems abstractions. stdio.h is included only because I have used printf() function inside the program. The Macro in third line declares a new Contiki process. First parameter of it is the variable for the process and the second parameter is a string name for the process. Fourth line specify that this process should be started in the startup of the Contiki operating system. Therefore when the hardware device which will be the destination of compiled code switches on, our process also begins running.

Fifth line of the code opens the definition of our process. As the first parameter, we pass the process' variable which is my_first_process in this scenario. The second parameter is 'ev' which is the event parameter. It can be used to make the program responding to events that occur in the system. The third parameter 'data' can be used to receive some data which are coming with those events.

The rest of the instructions of the Contiki process will be included inside two important lines which are  PROCESS_BEGIN() and PROCESS_END(). In our Contiki process we just print a string. This output goes to the standard output in the platform where our code will be running. If you compile this code to native platform and run in the terminal, the output will be printed in the terminal. If you run this in a mote, the output most probably will goto the UART port of the mote. However this behavier depends on the design of a particular WSN mote.

Now we have a Contiki program with us. To compile it, we need a Makefile which contains the instructions for the compilation tools to do the task. Therefore create a new file with your text editer and put the below content in it. Save it as Makefile. No file extention needed for it.
1:  CONTIKI=../contiki-2.4  
2:  include $(CONTIKI)/Makefile.include  

Lets compile our code. Before everything you have to have downloaded the Contiki source code. So, goto here and download the latest Contiki source code. Uncompress the zipped file and place in some where in your PC. First we compile the code to native platform. For that, do the following steps.

1) Put the hello_world.c and Makefile files in a new directory. Lets say you place them in a directory named "my_app". 

2) In the first line of your Makefile, put the correct path to the directory where your downloaded and unzipped Contiki source code resides. As an example if your Contiki source code is located in the path "/home/aps/Desktop/contiki-2.4", your first line of the Makefile should look like below.
1:  CONTIKI=/home/aps/Desktop/contiki-2.4  

You don't have to give absolute paths always. It's ok to give relative paths also.

3) Now goto "my_app" directory from the terminal and issue the following commands,

make TARGET=native hello_world

./hello-world.native

If everythings ok, terminal will give an output like this,
Starting Contiki
Hello WSN World!


4) To stop running Contiki, give a CTRL + C on the terminal.

As I have mentioned previously, compiling to the native platform is the simplest way. But I assume that you have real hardware also to test this. Lets say you have a MSB430 motes. Put the batteries to it and switch on. Plug it to your PC with the serial cable provided with the motes. Now goto "my_app" directory from the terminal and issue the following commands,

sudo chmod 777 /dev/parport0

make TARGET=msb430 hello_world.u


Now the compiled code will be written to the flash memory of the MSB430 mote. After it completed writing to flash, remove the mote from the serial cable. Actually as soon as the flash memory writing is complete, Contiki OS will be running with your application on the mote. But you can't see the output it generates on it's pins. To see it, connect the mote with the provided USB cable to the PC. Now open a termianl and issue the following command.

ls /dev/

Check whether there's a device in the listed output named something like ttyUSB0 or ttyUSB1. Lets say it's ttyUSB0. This is the mote that is connected to your PC. So, issue this command now. 

cat /dev/ttyUSB0

Terminal will now hang and listen to any output coming from there. Switch off the mote and on again. You will see the output from the terminal now. The way we connect different types of motes to the PC are different. But the command you issue in the terminal to compile and upload the Contiki source differ only from very small things. When you compile this application to the MicaZ motes, the command you issue will be,

make TARGET=micaz hello_world.upload PORT=/dev/ttyUSB0


Now you have a basic understanding on writing programs for Contiki. You can find more code examples in the 'examples' directory of the Contiki source code. If you don't have real sensor network hardware to run and test Contiki applications you write, you have another way to fulfill your requirement. There are Simulators for that purpose. Some simulators are instruction level simulators like MSPSIM and Avrora while some simulators are network level simulators like Cooja. I'll write about how to use these simulators for testing our Contiki programs later.

15 comments:

  1. I tried to upload this program to micaz 3 hours ago but with no avail, let me tell you what exactly I did.
    - I downloaded instant contiki-2.4 from this http://www.sics.se/contiki/instant-contiki.html
    - I navigated to contiki-2.4/examples/hello-world and tried to compile hello-world program to native target by this command:
    make TARGET=native hello-world
    it compiled okay, with no error.

    -BUT when I tried to upload it into Micaz this error appeared
    "make: uisp: Command not found
    make: ***[hello-world.upload] Error 127"
    I'm sure about connection and I connected micaz via mib520 programming board after I turned it on.

    so could you help me ???

    ReplyDelete
  2. I use Contiki source code directly on my Ubuntu PC instead of using Instant Contiki. However according to the error that you got, I think the UISP tool is not installed in your platform. Try typing uisp on the shell to find it. However the default UISP tool which gets installed by typing

    apt-get install usip

    doesn't work sometimes with MIB510 programmer boards for MicaZ as I've learnt. Therefore I installed it by downloading from somewhere else. Try this to install UISP,

    1. Download UISP from here,
    http://azadeha.at.ifi.uio.no/uisp.tar.gz

    2. Goto the downloaded location from the shell and issue the following commands,

    # tar -xvzf uisp.tar.gz
    # cd uisp
    # ./bootstrap
    # ./configure
    # make
    # sudo make install

    All the best!

    ReplyDelete
  3. Thanks
    It work well after I downloaded UISP as you said.

    ReplyDelete
  4. hai!do u have any simple code for a multihop routing protocol in this?

    ReplyDelete
  5. Well, I don't have such a protocol code with me. But if you look at the examples directory of the Contiki source code you can find some examples.

    If you have contiki source code, goto the location
    contiki-2.4/examples/rime.
    There's a file named "example-multihop.c" in that directory. You can try this example by running on Cooja simulator. To do it, goto that directory from the shell and issue the following command.

    make TARGET=cooja example-multihop.cooja

    This will open a Cooja window with the example. To trigger a mote for starting the functionality, right click on a mote and select the option "Click Button on Contiki". (I assumed that you are familiar with Cooja simulator)

    I hope it will help to learn. If I find much simpler example code than that, I will tell you. May be we can write a simpler one than that.

    ReplyDelete
  6. Hi,
    Did you find a simpler example for a multi-hop routing protocol?
    The thing is I have sensor nodes and I need to program one as a sender and the other as a receiver and the others as routers.

    ReplyDelete
  7. Hi
    I am new comer to WSN and really need your help I have followed every instruction to upload the hello-world application on to a micaz base station but I get an error as seen below!
    user@instant-contiki:~/contiki/examples/hello-world$ make TARGET=micaz hello-world.upload
    avr-objcopy -O srec hello-world.micaz hello-world.srec
    uisp -dprog=mib510 -dserial=/dev/ttyS0 -dpart=ATmega128 --wr_fuse_h=0xd1 --wr_fuse_e=ff --erase --upload if=hello-world.srec --verify
    Programmer is not responding.
    make: *** [hello-world.upload] Error 2
    rm hello-world.srec

    ReplyDelete
  8. @kavelit:
    hmmm... Are you sure that your programmer board (MIB510) is working ?

    Additionally I suggest you to install the UISP tool as described in a previous comment since MIB510 board seemed not working properly with the default UISP tool that comes with Linux repositories.

    ReplyDelete
  9. how to get data from serial to coordinator?

    PC->send_data_from_serial()->coordinator-> receive_data_from_serial()-> send_data_to_serial()->PC->display_data_from_serial()

    ReplyDelete
  10. hi
    I compiled hello world program and uploaded to micaz motes, when i tried the command, cat /dev/ttyUSB0
    nothing comes on the terminal, could you tell, what could be done to see the output?

    ReplyDelete
  11. why we write the third line in above program. what it is doing?

    ReplyDelete
  12. can any body tell me how to import java mote .....please explain me the steps please

    ReplyDelete
  13. hi, i'm using cooja for the first time and i want to simulate the cc2650 mote, but in cooja simulator when i do "add mote" they aren't.
    In all Examples that i found they uses only sky mote.
    Can you help me??

    ReplyDelete
  14. hi.. can u please tell me how to run my java class file in cooja..i want to create 2 motes one as a client and the other as server..can u please tell me how to do it??

    ReplyDelete
  15. Hi, I'm trying to run the collect protocol on z1 mote. But when I try to upload the code it gives an error
    File "../../tools/zolertia/z1-bsl-nopic", line 18, in
    import serial, os, glob
    ImportError: No module named serial
    ../../platform/z1/Makefile.common:133: recipe for target 'z1-r.-dev-ttyUSB0' failed
    make[2]: *** [z1-r.-dev-ttyUSB0] Error 1

    Please help regarding the matter.

    Thanks

    ReplyDelete