Tag Archives: tutorial

How to configure XBee Pro Series 2.

I have two XBee Pro Series2 shipped with “XBP24-B” with ZNet 2.5 protocol.

Previously, XBEE Pro Series1 was set up Zigbee protocol but Series 2 doesn’t.

What makes differences is configuration procedures vary to each module. I want to setup XBee as “Point-to-Point” setting and there are a lot of well-documented tutorials on the Internet which are not quite useful for ZNet 2.5 Protocol.

Fortunately, you could change protocol from ZNet 2.5 to Zigbee using X-CTU, a diagnosis tool that allows you to change mode. Following “XBeeOEM Modules Section” is able to re-configure your XBee modules.

Upgrading fromZNet 2.5 to ZB

Afterward, we are able to apply the following tutorial to our XBee pro series 2.

Please note that whenever you try to configure a XBee using X-CTU, make sure a baudrate is set to 9600bps(Default). If it sets as other else, you might be able to write but it fails at “Putting a XBee into command mode”. In addition, other parameters are forced to be configured at default values so that you need to set up again.

Make sure you saw all writing procedures are properly finished.

Make sure you saw all writing procedures are properly finished.

The following Source from : http://tutorial.cytron.com.my/2012/03/08/xbee-series-2-point-to-point-communication

XBee is very easy and popular wireless device. It is a transceiver, it can transmit data wirelessly and it can also receive data wirelessly. There are several types of XBee module and it might be confusing. The very popular XBee is Series 1 (802.15.4), comes with the firmware to create connection for point to point or star network. But bear in mind, many people actually thought it is using ZigBee protocol, but it is not compliance to ZigBee because it uses the low layer of ZigBee protocol only. Therefore XBee Series 1 (S1) cannot communicate with ZigBee device in the market. Anyway I don think many care because they just want to communicate among XBee, or wanted to have simple wireless communication. The XBee or XBee PRO is basically the same protocol, just PRO module have better transmit power and better receiver sensitivity. So I will be talking about XBee only, not the PRO.

 XBee Series 2 (ZB) does not offer any 802.15.4-only firmware; it is always running ZigBee mesh firmware. It is the new XBee module that we are carrying now. XBee S2 have better performance when you talk about mesh networking where it involve quite a lot of nodes: Coordinators, Routers and End Devices. You can read the datasheet if you want to, but I am going to talk about point to point only :) Bear in mind, XBee S2 CANNOT communicate with XBee S1, it is not compatible in term of wireless communication.

DSCF6313Left is XBee Series 1, Right is XBee Series 2

Point to point two way communication

The major question, can XBee S2 perform point to point communication?

OK, you want to do simple remote control between your control panel, computer or microcontroller to your robot. Besides sending control command to it, you also want to have feedback such as light, temperature, battery voltage, etc. Can I do that with XBee S2? Those that familiar with XBee S1, I am sure you know you will need to configure the Destination Address and Source Address correctly in order to do this right? For XBee S2, it is similar procedure, just you need to make one of the XBee S2 as Coordinator, another as Router.

So get yourself XBee S2, you will need to have two units in order to get communication up. As mentioned earlier, XBee S1 and S2 cannot communicate, so you need to have a pair of XBee S2. Do not mix them up.



If you order from Cytron Technologies, the price is similar with XBee S1 (at the time I write this), and it comes with sponge and BB-XBee, the breakout board for XBee. How to use the breakout? Refer to this tutorial. Now, I am not going to use the breakout board because XBee module is 3.3V and you will need to hook up to computer for communication in order to configure it. I will use theSKXBee without module.

  XBee Starter Kit without Module

Plug the XBee S2 module on to SKXBee socket. Mind the polarity. Plug the USB cable to it and computer. You will need to have X-CTU software, I am using version, you can get it from here.


If this is the first time you use SKXBee, you will need to install the driver. Please download the driver from here and refer to themethod to install it.

Assuming you have done installing USB driver and X-CTU, and the USB of SKXBee is plug in properly, you should see the POWER LED on SKXBee ON. There are two XBee S2 modules needed to be setup. I will show the 1st XBee S2 module setup, which I named it Coordinator 1. 2nd XBee S2 module will be Router 1.


