Data Acquisition using XBee

The XBee as a communications medium can be a reliable means of wireless data acquisition. A medium to large network would use the XBee version 2 using API mode and may look something like the following:

Each XBee measures analogue or digital signals from the environment at its location, and may be attached to a microcontroller for additional functionality. These communicate wirelessly, using the IEEE802.15.4 and Zigbee protocols, with a coordinator XBee attached to a Linux based processor which collects the incoming data and passes it to storage. A remote monitoring PC may perform control and configuration of the network through a local LAN or Internet.

This page describes software developed as a foundation for building a network of the above type. More documentation is provided in a LibreOffice file. All code and PCB files are provided via a GitHub repository.

(Note: software is licensed by the Apache licence which allows free copying for commercial projects, with protection).

Remote Node

The remote node must have an XBee in Router or End Device mode. The latter allows the XBee to enter sleep mode for power saving. The XBee communicates with the coordinator XBee to pass command and data.

Bootloader

A microcontroller node may have a bootloader for remotely installing application firmware. A bootloader has been developed for the Atmel ATMega168 but can be extended to other AVR microcontroller types with sufficient FLASH memory via a simple change. It is written in C specifically for AVR microcontrollers.

Firmware

The AVR program needs to communicate with the attached XBee using the so-called API messages via the serial port. The firmware provided has a number of forms for testing. The main firmware is designed specifically for a watermeter project transmitting battery voltage and a count of transitions on a digital port of the microcontroller. An ACK/NAK protocol is used to reduce the probability of loss of data due to adverse network conditions.

Base Station

The process xbee-acqcontrol is a program running continuously as a background process on a Linux based (assumed Ubuntu here) computer referred to as the Base Station. This provides an interface between the XBee network and an external user interface. The computer could be as small as a Raspberry Pi and is intended to sit permanently powered and unmonitored in the proximity of the network with an attached coordinator XBee. The program is written in C and relies on the libxbee libraries written and maintained by Attie Grande. It monitors the XBee network, collecting data messages and passing them to local storage. It also interacts with external PCs for monitoring and configuration by passing messages through to the remote or coordinator XBees. The software requires the libxbee package to be installed and compiled. This can either be installed to the system or the libxbee.a (binary library) and xbee.h (header) files may be copied over to the directory where the coordinator software has been unpacked.

The results files are stored in a predefined directory which by default is /data/XBee. This must be created and the permissions set to allow writing by the user under which the program is run. If another directory is desired it can be specified as a parameter at program invocation.

There is a setting in the xbee-acqcontrol.h file to define a DEBUG symbol. This should normally be commented out. However if debug printouts are desired leave this uncommented and run the program from a terminal. Certain messages are printed to the syslog local7 facility, which must be setup in one of the /etc/rsyslog.d files (see documentation for details).

The coordinator XBee must be present. The program currently looks for a device in /dev/ttyUSB0 by default but the device can be specified in the command line options. These device files appear when a USB to serial converter is connected, such as would be the case for a USB adapter for the XBee. If running under a user other than root, add the user to the dialout group. The baud rate defaults to 38400 baud but this also can be specified as a command line option.

After the process is started it issues a node discovery packet and delays for 10 seconds to await identification responses. These will come from all XBees that are awake. External programs cannot connect to the process before this time is complete.

The process can be started with:

$ ./xbee-acqcontrol -d data-directory -b baud-rate -P serial-device

Where each option can be omitted or placed in any order as desired. Typically the process will be started on bootup of the machine.

Check in /var/log/syslog for any error messages.

Monitor PC

The user interface or monitor program is written in C++ and uses QT5 for GUI support. It currently runs under Linux but could be adapted to a Windows or OS/X environment. This provides basic control of the coordinator process and configuration of the XBees. It can be expanded and developed as needed to support particular applications. The QT packages must be installed for compilation and use. There is a setting in the xbee-control.h file to define a DEBUG symbol which should be commented out. However if debug printouts are desired leave this uncommented and run the program from a terminal.

Usage

Run the program from the directory containing its binary with:

$ ./xbee-config

By default the coordinator program listens on the TCP port 58532. If attempting to connect from the Internet, any intervening routers and firewalls must be set to port-forward to the coordinator IP address on this port (note that on some routers Port Forwarding is called "Virtual Server").

Run the GUI and in the top left edit box enter the IP address or domain name of the coordinator machine. The example below has the coordinator running on the same machine as the GUI.

