Wednesday, April 20, 2016

RPL Routing on RIOT Native Platform

RPL is the de facto routing protocol to be used on low powered wireless devices such as Internet of Things (IoT) applications. Therefore it is the most important thing to try on an operating system designed to be run on low powered wireless network. Today I tried the RPL routing protocol implementation on RIOT OS and decided to write a short note about the steps I followed to get it done. It is important to note that I tried this on the native platform, so there's no any real wireless network in this scenario. It's just a set of virtual network interfaces on my Linux machine that interconnects the RIOT instances. Let's see.

(1) Let's start 4 TAP interfaces on our machine.

cd RIOT
./dist/tools/tapsetup/tapsetup -c 4

(2) We will be using the gnrc_networking application for this trial. So, move into that directory and build the application.

cd examples/gnrc_networking
make clean all

(3) Now we have to start 4 RIOT instances in 4 separate Linux terminals. In the first terminal, we start the first instance as follow.
make term PORT=tap0

On the second terminal,

make term PORT=tap1

On the third terminal,

make term PORT=tap2

On the fourth terminal,

make term PORT=tap3

(4) Now our RIOT instances are up and running on 4 different terminals. It's time to prepare the RPL boarder router. We choose the RIOT instance running in first terminal as the RPL boarder router. So, let's set an IP address for that instance.

First of all, we just use the following command to get to know the name of the available network interface for this RIOT instance,

ifconfig

Then, set an IP address as follows,

ifconfig 6 add 2001:db8::1

(5) Our boarder router is ready. We should now initialize RPL routing on all the RIOT instances by entering following command on each of the terminals. (note that the parameter given as 6 is the name of the networking interface on which RPL will be based on)

rpl init 6

(6) Now our all the RIOT instances are running RPL protocol but we don't have a real RPL network using a DODAG is not not available yet. For that, we should tell the boarder router that it is the root node of this routing tree as follows. Then it will automatically build the DODAG by communicating with the other nodes in the vicinity.

rpl root 1 2001:db8::1

(7) To see the status of the network now, we can use the rpl command on each terminal. It will show some important details of each RIOT instance such as it's rank and the parent nodes IP address. It is interesting to see that

rpl

(8) Let's ping from the second, third and fourth RIOT instances to the first instance and see whether the connectivity is available.

ping6 2001:db8::1

(9) Let's try the UDP example on this network. We will run the UDP server on the first RIOT interface and UDP clients on the other RIOT instances.

On first RIOT instance,
udp server start 80

On any other RIOT instance,

udp send 2001:db8::1 80 hello

(10) To wrap up our work, let's remove the virtual network interfaces (TAP) we created on our Linux machine for this trial as follows.

../../dist/tools/tapsetup/tapsetup -d

That's all folks!! :)

References:

Tuesday, April 19, 2016

UDP communication example in RIOT

In a previous post, I wrote about how to try the basic networking example in RIOT OS called "gnrc_networking". In that post, I wrote only about how to ping from one RIOT instance to another. However, this example application supports running a UDP server and a client to deliver data packets. In this blog post, I decided to try describe how I tried that UDB communication. Here we go.

(1) Start the TAP devices as usual.

cd RIOT
./dist/tools/tapsetup/tapsetup -c 2

(2) From a one shell window, run the following commands to start a RIOT instance and set its IP address. When setting the IP address, the value 6 given as the second parameter to the ifconfig command is the name of the network interface of this RIOT instance. We found out that by initially giving the ifconfig command.

cd RIOT/examples/gnrc_networking
make all term PORT=tap0
ifconfig
ifconfig 6 add 2001:db8::1/64

(3) Open another shell window and run the following commands to get another RIOT instance up and running.

make term PORT=tap1
ifconfig
ifconfig 6 add 2001:db8::2/64

(4) Now we can try pinging from the first RIOT instance to the other to check whether we have the connectivity properly.

ping6 2001:db8::2

