RSS feed

Linkedin Profile

Tags:
economy
programming
seattle
things that bug me
wall art

Posts by month: 12/08 (2)
10/08 (2)
08/08 (1)
06/08 (2)
05/08 (1)
03/08 (3)
02/08 (1)
01/08 (2)
12/07 (2)
11/07 (1)
07/07 (1)
05/07 (2)
02/07 (1)
01/07 (1)
12/06 (1)
11/06 (1)
10/06 (1)
08/06 (1)
07/06 (1)
06/06 (2)
05/06 (1)
04/06 (2)
02/06 (1)
01/06 (2)
12/05 (3)
11/05 (2)
09/05 (5)
08/05 (5)
07/05 (7)
06/05 (3)
05/05 (6)
04/05 (8)
03/05 (7)
02/05 (7)
01/05 (6)
12/04 (2)
11/04 (3)
10/04 (5)
09/04 (3)
08/04 (5)
07/04 (5)
06/04 (4)
05/04 (4)
04/04 (9)
03/04 (4)
02/04 (3)
01/04 (5)
12/03 (1)
11/03 (14)
10/03 (8)


GPS
2007-01-28

Last week, in a bout of gadget lust, I bought Streets & Trips 2007, which includes a little USB GPS locator.

GPS device

Not long after plugging it in I started to wonder if it was possible to pull GPS data directly off the device myself (rather than using Street and Trips), and so a hacking project was born. I decided to get my GPS location, somehow reverse geo-code it and publish my location to my weblog using a web service. Since my GPS device will only sometimes be plugged in, I decided to write a service that would pull my GPS location every few minutes and save it to disk. Likewise since my laptop will not always be connected to the internet, the same service would periodically try the geo-coding and publishing steps.

Getting data from the device was remarkably simple. A few internet searches revealed that GPS devices use the NMEA protocol, and that the device can be treated as a serial port, allowing me to start pulling data with a few lines of C# code like this:

	port = new SerialPort();
	port.PortName = "COM4";
	port.BaudRate = 4800;
	port.Parity = Parity.None;
	port.StopBits = StopBits.One;
	port.DataBits = 8;
	port.Open(); 
	int bufferSize = 256;
	byte[] data = new byte[bufferSize];
	port.Read(data, 0, bufferSize);

The NMEA data that is read into the buffer is an asci-encoded string formatted like so:

55 $GPGSA,A,3,16,07,06,21,10,18,,,,,,,2.4,1.3,2.0*39 $GPRMC,215238.000,A,4737.2885,N,12219.4668,W,0.19,350.69,270107,,,A*74 $GPGGA,215239.000,4737.2884,N,12219.4668,W,1,06,1.3,95.1,M,-17.3,M,,0000*55 $GPGSA,A,3,16,07,06,21,10,18,,,,,,,2.4,1.3,2.0*39 $

The GPRMC and GPGGA sections both contain latitude (47.37 N) and longitude (122.19 W), and the GPGGA also has altitude (95.1 M). Rather than worry about parsing this myself, I purloined some NMEA parsing code from this excellent sample by Scott Hanselman on MSDN's Coding4Fun.

With my coordinates retrieved, my attention now turned to reverse geo-coding them to find a place name. I had hoped that local.live.com would have an API, but I settled for the REST service provided by www.geonames.org. It accepts latitude and longitude as part of a HTTP get request like http://ws.geonames.org/findNearbyPlaceName?lat=47.3&lng=9, and returns location data as XML:

Location data

Now with coordinates and a place name, all that remained was to push this up to a SOAP service on my own blog that saves the location and time to a local database, and then add the most recent entry to the sidebar of my blog. And now you know where I am.

Tags: programming

Back to weblog