Click "Connect" to make a connection. The table should show a list of the nodes in the network as registered by the base station. The coordinator is not shown. The nodes above marked with an asterix had been detected in an earlier run, but on this occasion were not detected. This feature allows dead or removed nodes to be identified. A sleeping node may also be so marked if it wasn't detected at startup.

To configure the XBee in a node, select the checkbox on the right and click "Configure XBee". This brings up another window after a delay.


This may take some time if the XBee is sleeping, but it will wait for a specifid time. This allows a number of relevant configurations to be made, notably the analogue and digital I/O, sampling interval and sleep options (in the case of end-devices only). Click "Write Values" for these to be made permanent.

The coordinator attached to the coordinator processor can be configured by clicking "Configure XBee" when no remote node is selected. Note that if more than one node is selected the lowest numbered one is used.

If a node has been powered off or removed from the network then its entry in the table can be deleted by selecting it and clicking "Remove Node".

If the node has an attached microcontroller with a compatible bootloader, then new firmware can be uploaded by clicking the "Firmware" button. A progress bar will appear. Note that the upload process can be quite slow particularly for busy networks.

If a new node is inserted and the XBee has been properly configured (see the documentation for suitable settings) then on power up should will send out an identify message. The coordinator program should intercept this and add the node automatically to the table. If this doesn't happen for some reason, then the node's 64 bit serial number (in hex) can be entered in the top right edit box, and "Query Node" will attempt to contact it. The text node name (node-id) can also be used but this is less likely to work.

Installation

Coordinator

This is available only for Linux and has been tested under Ubuntu (although in principle light weight distributions without desktop would be more appropriate). It is written in C and compiled with the gcc compiler.

Download the libxbee source code and follow the instructions in the README to compile it. This should just require "make configure" and "make all" to install the libraries to a suitable system library directory.

Download the XBee-Acquisition package from GitHub into a suitable directory. Go into the XBee-acqcontrol directory and compile using "make". The makefile is generic and should work without modification on any system.

If error messages are to go to a specific log file rather than /var/log/syslog, edit the /etc/rsyslog.d/50-default.conf file and add the line:

local7.*    /var/log/user.log

This will allow the program to write messages to the designated log file.

A directory is needed to store the files of data received from the remote nodes. This can be entered as a command line option, eg:

$ ./xbee-acqcontrol -d /data/XBee

where the directory must exist and be writeable by the user running the program (if other than root). The directory /data/XBee is the default.

Finally to get the program to run with a non-root user, add the user to the dialout group, then log out and back in:

$ sudo adduser username dialout

Monitor (Linux Version)

For Linux use, the following QT5 development packages should be installed: qt4-designer, qt5-qmake.

Apart from this the program only uses the standard POSIX libraries.

Build the program with

  • make clean

  • qmake

  • make

Monitor (Windows Version)

Note: The program has been modified to compile under QT5. Therefore either modify the instructions below to use QT5 or download an earlier version of the software on or before December 24 2014.

For development or compilation on Windows, download the QT opensource version 4.8.4 for Mingw 4.4 (http://qt-project.org/downloads). Install to its default folder.

These versions date from early 2013 but the versions QT 4.8.6 for MinGW 4.8.2 should work provided the available version of MinGW is compatible.

There may be difficulties encountered installing to 64 bit machines if the binaries are 32 bit. This will usually result in missing libraries during the linking stage.

Download the Mingw install GUI (http://www.mingw.org/wiki/Getting_Started). Also install to its default folder, as that will be used by default by QT. Copy the source files for Xbee-GUI to a folder C:\Xbee-GUI. From the start menu, open the QT 4.8.4 command prompt in the "QT by Digia v4.8.4" (or similar) entry. In the window, change directory to C:\Xbee-GUI and issue the commands:

  • qmake

  • make clean

  • make

The executable will appear in the “release” folder. To make it distributable, add the following files from the MinGW\bin and QT\4.8.4\bin folders:

  • libgcc_s_dw2-1.dll

  • libstdc++-6.dll

  • mingwm10.dll

  • QtCore4.dll

  • QtGui4.dll

  • QtNetwork4.dll

The resulting set of files can then be bundled into a zip file for distribution and should run on any compatible Windows installation without additional compilation actions needed. This has been tested on Windows 7.

Bootloader

The bootloader is written in C and is compiled using avr-gcc which is available in the Ubuntu repositories. It should be possible to compile also under WinAVR on a Windows machine.

Todo list

1. Update Windows version of Monitor program to QT5.
First created 22 February 2013
Last Modified 24 May 2017
Ken Sarkies 2013