my code
#include <XBee.h>
#include <SoftwareSerial.h>
#include <stdlib.h>
#include <stdio.h>
//Declare pins for software serial
byte ssRX = 8;
byte ssTX = 9;
//New object software serial
SoftwareSerial nss(ssRX,ssTX);
//create an Xbee object
XBee xbee = XBee();
//VARIABLES
byte atCmd[5];
byte atCmdValue[8];
byte atCmdValueIndex=0;
byte atCmdValueSize;
byte textInput=0;
byte textHex[20];
byte temp1;
byte dbCommand[]={'D','B'};
byte p0Command[]={'P','0'};
byte payload[]={'P','I'};
unsigned long duration[10];
//aT command TX and RX class
AtCommandRequest atRequest =0;
AtCommandResponse atResponse =AtCommandResponse();
//response
XBeeResponse response = XBeeResponse();
// create reusable response objects for responses we expect to handle
ZBRxResponse rx = ZBRxResponse();
ModemStatusResponse msr = ModemStatusResponse();
// SH + SL Address of receiving XBee
XBeeAddress64 addr64 = XBeeAddress64(0x0013a200, 0x403e0f30);
ZBTxRequest zbTx = ZBTxRequest(addr64, payload, sizeof(payload));
ZBTxStatusResponse txStatus = ZBTxStatusResponse();
//SETUP==================================================
void setup()
{
pinMode(12,INPUT);
Serial.begin(9600); //begin hardware serial
//start soft serial for XBee
nss.begin(9600);
xbee.begin(nss);
Serial.println("Loading...Please Wait...");
delay(3000);
Serial.println("Press X to configure commands, S to send commands, R to receive payload.");
}
//=====================================================
//==========================================================
void atSendCommand() {
//Serial.println("Sending command to XBee");
//send the command
xbee.send(atRequest);
//wait for AT response, timeout 5seconds
if(xbee.readPacket(5000)) {
//got response, so check whether is it an AT response
if(xbee.getResponse().getApiId() == AT_COMMAND_RESPONSE) {
xbee.getResponse().getAtCommandResponse(atResponse);
if(atResponse.isOk()) {
//Serial.print("Command [");
//Serial.write(atResponse.getCommand()[0]);
//Serial.write(atResponse.getCommand()[1]);
//Serial.println("] was successfull!");
if(atResponse.getValueLength() > 0) {
//Serial.print("Command value length is ");
//Serial.println(atResponse.getValueLength(),DEC);
Serial.print("Command value is : ");
for(int i = 0;i< atResponse.getValueLength();i++){
Serial.print(atResponse.getValue()[i],DEC);
Serial.print(" ");
}
Serial.println("");
}
}
else {
Serial.print("Command return error code: ");
Serial.println(atResponse.getStatus(),HEX);
}
}
else {
Serial.print("Expected AT response but got ");
Serial.print(xbee.getResponse().getApiId(), HEX);
}
} else {
//at command failed
if(xbee.getResponse().isError()){
Serial.print("Error readin packet. Error code: ");
Serial.println(xbee.getResponse().getErrorCode());
}
else {
Serial.print("NO RESPONSE FROM RADIO");
}
}
}
//=====================================================
//==========================================================
void atCommandInput()
{
Serial.println("Enter 2 letter command : ");
Serial.print("Letter 1 of command : ");
while(Serial.available()==0);
textInput=Serial.read();
Serial.flush();
atCmd[0]=textInput;
Serial.write(atCmd[0]);
Serial.println("");
Serial.print("Letter 2 of command : ");
while(Serial.available()==0);
textInput=Serial.read();
Serial.flush();
atCmd[1]=textInput;
Serial.write(atCmd[1]);
atRequest.setCommand(atCmd);
Serial.println("");
Serial.println("Query(Y) or Setting(N)?");
while(Serial.available()==0);
textInput=Serial.read();
Serial.flush();
if(textInput=='Y') {
atRequest.clearCommandValue();
memset(atCmdValue,'\0',sizeof(atCmdValue));
memset(textHex,'\0',sizeof(textHex));
Serial.println("Query Selected.");
}
else if(textInput=='N') {
atCmdValueIndex=0;
Serial.print("Enter value in hex : ");
while(textInput!='Z') {
if(Serial.available()>0) {
textInput=Serial.read();
Serial.flush();
if(textInput!='Z') {
textHex[atCmdValueIndex]=textInput;
Serial.print(textHex[atCmdValueIndex],HEX);
Serial.print(" ");
atCmdValueIndex++;
}
}
}
atCmdValueSize=strlen((char*)textHex);
hextobyte((char*)textHex,atCmdValue);
Serial.println("");
for(int i=0;i<(atCmdValueSize/2);i++) {
Serial.print(atCmdValue[i]);
Serial.print(" ");
}
Serial.println("");
atRequest.setCommandValue(atCmdValue);
atRequest.setCommandValueLength(sizeof(atCmdValue));
}
}
//=====================================================
//==========================================================