Hi,
I'm trying to use the xbee library to make a communication between some ZB S2B modules.
I configured them in API mode (2, with escape character), at 57600 bps, and let them to associate themselves. It seems like all is OK. But I can't see any transmitting message between them, even when I use the library's examples.
I modified these examples, only to print message in serial, to try watching more info. I put them below.
Could you help me, please??
#include <XBee.h>
/*
This example is for Series 2 XBee
Sends a ZB TX request with the value of analogRead(pin5) and checks the status response for success
*/
// create the XBee object
XBee xbee = XBee();
uint8_t payload[] = {
0, 0 };
// SH + SL Address of receiving XBee
XBeeAddress64 addr64 = XBeeAddress64(0x0013a200, 0x408615d9); //@MAC coordinator
ZBTxRequest zbTx = ZBTxRequest(addr64, payload, sizeof(payload));
ZBTxStatusResponse txStatus = ZBTxStatusResponse();
int pin5 = 0;
int statusLed = 13;
int errorLed = 13;
unsigned long start = millis();
//___________________________________________________________________________________________
void flashLed(int pin, int times, int wait) {
for (int i = 0; i < times; i++) {
digitalWrite(pin, HIGH);
delay(wait);
digitalWrite(pin, LOW);
if (i + 1 < times) {
delay(wait);
}
}
}
//___________________________________________________________________________________________
void setup() {
pinMode(statusLed, OUTPUT);
pinMode(errorLed, OUTPUT);
Serial.begin(57600);
Serial.println("\n\t... XBee - ZB ...\n");
xbee.begin(57600);
flashLed(statusLed, 1, 500);
}
//___________________________________________________________________________________________
void loop() {
Serial.flush();
// break down 10-bit reading into two bytes and place in payload
pin5 = 135; //analogRead(5);
Serial.print("pin5 value: [");
Serial.print(pin5);
Serial.println("]");
payload[0] = pin5 >> 8 & 0xff;
payload[1] = pin5 & 0xff;
xbee.send(zbTx);
Serial.println("Sending TX packets.");
// flash TX indicator
flashLed(statusLed, 1, 100);
// 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!
Serial.println("\tHaving Response.");
// should be a znet tx status
if (xbee.getResponse().getApiId() == ZB_TX_STATUS_RESPONSE) {
xbee.getResponse().getZBTxStatusResponse(txStatus);
Serial.println("\t\tTX STATUS");
// get the delivery status, the fifth byte
if (txStatus.getDeliveryStatus() == SUCCESS) {
// success. time to celebrate
flashLed(statusLed, 5, 500);
Serial.println("\t\t\tSUCCESS");
}
else {
// the remote XBee did not receive our packet. is it powered on?
flashLed(errorLed, 3, 500);
Serial.println("\t\t\tERROR");
}
}
}
else if (xbee.getResponse().isError()) {
//nss.print("Error reading packet. Error code: ");
//nss.println(xbee.getResponse().getErrorCode());
Serial.println("\tWrong Response.\n");
}
else {
// local XBee did not provide a timely TX Status Response -- should not happen
flashLed(errorLed, 2, 500);
Serial.println("\tNo Response.");
}
Serial.flush();
delay(5000);
}
#include <XBee.h>
/*
This example is for Series 2 XBee
Receives a ZB RX packet and sets a PWM value based on packet data.
Error led is flashed if an unexpected packet is received
*/
XBee xbee = XBee();
XBeeResponse response = XBeeResponse();
// create reusable response objects for responses we expect to handle
ZBRxResponse rx = ZBRxResponse();
ModemStatusResponse msr = ModemStatusResponse();
int statusLed = 13;
int errorLed = 13;
int dataLed = 13;
//___________________________________________________________________________________________
void flashLed(int pin, int times, int wait) {
for (int i = 0; i < times; i++) {
digitalWrite(pin, HIGH);
delay(wait);
digitalWrite(pin, LOW);
if (i + 1 < times) {
delay(wait);
}
}
}
//___________________________________________________________________________________________
void setup() {
pinMode(statusLed, OUTPUT);
pinMode(errorLed, OUTPUT);
pinMode(dataLed, OUTPUT);
Serial.begin(57600);
Serial.println("\n\t... XBee - ZB ...\n");
// start serial
xbee.begin(57600);
flashLed(statusLed, 1, 500);
}
//___________________________________________________________________________________________
// continuously reads packets, looking for ZB Receive or Modem Status
void loop() {
xbee.readPacket();
Serial.println("Checking RX packets.");
if (xbee.getResponse().isAvailable()) {
// got something
Serial.println("There's some RX.");
if (xbee.getResponse().getApiId() == ZB_RX_RESPONSE) {
// got a zb rx packet
Serial.println("\tThere's RX packet.");
// now fill our zb rx class
xbee.getResponse().getZBRxResponse(rx);
if (rx.getOption() == ZB_PACKET_ACKNOWLEDGED) {
// the sender got an ACK
flashLed(statusLed, 10, 100);
Serial.println("\t\tACK");
Serial.print("\n\tData: ");
Serial.println(rx.getData(0));
}
else {
// we got it (obviously) but sender didn't get an ACK
flashLed(errorLed, 2, 200);
Serial.println("\t\tACK ERROR");
}
// set dataLed PWM to value of the first byte in the data
// analogWrite(dataLed, rx.getData(0));
}
else if (xbee.getResponse().getApiId() == MODEM_STATUS_RESPONSE) {
xbee.getResponse().getModemStatusResponse(msr);
// the local XBee sends this response on certain events, like association/dissociation
Serial.println("\tThere's Status Rx.");
if (msr.getStatus() == ASSOCIATED) {
// yay this is great. flash led
flashLed(statusLed, 10, 100);
Serial.println("\t\tASSOCIATED\n");
}
else if (msr.getStatus() == DISASSOCIATED) {
// this is awful.. flash led to show our discontent
flashLed(errorLed, 10, 100);
Serial.println("\t\tDISASSOCIATED\n");
}
else {
// another status
flashLed(statusLed, 5, 100);
Serial.println("\t\tOTHER STATUS\n");
}
}
else {
// not something we were expecting
flashLed(errorLed, 1, 25);
Serial.println("\tUnexpected Response\n");
}
}
else if (xbee.getResponse().isError()) {
//nss.print("Error reading packet. Error code: ");
//nss.println(xbee.getResponse().getErrorCode());
Serial.println("There's no RX packets.\n");
}
delay(5000);
}