PHP GPS Parser (NMEA / GPRMC)

I have a QSTARZ bluetooth GPS device which I paired with my Macbook pro. This code should work on any Mac/Linux operating system. Depending on your device, you may need to adjust the GPS location – currently it’s hardcoded to¬†/dev/tty.iBT-GPS-SPPslave

This code is able to grab the most important information, convert from degrees, minutes and seconds to decimal and create a Google map link that shows the location. It’s part of a bigger project that I’m working on but will operate perfectly fine on its own.

<?php
 
 // Converts DMS ( Degrees / minutes / seconds ) 
 // to decimal format longitude / latitude
 function DMStoDEC($dms, $longlat){
 
 	if($longlat == 'lattitude'){
 		$deg = substr($dms, 0, 2);
 		$min = substr($dms, 2, 8);
 		$sec = '';
 	}
 	if($longlat == 'longitude'){
 		$deg = substr($dms, 0, 3);
 		$min = substr($dms, 3, 8);
 		$sec='';
 	}
 	
 
     return $deg+((($min*60)+($sec))/3600);
 }   
 
 //Set timezone
 date_default_timezone_set('Asia/Manila');
 
 //Connect to GPS
 $gps = fopen("/dev/tty.iBT-GPS-SPPslave", "r+");
 
 
 //Read data from GPS
 while($gps){
 	$buffer = fgets($gps);
 	if(substr($buffer, 0, 6)=='$GPRMC'){
 		echo $buffer."\n";
 	
 		$gprmc = explode(',',$buffer);
 		$data['timestamp'] = strtotime('now');
 		$data['sat_status'] = $gprmc[2];
 
 		$data['lattitude_dms'] = $gprmc[3];
 		$data['lattitude_decimal'] = DMStoDEC($gprmc[3],'lattitude');
 		$data['lattitude_direction'] = $gprmc[4];
 		
 		$data['longitude_dms'] = $gprmc[5];
 		$data['longitude_decimal'] = DMStoDEC($gprmc[5],'longitude');
 		$data['longitude_direction'] = $gprmc[6];
 		
 		$data['speed_knots'] = $gprmc[7];
 
 		$data['bearing'] = $gprmc[8];
 		
 		$data['google_map'] = 'http://maps.google.com/maps?q='.$data['lattitude_decimal'].','.$data['longitude_decimal'].'+(PHP Decoded)&iwloc=A';
 	
 		print_r($data);
 		echo "\n\n";
 	}
 }
 
 
 ?>
Posted in .Menu
2 comments on “PHP GPS Parser (NMEA / GPRMC)
  1. Igor says:

    Thank you so much! It helped me a lot!

  2. Aayush Vetwaal says:

    I was wondering how I could parse GPS data effectively, I found this article and it proved to be helpful. Thank you.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>