The Complete Magazine on Open Source

Contiki OS Connecting Microcontrollers to the Internet of Things

, / 211 0


As the Internet of Things becomes more of a reality, Contiki, an open source OS, allows DIY enthusiasts to experiment with connecting tiny, low-cost, low-power microcontrollers to the Internet.

Contiki is an open source operating system for connecting tiny, low-cost, low-power microcontrollers to the Internet. It is preferred because it supports various Internet standards, rapid development, a selection of hardware, has an active community to help, and has commercial support bundled with an open source licence.

Contiki is designed for tiny devices and thus the memory footprint is far less when compared with other systems. It supports full TCP with IPv6, and the device’s power management is handled by the OS. All the modules of Contiki are loaded and unloaded during run time; it implements protothreads, uses a lightweight file system, and various hardware platforms with ‘sleepy’ routers (routers which sleep between message relays).
One important feature of Contiki is its use of the Cooja simulator for emulation in case any of the hardware devices are not available.

Installation of Contiki
Contiki can be downloaded as ‘Instant Contiki’, which 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 already installed. Most 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.5 GB, approximately ( and unzip it.
Step 3: Open the Virtual Machine and open the Contiki OS; then wait till the login screen appears.
Step 4: Input the password as ‘user’; this shows the desktop of Ubuntu (Contiki).


Figure 1 : Contiki OS desktop

Running the simulation
To run a simulation, Contiki comes with many prebuilt modules that can be readily run on the Cooja simulator or on the real hardware platform. There are two methods of opening the Cooja simulator window.
Method 1: In the desktop, as shown in Figure 1, double click the Cooja icon. It will compile the binaries for the first time and open the simulation windows.
Method 2: Open the terminal and go to the Cooja directory:

pradeep@localhost$] cd contiki/tools/cooja 
pradeep@localhost$] ant run

Figure 2 : Cooja compilation

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


Figure 3 : New Simulation

Creating a new simulation
To create a simulation in Contiki, go to File menu ? New Simulation and name it as shown in Figure 3.
Select any one radio medium (in this case) -> Unit Disk Graph Medium (UDGM): Distance Loss and click ‘Create’.


Figure 4 : Simulation window

Figure 4 shows the simulation window, which has the following windows.
Network window: This shows all the motes in the simulated network.
Timeline window: This 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: Users can start, stop and pause the simulation from here.
Adding the sensor motes
Once the simulation window is opened, motes can be added to the simulation using Menu: Motes-> Add Motes. Since we are adding the motes for the first time, the type of mote has to be specified. There are more than 10 types of motes supported by 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, go to Add Motes?Select any of the motes given above?MicaZ mote. You will get the screen shown in Figure 5.
Step 2: Cooja opens the Create Mote Type dialogue box, which gives 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. Then, click Compile.


Figure 5 : Mote creation and compilation in contiki

Step 3: Once compiled without errors, click Create (Figure 5).
Step 4: Now the screen asks you to enter the number of motes to be created and their positions (random, ellipse, linear or manual positions).
In this example, 10 motes are created. Click the Start button in the Simulation Control window and enable the mote’s Log Output: printf() statements in the View menu of the Network window. The Network window shows the output ‘Hello World’ in the sensors. Figure 6 illustrates this.


Figure 6 : Log output in motes

This is a simple output of the Network window. If the real MicaZ motes are connected, the Hello World will be displayed in the LCD panel of the sensor motes. The overall output is shown in Figure 7.


Figure 7 : Simulation window of contiki

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

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

Figure 8 : Complilation using the terminal

This will compile the Hello World program in the native target, which 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 (depicted in Figure 8):

pradeep@localhost$] ./hello-world.native
This will print out the following text:
Contiki initiated, now starting process scheduling
Hello, world

The program will then appear to hang, and must be stopped by pressing Control + C.
Developing new modules
Contiki comes with numerous pre-built 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 run only on specific sensors. For example, the energy of a sky mote can be used only on Sky Motes and gives errors if run with other motes like Z1 or MicaZ.
Developers can build new modules for various sensor motes that can be used with different sensor BSPs using conventional C programming, and then be deployed in the corresponding sensors.
Here is the C source code for the above Hello World application.

#include "contiki.h"
#include <stdio.h> /* For printf() */
PROCESS(hello_world_process, "Hello world process");
PROCESS_THREAD(hello_world_process, ev, data)
printf("Hello, world\n");

The Internet of Things is an emerging technology that leads to concepts like smart cities, smart homes, etc. Implementing the IoT is a real challenge but the Contiki OS can be of great help here. It can be very useful for deploying applications like automatic lighting systems in buildings, smart refrigerators, wearable computing systems, domestic power management for homes and offices, etc.