philcole76
Posts: 9
Joined: Thu Mar 17, 2022 5:36 pm

GPS USB Not updating Longitude and latitude

Thu Mar 17, 2022 6:29 pm

Hello, starting to tear my hair out with my GPS USB dongle not seeming to update it's location.

I have my Pi 3A+ setup with a GPS dongle (Glonass Ublox7), it is showing the correct results in cgps -s and gspmon, though I think cgps -s isn't refreshing? It is setup as ttyACM0.and seems to be updating the values when I view it in gpsmon

I'm running the below python code to output the GPS time, latitude, longitude and altitude BUT it's outputting the same latitude, longitude and altitude values even when I go for a wander outside. It is however correctly updating the GPS time which suggests I do have some sort of GPS refresh going on?...and that my python script is working?

Code: Select all

from gps3 import gps3
import csv

gps_socket = gps3.GPSDSocket()
data_stream = gps3.DataStream()
gps_socket.connect()
gps_socket.watch()

for new_data in gps_socket:
    if new_data:
        data_stream.unpack(new_data)
        print(str(data_stream.TPV['time']) + " | " + str(data_stream.TPV['lat']) + " | " + str(data_stream.TPV['lon']) + " | " + str(data_stream.TPV['alt']))
        with open('Test_GPS.csv', 'a', newline='') as f:
            thewriter = csv.writer(f)
            thewriter.writerow([data_stream.TPV['time'], data_stream.TPV['lat'], data_stream.TPV['lon'], data_stream.TPV['alt']])
  
It outputs the below (note the time is changing):

LogTime,Latitude,Longitude,Altitude
2022-03-17T17:34:03.000Z,54.57SECRET,-1.048SECRET,49.4294
2022-03-17T17:34:04.000Z,54.57SECRET,-1.048SECRET,49.4294
2022-03-17T17:34:04.000Z,54.57SECRET,-1.048SECRET,49.4294
2022-03-17T17:34:04.000Z,54.57SECRET,-1.048SECRET,49.4294
2022-03-17T17:34:05.000Z,54.57SECRET,-1.048SECRET,49.4294
2022-03-17T17:34:05.000Z,54.57SECRET,-1.048SECRET,49.4294
2022-03-17T17:34:06.000Z,54.57SECRET,-1.048SECRET,49.4294
2022-03-17T17:34:06.000Z,54.57SECRET,-1.048SECRET,49.4294
2022-03-17T17:34:06.000Z,54.57SECRET,-1.048SECRET,49.4294
2022-03-17T17:34:07.000Z,54.57SECRET,-1.048SECRET,49.4294
2022-03-17T17:34:07.000Z,54.57SECRET,-1.048SECRET,49.4294
2022-03-17T17:34:08.000Z,54.57SECRET,-1.048SECRET,49.4294
2022-03-17T17:34:08.000Z,54.57SECRET,-1.048SECRET,49.4294
2022-03-17T17:34:08.000Z,54.57SECRET,-1.048SECRET,49.4294

Any thoughts on any settings I should be checking? I've been through many forum posts and google searches but don;t seem to be getting anywhere.

Many thanks!

User avatar
DougieLawson
Posts: 42306
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK

Re: GPS USB Not updating Longitude and latitude

Fri Mar 18, 2022 10:37 am

Is your GPS receiver sitting on a table in a fixed position?

What happens if you move it half a metre from it's current position?

I'm using the PyPi gps3 package (sudo pip3 install gps3). This is the GPSD program I use

Code: Select all

#!/usr/bin/python3
from gps3 import agps3
gps_socket = agps3.GPSDSocket()
data_stream = agps3.DataStream()
gps_socket.connect()
gps_socket.watch()
for new_data in gps_socket:
    if new_data:
        data_stream.unpack(new_data)
        print('Altitude = ', data_stream.alt)
        print('Latitude = ', data_stream.lat)
        print('Longitude = ', data_stream.lon)
That gets a static fix until I move the receiver dongle (in a USB hub).
Languages using left-hand whitespace for syntax are ridiculous

DMs sent on https://twitter.com/DougieLawson or LinkedIn will be answered next month.
Fake doctors - are all on my foes list.

