Using XBEE AT Commands Using Arduino

Hello all,

I'm working on a localization project using XBEE's Series 2 and Arduino FIO. I tried this program on Arduino :

#define ledPin 13
byte pinState = 0;
int i = 0;
void setup() {
pinMode(ledPin, OUTPUT);
Serial.begin(9600);
}
void loop() {

Serial.print('+++');
delay(1000);

Serial.println("\n DL DH ");
delay(1000);
Serial.print('ATID');
delay(1000);

//char x = Serial.read() ;
//Serial.println(x);
//delay(1000);

// toggle an LED just so you see the thing's alive.
toggle(13);
delay(2000);
}
void toggle(int pinNum) {
// set the LED pin using the pinState variable:
digitalWrite(pinNum, pinState);
// if pinState = 0, set it to 1, and vice versa:
pinState = !pinState;
}

And while I'm supposed to get "OK" when sending "+++" and getting "11" when sending "ATID" , I get the following on the X-CTU :

.11051. DL DH
.18756
.11051. DL DH
.18756
.11051. DL DH
.18756
.11051. DL DH
.18756
.11051. DL DH
.18756
.11051. DL DH
.18756
.11051. DL DH
.18756
.11051. DL DH

Although the pan ID for the XBEE is 11.

Can anybody tell me what I'm doing wrong. Also, can anybody give me any sample code that uses AT commands on Arduino ?

thanks in advance

// Shouldn't there be a one second delay BEFORE the attention signal as well?
Serial.print('+++');
delay(1000);

Serial.print('ATID');  // That's a strange character constant.  You probably want the string constant "ATID". Note the double-quotes.

I don't understand some things. It looks like you have the XBee hooked to the arduino serial pins 0 & 1 and you are trying to send an AT command to the XBee and see the response to the serial command in XCTU. So, you must have a different XBee hooked to XCTU monitoring things. This will work, but not for AT commands.

AT commands are used to control the XBee and are not transmitted, just responded to. So if you say ATID to an XBee on its serial port nothing will be transmitted out the RF side but you will get a response back in the serial port. So, the listening XBee that I think you have connected to XCTU won't see anything.

So, what you're seeing in XCTU is a combination of things. As johnwasser pointed out, the single quote +++ single quote is going out as the numbers you're seeing and the command DH and DL are going out as well. The +++ you wanted is not being sent to the XBee at all.

So, in the loop, wait a couple of seconds, send "+++" , wait a little over a second to allow for drift and stuff, send the "ATID\r" and you'll get a response. However, you won't be able to see the response because it isn't transmitted over RF and you have your serial port tied up on the XBee. Notice that you have to send an entire AT command all at once for it to work. If you want the DL, you have to send ATDL, if you want the ID, send ATID, etc. So to get the destination low, send the "+++" (remember double quotes) then send "ATDL" after waiting for the OK to come back and so forth.

If you want the ability to debug this in an easier fashion, take a look at NewSoftSerial and move the XBee to different pins leaving your arduino serial port available for debugging and research. At 9600 baud NewSoftSerial will work just fine with an XBee and you don't have to disconnect anything to load new software to the arduino for experimentation. I have used NewSoftSerial at baud rates much higher successfully connected to an XBee so I highly recommend this.

Thank you all for your help.

However, I have a couple of questions regarding NSS. I read about it and downloaded the library and played a little bit with it. Now when I write :
NewSoftSerial mySerial(2, 3);

What does that mean exactly? And if it actually refers to pins 2 and 3 of the arduino as output pins or input pins, do I need to solder them to the XBEE to see results?

Thanx in advance

Also, I used the following code:

void setup() {
Serial.begin(9600);
}
void loop() {
delay(1000);
Serial.println("+++");
delay(1020);
Serial.println("ATDL");
int x = Serial.read();
delay(1020);

Serial.println(x);
delay(1000);
Serial.println("ATDH");
delay(1020);
int y = Serial.read();
Serial.println(y);

}

But still on the XBEE hooked to the X-CTU, I got :

.+++
.ATDL
.-1
.ATDH
.-1
.+++
.ATDL
.-1
.ATDH
.-1
.+++
.ATDL
.-1
.ATDH
.-1
.

What does this -1 mean?

-1 means "No character available" when you call Serial.read().

or it was the character sent. Which is a small part of what makes calling .available important.

tastewar:
or it was the character sent. Which is a small part of what makes calling .available important.

I would hope that the character 0xFF would come out of Serial.read() as the integer 0x00FF, not -1 (0xFFFF).

Doh! Thanks for the correction.

Here is the example from the library directory with my comments about what it is doing; I changed the mySerial to xbeeSerial to help it be a little more clear for your particular instance:

Someone correct me if I mess it up

SoftwareSerial xbeeSerial(2, 3);  //This creates the software serial port using pin 2 as receive and 3 as transmit
                                          // just like using the regular serial port  connect these to your device

void setup()  
{
  Serial.begin(57600);
  Serial.println("Goodnight moon!");

  // set the data rate for the SoftwareSerial port
  xbeeSerial.begin(4800);                   // This sets the baud rate for the new port on pins 2 and 3
  xbeeSerial.println("Hello, world?");     // and puts hello world out the port so you can see it
}

void loop() // run over and over
{
  if (xbeeSerial.available())                           //if there is a character on the software serial port
    Serial.print((char)mySerial.read());           // put it out the hardware serial port
  if (Serial.available())                                 // if there is a character on the hardware port
    xbeeSerial.print((char)Serial.read());         // put it out the software port
}

See, you use it just like the hardware port. This give you a place to put debugging text while leaving the software port open to handle the data traffic. It's a little bit more complicated because you have two ports to worry about, but you will get used to that pretty quickly.

Hmmmmmm, interesting. So it's like having multiple serial ports !!

Okay does this NSS library support AT commands like changing the value of DL on the XBEE.

For example, I have XBEE 1 and XBEE 2. Originally XBEE 1 is not sending to XBEE 2. Can I use NSS library to set DL of XBEE 1 to MY address of XBEE 2 to make XBEE 1 send to XBEE 2?

Is it even possible without NSS? Because the code in my second post should put something, other than -1, on the serial port, but it isn't.

Ugggh hardware is so difficult to debug =(

You're not understanding something. NSS isn't like a serial port, it actually creates a serial port, it takes two pins of your arduino and makes a serial port out of them. Just like pins 0 and 1. It's software not hardware, but you use it almost exactly the same.

So, whatever you can do with the hardware port and the serial library, you can do with the new port and the software serial library. Does it support AT commands? Yes and no. It will certainly send and return AT commands and responses, but there is nothing in the softwareserial library special about AT commands to an XBee.

However, if you compile the code I put above and make one little correction that I noticed when I compiled it to test this, it will take the +++ that you type into the arduino serial window and send it to the XBee. The XBee will respond with OK and you can continue from there.

Connect the XBee rx to arduino pin 3, XBee tx to arduino pin 2 and the arduino serial window needs to be set to "no line ending" (menubox down on the bottom) and just play away. From there, once you see the response, play with delays and other things until you get a better understanding.

Oh, the reason the last piece of code you posted didn't work was because the +++ has to be sent alone with a short guard time. You were using Serial.println which puts a end of line character on there as well. Try it again using Serial.print("+++"); Notice there no 'ln' at the end.

Good luck