Book Image

Building Wireless Sensor Networks Using Arduino

By : Matthijs Kooijman
Book Image

Building Wireless Sensor Networks Using Arduino

By: Matthijs Kooijman

Overview of this book

Arduino has been established as the de facto standard microcontroller programming platform, being used for one-off do-it-yourself projects as well as prototypes for actual products. By providing a myriad of libraries, the Arduino community has made it very easy to interact with pretty much any piece of hardware out there. XBee offers a great range of low-power wireless solutions that are easy to work with, by taking all of the complexity of wireless (mesh) networking out of your hands and letting you focus on what to send without worrying about the how. Building wireless sensor networks is cost-effective as well as efficient as it will be done with Arduino support. The book starts with a brief introduction to various wireless protocols, concepts, and the XBee hardware that enables their use. Then the book expands to explain the Arduino boards to you, letting them read and send sensor data, collect that data centrally, and then even control your home from the Internet. Moving further more advanced topics such as interacting through the standard Zigbee Home Automation protocol, or making your application power-efficient are covered. By the end of the book, you will have all the tools needed to build complete, real-world solutions.
Table of Contents (8 chapters)

Getting and running XCTU

XBee configuration and test utility (XCTU) can be downloaded from http://www.digi.com/xctu. In this book, the Next Generation XCTU, version 6.2.0, was used, but using the newest version available is recommended. Starting with this 6.2.0 version, Linux is supported in addition to Windows and OS X.

After running through the installer and starting XCTU, the first step is to add a module that tells XCTU which serial port to connect to, and what settings to use. There are two buttons for this:

Getting and running XCTU

You will normally use the left Add devices button, which asks you for the settings to use. The right Discover devices button can be used when you are not sure about the right settings to use. Though it is not as magical as it sounds (you still need to select the serial port to use and select what different settings to try), XCTU will simply try all settings one by one until it finds one that works.

Go ahead and click on the left button. You will be greeted by a window where you can select the serial port and enter settings:

Getting and running XCTU

The serial port you need to use depends a bit on the platform. On Windows, it is likely the highest COM port listed (described as USB Serial Port). On OS X, it will look like usbserial—xxxxxxxx and, on Linux, it will be ttyUSBx. Unplugging your Explorer and refreshing the list is an easy way to figure out what port belongs to it.

By default, XBee modules are configured for 9600 baud, 8N1, so you can probably just leave all settings at their default, as shown in the preceding screenshot, and click on Finish right away. XCTU will open up the serial port and start talking to your XBee module to find out its type and firmware version. Shortly afterwards, the left side of the screen should show your module's information. If you click it, XCTU will further query your module for its current configuration settings. You should have something that looks somewhat like this:

Getting and running XCTU

On the right part of the screen, you see what is called the Configuration working mode by XCTU. It shows all the current configuration values and lets you change them. If you change any of the values, they will not be sent to the device until you tell XCTU to write the changes. You can write all changed settings at once by clicking on the Write radio settings button at the top:

Getting and running XCTU

Alternatively, you can write just a single setting by clicking on the Write button next to the setting.

Note that both buttons update the current value in the module and let the module write the values to its non-volatile memory (so they are remembered across a power cycle).

In the rest of this book, when a configuration value is mentioned, this is the place to go to. All configuration values have a two-character name, so you will often see something like Set EE=1, meaning you change the EE configuration value to the value 1, and then write the new configuration to module by pressing the Write button.

Note

