Before I get into 'proper' coding I like to prototype what I'm trying to do so I have a better understanding of how everything works and if there are any gotcha's. So the code in the next few posts isn't fully formed, just hacked together to test the basic workings. I can then re-use it later as I start to build the full program.
For this project I'm going to use the Python programming language which should already be pre-installed on both the RasPi and your Debian Wheezy Virtual Machine.
I've never coded in Python before, so should be fun learning the language. I've coded in most of the modern languages and if you've had some coding experience then Python should be pretty easy to pick-up.
First a bit about Python.
Python is an interpreted language, and not a compiled one. What that means is that it 'interprets' one line of code at a time and then executes it, then moves onto the next. A compiled language takes all the code, converts it to and executable programme and then runs the compiled program.
The advantage of an interpreted language is mainly speed and simplicity to write the code, you just type the commands and the 'interpreter' executes them right in front of you. Where a compiled language can be a pain to get the code compiled to run, but when it's compiled it will run quicker as it's been converted to a faster machine language.
I don't intend teaching you how to use Python, there are good tutorials on the Pyhton website
here. Have a look under documentation and tutorial. If you have some coding experience already have a look through the first 3-4 chapters of the tutorial and then i'm sure you can work the rest out from there when you need to.
Ok, our first little bit of coding to get some GPS data.
First, make sure your GPS and GPSD are working as in the previous post.
We're going to use a Python script, rather than type each line into the interpreter, so fire up a text editor, like gedit and start coding.
Right the script we are going to write uses threading. So we will have a thread running that gets the GPS data in the background, then our main program will then get the data from the thread and output it to a file. This is based on a script created by Dan Mandle and you can get the original at
http://dan.mandle.me.
So first line we want is
#! /usr/bin/python
This tells the script where to find the python interpreter. But if you're used to Linux scripting you know that bit :-)
The first few lines import the external libraries we want to use. the gps library is the one that gives us access to gpsd.
import os
from gps import *
from time import *
import time
import threading
We then setup some variables we need and clear the screen
gpsd = None #seting the global variable os.system('clear') #clear the terminal (optional)
We now create the class thread that is going to get the GPS data from gpsd and put it in a variable called, surprise surprise gpsd
class GpsPoller(threading.Thread):
def __init__(self):
threading.Thread.__init__(self)
global gpsd #bring it in scope
gpsd = gps(mode=WATCH_ENABLE) #starting the stream of info
self.current_value = None
self.running = True #setting the thread running to true
def run(self):
global gpsd
while self.running:
gpsd.next() #this will continue to loop and grab EACH set of gpsd info to clear the buffer
ok, the next bit of code is executed first when we run the script. It creates and starts the thread, opens a file for us to write to, then waits until we get a line of GPS data that is Mode 3. Mode 3 means has a good lock on the satellites. It the builds a comma seperated string of data from the gspd data and writes it to the file. Waits 1 second, then loops back round and gets the next line and writes that etc etc etc.
The 'except' bit waits for you to do a control+c to kill the program and then tidies things up. Shuts down the thread and closes the file etc.
if __name__ == '__main__':
gpsp = GpsPoller() # create the thread
try:
gpsp.start() # start it up
gps_file = open('gps_data.csv','w') # Open a file to write the data to
while True:
if str(gpsd.fix.mode) == str(3): # If we have Mode 3, good lock, then write the data out
gps_data = str(gpsd.fix.latitude) + ',' + str(gpsd.fix.longitude) + '\n' # just print lat and lon data
print gps_data
gps_file.write (gps_data)
time.sleep(1) #set to whatever
except (KeyboardInterrupt, SystemExit): #when you press ctrl+c
print "\nKilling Thread..."
gpsp.running = False
gpsp.join() # wait for the thread to finish what it's doing
gps_file.close()
print "Done.\nExiting."
Ok, so type that all in, or copy and paste it, and save the file as something with the extension '.py'.
For us to be able to run it from the command line we need to make it executable. So start up a root terminal go to the directory where you saved it, probably your home directory.
At the prompt type
sudo chmod 755 filename.py
Where
filename.py is the name of your file.
Now to run it simply type
./filename.py
And if all is working then it should be printing rows of latitude and longitude data on the screen, the numbers will probably be all the same unless you happen to be moving. Let it run for about 30 seconds.
To stop the program hit CTRL+c and you should have a file in the directory called 'gps_data.csv' with the GPS data in. To check it type
cat gps_data.csv | more
So, now we have a way of getting the GPS data into a program and doing something with it.
Next time we'll do something more interesting with it like plot it on the screen.
The full copy of the python code can be found
here.