beatsal
Posts: 148
Joined: Tue Mar 21, 2017 2:46 pm

GPS module in Python

Fri Jan 12, 2018 1:07 am

From another post, I tried the following line to import the gps module into a python script but get error 'no module named gps'
sudo apt-get install python{,3}-rpi.gps (this line in PI terminal)
I then used the foll. line in python script:
from gps import *

Eliter
Posts: 23
Joined: Sat Dec 26, 2015 7:28 am

Re: GPS module in Python

Fri Jan 12, 2018 5:06 am

I do not code in Python, but I do believe apt-get packages generally do not contain the characters: "{", "}", ",", or ".".

For future advice, I recommend helping us help you better by providing more information on your post next time, because it is unclear what you want.

The command looks like you are trying to install the Python package. My guess on installing Python is the command: "sudo apt-get install python". What I am guessing is that you are confusing the concept of being able to use/interact with programming stuff from another library/file. My recommendation is to figure out how to do that in your code, NOT in the command line. Some typical keywords I recommend you lookup in your Python language book (or you can find one--probably free online) are: "include", "require", "import", "uses", or "inherit".

User avatar
DougieLawson
Posts: 30935
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website

Re: GPS module in Python

Fri Jan 12, 2018 11:24 am

Eliter wrote:
Fri Jan 12, 2018 5:06 am
I do not code in Python, but I do believe apt-get packages generally do not contain the characters: "{", "}", ",", or ".".
That's bash syntax to install both the python2 and python3 versions python-rpi.gps & python3-rpi.gps with a single command.
Microprocessor, Raspberry Pi & Arduino Hacker
Mainframe database troubleshooter
MQTT Evangelist
Twitter: @DougieLawson

Since 2012: 1B*5, 2B*2, B+, A+, Zero*2, 3B*3

Please post ALL technical questions on the forum. Do not send private messages.

dgordon42
Posts: 720
Joined: Tue Aug 13, 2013 6:55 pm
Location: Dublin, Ireland

Re: GPS module in Python

Fri Jan 12, 2018 12:07 pm

Code: Select all

From another post, I tried the following line to import the gps module into a python script but get error 'no module named gps'
Try running:

Code: Select all

sudo apt install python-gps
I don't see a Python3 version in the Raspbian repositories, so you will have to just use Python 2.

Hope this helps,
Dave.

scotty101
Posts: 2441
Joined: Fri Jun 08, 2012 6:03 pm

Re: GPS module in Python

Fri Jan 12, 2018 12:10 pm

There is a gps3 module that works for both Python2 and Python3
https://pypi.python.org/pypi/gps3/
Electronic and Computer Engineer
Pi Interests: Home Automation, IOT, Python and Tkinter

beatsal
Posts: 148
Joined: Tue Mar 21, 2017 2:46 pm

Re: GPS module in Python

Fri Jan 12, 2018 8:33 pm

Thanks. Tried the foll. script and used python 2 as it just would not accept py 3 although I tried with the bash command that Dougie mentioned. Now no error but for some reason, it does not write to the file locations.csv although it does open the file.

Code: Select all

from gps import *

import time
import threading
f = open("locations.csv","w")
 
gpsd = None
class GpsPoller(threading.Thread):
   def __init__(self):
       threading.Thread.__init__(self)
       global gpsd
       gpsd=gps(mode=WATCH_ENABLE)
       self.current_value = None
       self.running = True
   def run(self):
       global gpsd
       while gpsp.running:
           gpsd.next()
if  __name__ == "__main__":
   
   gpsp=GpsPoller()
    
   try:
       gpsp.start()
        
       while True:
           f.write(str(gpsd.fix.longitude) + "," + str(gpsd.fix.latitude) + "\n") 
            
           time.sleep(30)
       
   except(KeyboardInterrupt,SystemExit):
       f.close()
       gpsp.running = False
       gpsp.join()

saltydog
Posts: 31
Joined: Mon Dec 24, 2012 10:40 am

Re: GPS module in Python

Fri Jan 12, 2018 11:24 pm

Try:

Code: Select all

from gps import *

import time
import threading
 
gpsd = None
class GpsPoller(threading.Thread):
   def __init__(self):
       threading.Thread.__init__(self)
       global gpsd
       gpsd=gps(mode=WATCH_ENABLE)
       self.current_value = None
       self.running = True
   def run(self):
       global gpsd
       while gpsp.running:
           gpsd.next()
if  __name__ == "__main__":
   
   gpsp=GpsPoller()
    
   try:
       gpsp.start()
        
       while True:
           with open("locations.csv","w") as f:
               f.write(str(gpsd.fix.longitude) + "," + str(gpsd.fix.latitude) + "\n") 
            
           time.sleep(30)
       
   except(KeyboardInterrupt,SystemExit):
       gpsp.running = False
       gpsp.join()

Chris

beatsal
Posts: 148
Joined: Tue Mar 21, 2017 2:46 pm

