Sunday, May 21, 2006

MeHere: GPS & Python

I have been trying to use MeHere for a while. This time while learning some Python along the way I finally got it to work on my setup.

This includes a Bluetooth receiver connecting to a Windows XP laptop using the BlueSoleil USB to Bluetooth dongle. MeHere runs as a Win32 exe that embeds the Python runtime with a script (check py2exe or Freeze for more on this). MeHere.py passes the values obtained through the serial port to which your GPS is connected to JavaScript in a browser. While refreshing a page, the script uses the data and loads Google Local for the corresponding position. The code is available so I could see what was causing the script to fail.

Python calls

I ran the Windows Installer package available at python.org to install version 2.3 (version 2.4.3 is the most recent at this time, but I was playing with py2exe which requires version 2.3). That caused changes to the Registry and performed all the necessary setup.

You can run a Python script by invoking the interpreter from the command-line like this:

c:\Python23\python MeHere.py

MeHere.py requires a Serial module that you can find here. And Serial on its turn requires the win32file module that can be downloaded from here. I wish MeHere author Greg Murphy had some way to package these modules as a .lib type file with his source (I would hope so but at this point I'm not sure this is something you can do with Python).

win32file does install Python extensions to Windows32 including COM related modules which can be used for automation. And that did it, running MeHere.py again with the Python interpreter caused IE to popup with the Google Local page and a flow of strings getting displayed in a command-line window with COM1 as its default Serial port.

Some tweaking required

I had a Bluetooth GPS connected to COM4 so I killed the script and looked for the default COM port back at the MeHere.py script (I probably could call the script with an argument too, but I didn't check for it). Line #69 had the only occurrence. Then I was able to reproduce the original error that put me down this road:

Unhandled exception in thread started by
Traceback (most recent call last):
File "mehere.py", line 182, in gps
elif line[0:6] == '$GPGGA' and datablock[2] and datablock[4]:
IndexError: list index out of range
Basically it seemed that the Serial communication didn't always get a whole $GPGGA sentence so I made sure it had more than 5 elements and added a bit of a delay after it failed to obtain a full/valid sentence. I also forced localhost at line #658.

That did it. I can see now the valid Long/Lat and the map got updated correctly. Greg's script does quite a bit of magic by running a webserver, writing XML, KML and providing the JavaScript with a valid position to display. By clicking on the link at the HTML page you can also open the accompanying KML file in Google Earth to see your current location in 3D.

Kudos for the effort. Pretty cool.

No comments: