Contiki Shell
Contents
- 1 Introduction
- 2 Objective
- 3 You will learn
- 4 Source Code and Example Projects
- 5 How to Compile and Install a Contiki shell ?
- 6 Modify sky-shell project for this tutorial
- 7 Contiki Shell Commands, their usage and Contiki shell programming
- 8 How to write your own Shell commands
- 9 An example project for creating your own Shell with your commands
- 10 Issues you might face
- 11 References
- 12 Feedback
Introduction
The Contiki Shell is an interactive on-mote UNIX-style shell that allows for text‐based interaction with a sensor node or a network of sensor nodes through a set of commands that can be executed on a UNIX like command line terminal. It has features such as piping data, run in background, file system interaction, network commands, sensor measurement commands and system commands. The shell can be accessed either over a serial USB connection or over a network using Telnet.
Below are the various instances of shells provided in Contiki:
1) example-shell: This shell can be compiled only for the native target, but not on Tmote Sky due to the firmware image size limitation on the Tmote Sky.
2) sky-shell: This shell is a thinned out version, which can be compiled on Tmote Sky nodes, allowing the firmware image to fit the Tmote Sky memory.
3) sky-shell-exec: This shell is a further thinned out version and has features such as the exec command allowing to load and execute ELF files.
4) sky-shell-webserver: This is a shell that features the command sky-all-data, allowing to collect different sensor measurements and network statistics from a Tmote Sky mote.
In this tutorial, we will run the shell over a USB serial connection and will focus on example-shell, sky-shell provided in Contiki source.
Objective
At the end of this tutorial, goal is to make you at ease with the Contiki shell.
I enjoyed working with the Contiki Shell, I hope at the end you have a good learning:)
You will learn
In this tutorial you will learn about the following topics:
1) How to get a Contiki Serial Shell up and running on Tmote sky nodes.
2) Various Contiki Shell Commands, their usage and Contiki shell programming.
3) How to create your own Shell Commands.
4) How to create your own Shell for Contiki through an example project.
5) You get to execute various fun examples and understand the Contiki shell along the way.
Sounds exciting ? Game for it ? Let's begin then!
Source Code and Example Projects
Let's figure out where the source code for shell is in Contiki
This folder has all the source and headers for the Contiki Shell
~/contiki-2.7/apps/shell
This folder has the example-shell project
~/contiki-2.7/examples/example-shell
This folder has the sky-shell project
~/contiki-2.7/examples/sky-shell
How to Compile and Install a Contiki shell ?
example-shell is a Comprehensive Shell and can be used for initial development.
Pros: It's compiled on the native target, so you have the flexibility of developing your own shell commands and include it in the shell for further testing. Since, there's no stringent memory constraints on native targets, this project is useful for initial development phase.
Cons: Since this shell has comprehensive set of shell commands, it increases the memory footprint of the firmware and hence can't be compiled on Tmote Sky motes.
Navigate to this path to find the example-shell project : ~/contiki-2.7/examples/example-shell
example-shell.c is the shell file in this project.
*** Each shell_xx_init() adds a set of features to shell, hence if you run out of space while compiling try removing these. ***
These shell_xx_init() are located in contiki-2.7/apps/shell/ in files like shell-filename.c. You can explore the source code to check for what features they come with and what's in it, to make our lives simpler.
/*---------------------------------------------------------------------------*/
PROCESS(example_shell_process, "Contiki shell");
AUTOSTART_PROCESSES(&example_shell_process);
/*---------------------------------------------------------------------------*/
PROCESS_THREAD(example_shell_process, ev, data)
{
PROCESS_BEGIN();
serial_shell_init();
shell_base64_init();
shell_blink_init();
/*shell_checkpoint_init();*/
/*shell_coffee_init();*/
shell_download_init();
/*shell_exec_init();*/
shell_file_init();
shell_httpd_init();
shell_irc_init();
shell_netfile_init();
/*shell_ping_init();*/ /* uIP ping */
shell_power_init();
/*shell_profile_init();*/
shell_ps_init();
/*shell_reboot_init();*/
shell_rime_debug_init();
shell_rime_netcmd_init();
shell_rime_ping_init(); /* Rime ping */
shell_rime_sendcmd_init();
shell_rime_sniff_init();
shell_rime_init();
/*shell_rsh_init();*/
shell_run_init();
shell_sendtest_init();
/*shell_sky_init();*/
shell_tcpsend_init();
shell_text_init();
shell_time_init();
shell_udpsend_init();
shell_vars_init();
shell_wget_init();
PROCESS_END();
}
As we see above some of the shell_xx_init() are already commented out, this done on the pretext that these features are not supported on native target.
Compiling and installing the Contiki shell on native target
cd contiki-2.7/examples/example-shell make
Run the Contiki Shell on native target
./example-shell.native
After running the Contiki Shell, you will see the Contiki Shell prompt
user@instant-contiki:~/contiki-2.7/examples/example-shell$ ./example-shell.native Contiki 2.7 started Rime started with address 2.1 MAC nullmac RDC nullrdc NETWORK Rime 2.1: Contiki>
Punch in help on the Contiki Shell prompt to see all the available shell commands in Contiki shell.
help
You can now run any supported shell commands
For example, running echo hello will echo the <text> back on the shell prompt.
2.1: Contiki> echo hello hello 2.1: Contiki>
sky-shell is a thinned out version of Contiki Shell for Tmote Sky motes.
Navigate to this path to find the sky-shell project : ~/contiki-2.7/examples/sky-shell
sky-shell.c is the shell file in this project
*** Each shell_xx_init() adds a set of features to shell, hence if you run out of space on the mote try removing these. ***
/*---------------------------------------------------------------------------*/
PROCESS(sky_shell_process, "Sky Contiki shell");
AUTOSTART_PROCESSES(&sky_shell_process);
/*---------------------------------------------------------------------------*/
#define WITH_PERIODIC_DEBUG 0
#if WITH_PERIODIC_DEBUG
static struct ctimer debug_timer;
static void
periodic_debug(void *ptr)
{
ctimer_set(&debug_timer, 20 * CLOCK_SECOND, periodic_debug, NULL);
collect_print_stats();
}
#endif /* WITH_PERIODIC_DEBUG */
/*---------------------------------------------------------------------------*/
PROCESS_THREAD(sky_shell_process, ev, data)
{
PROCESS_BEGIN();
#if WITH_PERIODIC_DEBUG
ctimer_set(&debug_timer, 20 * CLOCK_SECOND, periodic_debug, NULL);
#endif /* WITH_PERIODIC_DEBUG */
serial_shell_init();
shell_blink_init();
/* shell_file_init();
shell_coffee_init();*/
/* shell_download_init();
shell_rime_sendcmd_init();*/
/* shell_ps_init();*/
shell_reboot_init();
shell_rime_init();
shell_rime_netcmd_init();
/* shell_rime_ping_init();
shell_rime_debug_init();
shell_rime_debug_runicast_init();*/
/* shell_rime_sniff_init();*/
shell_sky_init();
shell_power_init();
shell_powertrace_init();
/* shell_base64_init();*/
shell_text_init();
shell_time_init();
/* shell_checkpoint_init();*/
/* shell_sendtest_init();*/
shell_rime_unicast_init();
shell_collect_view_init();
#if DEBUG_SNIFFERS
rime_sniffer_add(&s);
#endif /* DEBUG_SNIFFERS */
PROCESS_END();
}
/*---------------------------------------------------------------------------*/
As we see above some of the shell_xx_init() are already commented out, this done on the pretext that these features if supported will overflow the Tmote Sky memory and the Code will not compile.
Hence, commented out shell_xx_init() which corresponds to specific set of shell commands, will not be available in this shell
Note: However, if we require any shell_xx_init() features, we can uncomment them but we need to make sure the code compiles such that the sky mote memory doesn't overflow. serial_shell_init() invokes the serial contiki shell
Compiling and uploading the Contiki Sky Shell on sky motes
To install a shell on the a node, go to the sky-shell, connect a TMote sky to your computer and upload the shell application on the node.
cd contiki-2.7/examples/sky-shell make TARGET=sky sky-shell.upload savetarget
Wait for the compilation and uploading to finish.
To connect to the shell over the USB port, run:
You can now login to the node with the command
make login
Press the return key to get a shell prompt.
After running the Contiki Shell, you will see the Contiki Sky Shell prompt
user@instant-contiki:~/contiki-2.7/examples/sky-shell$ make login using saved target 'sky' ../../tools/sky/serialdump-linux -b115200 /dev/ttyUSB0 connecting to /dev/ttyUSB0 (115200) [OK] SEND 1 bytes 5.0: Contiki>
Punch in help on the Contiki Shell prompt to see all the available shell commands in Contiki shell.
help
You can now run any supported shell commands
For example, running echo hello will echo the <text> back on the shell prompt.
5.0: Contiki> echo hello SEND 11 bytes hello 5.0: Contiki>
Modify sky-shell project for this tutorial
Replace the sky-shell.c file with the following version of sky-shell.c file
Please copy the below source code into the sky-shell.c file
/*
* Copyright (c) 2008, Swedish Institute of Computer Science.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the Institute nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* This file is part of the Contiki operating system.
*
*/
/**
* \file
* Tmote Sky-specific Contiki shell
* \author
* Adam Dunkels <adam@sics.se>
*/
#include "contiki.h"
#include "shell.h"
#include "serial-shell.h"
#include "collect-view.h"
#include "net/rime.h"
/*---------------------------------------------------------------------------*/
PROCESS(sky_shell_process, "Sky Contiki shell");
AUTOSTART_PROCESSES(&sky_shell_process);
/*---------------------------------------------------------------------------*/
#define WITH_PERIODIC_DEBUG 0
#if WITH_PERIODIC_DEBUG
static struct ctimer debug_timer;
static void
periodic_debug(void *ptr)
{
ctimer_set(&debug_timer, 20 * CLOCK_SECOND, periodic_debug, NULL);
collect_print_stats();
}
#endif /* WITH_PERIODIC_DEBUG */
/*---------------------------------------------------------------------------*/
PROCESS_THREAD(sky_shell_process, ev, data)
{
PROCESS_BEGIN();
#if WITH_PERIODIC_DEBUG
ctimer_set(&debug_timer, 20 * CLOCK_SECOND, periodic_debug, NULL);
#endif /* WITH_PERIODIC_DEBUG */
serial_shell_init();
shell_blink_init();
shell_file_init();
shell_ps_init();
shell_reboot_init();
shell_sky_init();
shell_power_init();
shell_powertrace_init();
shell_text_init();
shell_time_init();
shell_collect_view_init();
#if DEBUG_SNIFFERS
rime_sniffer_add(&s);
#endif /* DEBUG_SNIFFERS */
PROCESS_END();
}
/*---------------------------------------------------------------------------*/
This will create a sky shell with features or shell commands that we will go through in the next section
Please compile the sky-shell project and upload the sky-shell.upload to the Sky mote and Login to the mote as explained in previous section
If successful you should be able to see the following and get a Contiki shell prompt
user@instant-contiki:~/contiki-2.7/examples/sky-shell$ make TARGET=sky sky-shell.upload savetarget AR contiki-sky.a CC sky-shell.c CC ../../platform/sky/./contiki-sky-main.c LD sky-shell.sky msp430-objcopy sky-shell.sky -O ihex sky-shell.ihex make IHEXFILE=sky-shell.ihex sky-reset sky-upload make[1]: Entering directory `/home/user/contiki-2.7/examples/sky-shell' make -k -j 20 sky-reset-sequence make[2]: Entering directory `/home/user/contiki-2.7/examples/sky-shell' ../../tools/sky/msp430-bsl-linux --telosb -c /dev/ttyUSB0 -r MSP430 Bootstrap Loader Version: 1.39-telos-7 Use -h for help Reset device ... Done make[2]: Leaving directory `/home/user/contiki-2.7/examples/sky-shell' make -j 20 sky-upload-sequence make[2]: Entering directory `/home/user/contiki-2.7/examples/sky-shell' +++++ Erasing /dev/ttyUSB0 MSP430 Bootstrap Loader Version: 1.39-telos-7 Use -h for help Mass Erase... Transmit default password ... +++++ Programming /dev/ttyUSB0 MSP430 Bootstrap Loader Version: 1.39-telos-7 Invoking BSL... Transmit default password ... Current bootstrap loader version: 1.61 (Device ID: f16c) Changing baudrate to 38400 ... Program ... 45722 bytes programmed. +++++ Resetting /dev/ttyUSB0 MSP430 Bootstrap Loader Version: 1.39-telos-7 Use -h for help Reset device ... Done make[2]: Leaving directory `/home/user/contiki-2.7/examples/sky-shell' make[1]: Leaving directory `/home/user/contiki-2.7/examples/sky-shell' saving Makefile.target rm sky-shell.co obj_sky/contiki-sky-main.o sky-shell.ihex user@instant-contiki:~/contiki-2.7/examples/sky-shell$ make login using saved target 'sky' ../../tools/sky/serialdump-linux -b115200 /dev/ttyUSB0 connecting to /dev/ttyUSB0 (115200) [OK] +�{/#+�3++�&� SEND 1 bytes 5.0: Contiki>
If you don't end up getting the above set of messages on the comand prompt and end up getting the set of messages on command prompt as shown below, then your setup has not been successful
user@instant-contiki:~/contiki-2.7/examples/sky-shell$ make TARGET=sky sky-shell.upload savetarget msp430-objcopy sky-shell.sky -O ihex sky-shell.ihex make IHEXFILE=sky-shell.ihex sky-reset sky-upload make[1]: Entering directory `/home/user/contiki-2.7/examples/sky-shell' make -k -j 20 sky-reset-sequence make[2]: Entering directory `/home/user/contiki-2.7/examples/sky-shell' Done make[2]: Leaving directory `/home/user/contiki-2.7/examples/sky-shell' make -j 20 sky-upload-sequence make[2]: Entering directory `/home/user/contiki-2.7/examples/sky-shell' Done make[2]: Leaving directory `/home/user/contiki-2.7/examples/sky-shell' make[1]: Leaving directory `/home/user/contiki-2.7/examples/sky-shell' saving Makefile.target rm sky-shell.ihex user@instant-contiki:~/contiki-2.7/examples/sky-shell$
This indicates that your Tmote Sky mote is disconnected!!!
Make sure that your Tmote Sky mote is connected and try compiling again and it should work this time :) Did you heave a sigh there?
I did! When I figured out what was going wrong.
Nevertheless, we are All Set and Good to Launch now :) :)
Contiki Shell Commands, their usage and Contiki shell programming
Ready to dive in ?
Everyone needs help at some point in time, don't we? So let's punch in help on the Contiki Sky Shell to figure out what all shell commands are at our disposal and we will take them head on, one at a time :)
You should see a list of all available shell commands as shown below:
5.0: Contiki> help SEND 5 bytes Available commands: ?: shows this help append <filename>: append to file binprint: print binary data in decimal format blink [num]: blink LEDs ([num] times) collect-view-data: sensor data, power consumption, network stats echo <text>: print <text> energy: print energy profile exit: exit shell hd: print binary data in hexadecimal format help: shows this help kill <command>: stop a specific command killall: stop all running commands ls: list files nodeid: set node ID null: discard input power: print power profile powerconv: convert power profile to human readable output powertrace [interval]: turn powertracing on or off, with reporting interval <interval> ps: list all running processes quit: exit shell randwait <maxtime> <command>: wait for a random time before running a command read <filename> [offset] [block size]: read from a file, with the offset and the block size as options reboot: reboot the system repeat <num> <time> <command>: run a command every <time> seconds rfchannel <channel>: change CC2420 radio channel (11 - 26) rm <filename>: remove the file named filename sense: print out sensor data senseconv: convert 'sense' data to human readable format size: print the size of the input time [seconds]: output time in binary format, or set time in seconds since 1970 timestamp: prepend a timestamp to data txpower <power>: change CC2420 transmission power (0 - 31) write <filename>: write to file 5.0: Contiki>
Lets, go ahead and do the simplest first! echo :)
5.0: Contiki> echo howdy contiki shell? SEND 20 bytes SEND 6 bytes howdy contiki shell? 5.0: Contiki>
Contiki Shell Commands
1) Text Related Commands:
echo <text>: print <text>
binprint: print binary data in decimal format
hd: print binary data in hexadecimal format
size: print the size of the input
shell-text.c : This file holds all the text related shell commands. You can find the source file in this path ~/contiki-2.7/apps/shell
shell_text_init(): This function is used to register text related shell commands with the Contiki Shell.
Examples
echo carpe diem!
5.0: Contiki> echo carpe diem! SEND 17 bytes carpe diem! 5.0: Contiki>
The following commands prints time data in decimal format.
2943 secs have elapsed since the mote was last power up.
time | binprint
5.0: Contiki> time | binprint SEND 16 bytes 6 49032 34883 0 65535 0 2943 5.0: Contiki>
The following command prints data in hexadecimal format.
hd
Press Cntrl+Return
hello world
Press Cntrl+Return to get the shell prompt back after output is printed.
5.0: Contiki> hd SEND 3 bytes hello world SEND 12 bytes 0x6568 0x6c6c 0x206f 0x6f77 0x6c72
The following command prints size of the data.
size
Press Cntrl+Return
trojan for life!
Press Cntrl+Return to get the shell prompt back after output is printed.
5.0: Contiki> size SEND 5 bytes trojan for life! SEND 17 bytes SEND 1 bytes 16 5.0: Contiki>
2) Time related Shell commands time [seconds]: output time in binary format, or set time in seconds since 1970. repeat <num> : run a command every time seconds. randwait <maxtime> <command>: wait for a random time before running a command. timestamp: prepend a timestamp to data. shell-time.c : This file holds all the time related shell commands. You can find the source file in this path ~/contiki-2.7/apps/shell shell_time_init(): This function is used to register time related shell commands with the Contiki Shell. Examples
Following command will set the current time on mote to 10 secs after 1970.
That it forces the time to be set to a value given.
format of print : [len] [clock] [rtimer] [timesynch] [timesynch] [authority] time[1] time[0]
time 10 | binprint
5.0: Contiki> time 10 | binprint SEND 19 bytes 6 59950 11844 0 65535 0 10 5.0: Contiki>
Following command will set the current time on mote to 66000 secs after 1970.
Since 66000 > 65536 , time[0] overflows and time[1] has the order bits
time 66000 | binprint
5.0: Contiki> time 66000 | binprint SEND 20 bytes SEND 2 bytes 6 39554 33340 0 65535 1 464 5.0: Contiki>
Following command will print time elapsed every one sec, after waiting for randomtime from the time was reset to 0 secs.
time 0 | randwait 10 {repeat 10 1 {time | binprint}}
Press Cntrl+Return to get the shell prompt back after output is printed.
5.0: Contiki> time 0 | randwait 10 {repeat 10 1 {time | binprint}} SEND 20 bytes SEND 20 bytes SEND 13 bytes 5.0: Contiki> 6 4870 1641 0 65535 0 9 6 4998 34372 0 65535 0 10 6 5126 1604 0 65535 0 11 6 5254 34372 0 65535 0 12 6 5382 1605 0 65535 0 13 6 5510 34372 0 65535 0 14 6 5638 1604 0 65535 0 15 6 5766 34372 0 65535 0 16 6 5894 1604 0 65535 0 17 6 6022 34372 0 65535 0 18
Following command will print the time elapsed every sec for 100 secs
time | repeat 20 1 {time | binprint}
Press Cntrl+Return to get the shell prompt back after output is printed.
5.0: Contiki> time | repeat 20 1 {time | binprint} SEND 20 bytes SEND 17 bytes �z�^z��� 5.0: Contiki> 6 35963 31502 0 65535 0 251 6 36090 64068 0 65535 0 252 6 36218 31300 0 65535 0 253 6 36346 64068 0 65535 0 254 6 36474 31300 0 65535 0 255 6 36602 64068 0 65535 0 256 6 36730 31300 0 65535 0 257 6 36858 64068 0 65535 0 258 6 36986 31300 0 65535 0 259 6 37114 64068 0 65535 0 260 6 37242 31300 0 65535 0 261 6 37370 64068 0 65535 0 262 6 37498 31300 0 65535 0 263 6 37626 64068 0 65535 0 264 6 37754 31300 0 65535 0 265 6 37882 64068 0 65535 0 266 6 38010 31300 0 65535 0 267 6 38138 64068 0 65535 0 268 6 38266 31300 0 65535 0 269 6 38394 64068 0 65535 0 270
prints hello 4 times every 1 time step and immediately gets back to prompt and runs the command in the background
repeat 4 1 {echo hello} &
Press Cntrl+Return to get the shell prompt back after output is printed.
5.0: Contiki> repeat 4 1 {echo hello} & SEND 20 bytes SEND 6 bytes 5.0: Contiki> hello hello hello hello SEND 1 bytes 5.0: Contiki>
3) File System Related Commands:
ls: list files
write <filename>: write to file. To write a string of character to a file.
read <filename> [offset] [block size]: read from a file, with the offset and the block size as options. The offset is the number of characters that must be skipped before reading the file, starting from 0. The block size option separates the output of the read functions in blocks of size block size.
append <filename>: append to file.
rm <filename>: remove the file named filename.
shell-file.c : This file holds all the file system related shell commands. You can find the source file in this path ~/contiki-2.7/apps/shell
shell_file_init(): This function is used to register file system related shell commands with the Contiki Shell.
Examples
Following command echos the text and is written to a file
echo howdy? | write msg.txt
echo hi! | write hi.txt
read msg.txt 0 6
ls
5.0: Contiki> echo howdy? | write msg.txt SEND 20 bytes SEND 8 bytes howdy? 5.0: Contiki> echo hi! | write hi.txt SEND 20 bytes SEND 4 bytes hi! 5.0: Contiki> read msg.txt 0 6 SEND 18 bytes howdy? 5.0: Contiki> ls SEND 3 bytes 58 6 msg.txt 3 hi.txt Total size: 67 5.0: Contiki>
The following set of commands repeats hey text 4 times every time step and appends the text into a file. Removes all the existing files later
repeat 4 1 {echo hey | append logfile.txt}
read logfile.txt 0 3
ls
rm msg.txt
rm hi.txt
rm logfile.txt
ls
5.0: Contiki> repeat 4 1 {echo hey | append logfile.txt} SEND 20 bytes SEND 20 bytes SEND 3 bytes hey hey hey hey 5.0: Contiki> read logfile.txt 0 3 SEND 20 bytes SEND 1 bytes hey hey hey hey 5.0: Contiki> ls SEND 3 bytes 58 6 msg.txt 3 hi.txt 12 logfile.txt Total size: 79 5.0: Contiki> rm msg.txt SEND 11 bytes 5.0: Contiki> rm hi.txt SEND 10 bytes 5.0: Contiki> rm logfile.txt SEND 15 bytes 5.0: Contiki> ls SEND 3 bytes 58 Total size: 58 5.0: Contiki>
4) Sky mote Related Commands: sense: print out sensor data. senseconv: convert 'sense' data to human readable format. nodeid: set node ID, If no node ID was given on the command line, we print out the current nodeid or else we burn the new node ID. txpower <power>: change CC2420 transmission power (0 - 31).If no power was given on the command line, we print out the current power. rfchannel <channel>: change CC2420 radio channel (11 - 26).If no channel was given on the command line, we print out thecurrent channel. shell-sky.c : This file holds all the sky mote related shell commands. You can find the source file in this path ~/contiki-2.7/apps/shell. shell_sky_init(): This function is used to register sky mote related shell commands with the Contiki Shell. Examples
This set of commands prints the sensor data in human readable format
sense|senseconv
5.0: Contiki> sense|senseconv SEND 16 bytes Light 1 4 Light 2 13 Temperature 615.7 Relative humidity 2650 RSSI 8 Voltage 2.5 5.0: Contiki>
Following command prints out the txpower as 5, which is being set
Print format: msglen power
txpower 5 | binprint
5.0: Contiki> txpower 5 | binprint SEND 20 bytes SEND 1 bytes 1 5 5.0: Contiki>
Following command prints out the rfchannel as 15, which is being set
Print format: msglen channelno
rfchannel 15| binprint
5.0: Contiki> rfchannel 15| binprint SEND 20 bytes SEND 3 bytes 1 15 5.0: Contiki>
Following command outputs the sensor measurements every time step 4 times and appends to a file
repeat 4 1 {sense | senseconv | append logfile.txt}
read logfile.txt 0 36
5.0: Contiki> repeat 4 1 {sense | senseconv | append logfile.txt} SEND 20 bytes SEND 20 bytes SEND 12 bytes Light 1 5 Light 2 13 Temperature 615.7 Relative humidity 2650 RSSI 11 Voltage 2.5 Light 1 4 Light 2 13 Temperature 615.7 Relative humidity 2650 RSSI 8 Voltage 2.5 Light 1 4 Light 2 13 Temperature 615.7 Relative humidity 2650 RSSI 2 Voltage 2.5 Light 1 4 Light 2 13 Temperature 615.7 Relative humidity 2650 RSSI 0 Voltage 2.5 5.0: Contiki> read logfile.txt 0 36 SEND 20 bytes SEND 2 bytes Light 1 5Light 2 13Temperature 615.7 Relative humidity 2650RSSI 11Voltage 2.5Light 1 4Light 2 13Temperature 6 15.7Relative humidity 2650RSSI 8Volt age 2.5Light 1 4Light 2 13Temperatur e 615.7Relative humidity 2650RSSI 2V oltage 2.5Light 1 4Light 2 13Tempera ture 615.7Relative humidity 2650RSSI 0Voltage 2.5 5.0: Contiki>
set nodeid
nodeid 5
5.0: Contiki> nodeid 5 SEND 9 bytes Node ID: 5 5.0: Contiki>
5) LED Related Commands:
blink [num]: blink LEDs ([num] times)
shell-blink.c : This file holds all the LED blink related shell commands. You can find the source file in this path ~/contiki-2.7/apps/shell
shell_blink_init(): This function is used to register LED blink related shell commands with the Contiki Shell.
Examples
LED's blink 5 times
blink 5
5.0: Contiki> blink 5 SEND 8 bytes 5.0: Contiki>
6) Power Related Commands:
power: print power profile
energy: print energy profile
powerconv: convert power profile to human readable output
shell-power.c : This file holds all the power related shell commands. You can find the source file in this path ~/contiki-2.7/apps/shell
shell_power_init(): This function is used to register power related shell commands with the Contiki Shell.
Examples
Following power commands prints the power profile of sensor node in human readable format
power | powerconv
5.0: Contiki> power | powerconv SEND 18 bytes CPU 3% LPM 8% tx 0% rx 0% idle tx 0% idle rx 0% tot 719 uW 5.0: Contiki>
Experiment
echo change power to 0 | append powfile.log
txpower 0 | binprint | power | powerconv | append powfile.log
repeat 2 1 {power | powerconv} | append powfile.log
echo change power to 3 | append powfile.log
txpower 3 | binprint | power | powerconv | append powfile.log
repeat 2 1 {power | powerconv} | append powfile.log
echo change power to 10 | append powfile.log
txpower 10| binprint | power | powerconv | append powfile.log
repeat 2 1 {power | powerconv} | append powfile.log
echo change power to 31 | append powfile.log
txpower 31| binprint | power | powerconv | append powfile.log
repeat 2 1 {power | powerconv} | append powfile.log
read powfile.log 0 40
5.0: Contiki> echo change power to 0 | append powfile.log SEND 20 bytes SEND 20 bytes SEND 4 bytes change power to 0 5.0: Contiki> txpower 0 | binprint | power | powerconv | append powfile.log SEND 20 bytes SEND 20 bytes SEND 20 bytes SEND 2 bytes CPU 3% LPM 96% tx 0% rx 0% idle tx 0% idle rx 0% tot 2043 uW 5.0: Contiki> repeat 2 1 {power | powerconv} | append powfile.log SEND 20 bytes SEND 20 bytes SEND 12 bytes CPU 3% LPM 96% tx 0% rx 0% idle tx 0% idle rx 0% tot 2073 uW 5.0: Contiki> repeat 2 1 {power | powerconv} | append powfile.log SEND 20 bytes SEND 20 bytes SEND 12 bytes CPU 3% LPM 96% tx 0% rx 0% idle tx 0% idle rx 0% tot 2046 uW 5.0: Contiki> txpower 3 | binprint | power | powerconv | append powfile.log SEND 20 bytes SEND 20 bytes SEND 20 bytes SEND 2 bytes CPU 3% LPM 96% tx 0% rx 0% idle tx 0% idle rx 0% tot 2150 uW 5.0: Contiki> repeat 2 1 {power | powerconv} | append powfile.log SEND 20 bytes SEND 20 bytes SEND 12 bytes CPU 3% LPM 96% tx 0% rx 0% idle tx 0% idle rx 0% tot 2099 uW 5.0: Contiki> echo change power to 10 | append powfile.log SEND 20 bytes SEND 20 bytes SEND 5 bytes change power to 10 5.0: Contiki> txpower 10| binprint | power | powerconv | append powfile.log SEND 20 bytes SEND 20 bytes SEND 20 bytes SEND 2 bytes CPU 3% LPM 96% tx 0% rx 0% idle tx 0% idle rx 0% tot 2148 uW 5.0: Contiki> repeat 2 1 {power | powerconv} | append powfile.log SEND 20 bytes SEND 20 bytes SEND 12 bytes CPU 3% LPM 96% tx 0% rx 0% idle tx 0% idle rx 0% tot 2099 uW 5.0: Contiki> echo change power to 31 | append powfile.log SEND 20 bytes SEND 20 bytes SEND 5 bytes change power to 31 5.0: Contiki> txpower 31| binprint | power | powerconv | append powfile.log SEND 20 bytes SEND 20 bytes SEND 20 bytes SEND 2 bytes CPU 3% LPM 96% tx 0% rx 0% idle tx 0% idle rx 0% tot 2071 uW 5.0: Contiki> repeat 2 1 {power | powerconv} | append powfile.log SEND 20 bytes SEND 20 bytes SEND 12 bytes CPU 3% LPM 96% tx 0% rx 0% idle tx 0% idle rx 0% tot 2086 uW 5.0: Contiki> read powfile.log 0 40 SEND 20 bytes SEND 2 bytes change power to 0CPU 3% LPM 96% tx 0% rx 0% idle tx 0% idle rx 0% tot 2043 uWCPU 3% LPM 96% tx 0% rx 0% idle tx 0% idle rx 0% tot 2073 uWCPU 3% LPM 96% tx 0% rx 0% idle tx 0% idle rx 0% tot 2046 uWCPU 3% LPM 96% tx 0% rx 0% idle tx 0% idle rx 0% tot 2150 uWCPU 3% LPM 96% tx 0% rx 0% idle tx 0% idle rx 0% tot 2099 uWcha nge power to 10CPU 3% LPM 96% tx 0% rx 0 % idle tx 0% idle rx 0% tot 2148 uWCPU 3 % LPM 96% tx 0% rx 0% idle tx 0% idle rx 0% tot 2099 uWchange power to 31CPU 3% LPM 96% tx 0% rx 0% idle tx 0% idle rx 0 % tot 2071 uWCPU 3% LPM 96% tx 0% rx 0% idle tx 0% idle rx 0% tot 2086 uW 5.0: Contiki>
Other Commands
1)ps: this command will list all running processes. By default, the following processes are executed: Shell server, Shell, Contiki serial shell, Serial driver, CC2420 driver, ctimer process, Sensors, Event timer.
2)kill <command>: stop a specific command.
3)killall: stop all running commands.
4)reboot: this command reboots the mote.
5)collect-view-data: provides sensor data, power consumption, network stats for the sky mote.
6)powertrace: this gives the power profile of the mote.
Examples
This command lists all the running processes
ps
5.0: Contiki> ps SEND 3 bytes Processes: ps Shell server Shell Contiki serial shell Serial driver CC2420 driver Sensors Ctimer process Event timer 5.0: Contiki>
This command reboots the shell
time|binprint
reboot
After rebooting print time, to see the restart of the mote time
time|binprint
5.0: Contiki> time | binprint SEND 16 bytes 6 34454 38458 0 65535 0 269 5.0: Contiki> reboot SEND 7 bytes Rebooting the node in four seconds... Rime started with address 5.0 MAC 05:00:00:00:00:00:00:00 Contiki 2.7 started. Node id is set to 5. CSMA ContikiMAC, channel check rate 8 Hz, radio channel 26 Starting 'Sky Contiki shell' 5.0: Contiki> time | binprint SEND 16 bytes 6 2736 45115 0 65535 0 21 5.0: Contiki>
This command prints out the collect view data for the mote
Format of the print: [len][clock][timesynch_time][cpu][lpm][transmit][listen][parent][parent_etx][current_rtmetric][num_neighbors][beacon_interval][BATTERY_VOLTAGE_SENSOR][BATTERY_INDICATOR][LIGHT1_SENSOR][LIGHT2_SENSOR][TEMP_SENSOR][HUMIDITY_SENSOR][RSSI_SENSOR][ETX1_SENSOR][ETX2_SENSOR][ETX3_SENSOR][ETX4_SENSOR]
collect-view-data | binprint
5.0: Contiki> collect-view-data | binprint SEND 20 bytes SEND 9 bytes 22 26024 0 1672 50278 0 269 0 128 0 6 0 2075 1 6 3 65535 65535 0 0 0 0 5.0: Contiki>
This commands prints the power profile of the mote.
powertrace
5.0: Contiki> powertrace SEND 11 bytes 16188 P 5.0 0 130619 4009338 0 23333 0 23333 130619 4009338 0 23333 0 23333 (radio 0.56% / 0.56% tx 0.00% / 0.00% listen 0.56% / 0.56%) 5.0: Contiki>
How to write your own Shell commands
Step1: Write a shell-your-cmdfile.c and shell-your-cmdfile.h files and place them in ~/contiki-2.7/apps/shell
Step2: Add the shell-your-cmdfile.c for compilation in the shell_src var of Makefile.shell located in ~/contiki-2.7/apps/shell
Shell.c has all the basic code blocks to build your own shell command.
Example skeleton of code in shell-your-cmdfile.c to create your own shell command
PROCESS(multiplyNums_process, "Multiply two numbers");
SHELL_COMMAND(mulNum_command, "mn", "mn: Multiply two numbers", &multiplyNums_process);
/* --------------------------------- */
PROCESS_THREAD(multiplyNums_process, ev, data) {
PROCESS_BEGIN();
// ...
// ...
PROCESS_END();
}
.....
.....
shell_register_command(&mulNum_command);
An example project for creating your own Shell with your commands
Place the following two files in this path ~/contiki-2.7/apps/shell
1)shell-try-example.c
2)shell-try-example.h
Source code: shell-try-example.c
Two new shell commands are created
echo2: This shell command echoes the text twice back onto the shell prompt
blinkAllOnce: This shell command is used to make each of the LED's (Red,Blu,Green) blink once each.
Once the shell commands are created they are registered with the Contiki Shell using shell_try_init()
/**
* \file shell-try-example.c
* try Example Contiki shell command
* \author
* Abhilash Hegde <hegdea@usc.edu>
*/
#include "contiki.h"
#include "shell.h"
#include <ctype.h>
#include <stdio.h>
#include <string.h>
#include "dev/leds.h"
/*---------------------------------------------------------------------------*/
PROCESS(shell_echo2_process, "echo2");
SHELL_COMMAND(echo2_command,
"echo2",
"echo2: echoes back entered text twice",
&shell_echo2_process);
/*---------------------------------------------------------------------------*/
PROCESS_THREAD(shell_echo2_process, ev, data)
{
PROCESS_BEGIN();
/* Echo the data back twice */
shell_output(&echo2_command, data, (int)strlen(data), data, (int)strlen(data));
PROCESS_END();
}
/*---------------------------------------------------------------------------*/
PROCESS(shell_blinkAllOnce_process, "blinkAllOnce");
SHELL_COMMAND(blinkAllOnce_command,
"blinkAllOnce",
"blinkAllOnce: blink all LEDs one time",
&shell_blinkAllOnce_process);
/*---------------------------------------------------------------------------*/
PROCESS_THREAD(shell_blinkAllOnce_process, ev, data)
{
static struct etimer etimer;
PROCESS_EXITHANDLER(leds_off(LEDS_ALL);)
PROCESS_BEGIN();
etimer_set(&etimer, CLOCK_SECOND/2);
PROCESS_WAIT_UNTIL(etimer_expired(&etimer));
leds_on(LEDS_RED);
etimer_reset(&etimer);
PROCESS_WAIT_UNTIL(etimer_expired(&etimer));
leds_off(LEDS_RED);
etimer_reset(&etimer);
PROCESS_WAIT_UNTIL(etimer_expired(&etimer));
leds_on(LEDS_GREEN);
etimer_reset(&etimer);
PROCESS_WAIT_UNTIL(etimer_expired(&etimer));
leds_off(LEDS_GREEN);
etimer_reset(&etimer);
PROCESS_WAIT_UNTIL(etimer_expired(&etimer));
leds_on(LEDS_BLUE);
etimer_reset(&etimer);
PROCESS_WAIT_UNTIL(etimer_expired(&etimer));
leds_off(LEDS_BLUE);
PROCESS_END();
}
/*---------------------------------------------------------------------------*/
void
shell_try_init(void)
{
shell_register_command(&echo2_command);
shell_register_command(&blinkAllOnce_command);
}
/*---------------------------------------------------------------------------*/
Header: shell-try-example.h
/**
* \file shell-try-example.h
* try Example Contiki shell command
* \author
* Abhilash Hegde <hegdea@usc.edu>
*/
#ifndef __SHELL_TRY_H__
#define __SHELL_TRY_H__
#include "shell.h"
void shell_try_init(void);
#endif /* __SHELL_TRY_H__ */
Make the changes in Makefile.shell located in ~/contiki-2.7/apps/shell to include shell-try-example.c in shell_src
shell_src = shell.c shell-reboot.c \ shell-vars.c shell-ps.c shell-rime.c shell-sendtest.c \ shell-blink.c shell-text.c shell-try-example.c shell-time.c \ shell-file.c shell-netfile.c shell-run.c \ shell-rime-ping.c shell-rime-sniff.c shell-rime-netcmd.c \ shell-rime-debug.c shell-rime-debug-runicast.c shell-coffee.c \ shell-wget.c shell-httpd.c shell-irc.c \ shell-checkpoint.c shell-power.c \ shell-tcpsend.c shell-udpsend.c shell-ping.c shell-netstat.c \ shell-rime-sendcmd.c shell-download.c shell-rime-neighbors.c \ shell-rime-unicast.c \ shell-base64.c \ shell-netperf.c shell-memdebug.c \ shell-powertrace.c shell-collect-view.c shell-crc.c
Now create a new project mySky-shell under ~/contiki-2.7/examples
Add this source file mySky-shell.c in the project
/*
* Copyright (c) 2008, Swedish Institute of Computer Science.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the Institute nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* This file is part of the Contiki operating system.
*
*/
/**
* \file
* Tmote Sky-specific My Contiki shell
* Base code reference: sky-shell.c
* \author
* Abhilash Hegde <hegdea@usc.edu>
*/
#include "contiki.h"
#include "shell.h"
#include "serial-shell.h"
#include "collect-view.h"
//#include "net/rime.h"
/*---------------------------------------------------------------------------*/
PROCESS(mysky_shell_process, "My Sky Contiki shell");
AUTOSTART_PROCESSES(&mysky_shell_process);
/*---------------------------------------------------------------------------*/
#define WITH_PERIODIC_DEBUG 0
#if WITH_PERIODIC_DEBUG
static struct ctimer debug_timer;
static void
periodic_debug(void *ptr)
{
ctimer_set(&debug_timer, 20 * CLOCK_SECOND, periodic_debug, NULL);
collect_print_stats();
}
#endif /* WITH_PERIODIC_DEBUG */
/*---------------------------------------------------------------------------*/
PROCESS_THREAD(mysky_shell_process, ev, data)
{
PROCESS_BEGIN();
#if WITH_PERIODIC_DEBUG
ctimer_set(&debug_timer, 20 * CLOCK_SECOND, periodic_debug, NULL);
#endif /* WITH_PERIODIC_DEBUG */
serial_shell_init();
shell_blink_init();
shell_file_init();
shell_ps_init();
shell_reboot_init();
shell_sky_init();
shell_power_init();
shell_powertrace_init();
shell_text_init();
shell_time_init();
shell_collect_view_init();
shell_try_init();
PROCESS_END();
}
/*---------------------------------------------------------------------------*/
Add this Makefile in the project
CONTIKI_PROJECT = mySky-shell
all: $(CONTIKI_PROJECT)
APPS = serial-shell powertrace collect-view
CONTIKI = ../..
include $(CONTIKI)/Makefile.include
-include /home/user/nes/testbed-scripts/Makefile.include
Add another Makefile.target to the project
TARGET = sky
compile the project
user@instant-contiki:~/contiki-2.7/examples/mySky-shell$ make TARGET=sky mySky-shell.upload savetarget
After successful compilation, you will see the following messages on the command prompt
user@instant-contiki:~/contiki-2.7/examples/mySky-shell$ make TARGET=sky mySky-shell.upload savetarget msp430-objcopy mySky-shell.sky -O ihex mySky-shell.ihex make IHEXFILE=mySky-shell.ihex sky-reset sky-upload make[1]: Entering directory `/home/user/contiki-2.7/examples/mySky-shell' make -k -j 20 sky-reset-sequence make[2]: Entering directory `/home/user/contiki-2.7/examples/mySky-shell' ../../tools/sky/msp430-bsl-linux --telosb -c /dev/ttyUSB0 -r MSP430 Bootstrap Loader Version: 1.39-telos-7 Use -h for help Reset device ... Done make[2]: Leaving directory `/home/user/contiki-2.7/examples/mySky-shell' make -j 20 sky-upload-sequence make[2]: Entering directory `/home/user/contiki-2.7/examples/mySky-shell' +++++ Erasing /dev/ttyUSB0 MSP430 Bootstrap Loader Version: 1.39-telos-7 Use -h for help Mass Erase... Transmit default password ... +++++ Programming /dev/ttyUSB0 MSP430 Bootstrap Loader Version: 1.39-telos-7 Invoking BSL... Transmit default password ... Current bootstrap loader version: 1.61 (Device ID: f16c) Changing baudrate to 38400 ... Program ... 46214 bytes programmed. +++++ Resetting /dev/ttyUSB0 MSP430 Bootstrap Loader Version: 1.39-telos-7 Use -h for help Reset device ... Done make[2]: Leaving directory `/home/user/contiki-2.7/examples/mySky-shell' make[1]: Leaving directory `/home/user/contiki-2.7/examples/mySky-shell' saving Makefile.target rm mySky-shell.ihex user@instant-contiki:~/contiki-2.7/examples/mySky-shell$
Login in to the mote using the following command
make login
After logging into the mote, you will see the shell prompt as seen below
user@instant-contiki:~/contiki-2.7/examples/mySky-shell$ make login using saved target 'sky' ../../tools/sky/serialdump-linux -b115200 /dev/ttyUSB0 connecting to /dev/ttyUSB0 (115200) [OK] ��{/#��3�#���#
Hit Enter, too see shell prompt and Type help on the shell prompt. You will see the two new commands, echo2 and blinkAllonce
5.0: Contiki> help SEND 5 bytes Available commands: ?: shows this help append <filename>: append to file binprint: print binary data in decimal format blink [num]: blink LEDs ([num] times) blinkAllOnce: blink all LEDs one time collect-view-data: sensor data, power consumption, network stats echo <text>: print <text> echo2: echoes back entered text twice energy: print energy profile exit: exit shell hd: print binary data in hexadecimal format help: shows this help kill <command>: stop a specific command killall: stop all running commands ls: list files nodeid: set node ID null: discard input power: print power profile powerconv: convert power profile to human readable output powertrace [interval]: turn powertracing on or off, with reporting interval <interval> ps: list all running processes quit: exit shell randwait <maxtime> <command>: wait for a random time before running a command read <filename> [offset] [block size]: read from a file, with the offset and the block size as options reboot: reboot the system repeat <num> <time> <command>: run a command every <time> seconds rfchannel <channel>: change CC2420 radio channel (11 - 26) rm <filename>: remove the file named filename sense: print out sensor data senseconv: convert 'sense' data to human readable format size: print the size of the input time [seconds]: output time in binary format, or set time in seconds since 1970 timestamp: prepend a timestamp to data txpower <power>: change CC2420 transmission power (0 - 31) write <filename>: write to file 5.0: Contiki>
Lets execute the two new shell commands added to the shell
5.0: Contiki> echo2 Trojan SEND 13 bytes TrojanTrojan 5.0: Contiki>
blinkAllOnce command will blink all LED's exactly once one by one
5.0: Contiki> blinkAllOnce SEND 13 bytes 5.0: Contiki>
Voila! There it is blinking of LEDs, Notice it ?
Issues you might face
You may run out of memory on the mote, when more than possible shell_xx_init()'s are used in your firmware image of Contiki shell. This will result in compilation failure. By Trial and error figure what would be a reasonable set of shell_xx_init()'s that can be registered as pert of your Contiki Shell.
During the run of contiki serial shell, due to invalid data or commands, garbage values might be displayed in the shell. In such cases either use the reboot command to reboot the serial shell for Contiki or Build the shell again and upload it on the mote to solve the issue
References
http://www.bithappens.se/blog/2012/07/01/contiki-debugging-in-cooja/ http://ai.vub.ac.be/Robotics/wiki/index.php/Some_cool_demos http://www.nairaland.com/1167922/step-by-step-method-writing-contiki-programs http://contiki.sourceforge.net/docs/2.6/a01796.html
Feedback
Please post your feedback here for any further improvements to tutorials.
Comments Welcome :)
Hope you had a great time learning.