I am using xbee series 2.I am trying to receive data from router AT node to coordinator API monitoring on Arduino serial port and sending back output on the same node. but both of them are not working together. I have used the following Arduino code:
int readValue=0;
void setup() {
Serial.begin(9600);
pinMode(13,OUTPUT);
}
void loop() {
if(Serial.available()>21)
{
if(Serial.read()==0x7E)
{
for(int i=0;i<19;i++)
{
byte discardbyte = Serial.read();
}
readValue = Serial.read();
Serial.print(readValue);
if(readValue==0)
{
digitalWrite(13,HIGH);
delay(10);
setRemoteState(0x5);
}
else if(readValue==2)
{
digitalWrite(13,LOW);
delay(10);
setRemoteState(0x4);
}
}
}
}
void setRemoteState(char value)
{
Serial.write(0x7E); // Sync up the start byte
Serial.write((byte)0x0); // Length MSB (always 0)
Serial.write(0x10); // Length LSB
Serial.write(0x17); // 0x17 is the frame ID for sending an AT command
Serial.write((byte)0x0); // Frame ID (no reply needed)
Serial.write((byte)0x0); // Send the 64 bit destination address
Serial.write((byte)0x0); // (Sending 0x000000000000FFFF (broadcast))
Serial.write((byte)0x0);
Serial.write((byte)0x0);
Serial.write((byte)0x0);
Serial.write((byte)0x0);
Serial.write(0xFF);
Serial.write(0xFF);
Serial.write(0xFF); // Destination Network
Serial.write(0xFE); // (Set to 0xFFFE if unknown)
Serial.write(0x02);
Serial.write(‘D’);
Serial.write(‘2’);
Serial.write(value);
long sum=0x17 + 0xFF + 0xFF + 0xFF + 0xFE + 0x02 + ‘D’ + ‘2’ + value;
Serial.write(0xFF- (sum & 0xFF)) ;
}