0

I used this USB GPS dongle to retrieve GPS traces of user's trip. Device configuration is successful.

pi@raspberrypi:~ $ cgps -s
┌───────────────────────────────────────────┐┌──────────────────Seen 17/Used  7┐
│ Time:        2022-01-19T20:12:59.000Z (18)││GNSS   PRN  Elev   Azim   SNR Use│
│ Latitude:         11.66971820 N           ││GP 10   10  56.0  139.0  43.0  Y │
│ Longitude:         8.26336710 E           ││GP 16   16  31.0  334.0  22.0  Y │
│ Alt (HAE, MSL):   1333.127,   1257.352 ft ││GP 18   18  28.0   26.0  20.0  Y │
│ Speed:             0.06 mph               ││GP 23   23  49.0   80.0  30.0  Y │
│ Track (true, var):     0.0,  -0.0     deg ││GP 27   27  17.0  312.0  19.0  Y │
│ Climb:             0.20 ft/min            ││GP 31   31  42.0  210.0  26.0  Y │
│ Status:         3D DGPS FIX (19 secs)     ││SB127   40  35.0  101.0  34.0  Y │
│ Long Err  (XDOP, EPX):  0.86, +/- 10.6 ft ││GP 21   21   5.0  237.0   0.0  N │
│ Lat Err   (YDOP, EPY):  1.04, +/- 12.8 ft ││GP 25   25  17.0  134.0  16.0  N │
│ Alt Err   (VDOP, EPV):  3.83, +/-  843 ft ││GP 26   26  67.0  350.0  17.0  N │
│ 2D Err    (HDOP, CEP):  1.34, +/-  103 ft ││GP 29   29   7.0   71.0  25.0  N │
│ 3D Err    (PDOP, SEP):  4.06, +/-  288 ft ││GP 32   32  10.0  173.0  22.0  N │
│ Time Err  (TDOP):       2.34              ││SB124   37  69.0  131.0   0.0  N │
│ Geo Err   (GDOP):       4.69              ││SB126   39  66.0  124.0  32.0  N │
│ Speed Err (EPS):       +/-  1.4 mph       ││QZ  1  193   n/a    0.0   0.0  N │
│ Track Err (EPD):        n/a               ││QZ  2  194   n/a    0.0   0.0  N │
│ Time offset:            0.105887000 s     ││QZ  4  196   n/a    0.0   0.0  N │
│ Grid Square:            JK41dq10          ││                                 │
└───────────────────────────────────────────┘└─────────────────────────────────┘

But the GPS values don't change even as I move. It seems the device keeps the first value received all the time.

My Code:

import time
import gpsd

def get_location():
   gpsd.connect()
   packet = gpsd.get_current()
   lat, lon = packet.position()

   return lat, lon

while True:
    lat, lon = get_location()
    print(f'Lat: {lat} Long: {lon}')
    time.sleep(1)

Output:

Lat: 11.6697182 Long: 8.2633671
Lat: 11.6697182 Long: 8.2633671
Lat: 11.6697182 Long: 8.2633671
Lat: 11.6697182 Long: 8.2633671
Lat: 11.6697182 Long: 8.2633671
Lat: 11.6697182 Long: 8.2633671
...

EDIT

moved gpsd.connect() to top of script, comment-out time.sleep(1) to read data fast as suggested in comment. No change in GPS when moved around.

import time
import gpsd

gpsd.connect()

def get_location():
    packet = gpsd.get_current()
    lat, lon = packet.position()
    time = packet.time

    return lat, lon, time # to check _time received

while True:
    lat, lon, time_received = get_location()
    print(f'Lat: {lat} Long: {lon} received at {time_received}')
    #time.sleep(1)

Output:

Lat: 11.6644024 Long: 8.265238 received at 2022-01-20T21:03:54.000Z
Lat: 11.6644024 Long: 8.265238 received at 2022-01-20T21:03:54.000Z
Lat: 11.6644024 Long: 8.265238 received at 2022-01-20T21:03:54.000Z
Lat: 11.6644024 Long: 8.265238 received at 2022-01-20T21:03:54.000Z
Lat: 11.6644024 Long: 8.265238 received at 2022-01-20T21:03:54.000Z
Lat: 11.6644024 Long: 8.265238 received at 2022-01-20T21:03:54.000Z
Lat: 11.6644024 Long: 8.265238 received at 2022-01-20T21:03:54.000Z
Lat: 11.6644024 Long: 8.265238 received at 2022-01-20T21:03:54.000Z
Lat: 11.6644024 Long: 8.265238 received at 2022-01-20T21:03:54.000Z
Lat: 11.6644024 Long: 8.265238 received at 2022-01-20T21:03:55.000Z
Lat: 11.6644024 Long: 8.265238 received at 2022-01-20T21:03:55.000Z
Lat: 11.6644024 Long: 8.265238 received at 2022-01-20T21:03:55.000Z
Lat: 11.6644024 Long: 8.265238 received at 2022-01-20T21:03:55.000Z
Lat: 11.6644024 Long: 8.265238 received at 2022-01-20T21:03:55.000Z
Lat: 11.6644024 Long: 8.265238 received at 2022-01-20T21:03:55.000Z
Lat: 11.6644024 Long: 8.265238 received at 2022-01-20T21:03:55.000Z
Lat: 11.6644024 Long: 8.265238 received at 2022-01-20T21:03:55.000Z
Lat: 11.6644024 Long: 8.265238 received at 2022-01-20T21:03:55.000Z
Lat: 11.6644024 Long: 8.265238 received at 2022-01-20T21:03:55.000Z

