Brian
Created March 17, 2016

Cheap and Simple GPS Tracking

Uses GP-20U7 GPS Receiver Plots location on Google Maps

BeginnerProtip1 hour1,725
Cheap and Simple GPS Tracking

Things used in this project

Story

Read more

Code

Electron Code

C/C++
Flash to your Electron
// GPSTest

String inWord;
char inByte;
String data;
int LockLED = D1;  


void setup() {
    // cloud variable
    Particle.variable("STU", data);
    // GPS Serial
    Serial1.begin(9600);
    pinMode(LockLED, OUTPUT);
    digitalWrite(LockLED, HIGH);
    delay(2000);
    digitalWrite(LockLED, LOW);
}



void loop() {
    while (Serial1.available() > 0) {
        inByte = Serial1.read();
        
        if (inByte == '\n') {
            // END OF LINE
            
            // check is data we want
            // you can change this to get any data line values
            if (inWord.startsWith("$GPRMC")) {
                // put data string in variable
                data = inWord;
                // clear the inword variable
                inWord = "";
                
                // does the GPS Receiver have lock?
                // get the char at pos 17 and test to see if it is an A i.e VALID V = INVALID
                char lock = data.charAt(17);
                if (lock == 'A') {
                    // YES Switch on Lock LED
                    digitalWrite(LockLED, HIGH);
                } else {
                    // NO turn off lock led
                    digitalWrite(LockLED, LOW);  
                }
                
                
            } else {
                // clear the inword variable as not the data we want
                inWord = "";
            }
            
        } else {
            // build data string
            inWord += inByte;   
        }
        
    } // end if serial
} // end loop

Website Code

PHP
Put this on a web server to be remotely accessed and display map
<?PHP
# particle-map.php
// GPS UNIT
// https://cdn.sparkfun.com/datasheets/GPS/GP-20U7.pdf

$deviceID = "YOUR_PARTICLE_DEVIC_ID";
$access_token = "YOUR_PARTICLE_ACCESS_TOKEN";

$url = "https://api.particle.io/v1/devices/$deviceID/";
$formed_url ='?access_token='.$access_token;
$variable_name = "STU";

$headers = array( 
  	"GET /v1/devices/".$variable_name.$formed_url." HTTP/1.1",
  	"Host: api.particle.io");

  	// setup and make HTTP GET REQUEST
	$ch = curl_init();  
	curl_setopt($ch, CURLOPT_URL,$url.$variable_name.$formed_url);
	curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // return output
	$retrievedhtml = curl_exec ($ch); 
	curl_close($ch); 
	$json = json_decode($retrievedhtml,true);

	// see if there was an error connecting with electron
	if ($json['error'] != "") {
	    echo ("ERROR = " . $json['error'] . "<br>");
	} else {
	   // read the data into a variable
	   $DATA = $json['result'];
	   // output to screen
	   echo ("<b>result: </b>" . $DATA . "<br>");
	   // split data into array  is comma delimited
	   $pieces = explode(",", $DATA);
	   // A = valid, V = not valid
	   $status = $pieces[2];
	   if ($status == "V") {
			echo ("Data not valid<br>Can the GPS unit see the sky?");
	   } else {
			// put data in variables
			$LAT = $pieces[3];
			$LON = $pieces[5];
			$EW = $pieces[6];
			// Convert LAT
			$deg = substr($LAT, 0, 2);
			$min = substr($LAT, 2, 8);
			$sec = '';
			$resultLAT = $deg+((($min*60)+($sec))/3600);

			// Convert Longitude
			$deg = substr($LON, 0, 3);
			$min = substr($LON, 3, 8);
			$sec='';
			$resultLON = $deg+((($min*60)+($sec))/3600);

			// Is it East or West
			if ($EW == "W") {
			   $resultLON = $resultLON * -1;
			}
			
			// Thanks to Glen Morris for pointing out the N/S error
			// Is it North or South
			if ($NS == "S") {
				$resultLAT = $resultLAT * -1;
			}
			
			print ("Latitude " . $resultLAT . "<br>");
			print ("Longitude " . $resultLON . "<br>");
		}
	}
?>

<iframe
width="400"
height="400"
frameborder="0" style="border:0"
src="https://www.google.com/maps/embed/v1/place?key=YOUR_GOOGLE_MAP_API_KEY
&q=<?=$resultLAT?>,<?=$resultLON?>">
</iframe>

Credits

Brian

Brian

2 projects • 7 followers
Inventor

Comments