How to use GPSBee or XBee via XBee Shield & NewSoftSerial

As I didn’t find anything satisfying in this forum to get the GPSBee to output to serial while using a XBee Shield, I searched the net and found this link: forums.fungizmos.com/viewtopic.php?f=6&t=4

I will copy the code with some minor corrections:

[code]
#include <NewSoftSerial.h>

// To easily use the Xbee shield as a PC <-> Xbee passthrough we can tie the Xbee serial ports
// to unused arduino i/o pins and use a software serial library to interface with the Xbee shield.
// This leaves the hardware serial port fre to be used by the arduino usb connection to the PC.
// We can now send/receive commands from the computer, process on the host arduino if needed and then relay
// to the remote Xbee. Any data sent back from the remote Xbee can be received on the host Xbee, data again
// processed if needed and info can be sent via usb to the pc.

// Shield still needs to be removed before programming arduino.

//remove the two serial select jumpers on the Xbee shield and wire the two middle pins to unused i/o
//pins on the arduino. In this case we are using 2 & 3

//You can get the NewSoftSerial library from here: http://arduiniana.org/libraries/NewSoftSerial/
NewSoftSerial XbeeSerial = NewSoftSerial(3, 2);

void setup() {

Serial.begin(9600); //connection to host computer
Serial.println(“Xbee Software Serial Test!”);

XbeeSerial.begin(9600); //connection to Xbee shield

}

void loop()
{
//simple relay test. Of course we could be processing data here as needed before sending out in either direction.
if (XbeeSerial.available()) {
Serial.print((char)XbeeSerial.read()); //grab avail data from Xbee and send to host pc
}
if (Serial.available()) {
XbeeSerial.print((char)Serial.read()); //Now take any incoming data from pc and relay to Xbee
}
}[/code]

I just changed any references to adafruit’s AFSoftSerial and replaced it with the newer Mikal Hart’s NewSoftSerial. And it works!
Just be sure to follow the hardware steps (check attached photo as well) that you find in the sketch comments!

All thank you’s (including the photo) go towards willp!
Xbee_Soft_Serial.jpg