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

Table of Contents (13 chapters)


The Arduino platform makes it easy to get started with programming and electronics, but introducing wireless communication in your project can get complicated quickly. The XBee wireless platform hides most of the complicated details from you, and this book provides a step-by-step guide to using XBee modules with Arduino.

This book describes an example wireless sensor network, and invites you to build that network yourself. By following the steps in each chapter, you will build a network that can measure temperature and humidity in various rooms of your house, collect that data online, and automatically control your heating and/or cooling system to maintain the proper temperature in your house. This temperature can be configured through an online dashboard, ultimately putting control back in your hands.

All the concepts needed to build this example network will be explained, so you will have the knowledge to build your own project using these same concepts. Concepts that are closely related, but beyond the scope of this book, will be mentioned and appropriate references will be given so you can find out more if needed.

What this book covers

Chapter 1, A World without Wires, introduces the XBee platform and shows how to use the XCTU program to control and configure XBee modules. It covers the AT and API modes, firmware updates, and ZigBee network creation and security. It also shows how to transmit your first messages between two XBee modules.

Chapter 2, Collecting Sensor Data, provides more details on wiring up XBee modules, and introduces the xbee-arduino library that lets an Arduino take control of an XBee module. Reading a sensor, designing a packet format, and wirelessly transmitting data are discussed; thus, by the end of this chapter you will have a basic wireless sensor network where one or more Arduinos read temperature and humidity data and this is wirelessly collected by another Arduino.

Chapter 3, Storing and Visualizing Your Data, covers storing and visualizing your collected data with the Beebotte cloud service, using an Internet-connected Arduino and the MQTT protocol. Storing and visualizing your data on your own computer, using a Python program and database, are also briefly discussed.

Chapter 4, Controlling the World, shows how to let your network control things in addition to monitoring them, such as heating and/or cooling your house. Simple on/off control is covered in detail, either using a relay module connected to an Arduino, or using an off-the-shelf wireless power socket that supports the ZigBee Home Automation protocol.

Chapter 5, Standalone XBee Operation, lets you implement simple devices without using an Arduino, by letting the XBee module directly control or measure things. You will see how to simplify the relay module from Chapter 4, Controlling the World and add window open/closed detection to your network.

Chapter 6, Battery Power and Sleeping, discusses options for battery-powering your projects, as well as techniques to reduce the power used. This includes some hardware techniques, as well as applying various sleep modes to drastically reduce XBee and Arduino power usage when they are idle.

What you need for this book

To upload programs (sketches) to your Arduino boards, you will need the Arduino IDE, which can be downloaded from Version 1.6.5 was used in this book, but it is recommended to get the newest version. This book assumes you are already familiar with this program and know how to write and upload a sketch, which will not be covered in this book.

For configuring and interacting with XBee modules, you will need the XCTU program, which can be downloaded from Version 6.2.0 was used in this book, but it is recommended to get the newest version. No prior experience with this program is needed; it will be introduced in detail.

The example sketches in this book use a number of Arduino libraries. These libraries are:

  • AltSoftSerial by Paul Stoffregen (version 1.3.0)

  • XBee-Arduino library by Andrew Rapp (version 0.6.0)

  • DHT sensor library by Adafruit (version 1.2.0)

  • Adafruit MQTT library by Adafruit (version 0.11.1)

  • Adafruit SleepyDog library by Adafruit (1.0.0)

  • Adafruit CC3000 library by Adafruit (version 1.0.3 optional)

In general, it is recommended you download the newest version of a library using the library manager in the Arduino IDE. However, if you run into problems because a library has made changes that are not backwards-compatible, it might be useful to test the exact same version of a library that was used when writing this book. These versions are shown above, but also included in the provided code bundle.

The introduction of each chapter indicates what hardware you will need for the examples in that chapter. This always lists the hardware required to build an example once, sometimes also sharing items between examples. If you want to expand your network to include multiple temperature sensors, window sensors, and so on, you will of course need multiples of some of the components listed.

Who this book is for

This book is for those who have been playing with the Arduino platform and now want to make their creations wireless. There is no need to be a veteran programmer, though a basic understanding of the Arduino and Arduino programming is assumed. All examples make use of ready-made hardware, so no soldering skills are required and, electronically, things are limited to just connecting a few pins and wires. This book will also prove useful for anyone wanting to integrate XBee with microcontroller platforms other than the Arduino, since most of the advice regarding XBee is not specific to the Arduino.


In this book, you will find a number of text styles that distinguish between different kinds of information. Here are some examples of these styles and an explanation of their meaning.

Code words in text, folder names, filenames, file extensions, pathnames, dummy URLs, user input, and configuration values are shown as follows: "Sending a packet is handled by the sendPacket() function."

A block of code is set as follows:

// the loop function runs over and over again forever
void loop() {
  digitalWrite(13, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(1000);              // wait for a second
  digitalWrite(13, LOW);    // turn the LED off by making the voltage LOW
  delay(1000);              // wait for a second

Any command-line input or output is written as follows:

openssl rand -hex 16

New terms and important words are shown in bold. Words that you see on the screen, for example, in menus or dialog boxes, also appears in bold like this: "Click on the Update Firmware button to replace the firmware of your device."


Warnings or important notes appear in a box like this.


Tips and tricks appear like this.

Reader feedback

Feedback from our readers is always welcome. Let us know what you think about this book—what you liked or disliked. Reader feedback is important for us as it helps us develop titles that you will really get the most out of.

To send us general feedback, simply e-mail , and mention the book's title in the subject of your message.

If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, see our author guide at

Customer support

Now that you are the proud owner of a Packt book, we have a number of things to help you to get the most from your purchase.

Downloading the example code

You can download the example code files from your account at for all the Packt Publishing books you have purchased. If you purchased this book elsewhere, you can visit and register to have the files e-mailed directly to you.


Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you find a mistake in one of our books—maybe a mistake in the text or the code—we would be grateful if you could report this to us. By doing so, you can save other readers from frustration and help us improve subsequent versions of this book. If you find any errata, please report them by visiting, selecting your book, clicking on the Errata Submission Form link, and entering the details of your errata. Once your errata are verified, your submission will be accepted and the errata will be uploaded to our website or added to any list of existing errata under the Errata section of that title.

To view the previously submitted errata, go to and enter the name of the book in the search field. The required information will appear under the Errata section.


Piracy of copyrighted material on the Internet is an ongoing problem across all media. At Packt, we take the protection of our copyright and licenses very seriously. If you come across any illegal copies of our works in any form on the Internet, please provide us with the location address or website name immediately so that we can pursue a remedy.

Please contact us at with a link to the suspected pirated material.

We appreciate your help in protecting our authors and our ability to bring you valuable content.

eBooks, discount offers, and more

Did you know that Packt offers eBook versions of every book published, with PDF and ePub files available? You can upgrade to the eBook version at and as a print book customer, you are entitled to a discount on the eBook copy. Get in touch with us at for more details.

At, you can also read a collection of free technical articles, sign up for a range of free newsletters, and receive exclusive discounts and offers on Packt books and eBooks.


If you have a problem with any aspect of this book, you can contact us at , and we will do our best to address the problem.