EDIT-2

Following CoderMike answer, I proceeded to check with my device as follows:

pi@raspberrypi:~ $ dmesg
[    4.684442] systemd[1]: Set hostname to <raspberrypi>.
[    4.764797] usb 1-1.2: New USB device found, idVendor=1a86, idProduct=7523, bcdDevice= 2.64
[    4.764842] usb 1-1.2: New USB device strings: Mfr=0, Product=2, SerialNumber=0
[    4.764864] usb 1-1.2: Product: USB Serial
[    5.060743] usb 1-1.5: new high-speed USB device number 5 using dwc_otg
[    5.212089] usb 1-1.5: New USB device found, idVendor=0cf3, idProduct=9271, bcdDevice= 1.08
[    5.212134] usb 1-1.5: New USB device strings: Mfr=16, Product=32, SerialNumber=48
[    5.212157] usb 1-1.5: Product: USB2.0 WLAN
[    5.212176] usb 1-1.5: Manufacturer: ATHEROS
[    5.212193] usb 1-1.5: SerialNumber: 12345
[    5.510653] usb 1-1.4: new full-speed USB device number 6 using dwc_otg
[    5.643658] usb 1-1.4: New USB device found, idVendor=1546, idProduct=01a7, bcdDevice= 1.00
[    5.643700] usb 1-1.4: New USB device strings: Mfr=1, Product=2, SerialNumber=0
[    5.643721] usb 1-1.4: Product: u-blox 7 - GPS/GNSS Receiver
[    5.643741] usb 1-1.4: Manufacturer: u-blox AG - www.u-blox.com
[    6.052589] uart-pl011 20201000.serial: no DMA platform data
.
.
.
[   23.598475] vc4_hdmi 20902000.hdmi: Could not register sound card: -517
[   24.890816] Voltage normalised (0x00000000)
[   26.689587] cdc_acm 1-1.4:1.0: ttyACM0: USB ACM device
[   26.731357] vc4-drm soc:gpu: bound 20400000.hvs (ops vc4_hvs_ops [vc4])

I can confirm gpsd, gpsd-clients, and gpsd-py3 all installed.

Starting up gpsd:

pi@raspberrypi:~ $ sudo systemctl stop gpsd
Warning: Stopping gpsd.service, but it can still be activated by:
  gpsd.socket
pi@raspberrypi:~ $ sudo systemctl stop gpsd.socket
pi@raspberrypi:~ $ sudo systemctl disable gpsd.socket
pi@raspberrypi:~ $ sudo gpsd /dev/ttyACM0 -F /var/run/gpsd.sock

Then run my code above but the gps didn't change.

  1. Using pynmea2 encountered UnicodeDecodeError:
import time, serial, 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 -- Altittude: %s %s -- Satellites: %s' % (msg.timestamp, msg.lat, msg.lat_dir, msg.lon, msg.lon_dir, msg.altitude, msg.altitude_units,msg.sats))
    time.sleep(0.01)

Error:

pi@raspberrypi:~ $ python my_location.py
$GPTXT,01,01,02,u-blox ag - www.u-blox.com*50
$GPTXT,01,01,02,HW  UBX-G70xx   00070000 FF7FFFFFo*69
$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-FFFFFFFF-FFFFFFFF-FFFFFFFF-FFFFFFFD*2C
Traceback (most recent call last):
  File "/home/pi/my_location.py", line 8, in <module>
    str = serialPort.readline().decode().strip()
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xb5 in position 0: invalid start byte

It seems the device isn't using UTF-8 encoding:

