XBee Communication API Mode

Hello everyone,

I have 2 Arduino UNO, 2 XBee S2Pro modules and the great desire to communicate with each other!

In Transparent Mode (AT) they communicate perfectly.

When I load (through XCTU) the respective Firmware Coordinator and Router in API Mode, no communication is made.

I installed the powerful AndrewRapp library: Xbee-arduino And I tested many of his examples of communication.

[u]Simply the modules do not interact![/u]

I would like to use the API to perfectly manage packets with both correct send response and checksum.

Who has direct experience in this regard?

Thank you

Up

You have configured the XBees, but told us NOTHING about how you configured them.

You have some code running on one or more Arduinos with XBees connected. You have shown no code.

You have something between the XBees and the Arduino, but failed to tell us what that is, or how it is configured.

You really expect help?

I’m very sorry for the little information I provided.

  1. XBee XBP24-ZB Coordinator API: ID 444, AP 2, SP 1F4, DH 0, DL FFFF, BD 9600
    XBee XBP24-ZB Router API: ID 444, AP 2, SP 1F4, DH 0,DL FFFF, JV 1, BD 9600

  2. Transmitter:

#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, 0x403e0f30);
ZBTxRequest zbTx = ZBTxRequest(addr64, payload, sizeof(payload));
ZBTxStatusResponse txStatus = ZBTxStatusResponse();

int pin5 = 0;

int statusLed = 12;
int errorLed = 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);

  Serial.begin(9600);
  xbee.setSerial(Serial);
}

void loop() {   
  // break down 10-bit reading into two bytes and place in payload
  pin5 = analogRead(5);
  payload[0] = pin5 >> 8 & 0xff;
  payload[1] = pin5 & 0xff;

  xbee.send(zbTx);

  // flash TX indicator
  flashLed(statusLed, 1, 100);

  // after sending a tx request, we expect a status response
  // wait up to half second for the status response
  if (xbee.readPacket(500)) {
    // got a response!

    // should be a znet tx status            	
    if (xbee.getResponse().getApiId() == ZB_TX_STATUS_RESPONSE) {
      xbee.getResponse().getZBTxStatusResponse(txStatus);

      // get the delivery status, the fifth byte
      if (txStatus.getDeliveryStatus() == SUCCESS) {
        // success.  time to celebrate
        flashLed(statusLed, 5, 50);
      } else {
        // the remote XBee did not receive our packet. is it powered on?
        flashLed(errorLed, 3, 500);
      }
    }
  } else if (xbee.getResponse().isError()) {
    //nss.print("Error reading packet.  Error code: ");  
    //nss.println(xbee.getResponse().getErrorCode());
  } else {
    // local XBee did not provide a timely TX Status Response -- should not happen
    flashLed(errorLed, 2, 50);
  }

  delay(1000);
}

Receiver:

#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 = 11;
int errorLed = 12;
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);
  
  // start serial
  Serial.begin(9600);
  xbee.begin(Serial);
  
  flashLed(statusLed, 3, 50);
}

// 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);
            
        if (rx.getOption() == ZB_PACKET_ACKNOWLEDGED) {
            // the sender got an ACK
            flashLed(statusLed, 10, 10);
        } else {
            // we got it (obviously) but sender didn't get an ACK
            flashLed(errorLed, 2, 20);
        }
        // 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
        
        if (msr.getStatus() == ASSOCIATED) {
          // yay this is great.  flash led
          flashLed(statusLed, 10, 10);
        } else if (msr.getStatus() == DISASSOCIATED) {
          // this is awful.. flash led to show our discontent
          flashLed(errorLed, 10, 10);
        } else {
          // another status
          flashLed(statusLed, 5, 10);
        }
      } else {
      	// not something we were expecting
        flashLed(errorLed, 1, 25);    
      }
    } else if (xbee.getResponse().isError()) {
      //nss.print("Error reading packet.  Error code: ");  
      //nss.println(xbee.getResponse().getErrorCode());
    }
}
  1. I use the XBee Shield to connect Arduinos with XBees

  2. Transmitter has Pot to A5, Receiver has 3 LEDs to D11, D12, D13

Thank you

) XBee XBP24-ZB Coordinator API: ID 444, AP 2, SP 1F4, DH 0, DL FFFF, BD 9600 XBee XBP24-ZB Router API: ID 444, AP 2, SP 1F4, DH 0,DL FFFF, JV 1, BD 9600

You told both XBees to talk to the same XBee? That's what DH and DL are for.

What are SH and SL for the two XBees?

How are the XBees connected to the Arduinos? You really should get them off the hardware serial ports, so you can use the hardware serial port for debugging. Flashing an LED is a really tedious way of debugging.

Thanks a lot for reply.

I set DH 0 and DL FFFF because I would like to permit the communication between all XBees connected to the same PAN ID. Only two now, but four just I will fix the communication problem.

The SH and SL for Coordinator are: 13A200, 408B69F9 And for Router are: 13A200, 40AADF0E

The XBees are now connected with the XBee Shield to the Arduinos:

I tried the connection with simple adapter too, like this:

(To use the SoftwareSerial for debugging, as you suggest) The result is always: The transmitter send data, but the receiver does not receive them.

The SH and SL for Coordinator are: 13A200, 408B69F9 And for Router are: 13A200, 40AADF0E

XBeeAddress64 addr64 = XBeeAddress64(0x0013a200, 0x403e0f30);

So, which XBee are you sending data to?

I tried all the possible combinations:

  • XBeeAddress64 addr64 = XBeeAddress64(0x00000000, 0x0000ffff);
  • XBeeAddress64 addr64 = XBeeAddress64(0x0013a200, 0x40aadf0e);
  • XBeeAddress64 addr64 = XBeeAddress64(0x0013a200, 0x408b69f9);

Hi Zamundo,

I have a similar problem right (Using same shiled as you do but different XBee (Pro s1). Did you ever figure out what was wrong??