(5) It's time to try the UDP example. Goto the shell of the first RIOT instance and give the following command to start a UDP server there.

udp server start 8080

(6) Now, goto the second RIOT instance and use the following command to send a string message called  "hello" from the second RIOT instance to the first.

udp send 2001:db8::1 8080 hello

Once this command was entered in the shell of second RIOT instance, we should be able to see that first RIOT instance receives it and prints the details. In case we want to send multiple consecutive UDP packets from the client to the sender, we can specify the number  in the above command. Following line shows the command to send "hello" message 10 times.

udp send 2001:db8::1 8080 hello 10

One thing I like in RIOT more than any other OS for resource constrained devices is this shell prompt with some interesting tools to demonstrate its capabilities.

(7) Finally after exiting from the both RIOT instances by using Ctrl+C, we can stop the TAP devices as follows.

./dist/tools/tapsetup/tapsetup -d

That's all folks!

Thursday, April 14, 2016

Getting to know the shell in RIOT OS

In a previous article I wrote about writing a simple application for RIOT OS and and running it on native platform. It's time to go beyond that point and explore what's more we can do inside RIOT applications. In this article, I would like to show the things I have explored with shell module in RIOT. When we enable the shell module for a user application, we get a nice shell interface using which we can interact with the application.

Let's go ahead and modify our application program to have a shell interface. It can be done by adding few lines to the existing program. Below code shows the updated main.c program which invokes the shell module.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
#include<stdio.h>
#include "shell_commands.h"

int main(void)
{
    puts("Starting my shell program");

    char line_buff[SHELL_DEFAULT_BUFSIZE];
    shell_run(NULL, line_buff, SHELL_DEFAULT_BUFSIZE);

    return 0;
}

Then we can compile and run this program as usual.

make
./bin/native/helloworld.elf

Now we get the shell prompt of our program. We can type help on the prompt to see the available commands for us in this shell program. However in this shell we don't have any command. We can exit from the running RIOT instance by issuing a Ctrl+C on the prompt.

In order to make different RIOT commands available for the shell, we have to enable different modules at the compile time. We do that by updating the Makefile of our program. Update the right place to enable the two modules shell_commands and ps in our user program. Following excerpt is from the updated region of my Makefile.

1
2
3
4
5
6
7
8
# Modules to include:

USEMODULE += shell
USEMODULE += posix
USEMODULE += xtimer

USEMODULE += shell_commands
USEMODULE += ps

This time when you compile and run the application, you will see that two new commands are available to you. In my shell I got reboot and ps commands. Now we know how we get them by enabling different modules in the Makefile. What if I wanted some new commands in my program with the shell. Is there any way to add my own commands that do different things on the shell? Yes, there is a way to define new commands and make them available to your programs. Let's try that. Following code shows an updated version of our main.c program which has a new command called echo defined by ourself.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include<stdio.h>
#include "shell_commands.h"

int my_echo(int argc, char **argv)
{
    if (argc < 2) {
        printf("usage: %s [string]\n", argv[0]);
        return 1;
    }

    printf("%s\n",argv[1]);
    return 0;
}

static const shell_command_t shell_commands[] = {
    { "echo", "echo back the string given as a parameter", my_echo},
    { NULL, NULL, NULL } 
};

int main(void)
{
    puts("Starting my shell program");
    char line_buf[SHELL_DEFAULT_BUFSIZE];
    shell_run(shell_commands, line_buf, SHELL_DEFAULT_BUFSIZE);

    return 0;
}

When you compile and run this program you should see that a new command called echo is available to you. I hope this blog post helps to understand how we utilize the shell module in our programs.


Wednesday, April 13, 2016

Writing an application for RIOT OS

An application that runs on RIOT OS should have at least 2 files. The first thing is a Makefile in which we set various variables in order to do the compilation process properly. The second thing is a C program with a main function. Let's go ahead and write a new hello world program for RIOT OS. There's no need to mention that we should have downloaded RIOT OS source code first. It should be saved in somewhere in my file system.