Re: GPS module in Python

Sat Jan 13, 2018 3:05 pm

Thanks Chris.
No error but it only writes "0.0,0.0" to locations.csv

B.Goode
Posts: 4730
Joined: Mon Sep 01, 2014 4:03 pm
Location: UK

Re: GPS module in Python

Sat Jan 13, 2018 3:36 pm

beatsal wrote:
Sat Jan 13, 2018 3:05 pm
Thanks Chris.
No error but it only writes "0.0,0.0" to locations.csv
Have you checked that the GPS receiver is in a location where it has an unobstructed view of the sky in most directions, and that the receiver has actually established a 'fix' on no less than 4 'visible' GPS satellites?

There are other client tools that are available to interrogate a gpsd server, to check that it is operating correctly in conjunction with your GPS receiver.

bensimmo
Posts: 2056
Joined: Sun Dec 28, 2014 3:02 pm
Location: East Yorkshire

Re: GPS module in Python

Sat Jan 13, 2018 3:36 pm

Stick a print statement in before the writes and see what it's saying (or debug in Thonny if it now copes with threads).

Also use print in other places to see what is happening.

It might not be seeing a lock yet and hence no location. That or you're on a boat in the Gulf of Guinea (iirc?)

beatsal
Posts: 148
Joined: Tue Mar 21, 2017 2:46 pm

Re: GPS module in Python

Sat Jan 13, 2018 10:08 pm

Thanks. I do get the gps values (lat, lon) updating every second - I used:
gpsmon /dev/serial0 and it works fine
I used the script sent few hours ago by Chris verbatim. Still no luck; considering I am not in Guinea but freezing in Canada, just can't fathom what's going wrong!

beatsal
Posts: 148
Joined: Tue Mar 21, 2017 2:46 pm

Re: GPS module in Python

Sat Jan 13, 2018 10:27 pm

I tried the suggestion to add a print statement before the write. No change in file locations.csv i.e. it prints 0.0 0.0,0.0 0.0 as before, but now it prints 0.0 every 30 secs in the python shell.
Wonder if the gps data is written too fast i.e. every second, hence locations.py cannot keep up?

bensimmo
Posts: 2056
Joined: Sun Dec 28, 2014 3:02 pm
Location: East Yorkshire

Re: GPS module in Python

Sat Jan 13, 2018 10:34 pm

gpsmon shows it working :-)
But it uses its own code to get gps from it.

is gpsd setup and working? does it know shic serial location it can get info from (use cgps or xgps as they use gpsd)

the print shows what would be written, so the 'save' isn't a problem.

No idea how your code is suppose to work as I don't know that gps module.

beatsal
Posts: 148
Joined: Tue Mar 21, 2017 2:46 pm

Re: GPS module in Python

Sat Jan 13, 2018 11:04 pm

tried cgps and got a static table (no updates). similar with xgps, so may be they don't work? this is a popular gps module from Adafruit PA6H

User avatar
DougieLawson
Posts: 30935
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website

Re: GPS module in Python

Sat Jan 13, 2018 11:41 pm

Does your GPS device appear as a TTY device? Take a look with a ls -la /dev/ttyS* /dev/ttyA* command.

What do you get if you run gpsd -D 2 -N so that you get lots of debug info?
Microprocessor, Raspberry Pi & Arduino Hacker
Mainframe database troubleshooter
MQTT Evangelist
Twitter: @DougieLawson

Since 2012: 1B*5, 2B*2, B+, A+, Zero*2, 3B*3

Please post ALL technical questions on the forum. Do not send private messages.

beatsal
Posts: 148
Joined: Tue Mar 21, 2017 2:46 pm

Re: GPS module in Python

Sun Jan 14, 2018 12:36 am

Keyed in the first, got:
ctw-rw -1 /dev/ttyAMA0
ctw-rw -1 /dev/ttyS0

2nd cmd i.e. gpsd-D 2 -N :
gpsd: ERROR: can't run with control socket or devices

bensimmo
Posts: 2056
Joined: Sun Dec 28, 2014 3:02 pm
Location: East Yorkshire

Re: GPS module in Python

Sun Jan 14, 2018 9:01 am

Did you set gpsd up?
Because something is wrong with the setup.

