SOFTWARE SERIAL GPS AND XBEE PRO

Hi…
I have microcontroller using arduino pro micro. I want receive coordinate from GPS and send it with XBee to another microcontroller.

The problem is when i active XBee.Begin(9600), GPS cannot receive the coordinat. When i deactive XBee.Begin(9600), GPS can receive the coordinate.

I using SKNNAV SKM-53 for GPS module and XBee Pro S2 for wireless module…

Here the code:

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

unsigned long fix_age;
SoftwareSerial GPS(9, 11), XBee(10, 8);

TinyGPS gps;
void gpsdump(TinyGPS &gps);
bool feedgps();
void getGPS();
long lat, lon;
float LAT = 0, LON = 0;
char lati[20], longi[20];

void setup() {
GPS.begin(9600);
XBee.begin(9600);
Serial.begin(9600);
}

void loop() {
GPS.listen();
long lat, lon;
unsigned long fix_age, time, date, speed, course;
unsigned long chars;
unsigned short sentences, failed_checksum;
gps.get_position(&lat, &lon, &fix_age);
getGPS();
Serial.print("Latitude : “);
Serial.print(LAT);
Serial.print(” :: Longitude : ");
Serial.println(LON);
if(LAT != 0 ){
XBee.listen();
dtostrf(LAT, 4, 6, lati);
dtostrf(LON, 4, 6, longi);
strcat(dtframe, lati);
strcat(dtframe, “,”);
strcat(dtframe, longi);
Serial.println(dtframe);
XBee.write(dtframe);
}
delay(2000);
}

void getGPS() {
bool newdata = false;
unsigned long start = millis();
// Every 1 seconds we print an update
while (millis() - start < 1000) {
if (feedgps ()) {
newdata = true;
}
}
if (newdata) {
gpsdump(gps);
}
}

bool feedgps() {
while (GPS.available()) {
if (gps.encode(GPS.read()))
return true;
}
return 0;
}

void gpsdump(TinyGPS &gps) {
//byte month, day, hour, minute, second, hundredths;
gps.get_position(&lat, &lon);
LAT = lat / 1000000;
LON = lon / 1000000;
{
feedgps();
}
}

sorry for my bad english.
I hope the problem will be solved

regard

Jo Metropoles

You can’t use two SoftwareSerial instances at the same time. You can listen to GPS for a while (more than one second), then listen to the XBee for a while.

And what happens if you the GPS device doesn’t have a lat/lon yet? It may not be receiving from enough satellites to know the lat/lon.

Here’s a similar program. It listens to some Sonar pings for a while, then listens to GPS for 2.5 seconds, then writes to the SD card. This program also uses NeoSWSerial for the GPS device, instead of the awful SoftwareSerial, and NeoGPS instead of the marginal TinyGPS.

Here’s your program modified to use the Neo libraries:

#include <XBee.h>
#include <NeoSWSerial.h>
#include "NMEAGPS.h"

NeoSWSerial GPS(9, 11), XBee(10, 8);

NMEAGPS gps;
static gps_fix  fixdata;

bool feedgps();
void getGPS();

void setup() {
  GPS.begin(9600);
  XBee.begin(9600);
  Serial.begin(9600);
}

void loop() {
  getGPS();

  if (fixdata.valid.location) {
    char dtframe[64];
    unsigned long fix_age, time, date, speed, course;
    unsigned long chars;
    unsigned short sentences, failed_checksum;

    Serial.print( F("Latitude : ") );
    Serial.print(fixdata.latitude(), 6);
    Serial.print( F(" :: Longitude : ") );
    Serial.println(fixdata.longitude(), 6);
    Serial.flush(); // reduces interrupts during XBee.listen
    
    XBee.listen();
    dtostrf(fixdata.latitude(), 4, 6, &dtframe[0] );
    strcat(dtframe, ",");
    dtostrf(fixdata.longitude(), 4, 6, &dtframe[ strlen(dtframe) ] );
    XBee.print( dtframe );
    XBee.ignore();

    Serial.println(dtframe); // do this after all XBee ops!
  } else
    Serial.println( F("No fix yet...") );

  // Uncomment these to see if you're receiving *any* data from the GPS device
//  Serial.print( gps.statistics.ok );
//  Serial.print( F(" sentences ok, ") );
//  Serial.print( gps.statistics.chars );
//  Serial.print( F(" chars, " ) );
//  Serial.print( gps.statistics.crc_errors );
//  Serial.println( F(" crc errors") );
  
  delay(500); // was 1000+2000, now 2500+500
}


void getGPS() {
  // Get some fresh GPS data

  gps.data_init();
  fixdata.init();
  GPS.listen();

  uint32_t start_time = millis();
  do {
    while (GPS.available()) {
      if (gps.decode( GPS.read() ) == NMEAGPS::DECODE_COMPLETED) {
        fixdata |= gps.fix();
      }
    }
  } while (millis() - start_time < 2500);

  GPS.ignore();
}

Cheers,
/dev