unixgpsgpsd

use gpsd or cgps to return latitude and longitude then quit


I would like a dead-simple way to query my gps location from a usb dongle from the unix command line.

Right now, I know I've got a functioning software and hardware system, as evidenced by the success of the cgps command in showing me my position. I'd now like to be able to make short requests for my gps location (lat,long in decimals) from the command line. my usb serial's path is /dev/ttyUSB0 and I'm using a Global Sat dongle that outputs generic NMEA sentences

How might I accomplish this?

Thanks


Solution

  • telnet 127.0.0.1 2947

    ?WATCH={"enable":true}

    ?POLL;

    gives you your answer, but you still need to separate the wheat from the chaff. It also assumes the gps is not coming in from a cold start.

    A short script could be called, e.g.;

    #!/bin/bash
    exec 2>/dev/null
    # get positions
    gpstmp=/tmp/gps.data
    gpspipe -w -n 40 >$gpstmp"1"&
    ppid=$!
    sleep 10
    kill -9 $ppid
    cat $gpstmp"1"|grep -om1 "[-]\?[[:digit:]]\{1,3\}\.[[:digit:]]\{9\}" >$gpstmp
    size=$(stat -c%s $gpstmp)
    if [ $size -gt 10 ]; then
       cat $gpstmp|sed -n -e 1p >/tmp/gps.lat
       cat $gpstmp|sed -n -e 2p >/tmp/gps.lon
    fi
    rm $gpstmp $gpstmp"1"  
    

    This will cause 40 sentences to be output and then grep lat/lon to temporary files and then clean up.

    Or, from GPS3 github repository place the alpha gps3.py in the same directory as, and execute, the following Python2.7-3.4 script.

    from time import sleep
    import gps3
    
    the_connection = gps3.GPSDSocket()
    the_fix = gps3.DataStream()
    
    try:
        for new_data in the_connection:
            if new_data:
                the_fix.refresh(new_data)
            if not isinstance(the_fix.TPV['lat'], str):  # check for valid data
                speed = the_fix.TPV['speed']
                latitude = the_fix.TPV['lat']
                longitude = the_fix.TPV['lon']
                altitude = the_fix.TPV['alt']
                print('Latitude:', latitude, 'Longitude:', longitude)
                sleep(1)
    except KeyboardInterrupt:
        the_connection.close()
        print("\nTerminated by user\nGood Bye.\n")
    

    If you want it to close after one iteration also import sys and then replace sleep(1) with sys.exit()