Arduino XBee S1 API frame read problem

Hi, I have 2 series 1 xbees. 1 is setup as a remote sensor connected to an ADXL335 accelerometer (pins 0,1 & 2). The other as a Coordinator connected to an arduino uno via a funduino XBee shield.

I have setup the XBees and am receiving data to my serial port (I have connected the Coordinator XBee to my PC via a USB breakout and opened a connection on XCTU. This shows that i am receiving consistent frames every 20ms)

When i use the follwing code uploaded to my arduino (At the top i have written my XBee settings)

/*XBee Setups
 *** Configuration ***

SENDER: (REMOTE SENSOR RADIO)
 ATCH -> C
 ATID -> 3332
 ATDL -> 1
 ATMY -> 2
 ATNI -> SENSOR
 ATBD -> 3 (9600)
 ATAP -> 2 (API enabled w/PPP
 ATD0 -> 2 (ADC)
 ATD1 -> 2 (ADC)
 ATD2 -> 2  (ADC)
 ATIR -> 14 (20ms)

CORDINATOR:
 ATCH -> C
 ATID -> 3332
 ATDL -> 2
 ATMY -> 1
 ATNI -> COORDINATOR
 ATBD -> 3 (9600)
 ATAP -> 2 (API enabled w/PPP
 
*/


void setup() 
{ 
  Serial.begin(9600); //Begin serial connection
} 
void loop() 
{ 
  
  if (Serial.available()>=17){ //Check for a full API packet
    if (Serial.read() == 0x7E){//If start delimiter is read  
      for(int i = 0; i < 17; i++){ //For the rest of the frame
        Serial.print(Serial.read(),DEC); //Print each Byte read
        Serial.print(","); //Print a comma between each 
        }
        Serial.println(); //Println so the next packet starts on the next line
    }
  } 
}

I get what i believe are correct frames from which I can retrieve my analogue data interspersed with random frames

0,14,131,0,2,51,0,1,14,0,1,243,1,252,2,123,202

0,14,131,0,2,51,0,1,14,0,1,243,1,252,2,123,202

0,50,0,124,202,126,0,2,0,14,243,252,124,126,0,131,0

50,0,1,1,2,202,126,14,0,50,14,1,1,203,126,131,2

The top two shown above are what i expected, but then i will get a whole bunch of frames that do not look like what i’m expecting.

Any help in trying to understand why i’m getting this seemingly inconsistent output would be greatly appreciated. I have tried to give all the information I have about my setup, but if you feel I have missed something please let me know and I will provide anything you require.

You are getting inconsistent readings because you expect an API frame to be of a certain length. Maybe what you received isn’t of the length that you expect and the frames get sliced up and out of sync with your program. 0x7E is 126 in decimal. I can see several instances of this number at the end of the later packets. I’m betting on de-synched reading of the API packets. Assumptions are the mother of all mess-ups.

To make sense of this you should look at specific bytes following the 0x7E. What follows is the actual length of the packet and adres information. Read the manual of the XBee series 1 to see how the API protocol is structured. And pick the data apart in your code appropriately. There are libraries available to do that, but it doesn’t hurt knowing how to do it yourself.

XCTU (the modern versions) has an API packet interpreter if you enter the Hexadecimal values in it. You can use that to check what you receive.

Use an Ardino or Raspberry Pi/Python XBee library. All work is done for you.

API mode frames vary in length and format. It is a lot to learn to do API coding rather than using a library.