Search icon CANCEL
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Building Wireless Sensor Networks Using Arduino

You're reading from   Building Wireless Sensor Networks Using Arduino Leverage the powerful Arduino and XBee platforms to monitor and control your surroundings

Arrow left icon
Product type Paperback
Published in Oct 2015
Publisher
ISBN-13 9781784395582
Length 192 pages
Edition 1st Edition
Tools
Concepts
Arrow right icon
Author (1):
Arrow left icon
Matthijs Kooijman Matthijs Kooijman
Author Profile Icon Matthijs Kooijman
Matthijs Kooijman
Arrow right icon
View More author details
Toc

Receiving data

Now you have set up an XBee module to transmit data, the next step is to receive this data again on another module. The default firmware and configuration are probably sufficient for this but if they do not work, connect the second module to XCTU using your Explorer board and update the firmware to the ZigBee Router AT version (XBee ZB S2/S2B only) and check the DH and DL values.

To set up your second XBee module, you will use an Arduino Uno and the SparkFun XBee shield to forward all received data to the computer. Again, if you want to use different hardware, skip ahead to Chapter 2, Collecting Sensor Data, which has more details on how to connect an XBee module to an Arduino and how to modify the sketch to support a different setup.

Uploading the sketch

The sketch used for receiving data is available in the code bundle as SerialDump.ino. The exact contents of the sketch will not be discussed here, though you are encouraged to look inside to see what happens. Basically, the sketch opens up a serial connection to the XBee module at 9,600 bps (bits per second), and a serial connection to the computer at 115,200 bps. Any data received from the XBee module is forwarded to the computer, both in hexadecimal and readable ASCII format.

This sketch uses the AltSoftSerial library, so be sure to install that through the library manager, or download it from https://www.pjrc.com/teensy/td_libs_AltSoftSerial.html.

Note

It is recommended you upload the sketch before connecting the XBee module, to prevent damage caused by the pins being in an unexpected state (this can cause a short-circuit in some cases).

Connecting the XBee

To connect the XBee module to your Arduino, you will use the SparkFun XBee shield. To allow talking both to the computer and the XBee at the same time reliably, you will need some trickery to connect the XBee's serial pins to AltSoftSerial's pins 8 and 9. This will be explained in more detail in Chapter 2, Connecting Sensor Data, but for now:

  1. Put the switch on the shield into the DLINE position.
  2. Connect pin 2 to pin 8 and pin 3 to pin 9 on the shield.
  3. Insert the XBee module.

This should look a bit like this:

Connecting the XBee

Receiving data

To actually receive data, open up the serial monitor in the Arduino IDE and configure it for 115200 baud. Now head back to the console in XCTU and type a message to be sent:

Receiving data

It is best to first type the message in a text editor, such as Notepad, and then paste it into XCTU. This makes sure the entire message is sent in a single radio packet. If you type in XCTU directly, each letter will be put into its own packet, which can cause delays and, when broadcasting, even mess up the ordering of letters in some circumstances.

In the serial monitor, you should see the same message being received:

Starting....
48 65 6C 6C 6F 2C 20 77  Hello, w
6F 72 6C 64 21           orld!

As you can see, any data sent to the XBee module on the sending side is reproduced byte-for-byte identically on the receiving side. For this reason, this mode of operation is commonly called transparent mode and can be useful to quickly make an existing serial connection wireless.

The sketch also allows typing a message into the serial monitor, which will be sent to the XBee module and then on to the other XBee module, and is shown in XCTU, so go ahead and try that too.

lock icon The rest of the chapter is locked
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.
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 R$50/month. Cancel anytime