The use of crystal balls and mind reading is prohibited.

philcole76
Posts: 9
Joined: Thu Mar 17, 2022 5:36 pm

Re: GPS USB Not updating Longitude and latitude

Fri Mar 18, 2022 11:43 am

Thanks for the suggestions and the code. The GPS dongle isn't in a fixed position, I am moving around with it, even wandering around my neighbourhood with a pi and a power bank :)

I'm thinking it may be more along the lines of the USB setup as in cgps it is showing the lat and lon straight away but not updating them after that. Whereas gpsmon is showing lots of slight fluctuations in the lat and lon but the python code seems to be logging the cgps values which are annoyingly staying static.

Weirdly the gps time value is updating.


Going to get googling on the USB setup and see if something there is blocking/affecting ttyACM0.

User avatar
DougieLawson
Posts: 42306
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK

Re: GPS USB Not updating Longitude and latitude

Fri Mar 18, 2022 4:25 pm

If you're running gpsd (https://gpsd.gitlab.io/gpsd/index.html) then there's a helper program supplied with that gpscsv.

Code: Select all

dougie@apollo:~/python$ gpscsv
time,lat,lon,altHAE
2022-03-18T16:22:38.000Z,51.2400000,-1.1300000,175.947
2022-03-18T16:22:39.000Z,51.2400000,-1.1300000,175.947
2022-03-18T16:22:39.000Z,51.2400000,-1.1300000,175.947
2022-03-18T16:22:40.000Z,51.2400000,-1.1300000,175.947
2022-03-18T16:22:41.000Z,51.2400000,-1.1300000,175.947
2022-03-18T16:22:41.000Z,51.2400000,-1.1300000,175.947
^C
dougie@apollo:~/python$

Note: I've mushed the lat/long so you don't get an exact fix on my house.
Languages using left-hand whitespace for syntax are ridiculous

DMs sent on https://twitter.com/DougieLawson or LinkedIn will be answered next month.
Fake doctors - are all on my foes list.

The use of crystal balls and mind reading is prohibited.

Doug_
Posts: 209
Joined: Tue Oct 08, 2019 6:40 pm

Re: GPS USB Not updating Longitude and latitude

Sat Mar 19, 2022 8:52 am

My ublox receivers send non standard NMEA sentence names. Do a simple check of the raw serial output first then look into integrating with gpsd :

Code: Select all

stty -F /dev/ttyACM0 115200 raw -echo
cat /dev/ttyACM0

User avatar
bensimmo
Posts: 5753
Joined: Sun Dec 28, 2014 3:02 pm
Location: East Yorkshire

Re: GPS USB Not updating Longitude and latitude

Sat Mar 19, 2022 10:40 am

Doug_ wrote:
Sat Mar 19, 2022 8:52 am
My ublox receivers send non standard NMEA sentence names. Do a simple check of the raw serial output first then look into integrating with gpsd :

Code: Select all

stty -F /dev/ttyACM0 115200 raw -echo
cat /dev/ttyACM0
Shouldn't be a problem with Ublox 7, as my 6 ad 8's work fine/Plug and play with gpsd on PiOS last time I tried.


----
gpsmon doesn't need gpsd and may be 'talking directly with the GNSS/GPS.

One thing is gps3 is old and not maintained and cannot do altitude (iirc) any more as GPSD has moved on. (alt / altHAE problem)
I couldn't get HDOP out of it either which I needed.


You could have a look at what cat /dev/ttyACM0 is giving OR go over to reading it directly via pyserial and pynmea2
https://github.com/Knio/pynmea2
This method should work (I still use it over gpsd again now)
or try this simple gpsd module https://github.com/tfeldmann/gpsdclient
Be interesting to find out.

philcole76
Posts: 9
Joined: Thu Mar 17, 2022 5:36 pm

Re: GPS USB Not updating Longitude and latitude

Mon Mar 21, 2022 9:16 pm

Many thanks guys for the suggestions, I've removed the gps3 code and used gpsd, I'm getting values but they still don't seem to be updating as I move the dongle around my garden. gpsmon is showing the values fluctuating.