All numerical configuration values in XCTU are entered in hexadecimal (see this article if you are unfamiliar with hexadecimal numbers: https://learn.sparkfun.com/tutorials/hexadecimal). In this book, hexadecimal numbers are usually shown using the 0x prefix for clarity, but that prefix must be removed when entering values in XCTU.

Updating the firmware

As a first step in preparing your module for operation, you should check, and potentially replace, its firmware. For XBee ZB S2/S2B modules, this is needed to switch between different modes of operation, but for other modules it also makes sense to ensure your module is running the latest available firmware version.

The firmware is a bit of software, written by the manufacturer, that controls the XBee modules internally. It is responsible for operating the radio, taking care of the 802.15.4, ZigBee, and/or DigiMesh protocol processing (as well as any encryption), and communicating the results on the serial port.

For most XBee modules, there are several variants of the firmware, each with a slightly different function set. Some of these provide different features, but others are intended for hardware only. For now, you should just use the function set indicated next, but by the end of this chapter you will know what most of the function sets listed mean exactly.

To replace the firmware, some special commands have to be sent through the serial port to start the boot loader, which is a small piece of low-level software running on the XBee module that takes care of firmware updates. After starting the boot loader, the firmware file can be transferred through the serial port. Fortunately, there is no need to take care of this manually—the XCTU program can mostly handle firmware updates automatically.

To initiate a firmware update, select the module on the left, and click on the Update firmware button:

Updating the firmware

A window pops up, allowing you to select the Product family, Function set, and Firmware version. The right product family should already be selected (take a note of which one is selected in case you need to do recovery later). The first module will be the coordinator, so select the ZigBee Coordinator AT function set when using an XBee ZB S2 or S2B module, or ZIGBEE Reg function set when using S2C (where Reg is for Regular and indicates that this is not XBee-PRO). Finally, select the highest version listed to use the most recent firmware version.

You should probably also uncheck the Force the module to maintain its current configuration checkbox, in order to ensure you start with a clean factory default configuration.

Your selection should look like this:

Updating the firmware

By clicking on Update, the XCTU program will start replacing the current firmware with the one selected. This takes around half a minute to complete; be sure not to unplug the board during the process.

Note

Version numbers for XBee modules are a bit confusing at first glance. They consist of four hexadecimal digits. Of these digits, the second digit is not really part of the version number, but indicates the function set used. For example, at the time of writing, 21A7 is the most recent ZigBee Coordinator API version, while 23A7 is the most recent ZigBee Router API version. This might suggest that the router API firmware is more recent, but both of these are equally recent and based on the same codebase; they just use a different function set.

Furthermore, the third digit indicates whether this is a stable (even) or beta (odd) version. The previously listed versions are stable, since their third digit is even (0xA = 10).

Note that, when you replace an existing coordinator's firmware, it will also start a new network. If other XBee modules previously joined the coordinator's network, it is possible that these are now joined to the old network and will no longer talk with the new coordinator. You can check by looking at the OI configuration value, these should be equal on all modules. The easiest way to force the router to leave the old network, is to change its ID configuration value, click the Write button, then change ID back to its previous value and click Write again. Doing this on the router(s) after you change the coordinator's firmware should prevent issues.

Failed firmware updates

Replacing the firmware is not entirely without risk. If the process is interrupted, or fails for some other reason, there is a chance that the module will end up without a working firmware. This prevents the module from working normally, but also prevents a subsequent firmware update from being started in the normal way.

If the regular firmware is broken, recovery requires connecting the XBee to your computer with hardware flow control (RTS/CTS) lines connected. The SparkFun Explorer USB board supports this out-of-the-box, but see this page for information on using an Arduino Leonardo for configuration and recovery: http://www.stderr.nl/Blog/Hardware/Electronics/Arduino/XBee/XctuAndArduino.html.

For exhaustive instructions on this recovery procedure, refer to the Digi knowledge base (search for XBee recovery).

Configuration

For this initial example, only minimal configuration is needed:

  • The first module must be set to be a coordinator. When using the XBee ZB S2 or S2B modules, this is already done by uploading the coordinator firmware. When using the S2C modules, you will have to set CE=1 (Coordinator Enable) to turn the first module into a coordinator.
  • The destination address for transmissions must be configured. This is determined by the DH and DL (Destination High and Low) configuration values. Together, they contain a 64‑bit address of the destination (read on for more info on these addresses). For this example, it is best to set DH=0 and DL=0xFFFF, causing messages to be broadcast to all other nodes.

For example, to change the destination address:

  1. Make sure you are in the Configuration working mode using the button at the top right.
  2. Scroll down the list of configuration values in the right part of the screen, until you find the Addressing section.
  3. Set DH to the value 0 and DL to FFFF (leaving out the 0x part).
  4. Finally, click on the Write radio settings button above the list to apply the changes.

Setting CE is similar, but the value can be found in the Networking section instead.

Talking to the XBee module

Now, to talk to your XBee module, you can use the serial console built into XCTU (using another serial program is also possible, but do not forget to disconnect in XCTU first). To do so, head over to the Consoles working mode by clicking on its button at the top right:

Talking to the XBee module

Then click on the Open connection button in the top left of this area to enable the console:

Talking to the XBee module

The console is divided into two parts horizontally. The left side shows all data sent (blue) or received (red) as text and lets you type text to be sent. The right shows the same, but shows each byte value in hexadecimal.

Go ahead and type something in the left area. Anything you type there will be sent to the XBee module, which will then directly send the data through the radio again. So, you have sent your first radio message now, even though there is nobody listening yet.

To allow configuring the module, a special command mode is supported too (this is what is used under water when you change a configuration value in XCTU). By default, this command mode can be entered by sending a special sequence to the XBee module: a pause of at least 1 second, three iterations of a plus sign (+++, no Enter), and another pause of at least 1 second. When command mode is entered, the XBee module will reply with OK, and commands can be sent to the XBee module, terminated by a newline. These commands all start with AT, inspired by the Hayes command set originally designed for dial-up modems. Since these commands use normal text, it is easy to interact through AT mode simply by using a serial terminal. When no command is given for a while (10 seconds by default), the module leaves command mode again.

Try entering command mode and giving the ATVR command (do not forget to press Enter), which should retrieve the current firmware version:

Talking to the XBee module