If you try to search for Contiki in google, maybe the first link will be a traveling website what could be a little frustrating but don't be angry, the second link probably will be this, the Contiki-os system. Before introduce Contiki it is important to talk a little about 6LoWPAN. Constantly developed by IETF, the 6LoWPAN is the acronym of IPv6 over Low power Wireless Personal Area Networks, what mean that we have a minimal IPv6 protocol over the IEEE 802.15.4 networks. My term paper in the graduation was about 6LoWPAN and I can say that's a huge subject to study and learn more, but I wont extend more about this now, to get more good and quality information see this excellent video and read this book, one of the most important reference that I met during my research.

Getting Started

To start developing with Contiki-os, clone the official repository with all the sub modules dependencies with the recursive flag (Depending of you ISP, this make take a while, go to prepare a coffee :D).

sudo apt-get install build-essential binutils-msp430 gcc-msp430 msp430-libc binutils-avr gcc-avr gdb-avr avr-libc avrdude binutils-arm-none-eabi gcc-arm-none-eabi gdb-arm-none-eabi openjdk-7-jdk openjdk-7-jre ant libncurses5-dev doxygen srecord git
git clone --recursive https://github.com/contiki-os/contiki.git

Once you finish installing the dependencies and clone the repository, you're ready to compile some example for a native (x86) plataform.

cd contiki/examples/hello-world
make all

When you run the hello-world you'll see something like this:

Remember this information

It's important to understand from this moment that Contiki is a multi platform OS system, that is, when you just type make all Contiki looks for a file called Makefile.target, where in this file you define the target processor that're working, when you don't have this file defined (it's not necessary if want to work just for your computer as the target), Contiki builds for a native (x86) target, what does it means your computer using the gcc as the toolchain. When the compilation process is ready and you have the output file realize in the details in the name of the file.
This file highlighted on the picture above shows the example that you have compiled before, where hello-world is the name of the project (defined on the Makefile ~ CONTIKI_PROJECT = hello-world) and the .native is the plataform. One more example could be this same project compiled for another target as hello-world.srf06-cc26xx. In short, the output file always will be:

CONTIKI_PROJECT.target compiled

To list all the targets, just type in terminal make targets and you'll see a lot of targets available, but before comment that does not work compiling for other plataforms, remember to install the corresponding cross-compile-toolchain for your target. In the next section we'll do that for Z1-Zolertia target, pay attention on that.

Cooja - Simulation of motes

Start the Cooja with this:

cd contiki/tools/cooja
ant run

Some inital doubts that I had:

What is and why I need Cooja???

Answer: Cooja is the simulation tool that help you to test your code with Contiki learning about 6LoWPAN/Contiki with real-time graphical formation of a network and if you want to use Contiki it's important to complete understand about the process thread mechanism and with Cooja you can analyze a lot of parameters of execution process.

Why I need Cooja if I want to perform a 6LoWPAN network with real hardware devices, programming them as my final target???

Answer: Even if you know all about protocols and formation of a 6LoWPAN Network you still will need to test your code application (even Yoda test before deploy), and as you want to work with hardware, imagine every time taking the USB cable and programming a lot of nodes? It's complicated and takes a long time! With Cooja, you can instantiate a mote and point a firmware and after your make all all n motes will have the firmware up and running as if you had programmed them all.

If you have done all right, some java software will open on your screen, now I'm working on debian with gnome as desktop environment. Once you get the Cooja opened, you can create a simulation through file > New simulation. Create one simulation with any name just for we work on this (my I'll call as test_cooja). In Radio medium select UDGM that emulates a real scenario of RSSI in function by distance, when all be done, you'll have a screen like mine below:

Creating a 6LoWPAN simulation in Cooja

Presuming that you've searched for something about 6LoWPAN architecture (see this or the link in highlight), you may know that as the header picture of this post, we need a border router to create and form the network capable to communicate outside of 6LoWPAN infrastructure. As in the real hardware infrastructure, you need first the border-router. To create a border router, go to the terminal and compile the rpl-border-router firmware for the target Z1 (we create for Z1, because Contiki can simulate this device).

cd contiki/examples/ipv6/rpl-border-router
make TARGET=z1 all

In the end you'll have the border-router.z1 ready for Z1 in our simulation plan, if you have a problem in the end of the Makefile about cross-toolchain, probably you don't have the msp430-gcc installed on your computer (Z1 uses the MSP430F2617), see this link to install it, if needed.

With the firmware for the border-router ready, let's instantiate on Cooja. Go to Cooja in Motes >> Add motes >> Create new mote type >> Z1 mote..., in the description box call as "border-router" and then select the border-router.z1 firmware in the diablog box Contiki process/Firmware: as you had compiled before, in the end click on create and Add motes.
Now it's time to add motes or nodes, as you prefer to call, devices to our simulation. For this initial tutorial let's compile, the hello-world example for our nodes.

cd contiki/examples/hello-world
make TARGET=z1

Then create the node/mote with same process before to create the border-router device. Go to Cooja, Motes >> Add motes >> Create new mote type >> Z1 mote..., in the description call as "device hw" and select the firmware hello-world.z1.

Before simulate on Cooja, let's create the bridge between simulation and real computer socket, to tunnel the connection from simulator to outside, compile the tunslip6 program included in Contiki repository. Use this commands to compile tunslip6 software:

cd contiki/tools
make tunslip6

Once ready, you have the tunslip6 to tunnel your connection (you'll understand what's tunneling after do it, don't worry for now), to start tunneling the packets we need first configure the server localhost in the Cooja that'll emit our 6LoWPAN packets. For this, go to Cooja in Tools >> Serial socket (SERVER) >> Z1 1... and then configure in the listen port 60001.
Before start, change the Speed limit to 100%, in the Simulation Control dialog box, because we want to see a closer hardware execution flow, after click in Start button. Back to terminal on Linux and type:

cd contiki/tools
sudo chmod +x tunslip6
sudo ./tunslip6 -a aaaa::1/64

By default tunslip6 will listen for IPv6 packets in the, soon, if all it's right, you'll see something like this when you enter the last commands:
Take a look at the IPv6 address that shows in the terminal aaaa::c30c:0:0:1 this is the address of the border-route emulated in the Cooja. The rpl-border-router firmware has an embedded micro http server, where you can access by your browser typing with the assets [xxx::xxx], when you open chrome and type you'll see this:
When new device is added to our simulation, you'll see it in this html list probably as a neighbor with a route from br (border-router). After some seconds, the 6LoWPAN RPL dag root has been formed and you'll have something like this:
If you have something like the picture above, good work, you have created a 6LoWPAN network and you can see on your wireshark from localhost tunneled adapter the packets going in both ways.

To understand the bridge that you created before, you can even ping the IPv6 node that exist on simulation with the command above. The tunneling process makes a connection between the 6LoWPAN network and the Linux network adapter, and if you type ifconfig in this moment you'll see a tun0 network adapter created to talk with the 6LoWPAN.

ping6 aaaa::c30c:0:0:x #where x is the address of the node emulated on cooja

To help who those have some problem to understand my steps in this my limited English, I created a video showing all the process:

Share and subscribe if you enjoy and comment below for doubts....in the next tutorial we'll create a more usefull example with UDP sockets.