JFTHOI, I decided to figure out what reading RSSI inside a program actually entails.
A lot of handshaking has to be done when in XBee command mode. It's actually a
nontrivial mess in the sense you have to wait "unknown" [at least for me] amounts of
time for the XBee to respond at each step before taking the next step. Therefore,
this sketch is full of delays at every point, whose values were chosen per what seemed
to work, but are probably not optimal. Therefore, comms turn-around time is slow,
but it does work.
/*
file: XBee_RSSI_test
revised: 03/01/13, orig 02/28/13.
Simple RSSI echo test. This program runs on the **remote** node,
and characters are entered on the keyboard at the host node.
Remote responses will be as follows: "rcvd: a s d f g RSSI: 3A ...done"
**** IMPORTANT NOTE ***
Uses 1 h.w. UART for both USB and XBee comms. Therefore,
on a UNO-type board, this sketch must be uploaded, then
the USB disconnected and the XBee connected to header
pins 0,1 (Rx,Tx). And vice versa.
NOTES:
1. see Important Note above.
2. in the RSSI check routine, various delays() are inserted
to give the XBee time to respond to commands, the delay()
values chosen are basically pure guesswork.
***********************************************************/
#define LEDpin 5 // this board uses D5 for the Led.
// data holding buffers.
#define ARYSZ1 32
#define ARYSZ2 32
int ch, datary1[ARYSZ1], datary2[ARYSZ2];
int dcnt1=0, dcnt2=0; // data_in counters.
int fGot_it=0; // received data flag.
#define BAUD 57600 // set to XBee module baudrate.
/**************************************/
void setup()
{
Serial.begin(BAUD);
pinMode(LEDpin, OUTPUT);
delay(5000); /// give XBee a chance to bootup.
Serial.println("Remote ready... enter chars at host");
delay(500);
}
/**************************************/
void loop()
{
// echo_test();
rssi_test();
}
/*
simple test to check the comm link.
- echos back rcvd chars.
******************************************************/
void echo_test()
{
if( Serial.available() > 0) {
digitalWrite(LEDpin, HIGH); // blink Led w/each receipt.
Serial.write( Serial.read() );
digitalWrite(LEDpin, LOW);
}
}
/*
wait for message from host, read RSSI value from
XBee, and send message+RSSI back to host.
******************************************************/
void rssi_test()
{
// receive.
while( Serial.available() > 0) {
digitalWrite(LEDpin, HIGH); // blink Led w/each receipt.
ch=Serial.read();
if( dcnt1 < ARYSZ1 ) datary1[dcnt1++]=ch;
// Serial.write(ch); // immediate echo.
delay(1);
fGot_it=1;
digitalWrite(LEDpin, LOW);
delay(250); // wait a bit for additional characters.
// this delay is artificially long to allow chars
// to be entered from the keyboard.
}
// once msg received, ask XBee for RSSI.
// - delays inserted to give XBee time to respond.
// - all delay lengths used here is pure guesswork.
if( fGot_it ) {
fGot_it=0;
delay(1000); // wait 1-sec after any XBee comms.
Serial.print("+++");
delay(2000);
// should change following to: look for "OK".
while( Serial.available() > 0) Serial.read(); // dump "OK", etc.
Serial.print("ATDB"); // ask for RSSI.
Serial.write(13); // send CR.
delay(10);
// check for XBee response.
while( Serial.available() > 0) {
ch=Serial.read();
if( dcnt2 < ARYSZ2 ) datary2[dcnt2++]=ch;
delay(10);
}
Serial.println("ATCN"); // leave command mode.
// Serial.write(13); // send CR.
delay(1000);
while( Serial.available() > 0) Serial.read(); // dump "OK", etc.
// now send back.
Serial.println("");
Serial.print("rcvd: ");
for( int i=0; i<dcnt1; i++) {
Serial.write( datary1[i] );
Serial.print(" ");
}
Serial.print(" RSSI: ");
for( int i=0; i<dcnt2; i++) Serial.write( datary2[i] );
Serial.print(" ...done ");
dcnt1=dcnt2=0;
}
}