Monday, August 8, 2011

Cooja Sensor Network Simulator

Cooja is a simuator that comes with the Contiki operating system. It is very useful for testing sensor network applications written for Contiki platform without having to put them in real sensor network hardware. If we use real hardware motes for testing our Contiki programs, we will have to burn the flash memory of the motes sevaral times until we get a nicely tested program without any known bug. But is not easy to program motes which is a time consuming task. Even if we are ready for that, it may be not good for a mote to reburn its flash memory so many times since the number of read write cycles possible in flash memory is limited.

    Because of these reasons whenever we write a Contiki program, we better try it running on the Cooja simulator first and later on we can go for real hardware. Cooja is written in Java language and has a very modular structure which enables it to be extended with custom made plugins for it. When we compile a Contiki program for running on Cooja, it actually gets compiled into an executable of the native platform which is the operating system where our Cooja simulator is running on. Cooja runs these executables using Java Native Interface(JNI) from a higher level.

    Before using Cooja simulator for testing our programs we have to setup the Environment for it to work properly. First of all download the Contiki source code from here. Let's say you have downloaded it. Inside the contiki source code you can find the Cooja simulators source code in the path /contiki-2.4/tools/cooja. Go through the following steps to prepare the environment. You may skip some steps if your platform already have such configurations. Here I assume you are using Ubuntu Linux as your operating system since it is where I have tested all these stuff.

1) Installing Java
    Since Cooja is written in java, you have to have java installed in your platform. Do the following to install Java. If you have already installed Java, you can skip this step. Open a terminal and issue the following command with an active internet connection.

    sudo apt-get install java-6-openjdk

2) Installing Apache ant
    We use ant as the build tool to compile and build the Cooja simulator from its source. Therefore to install it, issue the following command in a termianl.

    sudo apt-get install ant

3) Setting JAVA_HOME and PATH variables
    Cooja simulator requires the environmental variables JAVA_HOME and PATH to work properly. Therefore follow this link to set those variables.

Now you have the proper environment for running Cooja simuator. It's time to deal directly with Cooja. Open a terminal and goto the downloaded Contiki source code. From there, goto Cooja source code directory which should be in the path /contiki-2.4/tools/cooja. From there issue the following commands.

    ant jar
    ant run


    If everything went fine, you'll get a window like the one showed in the figure-1. It contains only a menu bar. You can test whether all the environment setup has done correctly by using a simple testing wizard provided by Cooja. Open this testing wizard by going through this way.

 menubar >> Settings >> Compiler configuration wizard


You will see the wizard showed in the figure-2 now. Click on the "Start tests" button to begin the test. The first test will be "Compile and link Contiki library" as showed in fugure-3 and click on "Run test" button. If it prints "Test OK", you have done with first test. Click on "Close" button and then "Next test" button. In this way you should go through all the four tests. The final test window will be like the one showed in figure-4. After passing though the final test, you can "Close Wizard".



If one of these test is failed, that means you have missed something when setting up the environment as mentioned above. After passing the tests you can start using Cooja simulator for testing your Contiki programs.

To run the hello-world example which resides inside the examples directory of Contiki source code, do the following steps. Goto "File" menu and clieck on "New simulation" menu item. It will bring a window to create a new simulation. Enter a name for the simulation and click "Create". Now you'll get something like the figure-5. From there goto the menu "Mote Types" and from it click on "Contiki Mote Type". Now from that window, browse for the hello-world.c file in Contiki examples directory. Then click "Compile". After compilation completed, click on "Create" button.


A new window will ask you to add the number of motes for simulation. For now lets type 1 on the text field and then click "Create and Add". In the simulation visualizer window you can see a mote now like a circle. Now in the Control Panel window, set the Delay value to 1ms and click "Start" button. Now you can see the output of the virtual motes that run your program in the Log listener window. You can stop the simulation by clicking on "Pause" button in the Control Panel window.


    Alternatively you have another easy way to quick start your Contiki program with Cooja. To try it, close the Cooja simulator window first. From the terminal goto the hello-world directory in the examples directory of Contiki. From there type the following on the terminal.

       make TARGET=cooja hello-world

