Test the communication between xbee and arduino

dear all,

I configure my xbee and I tested with X-CTU (work 1000%)
I want to send data via xbee arduino!

Without ardunio (with X-CTU), I have sent data and received it in the ConnectPort gateway x4 (work (1000%)

First test with arduino:

I want to test the communication between xbee and arduino
I send “+ + +”
the response of the xbee : “ok”

It not work ,where is my fault !!! ?
Source :The book “Arduino Cookbook 2011”

#include <SoftwareSerial.h>

/*
XBeeMessage
Send a message to an XBee using its address
*/
boolean configured = false;
SoftwareSerial mySerial(2, 3); // RX, TX
boolean configureRadio() {
// put the radio in command mode:
Serial.print("+++");
mySerial.write("+++");
String ok_response = "OK\r"; // the response we expect.
// Read the text of the response into the response variable
String response = String("");

while (response.length() < ok_response.length()) {
if (mySerial.available() > 0) {
response += (char) mySerial.read();
Serial.print((char) mySerial.read());
return true; 
}else {
  
return false; // This indicates the response was incorrect.
}
}
}
void setup () {
Serial.begin(9600); // Begin serial
mySerial.begin(9600);
  
configured = configureRadio();
}
void loop () {
  if (configured) {
Serial.print("Hello!");
delay(3000);
}
else {
delay(30000); // Wait 30 seconds
Serial.print("ana!");
configureRadio(); // try again
}
}

I forgot to say that I use xbee shield v1.4

What hardware do you have? With the standard XBee shield or the wireless/sd shield you use Serial and not software serial.

Mark

arduino v1.0.2 xbee shield v1.4: http://www.dfrobot.com/index.php?route=product/product&product_id=61#.UUMiIHfQVdU XBee pro S2 (integrated antenna)

In Arduino 1.0.2, the String class has a major bug. There is a patch available, or you can upgrade to 1.0.4 where the patch is applied.

Don't use the String class with 1.0.4 or earlier. It's not even all that great with 1.0.4, because the Arduino has so little memory for dynamic allocation.

You should learn to use strings, not Strings.

also this code not work …

void setup()
{
  Serial.begin(9600);
}

void loop()
{  
  Serial.println("------------------");
  delay(1200);
  Serial.print("+++");
  delay(1200);
  bool bOK = false;
  while (Serial.available() > 0) {
    Serial.write(Serial.read());
     bOK = true;
  }
  
  if(bOK)
  {
    Serial.println();
    Serial.println("ATMY");
    delay(100);
    while (Serial.available() > 0) {
      Serial.write(Serial.read());   
    }
    Serial.println();
    Serial.println("ATCN");
    delay(100);
    while (Serial.available() > 0) {
    Serial.write(Serial.read());
    }
    Serial.println();
  }
  
  Serial.println();
}

http://code.google.com/p/easyrobot/wiki/ATCommandFromArduino

  while (Serial.available() > 0) {
    Serial.write(Serial.read());

Are you using the serial port to talk to the XBee or the PC? Both is the wrong answer.

i use the port for talk with arduino

i use the port for talk with arduino

Nobody gets annoyed when the Arduino talks to itself?

The serial port is how the Arduino talks to some other device.

I have module xbee /xbee shield / arduino uno

Port usb :to connect arduino to PC

I do not know if I have to use the library <xbee.h> and <SoftwareSerial.h> or not?!

I have module xbee /xbee shield / arduino uno

Unfortunately, of all the XBee shield designs available, the one you have is the worst. I know, I have two of them.

This one, in my opinion, is the best available, and it is far from perfect.

I do not know if I have to use the library <xbee.h> and <SoftwareSerial.h> or not?!

The XBee library makes it (much) more convenient to send messages, in API mode, from one XBee to another specific XBee, if you have more than 2 XBees in your network. If you don’t, then Series 2 modules aren’t the best choice. They will work, but they are more complicated to configure than the Series 1 modules.

The SoftwareSerial library is used to allow the Arduino to use other pins to talk to the XBee. But, since your shield doesn’t have a way to connect the XBee TX and RX to other Arduino pins, the SoftwareSerial library is NOT going to do you any good.

The problem that you have is that the Arduino has one hardware serial port, and that one hardware serial port is being used to talk to the XBee. It can NOT, therefore, also be used to communicate with the PC.

i have this xbee shield: xbee shield dfr0015