Compiling, uploading and interacting with Contiki: Hello World

From CoMo Robotics
Jump to: navigation, search

Back to Contiki page: Contiki

Back to Wireless Sensor networks page: Wireless Sensor Networks


Contents



Compiling, uploading and connecting to a TMote Sky

The Hello World application is the most simple example of a Contiki application. We will use this application to illustrate the basic commands used to compile, run, and get the data from a sensor node via the USB port. The Hello World is situated in the examples/hello-world folder. Go to this folder, connect a TmoteSky to your computer, and run the command

make hello-world.upload TARGET=sky

This compiles (which takes some time when run for the first time, as all the Contiki code is compiled), and uploads the code on the sensor node. Once compiled, you can log on to the node with the command

make login TARGET=sky

which displays

../../tools/sky/serialdump-linux -b115200 /dev/ttyUSB0
connecting to /dev/ttyUSB0 (115200) [OK]

This brings you on the mote. You can open the hello-world.c file in Eclipse to see what the content is. Basically, the program is made of one thread, which displays Hello world on the standard output when the program is started. Now, simply reset the Tmote Sky, and you should see displayed on your console:

Rime started with address 41.0
MAC 00:12:75:00:0c:57:4c:e7
Starting 'Hello world process'
Hello, world

In Contiki, nodes have IP addresses, and Rime is the IP communication stack of Contiki. The address 41.0 is the address of the node, in a IP-like subnetwork, whose address is hardcoded by default as 172.16 in different files of the OS, such as in the platform/sky/Makefile.sky file.

Getting the data from the USB port

A sensor node connected to the computer by means of the USB port sends data to the computer with the printf() command. The data can be retrieved on the computer with the serialdump-linux command, situated in the tools/sky folder. Go to this folder and run

serialdump-linux -b115200 /dev/ttyUSB0 

The arguments assume that a TMote Sky is connected, whose bit rate is 115200, and that the device is connected to the file /dev/ttyUSB0. Now, resetting again the node, you should see

Rime started with address 41.0
MAC 00:12:75:00:0c:57:4c:e7
Starting 'Hello world process'
Hello, world

This is the same output as the make login. Note that the argument /dev/ttyUSB0 is important, as even if the program seems to successfully connect, it may not be to the desired USB port. The serialdump-linux command actually allows you to interact with the node connected to your computer, and is the command used in the make login to have you access the node. This command allows not only to receive data from the node, but also to send data to the node. The Hello World program does not react to commands. Therefore, entering a string will simply display the number of bytes sent, i.e., entering help displays

help
SEND 5 bytes

Some applications, like the data collection demo (see Some cool demos), provide you with a wide range of command to interact with a sensor node.