This will start the hello-world program with Cooja simulator. There are so many things to explore in Cooja simulator. With the current understanding find about different things you can do with Cooja simulator.


  1. Hi,

    I'm new to COOJA,and need your help,after clicking on Run Tests on "menubar >> Settings >> Compiler configuration wizard" getting the following error on 1st screen:

    cc1:error:unrecognized command line option -mno-cygwin

    is this because of cygwin settings?I just installed Instant Contiki 2.5 and Installed Cygwin and set Path variable.I didn't set any Java Home /Path in Linux.

    Please help me in this.

  2. Hello Raju,
    I'm not using Instant Contiki nor Cygwin for my works. What I'm going is using Contiki source code on top of my Ubuntu PC. Therefore I have no clear idea about the error you get in Cooja. However java home path variables are required to work cooja as I know. If it is not set in the Instant Contiki, you have to set it. (Instant contiki is actually a live ubuntu image with all the necessary contiki development tools already installed for you)

  3. hi
    i am new user to cooja simulator. I am using instant contiki for my project. i am facing some problems in using cooja for simulation. plz can you help me in using cooja i mean which platform to use for program writing and how to run it on cooja.

  4. What kind of problems are you facing on using the Cooja simulator?
    I'm working on Ubuntu linux platform. There are lots of resources available in web about using Cooja simulator. I think the most detailed one is the Masters thesis of Fredrik Österlind available in the following URL,

  5. You might be interested in my ppa: Currently it's oneiric only, but I can change that.

  6. Hi,
    Is it possible to run data collection example of contiki on COOJA without using Tmote Sky or any other hardware?

    Thank you,

  7. @ Morty
    Have you done any modifications to it or is the same version given by SICS ?

    @ Raju
    Yes, I think it's possible according to contiki wiki. Have you came through this link?

    I haven't tried it yet since we have Sky motes in our lab. So, if you try it and work, please let me know.

  8. @ Asanka: Yes, there are some modifications:
    I updated the ppa-discription to make this clear.

    1. This comment has been removed by the author.

  9. hi...I am running cooja on ubuntu 12.04..but i am not understanding how to compile my scenarios.I have installed msp430 -gcc I need to set specific path or something?
    plz guide me

  10. Do you have a Contiki program written by you? If not first you can try the example programs that comes with Contiki source code. You can find them in the directory named as 'examples' inside Contiki source code directory.

    msp430-gcc compiler is required if you are going to compile a Contiki program to a real hardware platform which is having a MSP430 microcontroller. Otherwise you don't need it to test your program on Cooja simulator on your PC.

    I think I need more details about the scenarios you mentioned. Then I will try to help you :)

  11. Hi. I am new to cooja. I have tried the sample programs and understand how it works. I have a issue.

    Is it possible to get the node location computed by cooja within the simulated node (contiki c code). I see we have to use interfaces. I want this to work for emulated sky motes. I have tried with the Dummy Interface but it does not work. Can you please help.

  12. @Prem: Someone I know who worked on a Contiki based project on Cooja simulator had this requirement those days to get the node location on x and y axis. But we didn't know a way to obtain the node location from the simulator to our app. So what she did was just hard code the node x and y values on the app and then place the nodes in the simulator to appropriate places.

    However there may exist some way to access the simulator level information from our contiki application. But honestly I'm not aware of that way. May be searching on Cooja simulators manuals/ docs may help to find such a way if it is possible.

  13. Hi Asanka,
    I am currently doing my degree project and i need to install COOJA to simulate sensor node energy efficiency, but i am stuck with getting cooja to work when i type the command $Documents/contiki-2.5/tools/cooja$ ant run
    i get the following error

    Unable to locate tools.jar. Expected to find it in /usr/lib/jvm/java-6-openjdk/lib/tools.jar
    Buildfile: /home/jason/Documents/contiki-2.5/tools/cooja/build.xml

    [javac] Compiling 151 source files to /home/jason/Documents/contiki-2.5/tools/cooja/build
    /home/jason/Documents/contiki-2.5/tools/cooja/build.xml:72: Unable to find a javac compiler; is not on the classpath.
    Perhaps JAVA_HOME does not point to the JDK.
    It is currently set to "/usr/lib/jvm/java-6-openjdk/jre"

    Please help me.
    Thanks a lots

    1. Hello Jason,
      Even though I still don't exactly know the reason for that error I have some ideas.

      I feel like you have not set JAVA_HOME properly. So, please make sure that. Do you have installed just OpenJDK or both OpenJDK and Sun Java in your machine?

      Additionally I noticed that you are using Contiki 2.5 with OpenJDK 6. When I was writing this blog post I had Contiki 2.4 with OpenJDK 6. So, if you are sure that you have set JAVA_HOME path properly I kindly suggest you to download and try Contiki 2.4 and do the same thing. That way we can check whether this is some issue related to the version difference of my instructions.

      Unfortunately I have currently have Sun Java 6 in my machine at this time and therefore I can't try Contiki 2.5 with Open JDK.

    2. hi,
      m new to cooja simulator i have undergone the examples with the cooja tool, can u please suggest me how to open the source code and how to run the own programs

  14. Hi Asanka..
    I am currently doing my final year project for degree. I have install instant contiki enviroment and tried some example using cooja simulator. Now i have no idea what can i get from this cooja simulator to do my final year project. I want you to help me with the idea or some information that i can do or get from this cooja simulator..
    thank you...


  16. Hi,
    I want to compare two protocols (rpl and loadng) to evaluate their perfermences by changing the parameters of the simulation (debit, number of hope ) with cooja in contiki
    how I proceed
    thank you

  17. HI asanka
    Is it possible to simulate networking attacks like black hole attack and sinkhole attack using cooja simulator

  18. hi , I am trying to run rpl.c using command prompt.Can anyone help me with tis

  19. Hello
    I am new to cooja simulator.
    I want to flood packets from the leaf nodes to the sink, which is about 4-5 hops away from the source node and want check that which path is better in term packet delivery ratio.
    Kindly help...

  20. Dear All!
    Can some body help me in implementing energy efficient routing in RPL using cooja Simulator.i do not know how to change the existing to understand the flow of code already givenin contiki/core/net/rpl/mrhof.c

  21. i have problem with ant run
    Buildfile: /home/ayoub/Bureau/contiki-2.7/tools/cooja/build.xml



    /home/ayoub/Bureau/contiki-2.7/tools/cooja/build.xml:74: Unable to find a javac compiler; is not on the classpath.
    Perhaps JAVA_HOME does not point to the JDK.
    It is currently set to "/usr/lib/jvm/java-6-openjdk-amd64/jre"

    Total time: 0 seconds
    please help me ...