You can choose either one of the XBee S2 to configure as coordinator. At the end, the host do not know which is which as XBee will become transparent once it is setup correctly.

Launch X-CTU

X-CTU choose comChoose the COM Port, normally is the largest number. Easily you will see it because it is label as USB Serial Port (COM X). In this example is COM10. Your COM port might be different than mine, just choose those that being label as USB Serial port and click “Test/Query”.

X-CTU testIf the baudrate and COM port number is correct, the X-CTU will display simple information such as modem type, firmware version and serial number. If there is no response, choose the other COM port and ensure the baudrate is 9600 (default).

Coordinator SH edited

Proceed to the right tab on X-CTU, Modem Configuration. Click “Always Update Firmware” and click “Read” to grab data from XBee S2 module.


ZIGBEE COORDINATOR AT” under Function Set,

Set a preferable PAN ID, I simply set “1234”.

You can also set the Node ID, I set “COORDINATOR1” as node ID, this is not critical.

Record the SH and SL ID on this XBee S2 module, you will need it to set Router module. In my case, the SH is 13A200, SL is408C1470. This is Coordinator Source Address.

Click “Write” and X-CTU will start loading the parameter you just set.

Load Firmware CoordinatorIt will take around 30 seconds to 1 minute to complete the loading.

Coordinator Done program

We will need to configure Coordinator again when you get the SH and SL of router :) . For the time being, we are done for Coordinator. You can close X-CTU now.


If you have another SKXBee, connect it to your computer with another XBee S2 plug in. I will be using the same SKXBee, so I actually exchange the XBee 2S on it.

Launch X-CTU

Choose the COM Port, should be different number than the earlier SKXBee. Your COM port might be different than mine, just choose those that being label as USB Serial port and click “Test/Query”.

Router Test

If the baudrate and COM port number is correct, the X-CTU will display simple information such as modem type, firmware version and serial number. If there is no response, choose the other COM port and ensure the baudrate is 9600 (default).

Proceed to the right tab on X-CTU, Modem Configuration. Click “Always Update Firmware” and click “Read” to grab data from XBee S2 module.

Router Set DH DL edited


ZIGBEE ROUTER AT” under Function Set,

Set a preferable PAN ID, I simply set “1234”.

You can also set the Node ID, I set “ROUTER1” as node ID. This is not critical.

Record the SH and SL ID on this XBee S2 module, you will need it to set Coordinator module. In my case, the SH is 13A200, SL is4086A429.

Key in the DH and DL address using the SH and SL from Coordinator module. My case is DH: 13A200, DL: 408C1470.

Click “Write” and X-CTU will start loading the parameter you just set.

It will take around 30 seconds to 1 minute to complete the loading.

OK, we are done with Router module. If you like to check, you can go to Terminal tab and key in this command.

Router TerminalTyping “+++” will ask XBee module to enter command mode and will response with “OK”.

ATVR request for firmware version, and XBee will reply, my is 22A7

ATID  request for PAN ID

ATNI request for Node ID, which is ROUTER1 that I just set

ATDL request for Destination Low Address

ATSL request for Source Low Address

ATCN force XBee module to exit command mode. Is fine if you don enter ATCN, after 5 seconds if there is no UART activity, XBee module automatic exit command mode :)

Again, Coordinator

Do not forget, we need to set the DH and DL on Coordinator XBee S2 module. Again, plug in Coordinator XBee S2 module, open X-CTU, read the information, and set the DH and DL that you grab from router’s SH and SL. Click write to load the parameters into XBee module.

Coordinator set DH DL


OK, now both this XBee S2 modules are setup and linked together, once they are powered, they are paired. sending data via UART to one of the XBee module will automatically being transmitted wirelessly to the other XBee module and further transmit out from the UART, and this apply for both way.

Here I have install two SKXBee with Coordinator and Router that we setup. Connected to COM10 and COM25.


Xbee SEries 2

If you notice, the DL and SL is paired up. I enter command mode to grab the information we configured and exit the command mode with ATCN. the following text after ATCN and OK is actually happening in transparent mode. The text you type in Router1 XBee will transmit to Coordinator1 XBee and display on terminal and vise versa.