Will try again tomorrow and take the pi out for a proper trip out to see what the signals show.
You could have a look at what cat /dev/ttyACM0 is giving
I'm getting the below, is this good or bad?

Code: Select all

pi@raspberrypi:~ $ cat /dev/ttyACM0
cat: /dev/ttyACM0: Device or resource busy
Thanks again for the help!

Doug_
Posts: 209
Joined: Tue Oct 08, 2019 6:40 pm

Re: GPS USB Not updating Longitude and latitude

Mon Mar 21, 2022 9:38 pm

This message means the port is in use by some other task, probably gpsd. Check with

Code: Select all

ps -ax

And kill any of the jobs that may be using the port.

Code: Select all

kill -9 <relevant pid>

philcole76
Posts: 9
Joined: Thu Mar 17, 2022 5:36 pm

Re: GPS USB Not updating Longitude and latitude

Tue Mar 22, 2022 5:04 pm

I could see:
444 ? S<sl 0:00 /usr/sbin/gpsd -n /dev/ttyACM0
so killed it using:

Code: Select all

sudo kill -9 444
cat /dev/ttyACM0 then gave me:

Code: Select all

pi@raspberrypi:~ $ cat /dev/ttyACM0
�b��
"�������o�����b0��
+d���� 6
�0    � O	x��
|�
��|�hPC�y�b��
 ^�$GPTXT,01,01,02,u-blox ag - www.u-blox.com*50
$GPTXT,01,01,02,HW  UBX-G70xx   00070000 *77
$GPTXT,01,01,02,ROM CORE 1.00 (59842) Jun 27 2012 17:43:52*59
$GPTXT,01,01,02,PROTVER 14.00*1E
$GPTXT,01,01,02,ANTSUPERV=AC SD PDoS SR*20
$GPTXT,01,01,02,ANTSTATUS=OK*3B
$GPTXT,01,01,02,LLC FFFFFFFF-FFFFFFFD-FFFFFFFF-FFFFFFFF-FFFFFFF9*53
"�����j���������l���l�b0���
!�V
+d����
pi@raspberrypi:~ $ 
Running the python code then gave me co-ordinates (shown below but suppressed/muddled by me for security) but they didn't update despite me moving the pi around my house and garden.

2022-03-22T16:59:24.000Z|54.500000000|-1.048000000|70.3599
2022-03-22T16:59:24.000Z|54.500000000|-1.048000000|70.3599
2022-03-22T16:59:25.000Z|54.500000000|-1.048000000|70.3599
2022-03-22T16:59:26.000Z|54.500000000|-1.048000000|70.3599
2022-03-22T16:59:26.000Z|54.500000000|-1.048000000|70.3599
2022-03-22T16:59:27.000Z|54.500000000|-1.048000000|70.3599
2022-03-22T16:59:28.000Z|54.500000000|-1.048000000|70.3599
2022-03-22T16:59:28.000Z|54.500000000|-1.048000000|70.3599
2022-03-22T16:59:29.000Z|54.500000000|-1.048000000|70.3599

Now after ending the python code ttyACM0 is saying it is busy, is that to be expected?

User avatar
rpiMike
Posts: 2231
Joined: Fri Aug 10, 2012 12:38 pm
Location: Cumbria, UK

Re: GPS USB Not updating Longitude and latitude

Tue Mar 22, 2022 5:44 pm

Why not read and parse the serial data directly?

Code: Select all

import serial,time,pynmea2

port = '/dev/ttyACM0'
baud = 9600

serialPort = serial.Serial(port, baudrate = baud, timeout = 0.5)
while True:
    str = serialPort.readline().decode().strip()
    #print(str)
    if str.find('GGA') > 0:
        msg = pynmea2.parse(str)
        print("  Timestamp: %s -- Lat: %s %s -- Lon: %s %s -- Altitude: %s %s -- Satellites: %s" % (msg.timestamp,msg.lat,msg.lat_dir,msg.lon,msg.lon_dir,msg.altitude,msg.altitude_units,msg.num_sats))
    time.sleep(0.1)

philcole76
Posts: 9
Joined: Thu Mar 17, 2022 5:36 pm

Re: GPS USB Not updating Longitude and latitude

