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:
- Put the switch on the shield into the
DLINE
position. - Connect pin 2 to pin 8 and pin 3 to pin 9 on the shield.
- Insert the XBee module.
This should look a bit like this:
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:
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.