We start creating our program by first creating a new directory for our new program. We can do that anywhere in your file system and I will have it helloworld. Inside it, create a Makefile. The easiest way to do is copying the sample Makefile provided to us by RIOT OS.

mkdir helloworld
cd helloworld
cp ../../RIOT/dist/Makefile ./

Now, edit the Makefile to have a content like the following. We basically changed the application name, modified the path to RIOT OS source code and uncommented few lines.


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
####
#### Sample Makefile for building applications with the RIOT OS
####
#### The example file system layout is:
#### ./application Makefile
#### ../../RIOT
####

# Set the name of your application:
APPLICATION = helloworld

# If no BOARD is found in the environment, use this default:
BOARD ?= native

# This has to be the absolute path to the RIOT base directory:
RIOTBASE ?= $(CURDIR)/../../RIOT

# Uncomment this to enable scheduler statistics for ps:
CFLAGS += -DSCHEDSTATISTICS

# If you want to use native with valgrind, you should recompile native
# with the target all-valgrind instead of all:
# make -B clean all-valgrind

# Uncomment this to enable code in RIOT that does safety checking
# which is not needed in a production environment but helps in the
# development process:
CFLAGS += -DDEVELHELP

# Change this to 0 show compiler invocation lines by default:
QUIET ?= 1

# Modules to include:

USEMODULE += shell
USEMODULE += posix
USEMODULE += xtimer

# If your application is very simple and doesn't use modules that use
# messaging, it can be disabled to save some memory:

#DISABLE_MODULE += core_msg

#export INCLUDES += -Iapplication_include

# Specify custom dependencies for your application here ...
# APPDEPS = app_data.h config.h

include $(RIOTBASE)/Makefile.include

# ... and define them here (after including Makefile.include,
# otherwise you modify the standard target):
#proj_data.h: script.py data.tar.gz
# ./script.py


It's time to add our C program with the main function. Following sample code shows my C program saved with the name main.c inside the same helloworld directory.


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
#include<stdio.h>
#include "xtimer.h"

int main(void)
{
    puts("Starting helloworld application!");
    xtimer_usleep(SEC_IN_USEC);
    puts("returning from the application!");

    return 0;
}

Now we can compile and run our simple program to see how it works. We can do that by first issuing the command make inside the helloworld directory. The executable ELF file will be available inside the same directory but deep inside few other child directories. We can run the program as follows.

./bin/native/helloworld.elf 

It should show the output of our program and hangup. To terminate the execution of RIOT system, we have to give a Ctrl+C.


Simple RIOT networking example

After starting to try out simple examples of RIOT, one important thing I was struggling to get working is some networking activity. It is unfortunate to mention that many examples and tutorials that I found in the web seems obsolete. Even in the official RIOT wiki pages, most of the guidelines are obsolete and the developers seems too busy to update them. Anyway, after trying different ways finally I decided to write down about how to get a simple networking example working.

In various places over the web, it is mentioned that the default example can be used with the tapsetup to send simple packets between two RIOT instances on native platform. It was mentioned that txtsnd command can be used to send simple text strings between the RIOT instances connected via a TAP bridge. However it didn't work at least on my system and on few others. Finally I found in the mailing list that somebody has suggested to try it on gnrc_networking example instead of the default [1,2]. I tried that but txtsnd command still seems not working. However I found that I can at least ping from one instance to the other after setting IPv6 addresses on the two instances. So, in this article I will write down those steps.

cd RIOT
./dist/tools/tapsetup/tapsetup -c 2


cd RIOT/examples/gnrc_networking
make all term PORT=tap0
ifconfig
ifconfig 6 add 2001:db8::1/64


When setting the IP address, we need the name of the networking interface. In the above command sequence, we got to know that the networking interface name is 6 by first checking with ifconfig. That's why we used it to set IP address in the next line.

Open a new terminal and goto the same example directory and run the following commands to start another instance.

