Electronic – arduino – No luck trying to make two XBees communicate in API mode with arduino XBee library.

arduinoxbee

I have been trying to make two XBees communicate for a long time. I have checked everything and at last I come here for some help from the experts or who have already stumbled upon the same problem as mine. I am using series 2 XBees.

I am using this library.

It's a pretty basic circuit.

The coordinator has a button and the router has LED. When I press button on coordinator, I send two bytes as payload to the router. The router checks if packet was received. If it was, it checks if the correct byte is received. If so, it turns on the LED. But, no matter what I do, I am not able to turn on the LED. I appreciate your help.

The code for the coordinator (sender):

#include <XBee.h>

// create the XBee object
XBee xbee = XBee();

uint8_t payload[] = { 0, 0 };

// SH + SL Address of receiving XBee
XBeeAddress64 addr64 = XBeeAddress64(0x0013a200, 0x408698be);
ZBTxRequest zbTx = ZBTxRequest(addr64, payload, sizeof(payload));
ZBTxStatusResponse txStatus = ZBTxStatusResponse();

int pin5 = 0;

void setup() {
  Serial.begin(9600);
  xbee.setSerial(Serial);
}

void loop() {   
  payload[0] = 0x34;
  payload[1] = 0x76;

  if (digitalRead(7) == HIGH){
    xbee.send(zbTx);
    delay(1000);
  }
}

And the code for router (receiver) is:

#include <XBee.h>


XBee xbee = XBee();
XBeeResponse response = XBeeResponse();
// create reusable response objects for responses we expect to handle 
ZBRxResponse rx = ZBRxResponse();
ModemStatusResponse msr = ModemStatusResponse();


void setup() {
  // start serial
  Serial.begin(9600);
  xbee.begin(Serial);
}

// continuously reads packets, looking for ZB Receive or Modem Status
void loop() {
    xbee.readPacket();
    if (xbee.getResponse().isAvailable()) {
      // got something
      if (xbee.getResponse().getApiId() == ZB_RX_RESPONSE) {
        // got a zb rx packet
        // now fill our zb rx class
        xbee.getResponse().getZBRxResponse(rx);
        // I check both bytes (or)
        if (rx.getData(0) == 0x34 || rx.getData(0) == 0x76 ) {
          digitalWrite(8, HIGH); 
        }
      } 
   }
}

Best Answer

I fixed the problem. If you look at the code, you'll notice that I had not specified pinmode of the input and output pins.

The fix is to include the following lines in the router and coordinator code, respectively:

  • The line below is missing in router code:

    pinMode(8,  OUTPUT);
    
  • And the line below is missing in coordinator code:

    pinMode(7,  INPUT);