20 September 2014

Contiki OS - An Operating System and Framework for IOT (Internet Of Things)

This article is published in the "Open Source For you" magazine in India in this month September 2014. This article is under the creative commons license. For any queries related to this software, please input your queries through the comment section of this page.

Introduction to Contiki OS
Contiki is an open source operating system for connecting the tiny low cost microcontrollers, sensors to the internet. Contiki is preferred because it supports the various internet standards, rapid development, selection of hardware, active community to help and commercial support altogether with a open source license. 

Contiki is designed for tiny devices and thus the memory footprint is very less when compared with other systems, it supports the Full TCP with IPv6, it handles power awareness where in the device power management is handled by the OS, all the modules of Contiki is loaded and unloaded during the run time,  it implements protothreads, uses a light weight file system, various hardware platforms with sleepy routers (to make the routers sleep between message relays). 

One of the important feature of contiki is the use of Cooja Simulator to emulate if any of the hardware devices is not available.

Installation of Contiki
Contiki can be downloaded as “Instant Contiki”. Instant Contiki is available in a single download that contains an entire Contiki development environment. It is an Ubuntu Linux virtual machine that runs in VMWare player and has Contiki and all the development tools, compilers, and simulators used in Contiki development installed. Most of the users prefer instant Contiki over the Source code binaries. 

The current version of Contiki (at the time of writing this post) is 2.7.  
Step 1: Install VMWare Player (which is free for academic and personal use)
Step 2: Download the Instant Contiki virtual image of size 2.5GB approximately (http://sourceforge.net/projects/contiki/files/Instant%20Contiki/) and unzip it.
Step 3: Open the Virtual Machine  and open the Contiki OS and wait till the login screen appears
Step 4: Input the password as “user” and the shows the desktop of Ubuntu(Contiki)

Running the simulation
To run with a simulation, contiki comes with many prebuilt modules that can be readily run on the Cooja simulator or on the real ha7rdware platform.  There are two methods of opening the cooja simuator window.
contiki
Contiki OS Desktop
Method 1: 
In the desktop as shown in Figure 1 – Desktop of Contiki, double click the Cooja icon and it will compile the binaries for the first time and opens the Simulation windows
Method 2: 
Open the Terminal
Go the Cooja directory 
pradeep@localhost$] cd contiki/tools/cooja 

cooja
Figure 2: Cooja Compilation

You can see the simulation window as shown in Figure 2.

Creating a new Simulation
To create a simulation in Contiki, from the File menu New Simulation and name it as shown in Figure 3.
cooja
Figure 3 – New Simulation

Select any one radio medium (in this case) -> Unit Disk Graph Medium (UDGM): Distance Loss and Click Create
The following screen shows the Simulation window (Figure 4). The simulation window has the following windows
  • Network Window – Shows all the motes in the simulated network
  • Timeline Window – shows all the events over the time
  • Mote Output Window – all serial port outputs will be shown here
  • Notes Window – User notes information can be put here
  • Simulation Control Window – Start, stop and pause the simulation 
simulation
Figure 4 – Simulation Window


Adding the Sensor Motes
Once the simulation window is opened, motes can be added to the simulation using the Menu Motes-> Add Motes. Since we add the motes for the first time, the type of the motes have to be specified. There are more than 10 types of motes are supported in contiki
Here are some of them.
  • MicaZ 
  • Sky
  • Trxeb1120
  • Trxeb2520
  • cc430
  • ESB
  • eth11
  • Exp2420
  • Exp1101
  • Exp1120
  • WisMote
  • Z1
Contiki will generate object codes for these motes to run on the real hardware and also to run on the simulator if the hardware platform is not available.
Step 1: 
To add a Mote-> Add Motes Select any of the motes given above MicaZ mote.
You will get a screen like this Figure 6. 
Step 2: 
Cooja opens the Create Mote Type dialog, in which the name of the mote type as well as the Contiki application that the mote type will run. 
For this example click the button on the right hand side to choose the Contiki application and select /home/user/contiki/examples/hello-world/hello-world.c and click Compile.  
Step 3: 
Once compiled without errors, click “Create”.  This is shown in the Figure 6.

motes
Figure 6 – Mote Creation and Compile Contiki

Step 4: Now the screen asks to enter the number of motes to be created and its position (random or ellipse, linear or manual position)
In this example, 10 motes are created and click the “start” button in the Simulation Control window and enable the mote's “Log Output: printf()'s” statements in the view menu of the Network window. The network window shows the output Hello World in the sensors. Figure 7 is represented by this

motes
Figure 7 – Log Output in Motes 
This is a simple output of the Network window where it shows the “Hello World” application. If the real MicaZ motes are connected and are written the object to the motes, the Hello World will be displayed in the LCD Panel of the Sensor Motes.  The overall output is shown in Figure 8

contiki
Figure 8 – Simulation window of Contiki


Also the output of the above hello world application can be run using the terminal also 
To compile and test the program, go into the hello-world directory:
--------------------------------CODE-----------------------------

pradeep@localhost $]  cd /home/user/contiki/examples/hello-world

This will compile the hello-world program in the 'native' target.  This causes the entire Contiki operating system and the hello-world application to be compiled into a single program that can be run by typing the following command (This is depicted in Figure 9)
Figure 9 - Compilation using Terminal
pradeep@localhost $] ./hello-world.native

This will print out the following text:
Contiki initiated, now starting process scheduling
Hello, world
--------------------------------CODE-----------------------------

The program will then appear to hang, and must be stopped by pressing the C key
while holding down the Control key.

Developing new modules
Contiki comes with numerous prebuilt modules like IPv6, IPV6 udp, hello world, sensor nets, EEPROM, IRC, Ping, Ping-IPv6, etc. These modules can run with all the sensors irrespective of their make. Also there are modules that runs only on specific sensors. For example, energy of a sky mote can be used only on Sky Motes, they give errors if running with other motes like Z1 or MicaZ. 

Developers can build new modules for various sensor motes that can be used with various sensor BSPs using the conventional C programming and can be deployed in the corresponding sensors. 

Here is the C source code for above hello-world application.
--------------------------------CODE-----------------------------
#include "contiki.h"
#include <stdio.h> /* For printf() */
/*---------------------------------------------------------------------------*/
PROCESS(hello_world_process, "Hello world process");
AUTOSTART_PROCESSES(&hello_world_process);
/*---------------------------------------------------------------------------*/
PROCESS_THREAD(hello_world_process, ev, data)
{
  PROCESS_BEGIN();
  printf("Hello, world\n");
  PROCESS_END();
}
--------------------------------CODE-----------------------------

Conclusion: Internet of Things is an emerging technology that leads to Smart City, Smart home, etc applications. Over these years, implementing IOT was really a challenge and now there is one such OS contiki is available to start with.  Contiki can be very useful for deploying applications like automatic lighting system in buildings, Smart refrigerators, Wearble computing systems, domestic power management for homes, offices, etc. 

Reference: 


T S Pradeep Kumar