make term PORT=tap1
ifconfig
ifconfig 6 add 2001:db8::2/64



Now from the shell of second RIOT instance, we can ping to the first RIOT instance using the IPv6 address as follows.

ping6 2001:db8::1

Now, we should receive ping responses. To exit from the two instances, we have to give Ctrl+C on the shell. Moreover, it is necessary to stop the virtual network
interfaces as follows.

./dist/tools/tapsetup/tapsetup -d


References:

[1] https://github.com/RIOT-OS/RIOT/wiki/Virtual-riot-network
[2] http://article.gmane.org/gmane.os.riot.user/580

Monday, April 11, 2016

Hands-on RIOT OS

RIOT is an operating system for low-powered resource constrained wireless devices which are mostly used in IoT applications. Since I have experiences only on TinyOS and Contiki so far, I decided to tryout RIOT OS to see it's capabilities. It is important to note that unlike most of other operating systems for IoT applications where event-driven model is used, RIOT is a microkernel based OS with truely real-time process scheduling capability. So, I guess it will be more interesting to write applications for RIOT though I need more time to understand the basics.

RIOT OS source code can be downloaded from their Github page as follows.

git clone https://github.com/RIOT-OS/RIOT.git

Compiling to native platform:

There are some example applications available to try inside RIOT source code which you can find inside the "examples" directory. Let's try the "default" application first. Like in Contiki, we can compile this RIOT application to native platform enabling us to run it on Linux. We can easily do that as follows.

cd RIOT/examples/default
make

Now, run this compiled elf executable using the following command,

sudo ./bin/native/default.elf tap0 -c /dev/ttyS0 -c /dev/ttyS1

When running this application, we get a small shell program on which we can type simple commands and interact with the RIOT program. Type "help" on this shell prompt to see the available commands. To exit from this program, we have to give Ctrl+C on the prompt. To clean the compiled files from the directory space, we can use the following command later.

make BOARD=native clean

Compiling and running on real telosb platform:

Now, let's try compiling RIOT OS to some other real wireless sensor mote platform. I selected "telosb" platform since we have Tmote Sky modules in our lab which are equivalent to telosb platform. Following command can be used to compile RIOT to telosb platform.

cd RIOT/examples/default
make BOARD=telosb


Now, plug-in a telosb mote into a USB port of the computer and properly give permission to access it using chmod command. For example, if the mote is mounted to /dev/ttyUSB0, following command will provide permission to access it.

sudo chmod 777 /dev/ttyUSB0

We can use a single command to compile RIOT OS and write it into the flash memory of the plugged-in telosb mote as follows.

make BOARD=telosb PORT=/dev/ttyUSB0 flash

It will take a little time to write to the flash memory of the mote. Finally, if we want to clean up the temporary files generated during the compilation time, we can use the following command.

make BOARD=telosb clean

Compiling and running on Cooja simulator:


Another possibility of trying RIOT programs is available for us. That is compiling RIOT progam into some real mote platform and running the executable on Cooja sensor network simulator which usually comes with Contiki OS. Let's try that now. First, we have to compile our program to a specific sensor mote platform. I will use telosb platform as usual.

cd RIOT/examples/default
make clean all BOARD=telosb


Now rename the elf file default.elf to default.sky to be able to use in Cooja since Cooja simulator expects an executable that it is supposed to run on Cooja to be save with .sky extention. Then, this executable is ready to run on Cooja. I had Cooja on my InstantContiki VM so, I started the VM and then started the Cooja simulator inside it using "ant run" command. Copy the default.sky file into the instant contiki filesystem. Now create a new simulation and a sky mote type and for the firmware, browse and give the default.sky firmware. Running the simulation should print the RIOT OS output on the mote output log.

References:

1. https://github.com/RIOT-OS/RIOT/wiki/Introduction
2. http://riot-os.org/api/index.html
3. https://github.com/RIOT-OS/RIOT/wiki/RIOT-and-Cooja