I'm having trouble getting my Xbee (Series 1) to communicate. They worked fine when I was using xbee.h library v0.4, but there was a change in v0.5 that's causing me problems. The v0.5 change has to do with serial setup, here's the release note for it.
Originally I started started xbee like this:
Serial.begin(9600);
xbee.begin(9600);
With version 0.5, I'm supposed to do this (I think):
Serial.begin(9600);
xbee.setSerial(Serial);
I'm not using soft serial, just hard serial.
Here's the xbee library diff comparison for the changes from 0.4 to 0.5: xbee.h, xbee.cpp
Below are the Rx and Tx sketches. Basically the two xbees are not able to communicate. The error from the transmitter sketch is: "XBee did not provide a timely Tx Status Response"
Receiver sketch:
#include <XBee.h>
#define PRINT_DEBUG // comment out if you don't want anything to go to serial monitor
XBee xbee;
XBeeResponse response;
// create reusable response objects for responses we expect to handle
Rx16Response rx16;
void setup()
{
Serial.begin(9600);
#ifdef PRINT_DEBUG
Serial.println("Xbee Rx/Coordinator setup()");
#endif
xbee.setSerial(Serial);
}
void loop()
{
uint8_t option;
uint8_t signalStrength;
uint8_t dataLength;
static int16_t RxData[3]; // Array to hold data received
uint16_t Tx_Id; // Address of transmitter (MY ID)
xbee.readPacket();
if (xbee.getResponse().isAvailable())
{
// got something
#ifdef PRINT_DEBUG
Serial.println("Got Something:");
#endif
if (xbee.getResponse().getApiId() == RX_16_RESPONSE )
{
// got a Rx packet
xbee.getResponse().getRx16Response(rx16);
option = rx16.getOption();
dataLength = rx16.getDataLength();
Tx_Id = rx16.getRemoteAddress16();
signalStrength = rx16.getRssi();
for(int i=0; i < dataLength; i=i+2)
{
RxData[i/2] = rx16.getData(i) << 8;
RxData[i/2] |= rx16.getData(i+1);
#ifdef PRINT_DEBUG
// Print data received (16-bit integers that have been divided into 2 bytes)
Serial.println(RxData[i/2]);
#endif
}
#ifdef PRINT_DEBUG
Serial.print("Transmitter MY address: ");
Serial.println(rx16.getRemoteAddress16(), HEX);
Serial.print("Signal strength = ");
Serial.println(signalStrength);
Serial.print("Data Length = ");
Serial.print(dataLength);
Serial.println(" bytes");
#endif
}
else
{
// not something we were expecting
}
}
else if (xbee.getResponse().isError())
{ // Got something, but not a packet
#ifdef PRINT_DEBUG
Serial.print("Error reading packet. Error code: ");
Serial.println(xbee.getResponse().getErrorCode());
#endif
} // End Got Something
else
{
// xbee not available and no error
#ifdef PRINT_DEBUG
Serial.print("isAvailable(): ");
Serial.println(xbee.getResponse().isAvailable());
Serial.print("Error reading packet. Error code: ");
Serial.println(xbee.getResponse().getErrorCode());
#endif
}
delay(1000);
}
Transmitter sketch:
#include <XBee.h>
#define PRINT_DEBUG // comment out if you don't want anything to go to serial monitor
#define MY_ADDR_RX 0x250
#define NUM_DATA_PTS 3
// allocate array to hold bytes to send to other xbee. Size is 2x the number if integers being sent
uint8_t payload[NUM_DATA_PTS * 2];
XBee xbee;
// 16-bit addressing: Enter address of remote XBee, typically the coordinator
Tx16Request tx = Tx16Request(MY_ADDR_RX, payload, sizeof(payload));
TxStatusResponse txStatus;
void setup()
{
Serial.begin(9600);
#ifdef PRINT_DEBUG
Serial.println("Xbee Tx/End Node - setup()");
#endif
xbee.setSerial(Serial);
}
void loop()
{
int16_t xbeeData[NUM_DATA_PTS]; // Array to hold integers that will be sent to other xbee
// Test data to send to other xbee
xbeeData[0] = 1234;
xbeeData[1] = 5678;
xbeeData[2] = -500;
// break down integers into two bytes and place in payload
for(int i=0; i<NUM_DATA_PTS; i++)
{
payload[i*2] = xbeeData[i] >> 8 & 0xff; // High byte - shift bits 8 places, 0xff masks off the upper 8 bits
payload[(i*2)+1] = xbeeData[i] & 0xff; // Low byte, just mask off the upper 8 bits
#ifdef PRINT_DEBUG
Serial.print("Data = ");
Serial.print(xbeeData[i]);
Serial.print(", payload high byte = ");
Serial.print(payload[i*2]);
Serial.print(", payload low byte = ");
Serial.println(payload[(i*2)+1]);
#endif
}
xbee.send(tx);
// after sending a tx request, we expect a status response
// wait up to 5 seconds for the status response
if (xbee.readPacket(5000))
{
// got a response!
#ifdef PRINT_DEBUG
Serial.println("\nGot a response from receiver");
#endif
// should be a znet tx status
if (xbee.getResponse().getApiId() == TX_STATUS_RESPONSE)
{
xbee.getResponse().getZBTxStatusResponse(txStatus);
// get the delivery status, 0 = OK, 1 = Error, 2 = Invalid Command, 3 = Invalid Parameter
if (txStatus.getStatus() == SUCCESS)
{
// success. time to celebrate
#ifdef PRINT_DEBUG
Serial.println("Tx Succeeded");
#endif
}
else
{
// the remote XBee did not receive our packet. is it powered on?
#ifdef PRINT_DEBUG
Serial.print("\nTx Failed, xbee status = ");
Serial.println(txStatus.getStatus());
#endif
}
}
}
else if (xbee.getResponse().isError())
{
#ifdef PRINT_DEBUG
Serial.print("\nError reading packet. Error code: ");
Serial.println(xbee.getResponse().getErrorCode());
#endif
}
else
{
// local XBee did not provide a timely TX Status Response. Radio is not configured properly or connected
#ifdef PRINT_DEBUG
Serial.println("\nXBee did not provide a timely Tx Status Response\n");
#endif
} // Finished waiting for XBee packet
delay(1000);
}