Electronic – arduino – The appropriate code (Arduino) of Xbee

arduinocommunicationradioxbee

Can you tell me what is the appropriate code to send two variables (x,y) from an Xbee to another Xbee (series 1). I configured them and I can't write a code (because I don't know how to receive information and send them via Xbee and Arduino). Xbee is connected to the shield to Arduino UNO in 1st side, on the other side Xbee is connected to another shield to Arduino Mega.

I have configured Xbees yet and I tried to send commands from PC (Xbee) to Arduino (Xbee) and it worked.

Would someone help me by providing me with an example of sending variables (x,y) from UNO to MEGA (I use Arduino program which contain C/C++), or explain how to do it.

Best Answer

I'd advise you to start by configuring the XBees in AT "Serial Wire" mode. This turns the XBees into a simple serial connection between the Arduinos. From that point on, you use Serial.print()/Serial.write() and Serial.read() just as if the Arduinos were connected directly to each other by wires.

Let's name the XBees XBee_uno and XBee_mega for the following steps. You have most probably completed the first three already, but I'm writing them in full to help others who may have this question.

  • Use X-CTU to set both XBees to AT mode (this may necessitate a new firmware or function set, though Series 1 XBees I think only need the setting of one parameter )

  • Set both XBees to use the same PAN ID; set one to be coordinator and the other to be a router/end device. Again, you may need a firmware/function set update for this. It doesn't matter which one is the coordinator, as long as one is the coordinator and the other is a router/end device.

  • Set the serial speed of both XBees to a known value, e.g. 9600bps, no parity, 8 data 1 stop bits

Now set them so that they send to each other. This completes the "Serial Wire" configuration.

  • From X-CTU read the SH and SL of each (i.e. the serial number high/serial number low)

  • For XBee_uno, set DH and DL to the value of the SH and SL respectively from the XBee_mega

  • For XBee_mega, set DH and DL to the value of the SH and SL respectively from the XBee_uno

  • Save the settings to the XBees.

At this point the XBees are configured to function as a virtual wire-pair between the serial ports of the Arduinos.

Sample code:

//on the sender side
void setup() {
  Serial.begin(9600);//this has to match the serial speed you configured for the respective XBee
}
void loop() {
  Serial.write(20); //or the value you want to send   
}

//on the receiver side
int received;
void setup() {
  Serial.begin(9600);//this has to match the serial speed you configured for the respective XBee
}
void loop() {
  if (Serial.available()) {
    received=Serial.read();
  }
}

As an alternative for older XBees, you could read the MY address from each XBee and then set as follows:

  • For XBee_uno: set DH=0, DL = the MY from XBee_mega
  • For XBee_mega: set DH=0, DL = the MY from XBee_uno

This works with both Series 1 and Series 2 XBees, but has the disadvantage that the MY addresses may change upon power-cycling the XBees. The previous method uses the unique serial numbers from the XBees (SH/SL), so it is not affected.

Shameless plug: Should you wish to make use of other advanced features from the XBees, like broadcasting or using more than two XBees, you will need to use API mode instead of AT. I had to fight a bit with it, so I collected the tools, articles, and tips that I found useful: http://erion.elmasllari.com/2012/06/in-the-trenches-with-xbee-resources-and-tips/