NetStumbler.org Forums

Go Back   NetStumbler.org Forums > NetStumbler Community > Scripts
Register Search Today's Posts Mark Forums Read

Reply
 
LinkBack Thread Tools Display Modes
Old 06-26-2006   #1 (permalink)
HDR123
Registered Member
 
Join Date: Jul 2005
Posts: 8
Live Online GPS Updater

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...tivescript.php)

Netstumbler Script:
PHP Code:
//<?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));
}
Run this from command line to update,

PHP Code:
<?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'] != && $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);
        }
    }
}

Updater.php: puts data into database, put on a webserver
PHP Code:
<?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']}')");
}
XMLData.php: Returns database contents as an XML document:

PHP Code:
<?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();
SQL.inc : connects to SQL database
PHP Code:
<?php

$SQL 
= new mysqli('localhost''root''password''gps_db'3306);


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.

Last edited by HDR123 : 06-26-2006 at 06:46 PM.
HDR123 is offline   Reply With Quote
Old 06-26-2006   #2 (permalink)
mbolgian
 
Posts: n/a
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?
  Reply With Quote
Old 06-26-2006   #3 (permalink)
HDR123
Registered Member
 
Join Date: Jul 2005
Posts: 8
Quote:
Originally Posted by mbolgian
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, Send GPS Loc for Vehicle Tracking... ... If your still looking for that script, its right here

Last edited by HDR123 : 06-26-2006 at 06:45 PM.
HDR123 is offline   Reply With Quote
Old 06-27-2006   #4 (permalink)
HDR123
Registered Member
 
Join Date: Jul 2005
Posts: 8
New Version , 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 Code:
//<?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 && PLAY_SOUND){
        
$SNRwav intval($BestSNR 10);
        if (
$SNRwav 6$SNRwav 6;
        
win_play_wav("ns-signal-$SNRwav.wav");
    }
}

OnlineUpdate.php - Now uses HTTP POST as to not fill up server logs with GET data, and supports password protection.
PHP Code:
<?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'] != && $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(), 06)),
                ));
                
$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);
    }
}

Update.php - Modified to support password protection.
PHP Code:
<?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(), 06))) {
        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']}')");
}

CacheUpdate.php - Use instead of OnlineUpdate.php when theres no internet
PHP Code:
<?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'] != && $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);
        }
    }
}

SendCache.php - Sends cached data to the server.
PHP Code:
<?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(), 06)),
    ));
    
$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');

And the best of all,
Viewer.php - Shows GPS trail and current stats on Google Maps API
PHP Code:
<!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-colortransparent;
            
text-alignleft;
        }
    </
style>
    <
script src="http://maps.google.com/maps?file=api&amp;v=2&amp;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> 

XMLData.php and SQL.inc were not changed.

Last edited by HDR123 : 06-27-2006 at 08:16 PM.
HDR123 is offline   Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



Google
 
Web NetStumbler.org

All times are GMT -7. The time now is 02:20 PM.


Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0 ©2007, Crawlability, Inc.


All messages express the views of the author and are for entertainment purposes only. Netstumbler.org cannot be held responsible for the authenticity of the content or the actions of its members. By using this site and its services, you warrant that you will not post any messages that are discriminating, obscene, hateful, threatening, or otherwise violates any laws and you release Netstumbler.org from any future claims of any kind whatsoever including, but not limited to, addiction and loss of productivity. All forum messages, private messages and any other content are properties of Netstumbler.org. Even if publicly available, personal or copyrighted information are not to be posted without the consent of the owner. Distribution of licensed and copyrighted materials in any way not endorsed by the copyright owner is strictly prohibited. You may not use this site and its resources to spam other sites or individuals or perform any action that violates any law. Items sold or bought in the For Sale forum are sold as is and no warranty or insurance of any kind is provided. Netstumbler.org cannot be held responsible for the outcome of any transactions and no warranty of any kind is provided, either express or implied. Vulgar words are not allowed in the subject lines ; they may be used in the message body in any forum. The Administrator, Super Moderators and Moderators of Netstumbler.org have the right to remove, edit, move or close any thread for any reason and to reveal your identity and other known information in the event of a complaint or legal action arising from any message posted by you.