Tue Mar 22, 2022 7:23 pm

rpiMike wrote:
Tue Mar 22, 2022 5:44 pm
Why not read and parse the serial data directly?

Code: Select all

import serial,time,pynmea2

port = '/dev/ttyACM0'
baud = 9600

serialPort = serial.Serial(port, baudrate = baud, timeout = 0.5)
while True:
    str = serialPort.readline().decode().strip()
    #print(str)
    if str.find('GGA') > 0:
        msg = pynmea2.parse(str)
        print("  Timestamp: %s -- Lat: %s %s -- Lon: %s %s -- Altitude: %s %s -- Satellites: %s" % (msg.timestamp,msg.lat,msg.lat_dir,msg.lon,msg.lon_dir,msg.altitude,msg.altitude_units,msg.num_sats))
    time.sleep(0.1)

Hi Mike, I'm not familiar with the serial method, my initial googling took me down the gpsd route. Have tried running your suggested code (many thanks!) and am getting the below error with the decode() element. Will look into how to get around that, hopefully it's not an error due to my serial port settings

Code: Select all

Traceback (most recent call last):
  File "/home/pi/gps_s.py", line 10, in <module>
    str = serialPort.readline().decode().strip()
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xb5 in position 0: invalid start byte

User avatar
DougieLawson
Posts: 42306
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK

Re: GPS USB Not updating Longitude and latitude

Tue Mar 22, 2022 7:30 pm

philcole76 wrote:
Tue Mar 22, 2022 7:23 pm

Hi Mike, I'm not familiar with the serial method, my initial googling took me down the gpsd route. Have tried running your suggested code (many thanks!) and am getting the below error with the decode() element. Will look into how to get around that, hopefully it's not an error due to my serial port settings

Code: Select all

Traceback (most recent call last):
  File "/home/pi/gps_s.py", line 10, in <module>
    str = serialPort.readline().decode().strip()
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xb5 in position 0: invalid start byte
I got exactly the same error so I've dumped the direct serial read and went back to GPSD (because it works and doesn't fall foul of a python quirk).
Languages using left-hand whitespace for syntax are ridiculous

DMs sent on https://twitter.com/DougieLawson or LinkedIn will be answered next month.
Fake doctors - are all on my foes list.

The use of crystal balls and mind reading is prohibited.

Doug_
Posts: 209
Joined: Tue Oct 08, 2019 6:40 pm

Re: GPS USB Not updating Longitude and latitude

Tue Mar 22, 2022 8:22 pm

Curious that only part of the output is readable. Did you use the stty command prior to ‘cat’?
Possible you may have a defective unit.
Every GPS USB dongle I have used to date defaults to 115200 baud btw.
Last edited by Doug_ on Tue Mar 22, 2022 8:48 pm, edited 2 times in total.

User avatar
rpiMike
Posts: 2231
Joined: Fri Aug 10, 2012 12:38 pm
Location: Cumbria, UK

Re: GPS USB Not updating Longitude and latitude

Tue Mar 22, 2022 8:22 pm

philcole76 wrote:
Tue Mar 22, 2022 7:23 pm
rpiMike wrote:
Tue Mar 22, 2022 5:44 pm
Why not read and parse the serial data directly?

Code: Select all

import serial,time,pynmea2

port = '/dev/ttyACM0'
baud = 9600

serialPort = serial.Serial(port, baudrate = baud, timeout = 0.5)
while True:
    str = serialPort.readline().decode().strip()
    #print(str)
    if str.find('GGA') > 0:
        msg = pynmea2.parse(str)
        print("  Timestamp: %s -- Lat: %s %s -- Lon: %s %s -- Altitude: %s %s -- Satellites: %s" % (msg.timestamp,msg.lat,msg.lat_dir,msg.lon,msg.lon_dir,msg.altitude,msg.altitude_units,msg.num_sats))
    time.sleep(0.1)

Hi Mike, I'm not familiar with the serial method, my initial googling took me down the gpsd route. Have tried running your suggested code (many thanks!) and am getting the below error with the decode() element. Will look into how to get around that, hopefully it's not an error due to my serial port settings

Code: Select all

