1

I want to read my GPS coordinates with libgps.

This is my code:

 #include <unistd.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <gps.h>

 int main(void){

    struct gps_data_t gps_data;
    int ret = 0;

    ret = gps_open("localhost", "2947", &gps_data);

    gps_stream(&gps_data, WATCH_ENABLE | WATCH_JSON, NULL);

    if (gps_waiting (&gps_data, 500)) {


        if (gps_read (&gps_data) == -1) {

            fprintf(stdout, "Error #3: No GPS data available! Retrying ...\n");

        } 
        else {

            fprintf(stdout, "GPS-Data: Latitude: %f, Longitude: %f, Altitude: %.1f, Timestamp: %ld\n", gps_data.fix.latitude, gps_data.fix.longitude, gps_data.fix.altitude);

        }
    }

    /* When you are done... */
    gps_stream(&gps_data, WATCH_DISABLE, NULL);
    gps_close (&gps_data);

    return 0;
 }

Unfortunately, I always get this result:

GPS-Data: Latitude: nan, Longitude: nan, Altitude: nan, Timestamp: 301989888

So gpsd isn't returning any GPS coordinates ...

But if I execute sudo gpscat -s 4800 /dev/ttyACM0:

$GPGGA,190238.00,4819.14754,N,01512.57069,E,2,11,0.83,469.6,M,43.1,M,,0000*53

Does anyone know what could be wrong?

1 Answers1

0

Check if your GPSD is running properly using "CGPS -s". It should give you all your gps data if it is fixed (aka working and locked on your position).

If it is running, try to use a bigger delay on "if (gps_waiting (&gps_data, 500)) {". I used your same code and had same "nan" results until i increased the wait time for the gps_waiting function.

I tested using a BBB with ublox gps via UART.

Bene
  • 1
  • 2