Getting data from the GPS dongle

The Globalsat ND100 is a simple, well documented USB GPS dongle, that works out of the box on the Raspberry. One of the main reasons I chose this dongle :)
Basically, what all these simple GPS dongles do, is emulate a USB to serial device. After plugging it in to your Raspberry, you’ll probably end up finding something like this in the dmesg output:

It is recognized as a pl2303 serial converter. These devices typically output NMEA-0183 sentences. Although the protocol describes it as being 4800 baud, these devices go a bit quicker at 38400 baud. You can minicom in to see the NMEA sentences, but you’re probably better off apt-getting gpsd and gpsd-clients, and don’t forget to reconfigure gpsd:

Make sure to start gpsd automatically, and set it to handle USB GPS receivers automatically. It should probably detect your GPS dongle automatically as /dev/ttyUSB0. If you don’t plan to change hardware a lot, enter this (this saves autodetection). I also entered it as ‘options to gpsd’ in the next step. I used ‘/var/run/gpsd.sock’ as path to the socket.

gpsd comes with a few clients, so you can test your GPS. Place your Pi near the window and wait for a fix (my dongle starts flashing every second to indicate it has a fix, check your documentation). On the command line, you can use ‘cgps’. Use the option ‘-s’ to hide the JSON NMEA sentences. If you use X11 forwarding when connecting via ssh to your Pi (ssh -Y), you can also use xgps. This will also show you a graphical representation of the satellites used. I found Peter Mount’s Blog and used it as a guideline in setting up my GPS.

You should now have a working GPS receiver, and running gpsd. You can connect via TCP/IP port 2947 on localhost and it will output the NMEA data in JSON format, so you don’t have to do the NMEA decoding yourself. I’m currently hacking up a proof of concept, and my preferred scripting language is PHP. Turns out Johan Desmyter wrote a PHP class for interfacing with gpsd. Nice! By putting the example code in a loop, I now have a very simple script which outputs the current location every second.

This GPS data needs to be uploaded to a central server to be used on a website for tracking. I’m still thinking about the client-server interface, but my main idea is to figure out some code to calculate the difference between two GPS samples (taken a second apart); if this differs more than a certain amount, save the data to a local sqlite database. Ideally, I would end up with just a few samples per minute. A different process, running every minute, would then compress and upload the data which hasn’t been uploaded yet to the remote server, wait for the ‘OK’, and mark the data as sent.

The hard part is to find out when exactly a sample differs enough from a previous sample to save it. For example, when driving at 130 km/h on a straight highway, one sample per minute would suffice to create a nice track in Google Maps. But when navigating a city center with roundabouts and curvy roads, one might need a sample every ten seconds, or even every second.

To do my calculations, I’m going to use my created testing script to collect data, every second, while doing some real world driving. But as I mentioned in my previous post, I cannot just unplug the Pi in my car, as this would corrupt the database with samples. Fortunately, my UPS PIco shipped from Greece last Tuesday, and my Parcel app just notified me that the package arrived in ‘ΟΛΛΑΝΔΙΑ’ (The Netherlands)… That is, after I threw the line in Google Translate, as I can’t read any Greek :)


Leave a Reply