Now, you can use this pair of XBee S2 as wireless point to point communication. It has become same as the XBee S1 if you configure the DL and SL correctly. I hope the tutorial is simple to understand. We have yet to explore the mesh and complicated communication, if you wanted to do that, do come to our technical forum and discuss.

xargs: How To Control and Use Command Line Arguments

I am trying to use xargs command using shell pipes and not able to understand how to control and use command line arguments. For example I’d like to find out all *.c file located in 100s of sub-directories and move them to another directory called ~/old.src. How do I use command line args with xargs to achieve the same?

xargs command is designed to construct argument lists and invoke other utility. xargs reads items from the standard input or pipes, delimited by blanks or newlines, and executes the command one or more times with any initial-arguments followed by items read from standard input. Blank lines on the standard input are ignored.

xargs is more safer and easy to use

xargs functionality can be achived using the backquote feature of shell. But, it offers more options. It can deal with blanks or special characters in file names easily. It is often used with find, grep and other commands.

xargs examples

For example following example will print 1 2 3 4 using xargs (echo command is default)
$ echo 1 2 3 4 | xargs echo
$ echo 1 2 3 4 | xargs
You can force xargs to use at most max-args arguments per command line. For example following will use first two argument per command:
$ echo 1 2 3 4 | xargs -n 2
Find all .bak files in or below the current directory and delete them.
$ find . -name "*.bak" -type f -print | xargs /bin/rm -f

{} as the argument list marker

{} is the default argument list marker. You need to use {} this with various command which take more than two arguments at a time. For example mv command need to know the file name. The following will find all .bak files in or below the current directory and move them to ~/.old.files directory:
$ find . -name "*.bak" -print0 | xargs -0 -I {} mv {} ~/old.files
You can rename {} to something else. In the following example {} is renamed as file. This is more readable as compare to previous example:
$ find . -name "*.bak" -print0 | xargs -0 -I file mv file ~/old.files

  1. -0 If there are blank spaces or characters (including newlines) many commands will not work. This option take cares of file names with blank space.
  2. -I Replace occurrences of replace-str in the initial-arguments with names read from standard input. Also, unquoted blanks do not terminate input items; instead the separator is the newline character.

Dealing file names with blank spaces and newline

The following will work incorrectly if there are any filenames containing newlines or spaces (it will find out all .mp3 file located in current directory and play them using mplayer):
$ find . -iname "*.mp3" -print | xargs mplayer
To get rid of this problem use -0 option:
$ find . -iname "*.mp3" -print0 | xargs -0 -I mp3file mplayer mp3file
To find out all *.c file located in 100s of subdirectories and move them to another directory called ~/old.src, use:
$ find /path/to/dir -iname "*.c" -print0 | xargs -0 -I file mv file ~/old.src

Avoiding errors and resource hungry problems with xargs and find combo

To copy all media files to another location called /bakup/iscsi, you can use cp as follows:
$ cp -r -v -p /share/media/mp3/ /backup/iscsi/mp3
However, cp command may fail if an error occurs such as if the number of files is too large for the cp command to handle. xargs in combination with find can handle such operation nicely. xargs is more resource efficient and will not halt with an error:

$ find /share/media/mp3/ -type f -name "*.mp3" -print0 | xargs -0 -r -I file cp -v -p file --target-directory=/bakup/iscsi/mp3

Please note that all of the above commands are tested with GNU/xargs version. BSD and UNIX xargs command may not have options such as -r. Please refer to your local xargs man page for further info:
man xargs


Installing and Getting Started with OpenCV on Ubuntu

Here is how to get OpenCV 2.1 going on Linux (Ubuntu). It is written for beginners that are not familiar with the Unix/Linux development environment. It might be useful to you.



OpenCV, originally from Intel and now maintained by open source heroes at the Willow Garage robotics company. It is free and open. It contains a ton of image processing and computer vision algorithms and the data structures necessary for those algorithms.

As of the time I’m writing, the current version of Ubuntu is 10.04 and the current version of OpenCV is 2.1. To get them running together, you need to install from source. But don’t worry, it’s not too hard with cmake.


