Friday, March 23, 2012

Buzzing XBees

It's been a while since I've written, but I just have to share my experiences with this. I recently bought a pair of XBee Series 2 radios along with XBee Explorer break-out boards. I first heard about these radios in general back in school when I was working with CU's RECUV group, and then again in Tom Igoe's excellent book Making Things Talk, and I've been itching to try them out ever since. They're a bit like Wi-Fi or Bluetooth, but seem to lend themselves better to microprocessor projects, generally use less power, and have some unique features that are great for sensor networks. I understand they're also being used for modern home automation products.


However, getting these things configured and tested wasn't exactly a walk in the park! As it turns out, I probably should've gotten the Series 1 radios, as they use the simpler 802.15.4 protocol. Series 2 uses the Zigbee protocol, which allows for more advanced network topologies but is also harder to configure. In the end, though, it wasn't really that difficult, and I appreciate the greater flexibility, so I'm happy with my purchase. Because of my difficulty in the first few steps, I'm taking it upon myself to write a little about my lessons learned in getting these going.


Once I soldered headers onto the Explorer boards, my next task was to hook them up to USB-to-serial adapters to program them. There are Explorer boards with built-in serial adapters, but they're more expensive, and I already have a separate adapter. However, this meant I could only hook up one radio at a time, and it turned out to be very handy to hook up both. I eventually found a blog post that indirectly guided me to using my Arduino's USB-to-serial chip as a sort of pass-through. If you hook a jumper wire from GND to the RESET pin on the Arduino board, that effectively disables the ATmega chip so it doesn't interfere with the UART pins. (You can also remove the chip from the socket, but the jumper is easier.) After that you can hook up +5V, GND, and the two signal wires from the Arduino to the Explorer. In this case, the Explorer is acting as the UART's device, so you have to hook up RX to DIN and TX to DOUT in a straight-through configuration, instead of crossed-over as in normal device-to-device serial.


In that configuration, I was able to start two copies of the X-CTU configuration program (v. 5.2.7.5 under the General Diagnostics, Utilities and MIBs tab as I write), and start setting up the radios' software. On the opening tab, PC Settings, I set one copy of the program to each COM port (4 and 6 in my case) and made sure I could Test/Query both radios. My radios came set to 9600 baud.


After that, I started poking around in the Modem Configuration tab. By clicking Read, I was able to find out that both of my radios came loaded with the Zigbee Router AT firmware. If you read the Product Manual, you'll learn that a ZigBee network must have exactly one radio with Coordinator firmware, even for simple point-to-point connections. With that in mind, I changed one of the radios to a Coordinator by selecting Zigbee Coordinator AT in the Function Set dropdown box and then writing the new firmware. On both modules, I set the same PAN ID. In Zigbee (as opposed to plain 802.15.4), the 16-bit network address is either implicitly 0 for the coordinator, or randomly set for all other nodes, so those fields aren't editable. Since I'm starting with a point-to-point network, for both modules I set the destination address (DH and DL) to the serial number (SH and SL) of the other device.


I could be forgetting something, but that should be enough to get point-to-point communication going. From the Terminal tab on both windows, I'm able to type on one side and watch the other side receive. In the photo, blue is what I typed, and red is what was received by the other radio.


Once all of that was established, I decided to hook up the radio to the Arduino "for real." To switch the Arduino from a pass-through to a microcontroller, I just removed the wire from the RESET pin, and swapped the signal wires so that I had RX to DOUT and TX to DIN (as in normal, crossed-over serial communication). After that, I loaded the Arduino with a simple program using the Serial object. The sketch just listens to serial in, and when it gets a byte, increments it and writes it back out.


As you can see, the module attached to the computer (and visible through the X-CTU window) sends a letter (in blue). The radio attached to the Arduino gets the letter and passes it to the Arduino. The Arduino increments the letter, passes it back to the attached radio, which sends the new letter to the computer's radio and back to the terminal (in red).

That's the extent of my exploration so far. Nothing too exciting, but I'm quite pleased that I'm this far. Next up, maybe I'll control a servo on one Arduino using a potentiometer on another. We'll see!