Live Online GPS Updater

Take a script, leave a script - Like the penny tray only different

Live Online GPS Updater

Postby HDR123 » Mon Jun 26, 2006 3:24 pm

I coded this to be able to update the position of my car live on the internet. I used netstumbler's scripting only to have an easy way to access GPS from PHP, this dosen't use the scanning functions at all, although it would be easy to add them (have it send ap location instead of all gps info). Requires the PHP ActiveScript extension to be installed. (http://us2.php.net/manual/en/install.windows.activescript.php)

Netstumbler Script:
[PHP]
//<?php // Have IDE see this as PHP and not HTML

set_time_limit(0);

$Data = array();

function OnGPSPosition ($Latitude, $Longitude, $Altitude) {
global $Data;
$Data['lat'] = (double) $Latitude;
$Data['long'] = (double) $Longitude;
$Data['alt'] = (double) $Altitude;
$Data['nogps'] = false;
update();
}

function OnGPSSpeed ($Speed) {
global $Data;
$Speed = (double) $Speed;
$Speed = $Speed * 1.150779;
$Data['speed'] = $Speed;
$Data['nogps'] = false;
update();
}

function OnGPSNoFix () {
global $Data;
$Data['nogps'] = true;
update();
}

function update () {
global $Data;
if (!isset($Data['lat']) || !isset($Data['long']) || !isset($Data['alt']) || !isset($Data['speed'])) {
$Data['lat'] = 0;
$Data['long'] = 0;
$Data['alt'] = 0;
$Data['speed'] = 0;
$Data['nogps'] = true;
}
$Data['time'] = time();
file_put_contents('GPSData', serialize($Data));
}
[/PHP]

Run this from command line to update,

[PHP]
<?php

chdir('C:\\Program Files\\Network Stumbler');

set_time_limit(0);

$LastTime = 0;

while (true) {
if (file_exists('GPSData')) {
$Data = unserialize(file_get_contents('GPSData'));
var_dump($Data);
if ($Data['time'] !== $LastTime) {
if ($Data['lat'] != 0 && $Data['long'] != 0) {
$cURL = curl_init();
curl_setopt_array($cURL, array(
CURLOPT_URL => "http://url.to/Updater.php?spd={$Data['speed']}&alt={$Data['alt']}&lat={$Data['lat']}&lng={$Data['long']}&tim={$Data['time']}",
CURLOPT_HEADER => false,
CURLOPT_CONNECTTIMEOUT => 2,
CURLOPT_TIMEOUT => 5,
));
$ret = curl_exec($cURL);
curl_close($cURL);
if ($ret === false) {
file_put_contents('CACHED_DATA', serialize($Data)."\r\n", FILE_APPEND);
}
}
$LastTime = $Data['time'];
echo "\r\n";
sleep(10);
}else{
echo "\r\n";
sleep(2);
}
}
}
[/PHP]


Updater.php: puts data into database, put on a webserver
[PHP]
<?php

include('SQL.inc');

if (isset($_REQUEST['spd']) && isset($_REQUEST['alt']) && isset($_REQUEST['lat']) && isset($_REQUEST['lng']) && isset($_REQUEST['tim'])) {
$SQL->query("INSERT INTO `data` (`Time`,`Latitude`,`Longitude`,`Altitude`,`Speed`) VALUES ('{$_REQUEST['tim']}','{$_REQUEST['lat']}','{$_REQUEST['lng']}','{$_REQUEST['alt']}','{$_REQUEST['spd']}')");
}
[/PHP]

XMLData.php: Returns database contents as an XML document:

[PHP]
<?php

include('SQL.inc');

header('Content-type: text/xml');

$XMLWriter = new XMLWriter();
$XMLWriter->openMemory();
$XMLWriter->setIndent(true);
$XMLWriter->setIndentString("\t");
$XMLWriter->startDocument('1.0', 'UTF-8');
$XMLWriter->startElement('GPS');
$XMLWriter->startElement('data');

$Row = $SQL->query("SELECT * FROM `data` ORDER BY `Time` DESC LIMIT 1")->fetch_assoc();
$XMLWriter->writeAttribute('alt', $Row['Altitude']);
$XMLWriter->writeAttribute('spd', $Row['Speed']);
$XMLWriter->writeAttribute('tim', $Row['Time']);
$XMLWriter->writeAttribute('cti', time());

$Result = $SQL->query("SELECT * FROM `data` ORDER BY `Time` DESC LIMIT 120");
while ($Row = $Result->fetch_assoc()) {
$XMLWriter->startElement('point');
$XMLWriter->writeAttribute('lat', $Row['Latitude']);
$XMLWriter->writeAttribute('lng', $Row['Longitude']);
$XMLWriter->endElement();
}

$XMLWriter->endElement();
$XMLWriter->endElement();
$XMLWriter->endDocument();
echo $XMLWriter->outputMemory();
[/PHP]

SQL.inc : connects to SQL database
[PHP]
<?php

$SQL = new mysqli('localhost', 'root', 'password', 'gps_db', 3306);
[/PHP]



I have another file that uses the Google Maps API that downloads, parses and displays the XML data on a map, but it has too much of my site code mixed in to post right now.

If internet connection is unavaliable or lost, the updating script will serialize the $Data array and write it to a file every time the update fails. A script to unserialize the data, an write it to the database one you have access to an interent connection can asily be coded, I just haven't found it neccissary yet. I originally had it using file_get_contents(), but switched to using cURL in order to be able to set a short timeout so that it dosen't lock up if internet is lost. I plan on adding password protection to the update script later.
HDR123
Mini Stumbler
 
Posts: 8
Joined: Thu Jul 07, 2005 9:27 pm

Postby mbolgian » Mon Jun 26, 2006 3:27 pm

I'm assuming you have to have a separate, constant internet connection to upload the data? What are you using, if you don't mind me asking?
mbolgian
 

Postby HDR123 » Mon Jun 26, 2006 3:29 pm

mbolgian wrote:I'm assuming you have to have a separate, constant internet connection to upload the data? What are you using, if you don't mind me asking?



Cell phone internet

-------------------------------------
Edit: looks like someone else had the same idea I had, http://www.netstumbler.org/showthread.php?t=16611 ... If your still looking for that script, its right here :D
HDR123
Mini Stumbler
 
Posts: 8
Joined: Thu Jul 07, 2005 9:27 pm

Postby HDR123 » Tue Jun 27, 2006 6:02 pm

New Version :D, think I'll call it 0.2.0, and GPL, but please tell me if you decide to redistribute a modified version.


GPSUpdate.php - Now plays NS sounds since their disabled when scripting is enabled. Requires win32std extenstion, since NS functions aren't avaliable when PHP is used as scripting for some reason.
[PHP]
//<?php

set_time_limit(0);

define('PLAY_SOUND', false);

$Data = array();

function OnGPSPosition ($Latitude, $Longitude, $Altitude) {
global $Data;
$Data['lat'] = (double) $Latitude;
$Data['long'] = (double) $Longitude;
$Data['alt'] = (double) $Altitude;
$Data['nogps'] = false;
update();
}

function OnGPSSpeed ($Speed) {
global $Data;
$Speed = (double) $Speed;
$Speed = $Speed * 1.150779;
$Data['speed'] = $Speed;
$Data['nogps'] = false;
update();
}

function OnGPSNoFix () {
global $Data;
$Data['nogps'] = true;
update();
}

function update () {
global $Data;
if (!isset($Data['lat']) || !isset($Data['long']) || !isset($Data['alt']) || !isset($Data['speed'])) {
$Data['lat'] = 0;
$Data['long'] = 0;
$Data['alt'] = 0;
$Data['speed'] = 0;
$Data['nogps'] = true;
}
$Data['time'] = time();
file_put_contents('GPSData', serialize($Data));
}

function OnScanComplete ($FoundNew, $SeenBefore, $LostContact, $BestSNR) {
if ($FoundNew > 0) {
win_play_wav('ns-aos-new.wav');
}elseif ($LostContact > 0) {
win_play_wav('ns-los.wav');
}elseif ($SeenBefore > 0 && PLAY_SOUND){
$SNRwav = intval($BestSNR / 10);
if ($SNRwav > 6) $SNRwav = 6;
win_play_wav("ns-signal-$SNRwav.wav");
}
}
[/PHP]


OnlineUpdate.php - Now uses HTTP POST as to not fill up server logs with GET data, and supports password protection.
[PHP]
<?php

chdir('C:\\Program Files\\Network Stumbler');

set_time_limit(0);

$LastTime = 0;

while (true) {
if (file_exists('GPSData')) {
$Data = unserialize(file_get_contents('GPSData'));
var_dump($Data);
if ($Data['time'] !== $LastTime) {
if ($Data['lat'] != 0 && $Data['long'] != 0) {
$cURL = curl_init();
curl_setopt_array($cURL, array(
CURLOPT_URL => "http://URL.TO/Update.php",
CURLOPT_HEADER => false,
CURLOPT_CONNECTTIMEOUT => 3,
CURLOPT_TIMEOUT => 3,
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => "spd={$Data['speed']}&alt={$Data['alt']}&lat={$Data['lat']}&lng={$Data['long']}&tim={$Data['time']}&pwd=".md5('PASSWORD' . substr(time(), 0, 6)),
));
$ret = curl_exec($cURL);
curl_close($cURL);
if ($ret === false) {
file_put_contents('CACHED_DATA', serialize($Data)."\r\n", FILE_APPEND);
}
}
$LastTime = $Data['time'];
echo "\r\n";
sleep(10);
}else{
echo "\r\n";
sleep(2);
}
}else{
sleep(5);
}
}
[/PHP]


Update.php - Modified to support password protection.
[PHP]
<?php

include('SQL.inc');

if (isset($_REQUEST['spd']) && isset($_REQUEST['alt']) && isset($_REQUEST['lat']) && isset($_REQUEST['lng']) && isset($_REQUEST['tim']) && isset($_REQUEST['pwd'])) {
if ($_REQUEST['pwd'] !== md5('PASSWORD' . substr(time(), 0, 6))) {
die('Invalid pwd!');
}
$_REQUEST['spd'] = $SQL->escape_string($_REQUEST['spd']);
$_REQUEST['alt'] = $SQL->escape_string($_REQUEST['alt']);
$_REQUEST['lat'] = $SQL->escape_string($_REQUEST['lat']);
$_REQUEST['lng'] = $SQL->escape_string($_REQUEST['lng']);
$_REQUEST['tim'] = $SQL->escape_string($_REQUEST['tim']);
$SQL->query("INSERT INTO `data` (`Time`,`Latitude`,`Longitude`,`Altitude`,`Speed`) VALUES ('{$_REQUEST['tim']}','{$_REQUEST['lat']}','{$_REQUEST['lng']}','{$_REQUEST['alt']}','{$_REQUEST['spd']}')");
}
[/PHP]


CacheUpdate.php - Use instead of OnlineUpdate.php when theres no internet
[PHP]
<?php

chdir('C:\\Program Files\\Network Stumbler');

set_time_limit(0);

$LastTime = 0;

while (true) {
if (file_exists('GPSData')) {
$Data = unserialize(file_get_contents('GPSData'));
var_dump($Data);
if ($Data['time'] !== $LastTime) {
if ($Data['lat'] != 0 && $Data['long'] != 0) {
file_put_contents('CACHED_DATA', serialize($Data)."\r\n", FILE_APPEND);
}
$LastTime = $Data['time'];
echo "\r\n";
sleep(10);
}else{
echo "\r\n";
sleep(2);
}
}
}
[/PHP]


SendCache.php - Sends cached data to the server.
[PHP]
<?php

chdir('C:\\Program Files\\Network Stumbler');

if (!file_exists('CACHED_DATA')) {
die('File Not Exist');
}

$File = file('CACHED_DATA');

foreach ($File as $Data) {
$Data = unserialize($Data);
var_dump($Data);
$cURL = curl_init();
curl_setopt_array($cURL, array(
CURLOPT_URL => "http://UTL.TO/Update.php",
CURLOPT_HEADER => false,
CURLOPT_CONNECTTIMEOUT => 10,
CURLOPT_TIMEOUT => 10,
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => "spd={$Data['speed']}&alt={$Data['alt']}&lat={$Data['lat']}&lng={$Data['long']}&tim={$Data['time']}&pwd=".md5('' . substr(time(), 0, 6)),
));
$ret = curl_exec($cURL);
if ($ret === false) {
file_put_contents('CACHED_DATA2', serialize($Data)."\r\n", FILE_APPEND);
}else{
var_dump($Data);
}
curl_close($cURL);
}

unlink('CACHED_DATA');
[/PHP]


And the best of all,
Viewer.php - Shows GPS trail and current stats on Google Maps API
[PHP]
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/tr/xhtml11/DTD/xhtml11.dtd">
<html xml:lang="en" xmlns="http://www.w3.org/1999/xhtml" xmlns:v="urn:schemas-microsoft-com:vml">
<head>
<meta http-equiv="Content-Language" content="en-us" />
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>GPS</title>
<style type="text/css">
v\:* {
behavior:url(#default#VML);
}
td,tr,th {
background-color: transparent;
text-align: left;
}
</style>
<script src="http://maps.google.com/maps?file=api&v=2&key=GOOGLE_MAPS_API_KEY_HERE" type="text/javascript"></script>
</head>
<body onunload="GUnload()">
<div id="content"><!--CONTENT-->
<div style="text-align:center;">
<div id="map" style="width:100%;height:500px;"></div>
</div>
</div><!--END_CONTENT-->
<script type="text/javascript">
//<![CDATA[
var map = new GMap2(document.getElementById("map"));
var FirstRun = true;
var polyline;
var currlocation;
function LoadXMLData () {
GDownloadUrl("XMLData.php?" + Math.floor(Math.random()*1000000), function(data, responseCode) {
var xml = GXml.parse(data);
var newdata = xml.documentElement.getElementsByTagName("data");
var datapoints = xml.documentElement.getElementsByTagName("point");

if (FirstRun == true) {
map.setCenter(new GLatLng(parseFloat(datapoints[0].getAttribute("lat")), parseFloat(datapoints[0].getAttribute("lng"))), 16, G_HYBRID_MAP);
FirstRun = false;
}

var points = [];
for (var i = 0; i < datapoints.length; i++) {
points.push(new GLatLng(parseFloat(datapoints[i].getAttribute("lat")), parseFloat(datapoints[i].getAttribute("lng"))));
}
polyline = new GPolyline(points);
map.addOverlay(polyline);

currlocation = new GMarker(new GLatLng(parseFloat(datapoints[0].getAttribute("lat")), parseFloat(datapoints[0].getAttribute("lng"))));
map.addOverlay(currlocation);
currlocation.openInfoWindowHtml("<table><th style='background-color:transparent;'><b>Current Info:</b></th><tr><td>Speed:</td><td>" + parseFloat(newdata[0].getAttribute("spd")) + " MPH</td></tr><tr><td>Altitude:</td><td>" + parseFloat(newdata[0].getAttribute("alt")) + " ft</td></tr><tr><td>Last Update:</td><td>" + (Math.round(((parseFloat(newdata[0].getAttribute("cti")) - parseFloat(newdata[0].getAttribute("tim"))) / 60 ) * 100) / 100) + " minutes ago</td></tr></table>");

map.panTo(new GLatLng(parseFloat(datapoints[0].getAttribute("lat")), parseFloat(datapoints[0].getAttribute("lng"))));
});

}
LoadXMLData();
function ReloadXMLData () {
map.removeOverlay(polyline);
map.removeOverlay(currlocation);
LoadXMLData();
window.setTimeout("ReloadXMLData();", 7500);
}
window.setTimeout("ReloadXMLData();", 7500);
map.addControl(new GLargeMapControl());
map.addControl(new GMapTypeControl());
//]]>
</script>
</body>
</html>
[/PHP]


XMLData.php and SQL.inc were not changed.
HDR123
Mini Stumbler
 
Posts: 8
Joined: Thu Jul 07, 2005 9:27 pm


Return to Scripts

Who is online

Users browsing this forum: No registered users and 2 guests

cron