I am currently working on a project in which i must obtain the RSSI value from the messages arriving to an incoming xbee. So, in order to do that, i have decided to use AT Commands (by firstly sending the +++ and them the ATDB). Here is the code:
#include <XBee.h>
#include <SoftwareSerial.h>
// First element for Xbee Tx (Arduino RX)
// Second element for Xbee Rx (Arduino Tx)
SoftwareSerial xbee(3, 2);
int rssi;
String message;
void setup()
{
  xbee.begin(9600);       Â
  Serial.begin(9600); Â
         Â
}
void loop()
{
 delay(1000);
 xbee.print("+++");
 delay(1000);
Â
Â
 if(xbee.available())
 {Â
   //Read the OK and empty the buffer
   while(xbee.available())
   {
    Serial.write(xbee.read());
   }
  Â
   xbee.println("ATDB");
   rssi = xbee.read();
   Serial.print(rssi);
   Serial.println();
Â
 }
}
So for the output, i am getting the OK response from the xbee. However, when i attempt to read the rssi value i get the -1 for no data available instead of the 0 (the inital value for the DB parameter of a device who has not yet received any messages). I am not sending the command properly? Shouldn't i expect the parameter value to come in through the serial port?
Thank you for your time and for reading this post
Also thanks in advance for the help
   xbee.println("ATDB");
   rssi = xbee.read();
Exactly how long have you given the Arduino to shift the data out to the XBee AND for the XBee to recognize the command AND for the XBee to generate a response AND for the response to get back to the Arduino?
I just checked the datasheet the default time for the AT command mode is 10 seconds. But i believe it can be ended prematurely with ATCT.
Yes! I guess i will try adding a delay between sending the command and reading the response from the Xbee. I will test it out soon! (as i am really not waiting anything at all)
Thank you Paul it is working perfectly now! I will post the full working code here in case somebody has a similar problem.
#include <XBee.h>
#include <SoftwareSerial.h>
// First element for Xbee Tx (Arduino RX)
// Second element for Xbee Rx (Arduino Tx)
SoftwareSerial xbee(3, 2);
int rssi;
int firstB;
String message;
void setup()
{
  xbee.begin(9600);       Â
  Serial.begin(9600); Â
         Â
}
void loop()
{Â
 firstB = 0;
Â
 if(xbee.available())
 {Â
   //We check the first to see if the data is truly an incoming message
   firstB = xbee.read();
Â
   //We start our messages with M, ACII 77
   //If this was a message start the loop!!! Â
   if( firstB == 77)
   {
    //Read the incoming message
    while(xbee.available())
    {
     Serial.write(xbee.read());
    }
    Serial.println();
  Â
    //Setting it to command mode
    delay(1000);
    xbee.print("+++");
    delay(1000);
    //Read the OK confirmation
    while(xbee.available())
    {
     Serial.write(xbee.read());
    }
    Serial.println();
  Â
    //Add a delay to wait for the response
    xbee.println("ATDB");
    delay(100);
    //Read the DB parameter stored in the xbee
    Serial.print("The rssi value is: ");
    while(xbee.available())
    {
     Serial.write(xbee.read());
    }
    Serial.println();
  Â
    //Exit command mode
    xbee.println("ATCN");
   }
 }
}