Serial is working and the module is working (gpsmon, it doesn't use gpsd but it own internal 'drivers') but is show you that it is all working.

gpsd is the go between program your code is using.

First
sudo killall gpsd

These are the normal steps that are used to install and setup from scratch.

sudo apt-get install gpsd gpsd-clients python-gps

sudo systemctl stop gpsd.socket
sudo systemctl disable gpsd.socket

sudo gpsd /dev/serial0 -F /var/run/gpsd.sock


Wait and run cgps -s or xgps (xgps is only work in the desktop)


If you ever get in a mess then to reset gpsd
sudo killall gpsd
sudo gpsd /dev/serial0 -F /var/run/


(Often if you've not left it till it gets a lock for cgps)

You need to run this everything you reboot, normally before you run the program (could do it in code, I used to do it in rc.local)

You can setup the config file to do it automatically at boot, and re-enable the services we stopped, iirc, but I forget where I put the setup notes for that.

It's been a while since I've done it and that was under Jessie.

Hope that helps.

User avatar
DougieLawson
Posts: 30935
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website

Re: GPS module in Python

Sun Jan 14, 2018 11:51 am

I've just wired my ancient Garmin eTrex GPS to my RPiZ. It sends NMEA at 4800,8,N,1.

It appeared as /dev/ttyUSB0 so I can read that with

Code: Select all

#!/usr/bin/python3
import serial
while True:
  with serial.Serial('/dev/ttyUSB0', 4800, timeout=1) as ser:
    line = ser.readline()
    print (line)
If I set up gpsd with /etc/default/gpsd

Code: Select all

# Default settings for the gpsd init script and the hotplug wrapper.

# Start the gpsd daemon automatically at boot time
START_DAEMON="true"

# Use USB hotplugging to add new USB devices automatically to the daemon
USBAUTO="true"

# Devices gpsd should collect to at boot time.
# They need to be read/writeable, either by user gpsd or the group dialout.
DEVICES="/dev/ttyUSB0"

# Other options you want to pass to gpsd
GPSD_OPTIONS="-D 3"
for lots of debugging info.

I can use telnet localhost 2947 then enter ?WATCH={"enable":true, "json":true, "nmea": true}[ENTER]

Code: Select all

Trying ::1...
Connected to localhost.
Escape character is '^]'.
{"class":"VERSION","release":"3.16","rev":"3.16-4","proto_major":3,"proto_minor":11}
?WATCH={"enable":true, "json":true, "nmea": true}
{"class":"DEVICES","devices":[{"class":"DEVICE","path":"/dev/ttyUSB0","driver":"NMEA0183","activated":"2018-01-14T11:43:12.840Z","flags":1,"native":0,"bps":4800,"parity":"N","stopbits":1,"cycle":1.00}]}
{"class":"WATCH","enable":true,"json":true,"nmea":true,"raw":0,"scaled":false,"timing":false,"split24":false,"pps":false}
$GPRMB,A,,,,,,,,,,,,A,A*0B
$GPGGA,113310,5114.????,N,00108.????,W,1,05,2.5,121.3,M,48.3,M,,*5F
{"class":"TPV","device":"/dev/ttyUSB0","mode":3,"time":"2018-01-14T11:33:10.000Z","ept":0.005,"lat":51.2?????,"lon":-1.13????,"alt":121.300,"epx":26.965,"epy":22.776,"epv":85.100,"speed":0.000,"climb":-0.050,"eps":20.97,"epc":61.34}
$GPGSA,A,3,02,05,06,07,,13,,,,,,,4.5,2.5,3.8*3B
$GPGSV,3,3,11,23,08,078,00,29,07,307,00,30,56,165,00*45
{"class":"SKY","device":"/dev/ttyUSB0","xdop":1.80,"ydop":1.52,"vdop":3.80,"tdop":2.93,"hdop":2.50,"gdop":4.99,"pdop":4.50,"satellites":[{"PRN":2,"el":42,"az":242,"ss":44,"used":true},{"PRN":5,"el":46,"az":298,"ss":40,"used":true},{"PRN":6,"el":26,"az":200,"ss":40,"used":true},{"PRN":7,"el":64,"az":117,"ss":34,"used":true},{"PRN":9,"el":45,"az":74,"ss":0,"used":false},{"PRN":13,"el":11,"az":252,"ss":40,"used":true},{"PRN":16,"el":13,"az":31,"ss":0,"used":false},{"PRN":23,"el":12,"az":76,"ss":0,"used":false},{"PRN":29,"el":9,"az":311,"ss":0,"used":false},{"PRN":30,"el":52,"az":168,"ss":0,"used":false},{"PRN":23,"el":8,"az":78,"ss":0,"used":false},{"PRN":29,"el":7,"az":307,"ss":0,"used":false},{"PRN":30,"el":56,"az":165,"ss":0,"used":false}]}
$GPGLL,5114.????,N,00108.????,W,114312,A,A*5C
...
^C^]
telnet> quit
Connection closed.
And I can read that data with gps-py3

Code: Select all

#!/usr/bin/python3
import gpsd
gpsd.connect()
print (gpsd.device())
while True:
  print (gpsd.get_current())
Note: I've redacted my co-ordinates in the NMEA sentences.
Microprocessor, Raspberry Pi & Arduino Hacker
Mainframe database troubleshooter
MQTT Evangelist
Twitter: @DougieLawson

Since 2012: 1B*5, 2B*2, B+, A+, Zero*2, 3B*3

Please post ALL technical questions on the forum. Do not send private messages.

Return to “General discussion”

Who is online

Users browsing this forum: No registered users and 52 guests