Note that the command can take different options (see serialdump-linux -h. In particular, the output may be obtained in decimal (option -i) or hexadecimal (option -x) format. The command

serialdump-linux -b115200 -x /dev/ttyUSB0

outputs

connecting to /dev/ttyUSB0 (115200) [OK]
 00436F6E 74696B69 20322E33 20737461 72746564  .Contiki 2.3 started
 2E204E6F 64652069 64206973 20736574 20746F20  . Node id is set to 
 34322E0A 52696D65 20737461 72746564 20776974  41..Rime started wit
 68206164 64726573 73203432 2E300A4D 41432030  h address 41.0.MAC 0
 303A3132 3A37343A 30303A30 643A3963 3A64613A  0:12:74:00:0d:9c:da:
 63632058 2D4D4143 20636861 6E6E656C 2032360A  cc X-MAC channel 26.
 53746172 74696E67 20274865 6C6C6F20 776F726C  Starting 'Hello worl
 64207072 6F636573 73270A48 656C6C6F 2C20776F  d process'.Hello, wo
 726C640A                                                                                rld.

and the command

serialdump-linux -b115200 -i /dev/ttyUSB0

outputs

connecting to /dev/ttyUSB0 (115200) [OK]
 000 067 111 110 116 105 107 105 032 050 046 051 032 115 116 097 114 116 
 101 100 046 032 078 111 100 101 032 105 100 032 105 115 032 115 101 116 
 032 116 111 032 052 050 046 010 082 105 109 101 032 115 116 097 114 116 
 101 100 032 119 105 116 104 032 097 100 100 114 101 115 115 032 052 050 
 046 048 010 077 065 067 032 048 048 058 049 050 058 055 052 058 048 048 
 058 048 100 058 057 099 058 100 097 058 099 099 032 088 045 077 065 067 
 032 099 104 097 110 110 101 108 032 050 054 010 083 116 097 114 116 105 
 110 103 032 039 072 101 108 108 111 032 119 111 114 108 100 032 112 114 
 111 099 101 115 115 039 010 072 101 108 108 111 044 032 119 111 114 108 

Some make predefined commands

The Makefile provides a number of useful commands for interacting with the sensor nodes. We review hereafter some commands provided for interacting with TMote Sky nodes. These commands are defined in platform/sky/Makefile.sky, which is included when TARGET=sky is passed to the command line. Use

make command TARGET=sky

where command is:

  • sky-motes: List the devices of the nodes connected to the computer.
  • sky-motelist: List the reference of the nodes connected to the computer, together with their devices and description.
  • sky-reset: Reset the connected nodes.
  • sky-upload: Load the file tmpimage.ihex (default ihex file) to the nodes.
  • xxx.upload: Compile and load the file xxx.sky to the nodes.
  • linslip: Makes a serial connection over IP to the node. More will be said in Section \ref{webserver}.
  • serialdump: Print the output of the connected node to the console, preceded by a timestamp.
  • serialview: Same as serialdump, but allows to interact with the node.
  • login: Same as serialview, without the timestamp.

The Makefile is also used for building and compiling applications.


Getting the data from a Java program

It is useful for a number of applications to display the data collected in the USB port on a graphical interface. Java programs may for example be used for that purpose. In Java, the retrieval of the USB data can be easily done by the serialdump-linux command, whose output is connected to a Stream object. A simple example SimpleDataLog.java is given here:

/*This file allows to display the data sent by a node connected to 
the computer.
It is a 'much' simplified version of the Energest Demo code written 
by Frederik Osterlind - SICS Lab - Sweden.
The original code is in the energest demo folder, located in the 
examples/energest/src folder of the contiki-2.3 distribution */

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Vector;
import javax.swing.Timer;
import org.jfree.data.time.TimeSeries;
import java.awt.event.*;

public class Demo {
  public static final String SERIALDUMP_LINUX = "./tools/sky/serialdump-linux";

  private Process serialDumpProcess;

  private String comPort;

  public Demo(String comPort) {
    this.comPort = comPort;
    System.out.println("Listening on COM port: " + comPort);

    /* Connect to COM using external serialdump application */
    String fullCommand;
    fullCommand = SERIALDUMP_LINUX + " " + "-b115200" + " " + comPort;
 
    try {
      String[] cmd = fullCommand.split(" ");
      serialDumpProcess = Runtime.getRuntime().exec(cmd);
      final BufferedReader input = new BufferedReader
		(new InputStreamReader(serialDumpProcess.getInputStream()));
      final BufferedReader err = new BufferedReader
		(new InputStreamReader(serialDumpProcess.getErrorStream()));

      /* Start thread listening on stdout */
      Thread readInput = new Thread(new Runnable() {
        public void run() {
          String line;
          try {
            while ((line = input.readLine()) != null) {
              parseIncomingLine(line);
            }
            input.close();
            System.out.println("Serialdump process shut down, exiting");
            System.exit(1);
          } catch (IOException e) {
            System.err.println("Exception when reading from serialdump");
            e.printStackTrace();
            System.exit(1);
          }
        }
      }, "read input stream thread");

 
      readInput.start();
 
    } catch (Exception e) {
      System.err.println("Exception when executing '" + fullCommand + "'");
      System.err.println("Exiting demo application");
      e.printStackTrace();
      System.exit(1);
    }
  }

  
  
  public void parseIncomingLine(String line) {
    if (line == null) {
      System.err.println("Parsing null line");
      return;
    }

    /* Split line into components */
    String[] components = line.split(" ");
 
    for (int i=0; i<components.length; i++) {
        System.out.print(components[i]+" ");
    }
    System.out.println();
 
   }


 

  public static void main(final String[] args) {
    if (args.length != 1) {
      System.err.println("Usage: java Demo COMPORT [TRACK_NODE_ID]");
      return;
    }

    final String comPort = args[0];
    new Demo(comPort);
   }
}

Pretty much like the serialdump-linux command, the java program outputs

Contiki 2.3 started. Node id is set to 41. 
Rime started with address 41.0 
MAC 00:12:74:00:0d:9c:da:cc X-MAC channel 26 
Starting 'Hello world process' 
Hello, world 

when the reset button of the Tmote Sky is pushed.

Personal tools