SparkFun Forums 

Where electronics enthusiasts find answers.

Tips and questions relating to the GPS modules from SFE
By cmatkin
#63635
Hello to all of you,
I have read many topics on connecting the GM862 to the web and posting data, this all looks good.

My question is about the web server side of things.
I was wondering if anyone would have a script that can sit in my website and save data to a database.

Eg: http://mywebsite.com/scripts/gps?savethisdata

My server is running on FreeBSD servers with the following.
PHP, Pearl, CGI, Python, MySQL, PostgreSQL

Any help would be appreciated.

Regards
Craig Atkin
By TomasTrek
#63765
Okay first of all I'd say that if you have the time, you should learn how to write some multi-threaded server-side application that will establish a connecting with your device. I find this way more cost-effective than using web pages. Web pages are easier though so should get you up and running quickly.

I dont know what database management software you have (if any) but if you can install some then id recommend phpmyadmin. Anyway with your database manager create a new database. In my example I will call this "db_gps". In that database you will need a table, lets call it "tbl_gpsdata". It will need a key which I normally set to the auto-incrementing id field. I'd also recommend splitting up the lat and lon into separate columns, so in the end you should have:
id (key) int auto-increment
lat string
lon string
I used string to get around limitations in numerical variable types and negatives but you should probably do it properly eventually.

Okay so now you have this, you will need some web page with a server-side technology, my favourite of which is PHP so that is what I will explain.

Lets call this page "gps.php". By the looks of your post you only want to send the data as a GET, but its a little better practise to do it via POST. You can look that up yourself though as it'll require modification to your GM862 code (Python I assume). In PHP to get data passed via GET you need this:
Code: Select all
$_GET['varname'];
So lets say on your device you are getting the data, you will have to make a request to get the page "http://www.yourwebsite.com/scripts/gps. ... xx&lon=xxx". Obviously you need to fill in the xxx. Instead of splitting lat and lon on the device youre probably going to have more processing power on the server, so it might be better for you to pass it as one variable and split it once you receive it on the server, so you'd get the page as "http://www.yourwebsite.com/scripts/gps.php?data=xxx".

Well for ease I am just going to assume you split the data on the device and send it separately, so in your php page you will have exactly this:
Code: Select all
<?PHP
$lat = $_GET['lat'];
$lon = $_GET['lon'];
?>
Okay this is pretty useless right now, but its a start. If you dont know already, around all of your PHP code you need <?PHP and ?>, which tells your server to process everything between those two lines as PHP and not HTML or anything else.

For the purpose of testing, we can visit this page in the browser and use echo to show us what is being received, so you can add these two lines after the existing two (but BEFORE the "?>"):
Code: Select all
echo('LAT: '.$lat.'<br/>');
echo('LON: '.$lon);
Alright I will use MySQL for putting the data in your database but I will make another recommendation to add support to your server for MySQLi as it makes things a lot easier and is more powerful. Before you can do anything you need to open up a connection to your database server/daemon and authenticate, so you'll need this code adding:
Code: Select all
mysql_pconnect('localhost','username','password') or die(mysql_error());
mysql_select_db('db_gps') or die(mysql_error());
So now we have an open connection to the database server. You will obviously have to change the username and password to the ones you use on your server. Next we want to input the data:
Code: Select all
mysql_query('INSERT IN2 tbl_locations VALUES( NULL, '.$lat.',\''.$lon.'\')') or die(mysql_error());
NOTE: where above it says IN2 you should actually write INTO. I guess its some security measure by Sparkfun but it wont let you type INSERT followed by INTO, so I had to use a 2 instead

This will push the data into the database, the first value being NULL because this will be filled in for us - it is the auto-incrementing key. The second value is the latitude that you passed in, the third is the longitude.

Finally, we need to close the connection we just opened:
Code: Select all
mysql_close();
So thats about it. If you put it all together you should end up with this:
Code: Select all
<?PHP
$lat = $_GET['lat'];
$lon = $_GET['lon'];

echo('LAT: '.$lat.'<br/>');
echo('LON: '.$lon);

mysql_pconnect('localhost','username','password') or die(mysql_error());
mysql_select_db('db_gps') or die(mysql_error());

mysql_query('INSERT IN2 tbl_locations VALUES( NULL, '.$lat.',\''.$lon.'\')') or die(mysql_error());

mysql_close();
?>
REMEMBER to change IN2 back to INTO
Please note that this is only going to get your started, it is by no means code that you should rely on. For a start it doesnt associate its queries with database tokens so you cant open more than one connection at a time, it doesnt verify any of the data that is being taken in so it is open to mysql injection attacks and you will also be wanting to break the code up into some includes, for example the database connection should be in a single file that is not web-accessible and is included from within the other files that require a database. This will protect you as your username and password are stored only in one file rather than all of your php files, and that one file is not able to be accessed from over the internet so it cant accidentally slip through to others.

Also you should know that the data reported by the gps device is not going to appear correctly if you are using google maps api. They use two different coordinate systems however if you go to the google maps website and put the data in the search field then it will convert it for you. I have posted my code that I use for converting the module coordinates to google maps coordinates so have a search for it if you need it.

One last thing: Good luck :) and if you have any problems (since I didnt test my code, but ive written it enough times that it should be correct) then just let me know here or via PM.
By cmatkin
#63893
TomasTrek:
Wow, thanks so much for the information.
This should at least get me started. i'll be purchasing a PHP book this weekend.

Thanks again.
Regards
Craig