Reading and Display on position GPS

Hello, I meet a problem for the position GPS display. Actually, when I use this code :

#include <string.h>
#include <ctype.h>
 
 
 int rxPin = 0;                    // RX PIN 
 int txPin = 1;                    // TX TX
 int byteGPS=-1;
 char linea[300] = "";
 char comandoGPR[7] = "$GPRMC";
 int cont=0;
 int bien=0;
 int conta=0;
 int indices[13];
 void setup() {
   pinMode(rxPin, INPUT);
   pinMode(txPin, OUTPUT);
   Serial.begin(4800);
   for (int i=0;i<300;i++){       // Initialize a buffer for received data
     linea[i]=' ';
   }   
 }
 void loop() {
   byteGPS=Serial.read();         // Read a byte of the serial port
   if (byteGPS == -1) {           // See if the port is empty yet
     delay(100); 
   } else {
     // note: there is a potential buffer overflow here!
     linea[conta]=byteGPS;        // If there is serial port data, it is put in the buffer
     conta++;                      
     Serial.write(byteGPS); 
     if (byteGPS==13){            // If the received byte is = to 13, end of transmission
       // note: the actual end of transmission is <CR><LF> (i.e. 0x13 0x10)
       cont=0;
       bien=0;
       // The following for loop starts at 1, because this code is clowny and the first byte is the <LF> (0x10) from the previous transmission.
       for (int i=1;i<7;i++){     // Verifies if the received command starts with $GPR
         if (linea[i]==comandoGPR[i-1]){
           bien++;
         }
       }
       if(bien==6){               // If yes, continue and process the data
         for (int i=0;i<300;i++){
           if (linea[i]==','){    // check for the position of the  "," separator
             // note: again, there is a potential buffer overflow here!
             indices[cont]=i;
             cont++;
           }
           if (linea[i]=='*'){    // ... and the "*"
             indices[12]=i;
             cont++;
           }
         }
         Serial.println("");      // ... and write to the serial port
         Serial.println("");
         Serial.println("---------------");
         for (int i=0;i<12;i++){
           switch(i){
             case 0 :Serial.print("Time in UTC (HhMmSs): ");break;
             case 1 :Serial.print("Status (A=OK,V=KO): ");break;
             case 2 :Serial.print("Latitude: ");break;
             case 3 :Serial.print("Direction (N/S): ");break;
             case 4 :Serial.print("Longitude: ");break;
             case 5 :Serial.print("Direction (E/W): ");break;
             case 6 :Serial.print("Velocity in knots: ");break;
             case 7 :Serial.print("Heading in degrees: ");break;
             case 8 :Serial.print("Date UTC (DdMmAa): ");break;
             case 9 :Serial.print("Magnetic degrees: ");break;
             case 10 :Serial.print("(E/W): ");break;
             case 11 :Serial.print("Mode: ");break;
             case 12 :Serial.print("Checksum: ");break;
           }
           for (int j=indices[i];j<(indices[i+1]-1);j++){
             Serial.print(linea[j+1]); 
           }
           Serial.println("");
         }
         Serial.println("---------------");
       }
       conta=0;                    // Reset the buffer
       for (int i=0;i<300;i++){    //  
         linea[i]=' ';             
       }                 
     }
   }
 }

On the serial monitor we can see somthing like that :

However we would like to have only the $GPRMC to have the longitude and the latitude.

For information I use an Arduino Uno and a GPS that you can see here :
https://www.sparkfun.com/products/retired/11466

Thank you for your help, I hope that you will be able to do something.
Best regards :slight_smile:

Brother , why don't you use the TinyGPS+ Library ? it's easier , and you can easily get the Latitude and Longitude and much more.

here is a link to download it: http://arduiniana.org/libraries/tinygpsplus/

Hi longjump ! (For information : I work with cvitour) First of all thank you so much for your help and your very quick reply . We tried that you said but with Tinygps+ or Tinygps , we encounter the same problems and we don't know why ... Where could it come from ? Incorrect connection? Problem with our gps?

we encounter the same problems and we don't know why

Show us your your code with TinyGPS.

Show us your wire connections.

You know this is a 3.3V module, right?

Cheers, /dev

For the code, we use this one :

#include <SoftwareSerial.h>
#include <TinyGPS.h>
 
long lat,lon; // create variable for latitude and longitude object
 
SoftwareSerial gpsSerial(0, 1); // create gps sensor connection
TinyGPS gps; // create gps object
 
void setup(){
  Serial.begin(9600); // connect serial
  gpsSerial.begin(4800); // connect gps sensor
}
 
void loop(){
  while(gpsSerial.available()){ // check for gps data
   if(gps.encode(gpsSerial.read())){ // encode gps data
    gps.get_position(&lat,&lon); // get latitude and longitude
    // display position
    Serial.print("Position: ");
    Serial.print("lat: ");Serial.print(lat);Serial.print(" ");// print latitude
    Serial.print("lon: ");Serial.println(lon); // print longitude
   }
  }
}

And regarding installation, here it is :

And the result is always the same, even if we change the RX pin et the TX pin (for exemple Rxpin=2 and TXpin=3). It looks like something like that :

And yes, we know this is a 3.3V module.

We have tried almost everything … this will be a chance if someone would have the solution to our problem !

Thank you in advance to any one who may be able to give us some ideas.

Uh… Pin 0 and 1 are for Serial. Sorry I didn’t notice that in your first post.

For one thing, that is a HardwareSerial port, not SoftwareSerial.

The second thing is that you are trying to put three devices on those pins: the GPS, the Arduino, and the USB. Serial is what talks to the Serial Monitor on your PC, through USB.

What’s happening is that the GPS is sending almost constantly. After you upload your sketch, the GPS is able to send some data through the USB because the Arduino isn’t running yet. You see that data come out on the Serial Monitor window.

Then your sketch starts running and tries to use pins 0 and 1 for Serial, because of Serial.begin. With this conflict, nothing else will appear.

Instead, connect the GPS to two other pins (3 and 4 are common, but 2 and 3 should work ok) using SoftwareSerial. Remember, the GPS TX is connected to the Arduino RX pin you give to SoftwareSerial, and the GPS RX is connected to the Arduino TX pin. For this code:

SoftwareSerial gpsSerial(2, 3); // rx, tx

The GPS TX pin should be connected to pin 2, and the GPS RX pin should be connected to pin 3.

You may also have a wrong baud rate. Because the GPS data was coming out on the Serial Monitor window, it is apparently set to the same baud rate as the GPS device.

If your Serial Monitor window was set to 4800, change it to 9600 (that’s why the GPS data came out)

If your Serial Monitor window is set to 9600, then your GPS device is also running at 9600, and that’s why the GPS data came out. You need to change the gpsSerial.begin to 9600.

Whatever baud rate is in Serial.begin is the baud rate you must select in the Serial Monitor window.

Cheers,
/dev

The Adafruit GPS library has an example called leo_parsing that you should look at carefully. It answers many of your questions. See gps.h for definitions of commands you can send to the gps to have it output whatever NMEA sentences your sketch requires.

Wouahhhh !! Thank you so much ! It works ! :DDD You are geniuses ! :p