pi@raspberrypi:~ $ gpsmon
pi@raspberrypi:~ $
pi@raspberrypi:~ $
tcp://localhost:2947          JSON slave driver>
(82) {"class":"VERSION","release":"3.22","rev":"3.22","proto_major":3,"proto_minor":14}
(301) {"class":"DEVICES","devices":[{"class":"DEVICE","path":"/dev/ttyACM0","driver":"u-blox","subtype":"SW 1.00 (59842),HW 00070000","subtype1":"PROTVER 14.00,GPS;SBAS;GLO;QZSS"
,"activated":"2022-01-27T19:56:21.856Z","flags":1,"native":1,"bps":9600,"parity":"N","stopbits":1,"cycle":1.00,"mincycle":0.02}]}
(122) {"class":"WATCH","enable":true,"json":false,"nmea":false,"raw":2,"scaled":false,"timing":false,"split24":false,"pps":true}
pi@raspberrypi:~ $ gpsmon
pi@raspberrypi:~ $
tcp://localhost:2947          JSON slave driver>
(82) {"class":"VERSION","release":"3.22","rev":"3.22","proto_major":3,"proto_minor":14}
(301) {"class":"DEVICES","devices":[{"class":"DEVICE","path":"/dev/ttyACM0","driver":"u-blox","subtype":"SW 1.00 (59842),HW 00070000","subtype1":"PROTVER 14.00,GPS;SBAS;GLO;QZSS"
,"activated":"2022-01-27T19:56:34.845Z","flags":1,"native":1,"bps":9600,"parity":"N","stopbits":1,"cycle":1.00,"mincycle":0.02}]}
(122) {"class":"WATCH","enable":true,"json":false,"nmea":false,"raw":2,"scaled":false,"timing":false,"split24":false,"pps":true}
/dev/ttyACM0                  u-blox>
┌──────────────────────────┐┌─────────────────────────────────────────────────┐ or":14}
│Ch PRN  Az  El S/N Flag U ││ECEF Pos: +6182815.42m +898051.03m +1281690.83m  │ ver":"u-blox","subtype":"SW 1.00 (59842),HW 00070000","subtype1":"PROTVER 14.00,GPS;SBAS;GLO;QZSS"
│ 0  10 126  61  32 070d Y ││ECEF Vel:     -0.06m/s     +0.02m/s     -0.02m/s │ :"N","stopbits":1,"cycle":1.00,"mincycle":0.02}]}
│ 1  16 335  38  31 070d Y ││                                                 │ false,"timing":false,"split24":false,"pps":true}
│ 2  18  29  21  29 040d Y ││LTP Pos:  11.669152695°   8.264389309°   538.71m │
│ 3  21 243  10  25 0304   ││LTP Vel:    0.00m/s   0.0°   0.00m/s             │
│ 4  22 186   9  11 040d Y ││                                                 │
│ 5  23  68  47  21 040d Y ││Time: 4 19:57:05.00                              │
│ 6  25 138  12  17 040d Y ││Time GPS: 2194+417425.000     Day: 4             │
│ 7  26 353  76  22 040d Y ││                                                 │
│ 8  27 319  19  32 070d Y ││Est Pos Err   9.85m Est Vel Err   0.00m/s        │
│ 9  29  78   5  12 040d Y ││PRNs: 10 PDOP:  1.4 Fix 0x03 Flags 0xdd          │
│10  31 204  36  39 070d Y │└─────────────────── NAV_SOL ─────────────────────┘
│11  32 167  12   0 010c   │┌─────────────────────────────────────────────────┐
│12 120 245  59   0 0110   ││DOP [H]  0.8 [V]  1.1 [P]  1.4 [T]  0.7 [G]  1.5 │
│13 124 131  69   0 0110   │└─────────────────── NAV_DOP ─────────────────────┘
│14 126 124  66   0 0110   │┌─────────────────────────────────────────────────┐
│15 194   0 -91   0 0110   ││TOFF: -0.126272301       PPS:      N/A           │
└────── NAV_SVINFO ────────┘└─────────────────────────────────────────────────┘
(26) b56201041200c856e118ae009d004b007e005e0035004d002270
(24) b56201201000c856e11814380300920812070f00000059ad
(60) b56201063400b05ae118b1300300920803dd5c3ada242b515a0586b4a307db030000000000000000000001000000190000008b00020a84d30100d620
(232) b5620130e000b05ae11812030000030a0d07203d7e00dcffffff09100d071f264f01ab01000005120d071e151d000c02000011150403190af300000000000d160d040b09ba00dffbffff02170d04152f44004afeffff
08190d04130c8a0096feffff001a0d04164c610149ffffff0a1b0d0720133f0179ffffff0c1d0d040c054e001af9ffff011f0d072824cc00040000000b200c03120ca70064f9ffff07781001003bf50000000000047c100100
45830000000000067e100100427c000000000010c1100100a50000000000000fc4100100a50000000000000ec5100100a5000000000000276f
(26) b56201041200b05ae1189a008b0042006f00530033004100b7e0
(24) b56201201000b05ae118b1300300920812070f000000da6d

