Serial communication with an XBee module and AT commands

Good morning everyone!
I need to set up a serial communication with an XBee PRO 802.15.4 module in order to send AT commands and read the module's responses.

I'm using an Arduino Leonardo with a Sparkfun's XBee Shield. The switch on the shield is set on "UART" and I'm using the "Serial1" set of commands to communicate to the module via the Leonardo's 0(Rx) and 1(Tx) pins.

the activation of the AT mode requires 2 steps:
1 - to send the characters "+++" to the module
2 - wait for the module to reply "OK"

my problem is that the Arduino doesn't always recognize the presence of available data, and
in general doesn't seem to be able to read the output of the XBee on Serial1 correctly.

could anyone provide some examples or documentation on how to set up such an interface with the module?

Or else how can make the Arduino set or read the module's parameters?

in order to

To what?

I’m sorry, i just realized that I didn’t post correctly and edited the post.

my problem is that the Arduino doesn't always recognize the presence of available data,

Or you are not testing for it correctly.

in general doesn't seem to be able to read the output of the XBee on Serial1 correctly.

I have my doubts about that.

Where is your code?

Here's the code :

int startATmod(); //Transmits the "+++" command to start AT mode, waits for serial data and
                        //returns 1 if the response was "OK", else returns 0
void readATID(); //Reads the ID parameter of the module and prints it on serial module
void readATMY(); //Reads the MY parameter of the module and prints it on serial module

char readbuff;
int mod = 0;
int MY;

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

void loop() {
  
  delay(2000);
  mod =startATmod();
  delay(1000);
  readATID();
  delay(1000);
  void readATMY();
  delay(2000);

}

void readATID()
{
  Serial1.print("ATID");
  Serial1.write('\r');

  Serial.print("ID = ");
  while(Serial1.available()>0)
  {
    Serial.print(Serial1.read());
  }
  Serial.println();
}

void readATMY()
{
  Serial1.print("ATMY");
  Serial1.write('\r');

  Serial.print("MY = ");
  while(Serial1.available()>0)
  {
    Serial.print(Serial1.read());
  }
  Serial.println();
}


int startATmod()
{
  int i;
  int r;
  char resbuff[3];
  
  Serial1.print("+++");

  //waits for available data on Serial1
  while(Serial1.available() == 0)
  {
    Serial.println("waiting for response");
    delay(300);
  }
  
  i = 0;
  resbuff[0] = 'E'; // initialization of the response buffer
  resbuff[1] = 'R'; // with the two characters "ER" (error)
  
  // Puts the available data in resbuff if there is any 
  while(Serial1.available()>0)
  {
    resbuff[i] = (char(Serial1.read()));
    ++i;
    r = 1;
  }

  

  if(r == 0)
  {
    Serial.println("AT mod activation error");
  } else
  {
    if(resbuff[0] == 'O' && resbuff[1] == 'K')
    {
      Serial.println("AT mod is active");
    } else
    {
      Serial.print(resbuff[0]);
      Serial.println(resbuff[1]);
      r = 0;
    }
  }

  return r;
}

I'm using this library to talk with xbee, without a shield, just connecting arduino tx/rx to xbee din/dout. Care that xbee is 3.3v and arduino is 5v, so you need to lower the signal in the arduino tx line.
Not sure but i think that library works ok with shield

Actually my original plan was using the XBees in API mode, since I wanted to use them to provide communication between a master device and multiple (about 5) slave devices, wich are battery powered and so the API mode would have been better to implement sleep mode on the slaves and in general for information management (recognizing the sender and activate broadcasting just by changing a byte in the frame).

So I tried to use that library but couldn't work it out, not even the example sketches. I used to set up some tests with XCTU and a transmitting/receiving module on the Arduino but couldent get any traffic between the modules.

Can you tell me how you configured the modules or show me some of your code?

Thank you!

When you put an XBee in AT mode, how long does it stay that way? If no AT commands arrive within some time period, what does the XBee do? What it that time period?

How long do you have your thumb up your ass after putting the XBee in AT command mode?

malune:
Actually my original plan was using the XBees in API mode, since I wanted to use them to provide communication between a master device and multiple (about 5) slave devices, wich are battery powered and so the API mode would have been better to implement sleep mode on the slaves and in general for information management (recognizing the sender and activate broadcasting just by changing a byte in the frame).

So I tried to use that library but couldn't work it out, not even the example sketches. I used to set up some tests with XCTU and a transmitting/receiving module on the Arduino but couldent get any traffic between the modules.

Can you tell me how you configured the modules or show me some of your code?

Thank you!

I have one xbee (client) sending data to a xbee (server), so on the xbee client i just connected TX from arduino to xbee Din (with a signal shifter to lower 5V to safe value) and Rx to Dout. I use serialsoftware to send raw data, and have one xbee attached to laptop with XCTU to check everything.

The xbee server is connected the same way, arduino RX/TX to xbee Dout/Din and i'm using xbee library to check for incoming data. I also send ATcommands to the xbee server to get some status, i just used the code from the examples folder (there is a example called ATcommand).

About xbee configuration, just set the same PAN ID on both and AP=0 on the xbee client and AP=2 on the xbee server. This is important since the library only works for AP=2.

Regards