This tutorial aims at introducing the Cooja Simulator and guiding the reader in a simple debugging exercise. We are going to work with Hello World example for the debugging process. You can also study the Hello_World tutorial to get familiar with its source code.
Cooja Simulator is a network simulator specifically designed for Wireless Sensor Networks. A summary of how Cooja executes binaries of different platforms (different types of nodes) and a few details about its structure can be found here:[1].
The simples way of running Cooja is executing it inside its own directory (we will see later how to execute Cooja from any working directory):
cd contiki/tools/cooja ant run
We you execute Cooja the following window pops up.
In the File menu you can start a new simulation or open an existing one. At this moment we will start a new one. You should select: File > New simulation... The following window should show up.
In the Simulation name box you should enter an identifier for the new simulation, and in Advanced settings you can choose parameters of the simulation such as Radio medium, node startup delay and random seed generation. We are going to create a simulation called "First Cooja Sim", as shown above.
After creating a new simulation, Cooja's window is filled with the main simulating tools, as shown in the next image.
Here we briefly describe the functionalities of each tool:
In addition to the default tools, it is possible to exhibit other tools such as Breakpoints, Radio messages, Script editor, Buffer view and Mote duty cycle, which can be enable in the Tools menu.
You need to create a new mote type before starting any simulation. You can do this in the menu Motes > Add motes > Create new motes type. Let's select Sky mote in order to create a mote of the same type as the used Tmote Sky.
The window that shows up (see below) asks for the Description of the new mote type and the Contiki process / Firmware. You can name your mote type as First mote type and you can select the firmware that will be used during the simulation using the Browse button. After selecting the desired firmware you can test the compilation click the Compilebutton. In this example we will use the Hello World fimware, typically located at /contiki/examples/hello-world/hello-world.c. If the compiling process is successed, you will see a final message: LD hello-world.sky in the Compilation output tab.
After successfully compiling the firmware in the last step you should click on Create button (see figure above). A new mote type will be created and you will be able to add a few nodes of that same type in your simulation. You will see the window shown below, where you will be able to set the number of nodes that will be created and specify their positioning. In this example we are going to create 3 new nodes and they will have random positioning.
The simulation configuration and parameters such as number of nodes, type of nodes, firmware used, location of nodes, etc. can be stored in a file for future simulations. You can save your simulation configuration in File > Save simulation as.... The generated file has extension ".csc". In the case of Hello World firmware there already exist a .csc file inside contiki/examples/hello-world, called hello-world-example.csc, which contains 2 nodes. We are going to use this pre-existing Cooja file to show how to debug an application.
The easiest way to debug an application is to initially create a proper .csc simulation file, as described above. Then, inside the folder with the simulation file and all firmware source code (.c and .h files), you need to execute the following command.
make TARGET=cooja .csc
The command above will initialize Cooja Simulator loaded with the desired simulation file and will make Cooja recognized the source code of the application we intend to debug. In our case we are going to simulate and debug the Hello Worls application, so we need to go into /contiki/examples/hello-world folder and execute the command make TARGET=cooja hello-world-example.csc. The window shown below should be displayed.
If we click on Start button the simulation begins and we can see both nodes printing out log messages on the Mote output window.
All debugging tools can be accessed by right-clicking on a node and selecting Mote tools for Sky in the pop-up menu. The two main debugging tools that we are going to use are:Msp Code Watcher and Msp Stack Watcher. The first one can be used for analysing the source code of the application in a step-by-step fashion, with the possibility of setting breakpoints. The second tool is used to read and write the variables used throughout the execution.
The Msp Code Watcher tool can be seen below.
Back to Contiki Tutorials
Edited by: Pedro