You should install the Intel TBB libraries to enable parallel processing:

% sudo apt-get install libtbb-dev

if you’re not running Ubuntu 10.04 this probably won’t work. You’ll have to install the right version of TBB from source. See the OpenCV Install Guide for details.

You’ll definitely want cmake and pkg-config:

% sudo apt-get install cmake-gui pkg-config

There are a whole bunch of other libraries that OpenCV can use if you’ve installed them, including gtk2 for GUI stuff, libjpeg, libtiff, libjasper, libpng, and zlib for image formats, ffmpeg, libgstreamer, libxine, and unicap for video, and libdc1394 for firewire cameras. For each of these you need to use apt-get or synaptic to install the “Development” packages.

For ffmpeg:

% sudo apt-get install libavformat-dev libswscale-dev

Build and install

Once you’ve got all the preliminary things in place, you can go to the OpenCV Install Guide and follow the link to download OpenCV 2.1.

Go to the directory that the package got downloaded to and unpack it:

% cd ~/Desktop ; tar xjvf OpenCV-2.1.0.tar.bz2

Next run the cmake GUI:

% cmake-gui

Select the unpacked OpenCV archive directory (e.g., /home/username/Desktop/OpenCV-2.1.0) as the source code directory and a separate directory (e.g., /home/mdailey/Desktop/OpenCV-Build as the build directory, then click the “Configure” button. Select the “Unix Makefiles” option with “default native compilers.”

Change any build flags you want then click the “Configure” button again. If all goes well, click on the “Generate” button. If all goes well again, you are ready to build:

% cd OpenCV-Build
% make
% sudo make install

Compiler warnings are OK, but watch out for any errors along the way.

Finding Documentation

When you install OpenCV, the documentation, including the reference manuals in HTML and a lot of sample programs, is automatically installed in /usr/local/share/opencv/ directory.

When you’re doing OpenCV programming, you will find it very useful to refer to the reference documentation. You should bookmark the HTML pages in your Web browser, either the local versions or the ones at Willow Garage.

Building a sample program

Here we’ll go step-by-step through writing and running a simple program assuming you did installed OpenCV as described above.


We’ll use the newer-style C++ API to OpenCV. The “classic” C API is nice and easy to use as well, but most folks say they prefer C++ these days.

First, we need a makefile to build the code for us so we don’t have to type hairy g++ command lines.

Below is a makefile to automatically compile a sample program stored in the source code file opencv-test.cpp. Take the make instructions below and put in a file called Makefile in some project directory outside of the OpenCVinstallation directory.

SRC = opencv-test.cpp
TARG = opencv-test

CC = g++

CPPFLAGS = -g -Wall -Wno-unused-function `pkg-config --cflags opencv`

LDFLAGS = `pkg-config --libs opencv`

OBJ = $(SRC:.cpp=.o)

all: $(TARG)

        rm -f *~ *.o $(TARG)


Now here’s some C++ source code that uses OpenCV to take the SVD of a matrix. Put it in a file called opencv-test.cpp:

#include <iostream>
#include <iomanip>
#include <cv.h>

using namespace cv;
using namespace std;

// Simple function to print out a matrix

void myMatPrint( ostream &outStream, string stringLabel, Mat &mat )
    outStream << stringLabel << endl;
    for (int iRow = 0; iRow < mat.rows; iRow++)
        for (int iCol = 0; iCol < mat.cols; iCol++)
            outStream << setw(12) << mat.at<double>(iRow,iCol);
        outStream << endl;

// Test code to take the SVD of a matrix and check its correctness

void testSvd( void )
    // The data for the matrix we want to take the SVD of

    double aData[] = { 0.078286, 0.152800, 0.483599, 0.132373,
                       0.809265, 0.040108, 0.420195, 0.468431,
                       0.188109, 0.479326, 0.245392, 0.217179,
                       0.345795, 0.915636, 0.618565, 0.786891 };

    // 4x4 double-precision matrix initialized from aData[]

    Mat matA(4, 4, CV_64F, aData);

    // Perform the SVD: A = U D V'

    SVD svdA(matA);

    // Check result

    Mat matD = Mat::eye(matA.rows, matA.rows, CV_64F);
    for (int iDiag = 0; iDiag < matA.rows; iDiag++)
        matD.at<double>(iDiag, iDiag) = svdA.w.at<double>(iDiag, 0);
    Mat matAhat(4, 4, CV_64F);
    matAhat = svdA.u * matD * svdA.vt;

    double dError = norm( matA, matAhat );

    // Print out results

    myMatPrint( <a href="http://www.opengroup.org/onlinepubs/009695399/functions/cout.html">cout</a>, "Original matrix A:", matA );
    myMatPrint( <a href="http://www.opengroup.org/onlinepubs/009695399/functions/cout.html">cout</a>, "U:", svdA.u );
    myMatPrint( <a href="http://www.opengroup.org/onlinepubs/009695399/functions/cout.html">cout</a>, "D:", svdA.w );
    myMatPrint( <a href="http://www.opengroup.org/onlinepubs/009695399/functions/cout.html">cout</a>, "V (transposed):", svdA.vt );
    myMatPrint( <a href="http://www.opengroup.org/onlinepubs/009695399/functions/cout.html">cout</a>, "U D V':", matAhat );
    <a href="http://www.opengroup.org/onlinepubs/009695399/functions/cout.html">cout</a> &lt;&lt; "Error (L2 norm): " &lt;&lt; dError &lt;&lt; endl;


// Main

int main( void )
    <a href="http://www.opengroup.org/onlinepubs/009695399/functions/cout.html">cout</a> &lt;&lt; "Testing the SVD class..." &lt;&lt; endl;
    return 0;

Compiling and running

Now you can compile your program just by running make in the same directory as the Makefile and C source:

% make
g++  -g -Wall -Wno-unused-function `pkg-config --cflags opencv` `pkg-config --libs opencv`  opencv-test.cpp   -o opencv-test

Now, you’re (almost) ready to run the program. Just type

% ./opencv-test

at the command line. The first time you try this with a per-user installation, you’ll probably get the error message

./opencv-test: error while loading shared libraries: libml.so.2.1: cannot open shared object file: No such file or directory

the reason is that the Linux loader is looking for the shared library (DLL in Windows parlance) libml.so.2.1 but can’t find it since it’s in a non-standard directory (/usr/local/lib if you followed the instructions above).

The solution is to tell the dynamic linker where your DLLs are. Add the line

export LD_LIBRARY_PATH=/usr/local/lib

to your ~/.bashrc and (you only have to do this when .bashrc is changed), source it:

% source ~/.bashrc

To check that your library path is set correctly, use the env command:

% env | grep LD_L

OK, now we can just run ./opencv-test without trouble. If everything is working, you should see the matrix A print out, the SVD of A (U, D, and VT), the reconstruction of A from U, D and V, and finally the norm of the difference, which was 2.13994e-15 in my test:

% ./opencv-test
Testing the SVD class...
Original matrix A:
    0.078286      0.1528    0.483599    0.132373
    0.809265    0.040108    0.420195    0.468431
    0.188109    0.479326    0.245392    0.217179
    0.345795    0.915636    0.618565    0.786891
   -0.245183   0.0184143    0.968008  -0.0500616
   -0.471097   -0.875102   -0.104562  -0.0364773
   -0.328201    0.230509   -0.134376   -0.906142
   -0.781177    0.425115   -0.184309    0.418414
V (transposed):
   -0.418902    -0.53139   -0.503545   -0.537204
   -0.739783    0.669684  -0.0562768   -0.032817
   -0.304207   -0.278107    0.861844   -0.295532
   -0.429768   -0.437954  -0.0223628    0.789301
U D V':
    0.078286      0.1528    0.483599    0.132373
    0.809265    0.040108    0.420195    0.468431
    0.188109    0.479326    0.245392    0.217179
    0.345795    0.915636    0.618565    0.786891
Error (L2 norm): 2.13994e-15


You’ve seen how to install the OpenCV library and how to write and run OpenCV programs under Linux, Ubuntu Edgy in this case. Comments and improvements are welcome.