Traceback (most recent call last):
  File "/home/pi/gps_s.py", line 10, in <module>
    str = serialPort.readline().decode().strip()
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xb5 in position 0: invalid start byte
Did you run it with 'python3 gps_s.py' ?

philcole76
Posts: 9
Joined: Thu Mar 17, 2022 5:36 pm

Re: GPS USB Not updating Longitude and latitude

Tue Mar 22, 2022 8:40 pm

rpiMike wrote:
Tue Mar 22, 2022 8:22 pm

Did you run it with 'python3 gps_s.py' ?
Hi Mike, I did and unfortunately still got the "codec can't decode byte" error

philcole76
Posts: 9
Joined: Thu Mar 17, 2022 5:36 pm

Re: GPS USB Not updating Longitude and latitude

Tue Mar 22, 2022 8:44 pm

Doug_ wrote:
Tue Mar 22, 2022 8:22 pm
Did you use the stty command prior to ‘cat’?
Every GPS USB dongle I have used to date defaults to 115200 baud btw.
Hi Doug, yes I used the

Code: Select all

stty -F /dev/ttyACM0 115200 raw -echo
command before I ran

Code: Select all

cat /dev/ttyACM0

philcole76
Posts: 9
Joined: Thu Mar 17, 2022 5:36 pm

Re: GPS USB Not updating Longitude and latitude

Tue Mar 22, 2022 8:50 pm

DougieLawson wrote:
Tue Mar 22, 2022 7:30 pm
I got exactly the same error so I've dumped the direct serial read and went back to GPSD (because it works and doesn't fall foul of a python quirk).
Hi Dougie, glad it wasn't just me getting that error. Any chance you could share your /etc/default/gpsd setup?...I can then see if my setup is near to yours.

Cheers!

Doug_
Posts: 209
Joined: Tue Oct 08, 2019 6:40 pm

Re: GPS USB Not updating Longitude and latitude

Tue Mar 22, 2022 8:56 pm

Very odd output then. Seems part of the message is at one baud rate and the rest at another.
I am unfamiliar with the other tools you were using - perhaps one of these has reconfigured to unit in some way.
Try other baudrates by substituting the 115200 in the stty command, say 9600, 19200, and up.

User avatar
rpiMike
Posts: 2231
Joined: Fri Aug 10, 2012 12:38 pm
Location: Cumbria, UK

Re: GPS USB Not updating Longitude and latitude

Tue Mar 22, 2022 8:58 pm

Just added a try except around the decode.

Did you try changing the baud rate - my GPS seems to adjust itself!

Code: Select all

import serial,time,pynmea2

port = '/dev/ttyACM0'
baud = 9600

serialPort = serial.Serial(port, baudrate = baud, timeout = 0.5)
while True:
    str = ''
    try:
        str = serialPort.readline().decode().strip()
    except Exception as e:
        print(e)
    print(str)
    if str.find('GGA') > 0:
        msg = pynmea2.parse(str)
        print("  Timestamp: %s -- Lat: %s %s -- Lon: %s %s -- Altitude: %s %s -- Satellites: %s" % (msg.timestamp,msg.lat,msg.lat_dir,msg.lon,msg.lon_dir,msg.altitude,msg.altitude_units,msg.num_sats))
    time.sleep(0.1)

ame
Posts: 6091
Joined: Sat Aug 18, 2012 1:21 am
Location: New Zealand

Re: GPS USB Not updating Longitude and latitude

Tue Mar 22, 2022 9:22 pm

DougieLawson wrote:
Tue Mar 22, 2022 7:30 pm
philcole76 wrote:
Tue Mar 22, 2022 7:23 pm

Hi Mike, I'm not familiar with the serial method, my initial googling took me down the gpsd route. Have tried running your suggested code (many thanks!) and am getting the below error with the decode() element. Will look into how to get around that, hopefully it's not an error due to my serial port settings

Code: Select all

Traceback (most recent call last):
  File "/home/pi/gps_s.py", line 10, in <module>
    str = serialPort.readline().decode().strip()
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xb5 in position 0: invalid start byte
I got exactly the same error so I've dumped the direct serial read and went back to GPSD (because it works and doesn't fall foul of a python quirk).
It has nothing to do with Python. Your bias is showing.

