EM506 GPS Module with PHPoC Blue

EM506 GPS Module with PHPoC Blue

Postby Jack » Mon Nov 30, 2015 6:07 pm

This example uses an EM-506 GPS module and google map to get and show latitudes and longitudes.

EM-506 GPS module
004.jpg
004.jpg (81.2 KiB) Viewed 1927 times

EM-506 uses an NMEA protocol to give information through its UART(4,800bps).
Latitudes and longitudes are read from a GGA(Global Positioning System Fixed Data) output command and marked the coordinate on google map.
The image below shows you how to connect EM-506 to PHPoC Blue.

003.png
003.png (188.77 KiB) Viewed 1927 times


Source code - Index.php
Code: Select all

<?php
include_once 
"/lib/sd_340.php";

$gps_cmd = _GET("gps");

$latitude = 0.0;
$longitude = 0.0;

if(
$gps_cmd == "read")
{
    uart_setup(0, 4800);

    $msg = "";
    
    while
(1)
    {
        $rbuf = "";
        $read = uart_read(0, $rbuf);
        if($read > 0)
        {
            $msg .= $rbuf;
            if(strpos($msg, "\r\n") !== false)
            {
                $idx = strpos($msg, "\r\n");
                $cmd = substr($msg, 0, $idx);
            
                if
(strpos($cmd, "$") !== false)
                {
                    $idx = strpos($cmd, "$");
                    if(($m_id = substr($cmd, $idx + 1, 5)) !== false)
                    {
                        if($idx > 0)
                        {
                            $cmd = substr($cmd, $idx);
                        }
                        if($m_id == "GPGGA")
                        {
                            $cmd_crc = str_replace("$", "", $cmd);
                            $cmd_crc = str_replace("*", "", $cmd_crc);
                        
                            $crc 
= 0;
                            $len = strlen($cmd_crc);
                            for($i = 0;$i < $len - 2;$i++)
                                $crc ^= bin2int($cmd_crc[$i], 0, 1);
                                
                            $str_crc 
= substr($cmd_crc, $len - 2);
                        
                            if
(bin2int(hex2bin($str_crc), 0, 1) == $crc)
                            {
                                $arr = explode(",", $cmd);
                            
                                $latitude 
= (int)substr($arr[2], 0, 2) + (float)substr($arr[2], 2, 7) / 60;
                                $longitude = (int)substr($arr[4], 0, 3) + (float)substr($arr[4], 3, 7) / 60;
                                break;
                            }
                        }
                    }
                }
                $msg = "";
            }
        }
    }
}

?>
<!DOCTYPE html>
<html>
  <head>
    <title>P4S-342</title>
  </head>
  <body align=center>
      <a href="index.php?gps=read">Read GPS</a><br>
      <?php
          if
($gps_cmd == "read")
          {
              printf("GPS - Latitude: N%.4f, Logitude: E%.4f<br><br>", $latitude, $longitude);
              printf("<a href=http://maps.google.com/?q=%f,%f&amp;hl=en target=_g_map>View in Google Map</a>", $latitude, $longitude);
        }
      ?>
  </body>
</html>


Video
phpBB [video]
Jack
 
Posts: 5
Joined: Wed May 14, 2014 5:38 pm

Return to Project

Who is online

Users browsing this forum: No registered users and 2 guests

cron