xbee api mode help!!

I have two xbees one configured as coordinator api and another configured as router api.

I have attached those two xbees with two arduinos and just to test communication between them i have a simple code

Code in arduino hooked to coordinator xbee

Description: This will simply check button press. Upon press it will send zigbee transmit request frame.

int BUTTON1 = 3;

void setup(){

pinMode(BUTTON1, INPUT);

Serial.begin(9600);

}

void loop(){

if (digitalRead(BUTTON1) == HIGH){

sendData();

}

}

void sendData(){

Serial.write(0x7E);

Serial.write(0x00);
Serial.write(0x16);

Serial.write(0x10);

Serial.write(0x01);

Serial.write(0x00);
Serial.write(0x13);
Serial.write(0xA2);
Serial.write(0x00);
Serial.write(0x40);
Serial.write(0x8C);
Serial.write(0xC6);
Serial.write(0xD4);

Serial.write(0x20);
Serial.write(0x01);

Serial.write(0x00);

Serial.write(0x00);

//data start
Serial.write(0x54);
Serial.write(0x78);
Serial.write(0x44);
Serial.write(0x61);
Serial.write(0x74);
Serial.write(0x61);
Serial.write(0x30);
Serial.write(0x41);

Serial.write(0x13);

delay(10);

}

Next is the code for arduino hooked to router xbee.

Description: This simply scans “0x54” byte and if it is there then lights up a led. As you can see coordinator sends this byte 0x54 along with other bytes.

int BELL = 5;

void setup(){

pinMode(BELL, OUTPUT);

Serial.begin(9600);

}

void loop(){

if(Serial.available() >= 25){

if(Serial.read() == 0x7E){

for(int i=0;i<24;i++){

if(Serial.read() == 0x54)
{

digitalWrite(BELL, HIGH);

}

}

}

}

}

I would like to add that when i turn on serial monitor when i press button the serial monitor displays correctly what data was passed also tx lights up. So my assumption is that the problem is in the router not getting the data rather than coordinator not sending the data. I have been trying to hunt this problem for couple of hours now with no success. Any help is highly appreciated!

I have attached those two xbees with two arduinos and just to test communication between them i have a simple code

Maybe you need to use rubber bands, rather than duct tape.

If you are really connecting the XBees to the Arduinos in a reasonable way, it would be good to describe what that is. Specifically, how the XBee gets data from/sends data to, the Arduino and what lights the device has.

The sender sends 26 bytes. The receiver waits for 25 bytes and then reads 25. Why? After the first packet, you're all out of sync.

Thank you Paul, I updated the code. Still no success!

Coordinator code

int BUTTON1 = 3;

void setup(){

pinMode(BUTTON1, INPUT);

Serial.begin(9600);
  
}

void loop(){ 
  
  
if (digitalRead(BUTTON1) == HIGH){
 
 
 sendData();
 
  
}


  
}



void sendData(){
  
  
  Serial.write(0x7E);
  
  //length
  Serial.write(0x00);
  Serial.write(0x16);
  
  
  
  Serial.write(0x10);
  
  Serial.write(0x01);
  
  
  Serial.write(0x00);
  Serial.write(0x13);
  Serial.write(0xA2);
  Serial.write(0x00);
  Serial.write(0x40);
  Serial.write(0x8C);
  Serial.write(0xC6);
  Serial.write(0xD4);
  
  
  Serial.write(0x20);
  Serial.write(0x01);
  
  
  Serial.write(0x00);
  
  
  Serial.write(0x00);
  
  
  Serial.write(0x54);
  Serial.write(0x78);
  Serial.write(0x44);
  Serial.write(0x61);
  Serial.write(0x74);
  Serial.write(0x61);
  Serial.write(0x30);
  Serial.write(0x41);
  
  
  
  long sum = 0x10 + 0x01 + 0x13 + 0xA2 + 0x40 + 0x8C + 0xC6 + 0xD4 + 0x20 + 0x01 + 0x54 + 0x78 + 0x44 + 0x61 + 0x74 + 0x61 + 0x30 + 0x41;
  
  
  Serial.write(0xFF - (sum&0xFF));
   
 
    
  
  delay(10);
  
  
  
  
}

Router Code

int LED = 5;

void setup(){

  pinMode(LED, OUTPUT);

  Serial.begin(9600);


}


void loop(){



  if(Serial.available() >= 26){
    
    
    
    if(Serial.read() == 0x7E){

      
      
      for(int i=0;i<16;i++){
       
       byte discard = Serial.read(); 
        
      }
      
      
      int val = Serial.read();
      
      
      if((val == 0x54) || (val == 0x41)){
       
          digitalWrite(LED, HIGH); 
         
      }
      

}


  }

}

What type of XBees are you using?

I think i am using this one.

http://wiki.groundlab.cc/doku.php?id=xbee2

if all you are going to do is Point-To-Point, then you dont really need to use API mode. You can just do Serial.print("a") and the results will go from Arduino 1 to Arduino 2 via XBees. Then you can write IF statements and check if the data coming from Serial is "a" then the button was pressed?

Thanks! LucidGold, i want to understand how API mode works.

Still no success!

The code you posted does something on each end. What it does you haven't described.

Also, please re-read reply # 1, and make an attempt to answer all the questions.

If you can, get the XBee off the hardware serial port so that you can use it for debugging.

i am using series 2 xbee. And the purpose is simple. The coordinator sends

Serial.write(0x54);
Serial.write(0x78);
Serial.write(0x44);
Serial.write(0x61);
Serial.write(0x74);
Serial.write(0x61);
Serial.write(0x30);
Serial.write(0x41);

when a button is pressed to the router xbee.

The router checks the first byte, if it is 0x54 then lights up the led.

Did you try to use XBee.h? This can solve the hedege of writing the "wrong" code. They also have great examples on using their library.

https://code.google.com/p/xbee-arduino/