How to use pair of adafruit GPS and Adafruit xbee adapter with xbees
Moderators: adafruit_support_bill, adafruit

How to use pair of adafruit GPS and Adafruit xbee adapter with xbees

by Umar Khalid on Sat Mar 16, 2013 10:45 am

Please can you help me merge these 2 codes together because I tried to merge these but couldn’t get the desired results?

Basically I am using one Arduino as transmitter to send latitude and longitude data from one Arduino to another wirelessly. From transmitter I am outputting data in this format: 12223,123232. Longitude and latitude separated by coma. The code below saves these values into an array I want to merge this code with the gps code. So that I can add the value from transmitter to the value I have from gps which is connected to Arduino which is acting as receiver.
Code: Select all | TOGGLE FULL SIZE
#include <SoftwareSerial.h>

SoftwareSerial xbee(8, 9);


void setup()  {

  xbee.begin(9600);
}



void loop()                     // run over and over again
{
      const int NUMBER_OF_FIELDS = 2; // how many comma-separated fields we expect
      int fieldIndex = 0; // the current field being received
      double values[NUMBER_OF_FIELDS]; // array holding values for all the fields
      if (xbee.available()) {
     
        for(fieldIndex = 0; fieldIndex < 2; fieldIndex ++)

       
        {
          values[fieldIndex] = xbee.parseFloat(); // get a numeric value
      }
        Serial.println(values[0],7);
        Serial.println(values[1],7);
        fieldIndex = 0; // ready to start over
      }

  if (Serial.available()) {
      xbee.print((char)Serial.read());
  }
  delay(100);
}

Code: Select all | TOGGLE FULL SIZE
 #include <Adafruit_GPS.h>
#include <SoftwareSerial.h>

SoftwareSerial mySerial(10, 11);
Adafruit_GPS GPS(&mySerial);


#define GPSECHO  false

void setup() 
{

  Serial.begin(115200);
  delay(5000);
 
 
  GPS.begin(9600);
 

  GPS.sendCommand(PMTK_SET_NMEA_OUTPUT_RMCGGA);

  GPS.sendCommand(PMTK_SET_NMEA_UPDATE_1HZ);

  GPS.sendCommand(PGCMD_ANTENNA);

  delay(1000);

  mySerial.println(PMTK_Q_RELEASE);
}

uint32_t timer = millis();
void loop()                     
{
  char c = GPS.read();
 
  if ((c) && (GPSECHO))
    Serial.write(c);
 

  if (GPS.newNMEAreceived()) {

 
    if (!GPS.parse(GPS.lastNMEA()))   /
      return;
  }


  if (timer > millis())  timer = millis();


  if (millis() - timer > 2000) {
    timer = millis(); /
   

    if (GPS.fix) {
     
      float new_lat = GPS.latitude + value[0];  // this is how I want to use array to add the 2 values
      float new_long = GPS.longitude + value[1];
     
     
     
      Serial.print("Location: ");
      Serial.print(new_long, 4); Serial.print(GPS.lon);
      Serial.print(", ");
      Serial.print(new_lat, 4); Serial.println(GPS.lat);


    }
  }
}
Umar Khalid
 
Posts: 11
Joined: Sun Nov 11, 2012 8:00 am

Re: How to use pair of adafruit GPS and Adafruit xbee adapter with xbees

by adafruit_support_bill on Sun Mar 17, 2013 6:46 am

I tried to merge these but couldn’t get the desired results

Maybe if you post your merged code and tell us what didn't work.
User avatar
adafruit_support_bill
 
Posts: 29632
Joined: Sat Feb 07, 2009 9:11 am

Re: How to use pair of adafruit GPS and Adafruit xbee adapter with xbees

by Umar Khalid on Sun Mar 17, 2013 9:24 am

This is the combined code but this does not show added value on the serial monitor after the gps achieves a fix.

Code: Select all | TOGGLE FULL SIZE
#include <Adafruit_GPS.h>
#include <SoftwareSerial.h>

SoftwareSerial mySerial(10, 11);
SoftwareSerial xbee(8, 9);
Adafruit_GPS GPS(&mySerial);


#define GPSECHO  false

void setup() 
{

  Serial.begin(115200);
  delay(5000);
 
 
  GPS.begin(9600);
 
  xbee.begin(9600);
 

  GPS.sendCommand(PMTK_SET_NMEA_OUTPUT_RMCGGA);

  GPS.sendCommand(PMTK_SET_NMEA_UPDATE_1HZ);

  GPS.sendCommand(PGCMD_ANTENNA);

  delay(1000);

  mySerial.println(PMTK_Q_RELEASE);
}

uint32_t timer = millis();
void loop()                     
{
  char c = GPS.read();
 
  if ((c) && (GPSECHO))
    Serial.write(c);
 

  if (GPS.newNMEAreceived()) {

 
    if (!GPS.parse(GPS.lastNMEA())) 
      return;
  }


  if (timer > millis())  timer = millis();


  if (millis() - timer > 2000) {
    timer = millis();
   

    if (GPS.fix) {
     
            const int NUMBER_OF_FIELDS = 2; // how many comma-separated fields we expect
      int fieldIndex = 0; // the current field being received
      double values[NUMBER_OF_FIELDS]; // array holding values for all the fields
      if (xbee.available()) {
     
        for(fieldIndex = 0; fieldIndex < 2; fieldIndex ++)

       
        {
          values[fieldIndex] = xbee.parseFloat(); // get a numeric value
      }
        Serial.println(values[0],7);
        Serial.println(values[1],7);
         
     
     
      float new_lat = GPS.latitude + values[0];  // this is how I want to use array to add the 2 values
      float new_long = GPS.longitude + values[1];
     
     
     
      Serial.print("Location: ");
      Serial.print(new_long, 4); Serial.print(GPS.lon);
      Serial.print(", ");
      Serial.print(new_lat, 4); Serial.println(GPS.lat);
        fieldIndex = 0; // ready to start over

      if (Serial.available()) {
      xbee.print((char)Serial.read());
    }
    }
  }
}
}
Umar Khalid
 
Posts: 11
Joined: Sun Nov 11, 2012 8:00 am

Re: How to use pair of adafruit GPS and Adafruit xbee adapter with xbees

by adafruit_support_bill on Sun Mar 17, 2013 10:18 am

If you are using software serial on multiple ports, only one can be used for receive at a time. http://arduino.cc/en/Reference/SoftwareSerial
You can use the "listen()" function to switch between them: http://arduino.cc/en/Reference/SoftwareSerialListen
But in your case, you will may lose some data in the process. To do what you want, you probably will require an Arduino with 2 hardware serial ports such as the Mega.
User avatar
adafruit_support_bill
 
Posts: 29632
Joined: Sat Feb 07, 2009 9:11 am