We use the existing program sample from Contiki or Contiki-NG, take the Hello World program as example, which is located in the <contiki_folder>/examples/hello-world folder. The Hello World program displays simple words in the WSN mote’s Terminal.
1
Hello, world
This program consists of hello-world.c and Makefile files. After the program is compiled, we could see several files, such as *.obj and *.hex files.
To write a C program, we have to chose a favorite terminal editor, such as vim or nano. Also, use a visual editor, for instance, Eclipse, Sublime Text, and Visual Studio Code.
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""" " 显示相关 """""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""" setnu" 显示行号 syntaxon" 语法高亮 set showcmd " 输入的命令显示出来 set novisualbell " 不要闪烁 set statusline=%F%m%r%h%w\ [FORMAT=%{&ff}]\ [TYPE=%Y]\ [POS=%l,%v][%p%%] " 状态行显示的内容 set laststatus=2" 启动显示状态行(1),总是显示状态行(2) set foldenable " 允许折叠 set nocompatible " 去掉讨厌的有关vi一致性模式 " 显示中文帮助 ifversion >= 603 set helplang=cn set encoding=utf-8 endif " 设置配色方案 colorscheme yowish set fencs=utf-8,ucs-bom,shift-jis,gb18030,gbk,gb2312,cp936 set termencoding=utf-8 set encoding=utf-8 set fileencodings=ucs-bom,utf-8,cp936 set fileencoding=utf-8
" 代码补全 set completeopt=preview,menu " 允许插件 filetype plugin on
" 共享剪贴板 set clipboard+=unnamed " 从不备份 set nobackup
" make 运行 :set makeprg=g++\ -Wall\ \ % " 自动保存 set autowrite set cursorline " 突出显示当前行 set magic " 设置魔术 set foldcolumn=0 set foldmethod=indent setfoldlevel=3
" 不要使用vi的键盘模式,而是vim自己的 set nocompatible " 在处理未保存或只读文件的时候,弹出确认 setconfirm
" 自动缩进 set autoindent setcindent
" Tab键的宽度 set tabstop=4
" 统一缩进为 4 set softtabstop=4 setshiftwidth=4 " 不要用空格代替制表符 set noexpandtab
" 在行和段开始处使用制表符 set smarttab " 显示行号 setnumber
" 历史记录数 sethistory=100 " 禁止生成临时文件 set nobackup set noswapfile
" 搜索忽略大小写 set ignorecase " 搜索逐字符高亮 set hlsearch set incsearch
" 行内替换 set gdefault " 编码设置 set enc=utf-8 set fencs=utf-8,ucs-bom,shift-jis,gb18030,gbk,gb2312,cp936
" 语言设置 set langmenu=zh_CN.UTF-8 set helplang=cn set cmdheight=2 " 侦测文件类型 filetypeon
" 载入文件类型插件 filetype plugin on " 为特定文件类型载入相关缩进文件 filetypeindenton
" 保存全局变量 set viminfo+=! " 带有如下符号的单词不要被换行分割 set iskeyword+=_,$,@,%,#,-
" 字符间插入的像素行数目 set linespace=0 " 增强模式中的命令行自动完成操作 set wildmenu
" 使回格键(backspace)正常处理indent, eol, start等 set backspace=2 " 允许backspace和光标键跨越行边界 set whichwrap+=<,>,h,l
" 可以在buffer的任何地方使用鼠标(类似office中在工作区双击鼠标定位) set mouse=a set selection=exclusive set selectmode=mouse,key " 通过使用: commands命令,告诉我们文件的哪一行被改变过 set report=0
" 在被分割的窗口间显示空白,便于阅读 set fillchars=vert:\ ,stl:\ ,stlnc:\ " 高亮显示匹配的括号 set showmatch
" 匹配括号高亮的时间(单位是十分之一秒) set matchtime=20 " 光标移动到buffer的顶部和底部时保持3行距离 set scrolloff=3
" 为 c 程序提供自动缩进 set smartindent " 高亮显示普通txt文件(需要txt.vim脚本) au BufRead,BufNewFile * setfiletype txt
" 打开文件类型检测, 加了这句才可以用智能补全 set completeopt=longest,menu """"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""" " CTags的设定 """""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
let Tlist_Sort_Type = "name"" 按照名称排序 let Tlist_Use_Right_Window = 1" 在右侧显示窗口 let Tlist_Compart_Format = 1" 压缩方式 let Tlist_Exist_OnlyWindow = 1" 如果只有一个buffer,kill窗口也kill掉buffer let Tlist_File_Fold_Auto_Close = 0" 不要关闭其他文件的tags let Tlist_Enable_Fold_Column = 0" 不要显示折叠树 autocmd FileType java settags+=D:\tools\java\tags autocmd FileType h,cpp,cc,csettags+=D:\tools\cpp\tags let Tlist_Show_One_File=1" 不同时显示多个文件的tag,只显示当前文件的 " 设置tags settags=tags
" 默认打开Taglist let Tlist_Auto_Open=1
"""""""""""""""""""""""""""""""" " Tag list (ctags) """""""""""""""""""""""""""""""" let Tlist_Ctags_Cmd = '/usr/bin/ctags' let Tlist_Show_One_File = 1" 不同时显示多个文件的tag,只显示当前文件的 let Tlist_Exit_OnlyWindow = 1" 如果taglist窗口是最后一个窗口,则退出vim let Tlist_Use_Right_Window = 1" 在右侧窗口中显示taglist窗口
To start to write a Contiki program, we create a folder called hello-world. Then, we create a hello-world.c within the hello-world folder. Also, we can run a program sample, hello-world, from <contiki_folder>/examples/hello-world folder. Navigate to Terminal and then build that program. If we want to build a project from scratch, we must continue our development. The following is the complete code for the hello-world.c file.
1 2 3 4 5 6 7 8 9 10 11 12
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(); }
After that, we should make a Makefile file. The following is the content of the Makefile file:
1 2 3 4
CONTIKI_PROJECT = hello-world all: $(CONTIKI_PROJECT) CONTIKI = /home/user/Documents/book/contiki include $(CONTIKI)/Makefile.include
For Contiki-NG, three items are required in our Makefile file, which are described as follows:
CONTIKI_PROJECT is used to declare our project name.
CONTIKI is a Contiki root directory where Contiki libraries are.
We include $(CONTIKI)/Makefile.include file in our program.
Change CONTIKI for our own Contiki root folder where the Contiki source code files, Contiki-OS or Contiki-NG, are located.
If all required program setup is done, we can compile C program using make by passing in the WSN platform. For our demo, we use the native app as the target, so we pass native while compiling:
1
make TARGET=native
This compiling generates a <project_name>.native binary file. To run this Contiki app, we can type this command:
1
./hello-world.native
If we get an error due to security issues, we probably need to run this program with administrator privilege. We can type this command:
1
sudo ./hello-world.native
It shows its IPv6 address and displays Hello, world in Terminal. To stop Contiki program, we can press CTRL+C.
After the successful testing, next step, we deploy the Contiki application to the Contiki hardware. For instance, we decided to use TelosB as our WSN mote target. TelosB is a Sky platform from Contiki because TelosB uses MSP430 MCU.
Before deploying the Contiki program to the Contiki mote, make sure our Contiki mote is already successfully attached to our computer.
For instance, the TelosB is detected as /dev/ttyUSB0. we can verify the attached Contiki mote using the following command:
1
ls /dev/ttyUSB*
Now, we can compile and save the target platform by this command:
1 2
make TARGET=sky make TARGET=sky savetarget
This compiling command will generate a <project_name>.<platform> file. In our case, this will generate a hello-world.sky.ihex file:
1
make hello-world.upload
If we get a permission error, we can run it using the administrator level:
1
sudo make hello-world.upload
This program will be flashed to Contiki hardware. It may take several minutes. For Contiki-NG motes from Texas Instruments, we will probably get errors while uploading a program to the board. We have to use SmartRF Flash Programmer. We can download it from TI Software. Unfortunately, this tool currently only runs on Windows platforms(cited in 2017, fail to check in 2021 because I have no TI devices to test it.).
To see the program output from the Contiki hardware, we listen to incoming messages from the serial port of the Contiki hardware by typing this command:
1
sudo make login
Also, we can specify a serial port of the Contiki hardware (if we have connected more than one Contiki hardware). For instance, Contiki hardware on serial port /dev/ttyUSB0. We can type this command:
1
sudo make MOTES=/dev/ttyUSB0 login
Tips - We may not get any message from the hello-world program. Since this program runs the first process, we may miss this process. Try to reset the Contiki hardware so Contiki will reboot. Then, run the program.