Reader small image

You're reading from  Building Wireless Sensor Networks Using Arduino

Product typeBook
Published inOct 2015
Publisher
ISBN-139781784395582
Edition1st Edition
Tools
Concepts
Right arrow
Author (1)
Matthijs Kooijman
Matthijs Kooijman
author image
Matthijs Kooijman

Matthijs Kooijman is an independent embedded software developer who is firmly connected with the maker movement through a local fab lab and his work on the Arduino project. Since his youth, Matthijs has been interested in making things; for example, he built his first television remote control before the age of 10 (using a piece of rope to pull on the volume slider, not a solution that he would choose today). Matthijs has a firm belief in the merits of open source software and enjoys contributing to the software that he uses—both by coding and helping out other users. His work experience is broad—ranging from Web development to Linux driver hacking, from tech support to various forms of wireless networking, but almost always related to open source software in some way.
Read more about Matthijs Kooijman

Right arrow

Chapter 2. Collecting Sensor Data

In the previous chapter, you connected your XBee modules to your computer and sent a bit of data between them. In this chapter, you will take a few more steps and put your Arduino boards in control of the XBee modules. You will even send over some measured temperature and humidity data, creating your very first actual wireless sensor network.

You will build towards this step by step. The first step is to find out how to make an Arduino and XBee module talk to each other. Then you will send a simple Hello, world! message from one Arduino to another and finally replace that message with some actual live sensor data.

To follow the examples, the following components are recommended:

Hardware setup


In this section, you will look in more detail at how to connect an XBee module. By the end of this section, you will better understand the connections described in the previous chapter, and will be able to figure out how to wire up other shields and adapters too.

Serial on XBee

Interfacing with an XBee module is not terribly complicated. All XBee modules offer a serial connection. In the most basic version, this involves just three pins on the XBee board: DIN (where the XBee module receives data), DOUT (where the XBee module sends data), and GND to establish a common ground reference.

This type of connection does not have any official name, but is commonly (but inaccurately) called TTL (Transistor-to-Transistor Logic) serial and is also used on the Arduino Uno between the integrated USB-to-serial converter and the main microcontroller.

It would be more accurate to say that it is an asynchronous, single-ended serial protocol running at 3.3V. Asynchronous indicates that there is...

Software setup


Now that the hardware is covered, it is time to look at the software side of things. In this section, you will create a small sketch (called Connect.ino in the code bundle) that makes the Arduino send a VR command to the XBee module to query the current firmware version of the XBee module. The Arduino code is set up so that all API frames received from the XBee module are printed, so you can see the reply to this command as well, confirming that connectivity is working. No data is transmitted wirelessly yet; this just tests the Arduino-to-XBee connection.

Example sketches

In this and the following chapters, you will write some Arduino sketch code. All of the example code shown in the book is also available in the code bundle, which you can download from the Packt website.

The example code in this book is sometimes a bit more verbose than strictly needed. Sometimes this makes it easier to expand an example later, or sometimes the example illustrates some good coding practice that...

Sending and receiving data


Now that the XBee connectivity is covered, it is time to actually exchange some data wirelessly between your Arduinos. In the previous chapter, you have already seen the API frames involved in transmitting and receiving data through the network. In this section, you will let the Arduino use those API frames to exchange a simple Hello, world! message again. For this, the sender will use the XBee module that you configured as a router, while the receiver will use the coordinator XBee.

Sending data

Remember that sending a message involves a few different API frames:

  • The sender sends a ZigBee Transmit Request API frame to the XBee module, containing the destination address and the message.

  • The receiver receives a ZigBee Receive Packet API frame, containing the sender address and the message. The receiver XBee will also (automatically) send an acknowledgement (ack) back to the sender so the sender knows the transmission was successful.

  • The sender receives a ZigBee Transmit...

Collecting sensor data


Even though sending over Hello, world! is already pretty cool, it is not very useful yet. Instead of sending a fixed message, you will want to send some variable data, such as coming from a sensor.

In this section, you will connect a combined temperature and humidity sensor to the sending Arduino and have it send over its readings to the coordinator. The coordinator will then receive these readings and, for now, display them on the Serial console (later, you will see how to put the readings in pretty graphs, too!).

There are a lot of different sensors available that can be used with an Arduino. There is not a single unified way to wire up and talk to all sensors, but Arduino-specific instructions and tutorials can be found for a lot of hardware. A good source of inspiration are Arduino-oriented online shops such as http://www.sparkfun.com or http://www.adafruit.com. They stock all kinds of sensors and typically provide appropriate Arduino libraries and/or examples as...

Troubleshooting


In an ideal world, everything works as expected right away. In the real world, you will run into problems every now and then. Here are some pointers to help you figure out what is wrong and how to fix it.

Communication with the XBee module is not working

Is serial communication not working at all? Check your connections: DIN to TX, DOUT to RX.

A good way to test this is to do a loopback test:

  1. Remove the XBee module from your board and use a jumper wire to connect DOUT (pin 2) and DIN (pin 3) on the board.

  2. Upload the SerialDump.ino sketch to the Arduino and open up the serial port using the serial monitor in the Arduino IDE (or another serial console program).

  3. If everything is wired correctly, any data you send should be received and displayed again.

If you are using a separate USB-to-serial converter, try swapping the DIN and DOUT wires; some of these converters have their labels inverted.

Make sure you connect GND between the XBee and USB-to-serial converter too, to establish a...

Summary


In this chapter, you have put your Arduinos in control of your radio modules, and set up your very first wireless sensor network by wirelessly transmitting measured values to your coordinator node. By applying what you have learned, you can extend your network by adding more Arduinos with DHT sensors. You should even be able to add different kinds of sensors by adding a new packet type!

Now, reading your measurements in the serial console is nice, but not very convenient. In the next chapter, you will find out how to store all this collected data on your computer, or even directly in the cloud, and how to gain more insight by generating convenient graphs out of the data.

lock icon
The rest of the chapter is locked
You have been reading a chapter from
Building Wireless Sensor Networks Using Arduino
Published in: Oct 2015Publisher: ISBN-13: 9781784395582
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime

Author (1)

author image
Matthijs Kooijman

Matthijs Kooijman is an independent embedded software developer who is firmly connected with the maker movement through a local fab lab and his work on the Arduino project. Since his youth, Matthijs has been interested in making things; for example, he built his first television remote control before the age of 10 (using a piece of rope to pull on the volume slider, not a solution that he would choose today). Matthijs has a firm belief in the merits of open source software and enjoys contributing to the software that he uses—both by coding and helping out other users. His work experience is broad—ranging from Web development to Linux driver hacking, from tech support to various forms of wireless networking, but almost always related to open source software in some way.
Read more about Matthijs Kooijman