XBee API tutorials for Arduino?

I'm working on a project where I need multiple ultrasonic sensors to communicate wirelessly to my computer and visually output the data in processing.

Right now I have three Arduino Fios with XBee wm2s and Maxbotix ultrasonic sensors

I have the XBee explorer from Sparkfun with my coordinator XBee plugged in to my computer.

I've been following the book Building Wireless Sensor Networks by Robert Faludi, and I've gotten to the point where I'm building a simple sensor network with my XBees in API mode, but his example does not use the Arduino, it connects the sensor directly to a pin on the XBee.

I know that there's an API library for Arduino, and an API library for Processing, but I'm a bit of a beginner, and I need a little hand holding through the setup process.

Basically what I need is the API to identify which XBee the data is coming from, and a way to confirm that the data is less than three characters in length.

Does anyone know of a tutorial or some kind of resource or example code that I can look at that would help me get these things talking?


Basically what I need is the API to identify which XBee the data is coming from, and a way to confirm that the data is less than three characters in length.

Which API (Application Programming Interface) are you referring to? The XBee library manages the first part (which XBee is talking). The packet size is part of the packet. Your code needs to check the packet size, not the API/XBee library.

Okay, I guess the question would be; how do I get processing to differentiate the different XBees and check the packet size.

Assume you have S2 XBees?

Get Andrew Rapp’s XBee library, http://code.google.com/p/xbee-arduino/

Here is some code to show the very basics, not meant to do anything useful, but just to show how to use some of the classes and get you barking up the right trees.

#include <XBee.h>

XBee xbee = XBee();                    //instantiate the XBee object
ZBRxResponse zbRX = ZBRxResponse();    //XBee RX packet
byte dataLen;
XBeeAddress64 senderAddr;

void setup(void)
    xbee.begin(115200);                  //also does Serial.begin()

void loop(void)
    if (xbee.getResponse().isAvailable()) {
        switch (xbee.getResponse().getApiId()) {             //what kind of packet was received
            case ZB_RX_RESPONSE:                             //rx data packet
                xbee.getResponse().getZBRxResponse(zbRX);    //get the received data
                switch (zbRX.getOption()) {
                    case ZB_PACKET_ACKNOWLEDGED:
                        dataLen = zbRX.getDataLength();
                        senderAddr = zbRX.getRemoteAddress64();

Thanks Jack- I guess what I’m having trouble with is the different devices communicating with each other.

I think I’ll be able to figure out how to use Andrew Rapp’s XBee library, I think a major road block is getting that information in a useful form on the other side. I’m using Processing, and I have been able to get useful data readouts from the serial port, I just don’t know how to get them talking to each other- if that makes sense.

The various sensors just need to communicate to the one XBee attached to the computer, and not with each other? Don’t know much about Processing, but if there’s an API library for it, hopefully things can be made to talk.

When the sensor nodes need to send data, I do something like this:

#include <XBee.h>                //http://code.google.com/p/xbee-arduino/


XBee xbee = XBee();                    //instantiate the XBee object
union {byte b; char c;} xbeePayload[XBEE_PAYLOAD_LEN];
ZBTxRequest zbTX = ZBTxRequest();
XBeeAddress64 coordAddr = XBeeAddress64(0x0, 0x0);

void setup(void)

void loop(void)

void buildDataPayload()
    //whatever is needed to build the payload

Thanks Jack, that gives me a lot to build on. I appreciate it!

If there is anyone who has used the Processing API, please let me know- I've hit a bit of a wall here.

Are you talking about the library that Rob Faludi created for processing? If so, he has a lot of info in his book and there is quite a bit online as well. A little google search using his name and processing will show this stuff.

Thanks for the heads up!

Hi Jack Christensen,

Really appreciate if you can help with this case:

  • I have 2 Xbee Series 2
  • I used X-CTU to set one as COORDINATOR API and the other ROUTER API
  • Using two Xbee Explorers I could send a packet from ROUTER to COORDINATOR using
  • 64-bit dest. address: 0x0
  • 16-bit dest. address: 0x0
  • payload: ABCD
  • packet: 7E 00 12 10 01 00 00 00 00 00 00 00 00 00 00 00 00 41 42 43 44 E4
  • Then I removed the ROUTER from my laptop and connected it to Arduino Leonardo board
  • I used your program at post #5 in which I changed 16-bit dest. address to 0x0 and I set 'ABCD' as the payload
  • The COORDINATOR did not received anything
  • Then I revised some line in Xbee.cpp. In particularly, in the function void XBee::sendByte(uint8_t b, bool escape) I substituted
    write(b ^ 0x20);

Serial.print(b ^ 0x20,HEX);


to see the packet sent. Surprisingly, this was what I got
7E 01 21 01 00 00 00 00 00 00 41 42 43 44 E4
which was different from the correct one.

Do you have any suggestion for me?

Thank you very much.