Hi I am fairly new to xbee and arduino and had some questions about them. I would really appreciate any help.
I had a trouble getting the two xbee’s each attached to a sparkfun xbee shield http://www.sparkfun.com/commerce/produc … ts_id=9588 which are then hooked up to separate arduino boards to work. I have one XBee act as the transmitter and one as the receiver. The “receiver” XBee is not receiving any of the transmissions however.
CODE FOR THE “TRANSMITTER”:
void setup(){
Serial.begin(9600);
pinMode(13, OUTPUT);
}
void loop() {
Serial.println('H');
digitalWrite(13, HIGH);
delay(1000);
Serial.println('L');
digitalWrite(13, LOW);
delay(1000);
}
CODE FOR THE “RECEIVER”:
void setup() {
pinMode(13, OUTPUT);
Serial.begin(9600);
}
void loop() {
if(Serial.available() > 0) {
data= Serial.read();
if (data == 'H') {
digitalWrite(13, HIGH);
} else if {
digitalWrite(13, LOW);
}
}
}
I checked the RX/TX pin connections and that the switch on the shield that controls which pins serve as the datain and dataout pins was switched to the correct position, and the grounds are connected and the 5V on the arduino board is connected to that pin on the shield. Are there any other things to check with the setup or is there something wrong with the code?
I also tried using an existing library that the arduino site recommended but in order to use it I need the SH and SL of the XBee. I tried to access these through the command mode but was unable to enter command mode. The code for this is below and based largely on something I found online:
// serial out is on port 1
// serial in is on port 0
// a status light is on port 13
int ledPin = 13;
// a byte to send out data:
char thisByte = 0;
void setup () {
// set pins to input and output appropriately
pinMode(ledPin, OUTPUT);
pinMode(switchPin, INPUT);
// start up the serial connection with 9600-8-n-1-true (non-inverted):
Serial.begin(9600);
// blink the status LED
blinkLED(ledPin, 3);
// for some reason it seems to help to send an arbitrary character first
//then pause for the guard time before requesting command mode
Serial.print("X");
delay(1100);
// put the XBee in command mode
Serial.print("+++");
delay(1100);
// wait for a response from the XBee for 2000 ms, or start
// over with setup if no valid response comes
if (returnedOK() == 'T') {
// if an OK was received then continue
}
else {
setup(); // otherwise go back and try setup again
}
Serial.println("ATSH");
while (Serial.available() == 0) {};
char data= Serial.read();
Serial.println("CN");
// wait for a response from the XBee for 2000 ms, or start
// over with setup if no valid response comes
if (returnedOK() == 'T') {
// if an OK was received then continue
}
else {
setup(); // otherwise go back and try setup again
}
}
void loop () {
blinkLED(13, 3);
}
void blinkLED(int targetPin, int numBlinks) {
// this function blinks the status LED light as many times as requested
for (int i=0; i<numBlinks; i++) {
digitalWrite(targetPin, HIGH); // sets the LED on
delay(250); // waits for a second
digitalWrite(targetPin, LOW); // sets the LED off
delay(250);
}
}
char returnedOK () {
// this function checks the response on the serial port to see if it was an "OK" or not
char incomingChar[3];
char okString[] = "OK";
char result = 'n';
int startTime = millis();
while (millis() - startTime < 2000 && result == 'n') { // use a timeout of 10 seconds
if (Serial.available() > 1) {
// read three incoming bytes which should be "O", "K", and a linefeed:
for (int i=0; i<3; i++) {
incomingChar[i] = Serial.read();
}
if ( strstr(incomingChar, okString) != NULL ) { // check to see if the respose is "OK"
// if (incomingChar[0] == 'O' && incomingChar[1] == 'K') { // check to see if the first two characters are "OK"
result = 'T'; // return T if "OK" was the response
}
else {
result = 'F'; // otherwise return F
}
}
}
return result;
}
This code never puts the XBee device into command mode. Again, I was just hoping for suggestions about how to get this working.