0

I have two XBees S2, one is configured as an API coordinator connected to Arduino and the other as an AT router.

The coordinator sends a frame to the router to switch on a LED. I want to switch off that LED with a button and the router send a message to the coordinator to tell him that it switch off the LED.

How can I do this?

I have one Arduino with the coordinator. I'm asking what I have to put in the script to make the coordinator receive the data.

Here is my code, how the coordinator sends the data to the router:

String inputString;
int led = 13;
void setup() {
pinMode(led, OUTPUT);
Seria.begin(9600);
}
void loop() {
while (Serial.available() ) {
// get the new byte:
delay(3);  
char inChar = Serial.read();
// add it to the inputString:
inputString += inChar;
}
if (inputString.length() >0) {
Serial.println(inputString);
if (inputString == "on"){
digitalWrite(led,HIGH);
setRemoteState(5);

}
inputString=""; 
}
}

void setRemoteState(char value){
Serial.write(0x7E); // start byte
Serial.write((byte)0x0);
Serial.write(0x10);
Serial.write(0x17);
Serial.write((byte)0x0);
// id of recipient or use 0xFFFF for broadcast
Serial.write((byte)00);
Serial.write((byte)00);
Serial.write((byte)00);
Serial.write((byte)00);
Serial.write((byte)00);
Serial.write((byte)00);
Serial.write(0xFF);
Serial.write(0xFF);
// 16 bit of reciepent 
Serial.write(0xFF);
Serial.write(0xFE);  

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) );
Serial.print(sum,HEX);
}

2 Answers 2

1

When you have one working as AT mode, it puts out whatever comes in, it is also called transparent mode.

You should first define which mode you want to use them and start coding.

If you choose AT, load the firmware for both and use Arduino to check the commands and execute tasks (supposing it is connected to the router).

If the task was done correctly, you send an okay message, otherwise, send error. This is a simple task in AT mode, just use Serial:

if (led == 1)
   Serial.println("ok");
else
   Serial.println("error");

If you choose API mode, it is a little bit different, where you have to mount the packet for sending data and put a message inside it. There are some libs out there you can achieve this.

On the other side, the coordinator will receive the message and interpret it.

If it is in AT mode, just read the serial. In API, you have to decode the packet and check the payload field where the data is located.

0

To change the state of digital pin 2,D2 set the "value" to 0x05 to switch it HIGH and set the "value" to 0x04 to switch the pin LOW

1
  • This is not a very good answer, it is not clear what you are talking about. You need to consider what you are trying to explain and then read it back from the perspective of someone who does not have the knowledge you have. I'd suggest reading some other peoples answers and get tips on how to explain what you mean before you answer anymore questions. I have no doubt you understand what you are talking about, its just that it doesn't come across very well. Feb 15, 2017 at 8:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.