Not sure why this device behaves different from most answers I read.

6
  • What happens if you move 'gpsd.connect()' to the start of your code?
    – CoderMike
    Jan 19, 2022 at 22:47
  • Oh - are you reading too slowly - data is serial - read as fast as you can. Remove your time.sleep(1).
    – CoderMike
    Jan 19, 2022 at 23:43
  • @CoderMike gpsd.connect() moved to top, time.sleep(1) commented out (as in eidt), no change.
    – arilwan
    Jan 20, 2022 at 21:20
  • What command are you using to point gpsd to your gps?
    – CoderMike
    Jan 20, 2022 at 21:21
  • I do not understand what you mean, but I simply installed gpsd-py3 and import the library in python as in the code.
    – arilwan
    Jan 20, 2022 at 21:36

1 Answer 1

1

I got your code working with my USB GPS using the following:

dmesg shows my USB GPS connected on ttyACM0:

[557325.587821] usb 1-1.3: New USB device found, idVendor=1546, idProduct=01a8, bcdDevice= 3.01
[557325.587827] usb 1-1.3: New USB device strings: Mfr=1, Product=2, SerialNumber=0
[557325.587833] usb 1-1.3: Product: u-blox GNSS receiver
[557325.587838] usb 1-1.3: Manufacturer: u-blox AG - www.u-blox.com
[557325.590493] cdc_acm 1-1.3:1.0: ttyACM0: USB ACM device

Install gpsd, clients and python library:

sudo apt install gpsd,gpsd-clients
pip3 install gpsd-py3

Start gpsd:

sudo systemctl stop gpsd
sudo systemctl stop gpsd.socket
sudo systemctl disable gpsd.socket
sudo gpsd /dev/ttyACM0 -F /var/run/gpsd.sock

Results from your code:

Lat: 54.2622055 Long: -3.142765667 received at 2022-01-20T22:08:57.000Z
Lat: 54.262192 Long: -3.142775167 received at 2022-01-20T22:08:58.000Z
Lat: 54.2621855 Long: -3.142767833 received at 2022-01-20T22:08:59.000Z
Lat: 54.2621855 Long: -3.1427735 received at 2022-01-20T22:09:00.000Z
Lat: 54.262178167 Long: -3.142792667 received at 2022-01-20T22:09:01.000Z
Lat: 54.262181833 Long: -3.142791167 received at 2022-01-20T22:09:02.000Z

Personally I prefer using the serial and pynmea2 libraries to read GPS data:

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 -- Lon: %s -- Altitude: %s %s -- Satellites: %s" % (msg.timestamp,round(msg.latitude,6),round(msg.longitude,6),msg.altitude,msg.altitude_units,msg.num_sats))
    time.sleep(0.01)

Output:

  Timestamp: 11:33:46 -- Lat: 54.262155 -- Lon: -3.142469 -- Altitude: 136.3 M -- Satellites: 03
  Timestamp: 11:33:47 -- Lat: 54.262143 -- Lon: -3.142469 -- Altitude: 136.3 M -- Satellites: 03
  Timestamp: 11:33:48 -- Lat: 54.262169 -- Lon: -3.142494 -- Altitude: 136.3 M -- Satellites: 04
  Timestamp: 11:33:49 -- Lat: 54.262149 -- Lon: -3.14249 -- Altitude: 136.0 M -- Satellites: 04
  Timestamp: 11:33:50 -- Lat: 54.262141 -- Lon: -3.142446 -- Altitude: 136.0 M -- Satellites: 04
  Timestamp: 11:33:51 -- Lat: 54.262139 -- Lon: -3.142444 -- Altitude: 136.0 M -- Satellites: 04
4
  • Thank you for this. I would have to admit the problem is with my device. I followed all steps in your answer correctly, yet no change in gps.
    – arilwan
    Jan 27, 2022 at 20:12
  • How log have you left the gps running for - it can take 15 minutes or so for my GPS to pick up satellites positioned on a window sill. You should run my code with python3 ('python3 my_location.py').
    – CoderMike
    Jan 27, 2022 at 21:15
  • So this device is not suitable for mobile application where user's gps traces is required as he moves if it could take up to 10mins to read current location...?
    – arilwan
    Jan 27, 2022 at 21:20
  • 1
    I don't know about your device, I'm sure mine would be quicker outside in the open, its just the initial receiving of data, its quick once it starts receiving. I have another GPS that connects quickly but cost a lot more money.
    – CoderMike
    Jan 27, 2022 at 21:27

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.