GPS + xbee cause a problem

Hi guys,

I am using an Arduino UNO, em406a GPS and 2 xbee s2b modules(one connected to the Arduino and one connected on the pc).

I am trying to send the GPS string from one xbee to another, but it seems to affect the generated GPS string whenever i add the code for the xbee.

My Code:

#include <XBee.h>
#include <TinyGPS.h>
#include <SoftwareSerial.h>

// Define SoftwareSerial TX/RX pins
#define xbeeRX 2
#define xbeeTX 3
#define gpsRX 4
#define gpsTX 5
//create xbee connection
SoftwareSerial xbeess = SoftwareSerial(xbeeRX, xbeeTX);
//create gps connection
SoftwareSerial gpsss = SoftwareSerial (gpsRX, gpsTX);

//Create an XBee object
XBee xbee = XBee();
TinyGPS gps;

//Define the Destination address of the packets
XBeeAddress64 destaddress = XBeeAddress64(0x0013A200, 0x406F1997);

char Hello[] = "Hello World\n\r";

void setup()
{
Serial.begin(9600); //connect serial
xbeess.begin(9600); //connect xbee
gpsss.begin(4800); //connect gps

//We hook the XBee into the Software Serial
xbee.setSerial(xbeess);

Serial.println("Goodnight moon!");
}

void loop() // run over and over
{
while (gpsss.available()>0){
char c = gpsss.read();
if (c == '$'){

Serial.println();
ZBTxRequest zbtx = ZBTxRequest(destaddress, (uint8_t *)Hello, strlen(Hello));
xbee.send(zbtx);
}
Serial.print(c);
}
}

Output:

Goodnight moon!

$4jíZhË03f43??3eF4fË7³gÆÌ±33Ì?.f6c1fÍ?3»Æl3s3M,26.2,M,,0000*60

$4jmZhËAfË?3eÌ?fÌ?3fË,£Æ?±?.3e®3eæ?3?BaÉ£5jÓ¡f3fÌ?3.372,A,3441.7397,N,03301.3306,E,0.18,148.65,290413,,,A*6C

$4jíZhË03f43??3eF4fË7³gÆÌ±33Ì?.f7c1fÍ?3»Æl3s5M,26.2,M,,0000*62

$4jmZhËAfË?3eÌ?fÌ?3f?,cÆ?±?.3e.3eæ?3?BaÉ£µ?fÙ3Ë?,12,20,67,108,42,32,38,110,50,16,21,096,40,01,20,178,36*7C

$4jm?c6,fË13e?3ÙV,³Í?,f̬³fË53e?9±Æ ³c&fÌ7³Æ c&7Í4?²3´a?
$GPG5c3fË?3eÌ?fÌ,3f?,fÎ,3fË23Î?,fÌ?3fË13fË33Ë??,³v,²?
²4j©MC,070144.372,A,3441.7395,N,03301.3307,E,0.19,154.63,290413,,,A*62

$4jíZhË03f43?v3eF4fË7³gÆÌ±33f?³fÌ9c,3Í??»Æl3s?Yc&fæL3eÓ?,0000*67

$4jmZhËAfË33eÌ?fÌ?3f?,cÆ?±?.3eæì3e.3e&­)FìjÔM4e?7fÌ?3?v2c6FFæì³fÎ?,N,03301.3309,E,0.18,155.46,290413,,,A*6C

$4jíZhË03f43?v3eF4fË7³g?,±33f.³fÌ?c,3Í?3»Æl³s?M,26.2,M,,0000*64

For the time being I am trying to send a "Hello World" message and even that is not transmitted properly. But when i remove the code that transmits the data from one xbee to another, the GPS strings are displayed properly.

Any ideas would be most appreciated.

Regards,
gabys

Only one instance of SoftwareSerial can be active at a time. Your code keeps switching back and forth between instances without ever activating the other instance (done using the listen() method).

You really need a Mega.

when you say "Mega" you are referring to the microprocessor? so you are saying that i need 2 microprocessors in order to get the job done?

when you say "Mega" you are referring to the microprocessor?

I am referring to this: http://arduino.cc/en/Main/ArduinoBoardMega2560

so you are saying that i need 2 microprocessors in order to get the job done?

No, I never said that.

gabys:
when you say "Mega" you are referring to the microprocessor? so you are saying that i need 2 microprocessors in order to get the job done?

No, he just thinks you should use a processor that has more than one hardware serial port. He's probably right since SoftwareSerial is not really going to work the way you want it to in this situation. AltSoftSerial is a major improvement, but only allows one port definition and it also dictates what pins you are going to use (8 and 9). I make use of the main serial port for things, but it can give you some trouble when trying to upload new code thru the bootloader.

i tried the example in this link http://arduino.cc/en/Tutorial/TwoPortReceive, but it didn't work. Is there an alternative solution rather than buying another Arduino board?

gabys:
i tried the example in this link http://arduino.cc/en/Tutorial/TwoPortReceive, but it didn't work. Is there an alternative solution rather than buying another Arduino board?

Like I said, AltSoftSerial will give you one port that's almost as good as a hardware port, if you can live with the constraints. You should also be able to make use of your true hardware port instead of ignoring it, just learn to cope with the consequences which aren't that severe. It beats wasting the only hardware port you have or dedicating it to debug messages only. Use SoftwareSerial as a last resort for output message only, like debugging. Use as high of a baud rate as you can, because it runs with ints disabled while sending or receiving. That's why it can never do both at the same time, or listen to more than one port at a time.

I was going to "fix" SoftwareSerial at one time, but I lost interest and I couldn't come up with anything I really liked that could listen to more than port using interrupts to receive the bits. But given the documented constraint that it can only "listen" to one port at a time, using interrupts would be a lot easier. I may tinker with it some more "when I have time". :wink:

AltSoftSerial uses interrupts to receive and generate the data cleanly. It's pretty clever IMO how it uses the hardware to generate the output bit changes at precisely the right time even though it may have already exited the ISR and is back at main level running code when the pin toggle actually happens. Slick. It starts receiving the data by using the pin change interrupt to detect the start bit, but then it uses the timer to generate a callback to sample the bits with tying everything up between incoming bit samples. This is pretty much what I wanted to do to SoftwareSerial for receive. Sending and allowing the user to pick which pins they want to use will not allow generating the bits the same way. That's ok, there are other ways that will work with other pins than the ones that have hardware PWM support.

afremont, the AltSoftSerial class worked just fine!!

Thank you guys!! You' ve been a great help!