It seems that the GPS is in binary mode, so it is not sending ASCII NMEA sentences. Which is why the data can't be decoded as such.

gpsd likes to "take over" the GPS receiver, and switch it to binary mode if it's capable. It's possible that the GPS has been left in binary mode before this script was run, meaning that the incoming data can not be parsed.

Read about gpsd here:
https://gpsd.gitlab.io/gpsd/gpsd.html

There is also gpsctl which can switch the GPS between modes:
https://gpsd.gitlab.io/gpsd/gpsctl.html
Hmm. What can I put here?

Doug_
Posts: 209
Joined: Tue Oct 08, 2019 6:40 pm

Re: GPS USB Not updating Longitude and latitude

Tue Mar 22, 2022 9:26 pm

rpiMike are you using a uBlox GPS dongle?

philcole76. Try disconnecting and reconnecting the dongle, perhaps any baudrate change is not permanent.

Doug_
Posts: 209
Joined: Tue Oct 08, 2019 6:40 pm

Re: GPS USB Not updating Longitude and latitude

Tue Mar 22, 2022 9:32 pm

ame wrote:
Tue Mar 22, 2022 9:22 pm
gpsd likes to "take over" the GPS receiver, and switch it to binary mode if it's capable. It's possible that the GPS has been left in binary mode before this script was run, meaning that the incoming data can not be parsed.
Good grief - how to unnecessarily complicate something. No wonder it is not working.

A few lines Python is all you really need.

ame
Posts: 6091
Joined: Sat Aug 18, 2012 1:21 am
Location: New Zealand

Re: GPS USB Not updating Longitude and latitude

Tue Mar 22, 2022 9:41 pm

Doug_ wrote:
Tue Mar 22, 2022 9:32 pm
ame wrote:
Tue Mar 22, 2022 9:22 pm
gpsd likes to "take over" the GPS receiver, and switch it to binary mode if it's capable. It's possible that the GPS has been left in binary mode before this script was run, meaning that the incoming data can not be parsed.
Good grief - how to unnecessarily complicate something. No wonder it is not working.

A few lines Python is all you really need.
Well, yes and no. At the basic level, yes. Parsing the NMEA sentences is simple and straightforward, and should always work. However, some GPS receivers have more functionality, which you could code yourself for one model of GPS, or you could contribute to a library that will support many models of GPS. This is explained in the gpsd link I posted.

gpsd also performs a lot of useful tasks, such as making one GPS accessible by several clients.
Hmm. What can I put here?

User avatar
Gavinmc42
Posts: 6912
Joined: Wed Aug 28, 2013 3:31 am

Re: GPS USB Not updating Longitude and latitude

Tue Mar 22, 2022 11:42 pm

The few times it did not get a fix was when the antenna was loose.
I have bit of blank copper PCB between the Pi and the module as a shield too.

I use the uBlox U-center software to program the modules to do exactly what I need.
https://www.u-blox.com/en/product/u-center
Handy for initial baud rate and data output testing too.
Make sure the settings are saved to EEPROM ;)

I found I don't need to wander around the garden, just sitting it a parked car the unit wanders all over the driveway.

Don't like complex Linux software, not needed for most simple apps.
https://github.com/Gavinmc42/GPS-logger
https://github.com/Gavinmc42/GPSsimulator

Patience is needed for first fix too, tables, almanac etc.
After that I found if I turned it on inside the house, walked outside and down the stairs into the car it would have a fix before I had got the car started and moving.
I'm dancing on Rainbows.
Raspberries are not Apples or Oranges

User avatar
rpiMike
Posts: 2231
Joined: Fri Aug 10, 2012 12:38 pm
Location: Cumbria, UK

Re: GPS USB Not updating Longitude and latitude

Wed Mar 23, 2022 12:09 am

Doug_ wrote:
Tue Mar 22, 2022 9:26 pm
rpiMike are you using a uBlox GPS dongle?

philcole76. Try disconnecting and reconnecting the dongle, perhaps any baudrate change is not permanent.
I’m using a ublox neo-m8p rtk GPS.

Return to “Python”