0

I am trying to stream my gps data to google earth using this code. I have already tried with the version for python 3.4 but I got errors, and someone said that the gps3 didn`t really work for python3, so I tried using python2

Code for python 2.7 for showing GPS stream in Google earth #!/usr/bin/python

# Copyright (C) 2007 by Jaroslaw Zachwieja <grok!warwick.ac.uk>
# Copyright (C) 2008 by TJ <linux!tjworld.net>
# Published under the terms of GNU General Public License v2 or later.
# License text available at http://www.gnu.org/licenses/licenses.html#GPL

import serial 
import string
import sys
import getopt

def usage():
                print ("Usage:")
                print (" -p | --port <device>   e.g. /dev/serial0")
                print (" -b | --baud <speed>    e.g. 9600")
                print (" -f | --file <filename> e.g. /tmp/gps.kml")
                print (" -h | --help     display options")

def main():
            # defaults
            serial_port = "/dev/serial0"
            serial_baud = 9600
            file = './realtime/Realtime GPS.kml'

        try:
                opts, args = getopt.getopt(sys.argv[1:], "p:b:f:h", ["port=", "baud=", "file=", "help"])
        except getopt.GetoptError:
                usage()
                sys.exit(1)
        else:
                for opt, arg in opts:
                        if opt in ("-p", "--port"):
                                serial_port = arg
                        elif opt in ("-b", "--baud"):
                                serial_baud = string.atof(arg)
                        elif opt in ("-f", "--file"):
                                file = arg
                        elif opt in ("-h", "--help"):
                                usage()
                                sys.exit(0)
                        else:
                                print ("Unknown option")
        gps = serial.Serial(serial_port, serial_baud, timeout=1)

        print "Serving data from %s (%d baud) to %s" % (serial_port, serial_baud, file)

        latitude = 0
        longitude = 0
        speed = 0
        heading_in = 0
        altitude = 0
        range = 1000
        tilt = 30
        while 1:
            line = gps.readline()
            datablock = line.split(',')
            print datablock
            if line[0:6] == '$GPRMC':
                latitude_in = string.atof(datablock[3])
                longitude_in = string.atof(datablock[5])
                try:
                    altitude = string.atof(datablock[8])
                except ValueError: # use last good value
                        altitude = altitude
                        speed_in = string.atof(datablock[7])
                        try:
                            heading_in = string.atof(datablock[8])
                        except ValueError:
                                # use last good value
                                heading_in = heading_in
                        if datablock[4] == 'S':
                                latitude_in = -latitude_in
                        if datablock[6] == 'W':
                                longitude_in = -longitude_in

                        latitude_degrees = int(latitude_in/100)
                        latitude_minutes = latitude_in - latitude_degrees*100

                        longitude_degrees = int(longitude_in/100)
                        longitude_minutes = longitude_in - longitude_degrees*100

                        latitude = latitude_degrees + (latitude_minutes/60)
                        longitude = longitude_degrees + (longitude_minutes/60)

                        speed = int(speed_in * 1.852)
                        range = ( ( speed / 100  ) * 350 ) + 650
                        tilt = ( ( speed / 120 ) * 43 ) + 30
                        heading = heading_in

                        if speed < 10:
                                range = 200
                                tilt = 30
                                heading = 0

                        output = """<?xml version="1.0" encoding="UTF-8"?>
        <kml xmlns="http://earth.google.com/kml/2.0">
                <Placemark>
                        <name>%s km/h</name>
                        <description>^</description>
                        <LookAt>
                                <longitude>%s</longitude>
                                <latitude>%s</latitude>
                                <range>%s</range>
                                <tilt>%s</tilt>
                                <heading>%s</heading>
                        </LookAt>
                        <Point>
                                <coordinates>%s,%s,%s</coordinates>
                        </Point>
                </Placemark>
        </kml>""" % (speed,longitude,latitude,range,tilt,heading,longitude,latitude,altitude)

                        f=open(file, 'w')
                        f.write(output)
                        f.close()

        ser.close()

if __name__ == "__main__":
            main()

I have added the line print datablock, and here you can see what it prints.

Serving data from /dev/serial0 (9600 baud) to ./realtime/Realtime GPS.kml
['\x14\xd5\xd4\x95\x15\x89b\x8ab\x8a\x9ab\x82\x8ab\x8a\x8ab\x82\x82\x8abb\x82\xb2b\x8a\x8ab\x8a\x82\x9abb\x8a\x82b\x8a\x82b\x92\xba\x92bb\x8a\x92b\xb2\x92b\x92\x92\x82b\x92\x92R\xba\n']
['5)\x91\x1dA\x1dMY\xb1\xd1\xb1\xc9\xb1\xc5\xcd\xb1\xc5\xd1\xb1\xc5\xe5\xb1\xcd\xc9\xc5\xb1\xb1\xc5\xd5\xb1\xc5\xc1\xb1\xc5\xe1\xd9\xb1\xb1\xc5\xdd\xb1\xcd\xd9\xb1\xc1\xd5\xd5\xb1\xb1\xc5\xe5\xb1\xd1\xdd\xb1\xc1\xe1\xc5\xb1\xc9\xc1\xa9\xdd\xe55)\x91\x1dA\x1dMY\xb1\xd1\xb1\xcd\xb1\xc5\xcd\xb1\xc9\xc9\xb1\xc1\xe5\xb1\xc1\xc1\xd1\xb1\xb1\xc9\xd1\xb1\xd9\xdd\xb1\xc5\xd9\xd9\xb1\xc9\xe1\xb1\xc9\xd5\xb1\xc9\xc1\xb1\xc9\xd1\xc5\xb1\xc9\xc1\xb1\xc9\xd9\xb1\xb1\xb1\xc9\xc9\xa9\xd1\xe55)\x91\x1dA\x1dMY\xb1\xd1\xb1\xd1\xb1\xc5\xcd\xb1\xcd\xc9\xb1\xcd\xd5\xb1\xcd\xc1\xd1\xb1\xc9\xc9\xa9\xd1\t5)\x91\x1dA\x1d11\xb1\xb1\xb1\xb1\xb1\xc5\xc5\xd5\xd5\xcd\xdd\xb9\xc1\xc1\xb1Y\xb19\xa9\xd1\x155)\x91\x1dA\x1dMQ\xb1\xc5\xc5\xd5\xd5\xcd\xdd\xb9\xc1\xc1\xb1\xc1\xb9\xc1\xc1\xc1\xc1\xb1\xb1\xb1\xb1\xc5\xcd\xc1\xd9\xb1\xc5\xc5\xe5\xd1\xb1\xc5\xc9\xc9\xd5\xa9\xd9\x155)\x91\x1dAi\x11\x05\xb1\xc5\xc5\xd5\xd5\xcd\xdd\xb9\xc1\xc1\xb1\xc9\xe1\xb1\xc1\xd1\xb1\xc9\xc1\xc5\xdd\xb1\xc1\xc1\xb1\xc1\xc1\xa9\xd9\xe15)\x91\x1dA\x1d\tM\xb1\xc5\xc5\xd5\xd5\xcd\xdd\xb9\xc1\xc1\xb1\xb1\xb1\xb1\xb1\xb1\xb1\xa9\xd9\t5)\xff$GPRMC', '115538.00', 'V', '', '', '', '', '', '', '280417', '', '', 'N*7E\r\n']
['$GPVTG', '', '', '', '', '', '', '', '', 'N*30\r\n']
['$GPGGA', '115538.00', '', '', '', '', '0', '00', '99.99', '', '', '', '', '', '*6D\r\n']
['$GPGSA', 'A', '1', '', '', '', '', '', '', '', '', '', '', '', '', '99.99', '99.99', '99.99*30\r\n']
['$GPGSV', '4', '1', '13', '01', '11', '001', '', '06', '11', '103', '', '10', '10', '272', '', '12', '62', '220', '22*7A\r\n']
['$GPGSV', '4', '2', '13', '14', '19', '321', '', '15', '10', '186', '21', '17', '36', '055', '', '19', '47', '081', '21*7B\r\n']
['$GPGSV', '4', '3', '13', '22', '09', '004', '', '24', '67', '166', '29', '25', '20', '241', '18', '26', '', '', '22*43\r\n']
['$GPGSV', '4', '4', '13', '32', '35', '304', '22*4B\r\n']
['$GPGLL', '', '', '', '', '115538.00', 'V', 'N*41\r\n']
['$GPGST', '115538.00', '0.0000', '', '', '', '1305', '1193', '1224*64\r\n']
['$GPZDA', '115538.00', '28', '04', '2017', '00', '00*67\r\n']
['$GPGBS', '115538.00', '', '', '', '', '', '', '*64\r\n']
['$GPRMC', '115539.00', 'V', '', '', '', '', '', '', '280417', '', '', 'N*7F\r\n']

Traceback (most recent call last):
  File "/home/pi/gegpsd0.2.2", line 120, in <module>
    main()
  File "/home/pi/gegpsd0.2.2", line 59, in main
    latitude_in = string.atof(datablock[3])
  File "/usr/lib/python2.7/string.py", line 382, in atof
    return _float(s)
ValueError: could not convert string to float: 
>>> 

Please help

11
  • 1
    datablock[3] doesn't represent a decimal number. Commented Apr 26, 2017 at 19:38
  • 1
    Please provide a Minimal, Complete, and Verifiable example, see stackoverflow.com/help/mcve Commented Apr 26, 2017 at 19:39
  • I'd try printing datablock, as I suspect what it's getting is not what it expects (as Peter Wood mentioned). This code is nearly ten years old. Commented Apr 26, 2017 at 19:40
  • 2
    You shouldn't be trying to fix your python installation. It is extremely unlikely to be broken. Commented Apr 26, 2017 at 19:42
  • 1
    Also, string.atof is deprecated, you should use float() instead Commented Apr 26, 2017 at 19:43

1 Answer 1

1

The GPRMC should contain:

  1   220516     Time Stamp
  2   A          validity - A-ok, V-invalid
  3   5133.82    current Latitude
  4   N          North/South
  5   00042.24   current Longitude
  6   W          East/West
  7   173.8      Speed in knots
  8   231.8      True course
  9   130694     Date Stamp
  10  004.2      Variation
  11  W          East/West
  12  *70        checksum

The line you're failing on is:

['$GPRMC', '115539.00', 'V', '', '', '', '', '', '', '280417', '', '', 'N*7F\r\n']

which shows empty strings for all the positioning arguments. Notice that datablock[2] contains a validity argument, which is V for invalid.

I'd change your check to:

if line[0:6] == '$GPRMC' and datablock[2] == 'A':

So you only try to parse valid GPRMC messages and give that a go.


Digging in a bit more into the GPRMC navigation receiver warning:

A status of V means the GPS has a valid fix that is below an internal quality threshold, e.g. because the dilution of precision is too high or an elevation mask test failed.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.