views:

445

answers:

2

I have a gps simulator, which sends continuous location data (nmea strings) through tcpip to 192.168.0.178:2323. How do I get that data using tcp socket and write to DB (php & mysql)?

Thanks!

A: 

Start here: PHP Sockets

One of the user comments gives a helpful sample implementation of a TFTP client

<?php
  function tftp_fetch($host, $filename)
  {
    $socket = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP);

    // create the request packet
    $packet = chr(0) . chr(1) . $filename . chr(0) . 'octet' . chr(0);
    // UDP is connectionless, so we just send on it.
    socket_sendto($socket, $packet, strlen($packet), 0x100, $host, 69);

    $buffer = '';
    $port = '';
    $ret = '';
    do
    {
      // $buffer and $port both come back with information for the ack
      // 516 = 4 bytes for the header + 512 bytes of data
      socket_recvfrom($socket, $buffer, 516, 0, $host, $port);

      // add the block number from the data packet to the ack packet
      $packet = chr(0) . chr(4) . substr($buffer, 2, 2);
      // send ack
      socket_sendto($socket, $packet, strlen($packet), 0, $host, $port);

      // append the data to the return variable
      // for large files this function should take a file handle as an arg
      $ret .= substr($buffer, 4);
    }
    while(strlen($buffer) == 516);  // the first non-full packet is the last.
    return $ret;
  }
?>
bobbymcr
A: 

This is a loaded question. First you need to read the data. Then you have to put some structure to the data for it to be usable. It doesn't do you much good to just dump lat-lon to a database. You probably want to save it as a waypoint or part of a track etc.

So I have no answer to the DB question. Here is part of the PHP program I use to read GPS data off my phone connected to a CradlePoint router. It's modified from GoogleNav code.

function read_gps() {
        set_time_limit(5);
        $fp = fsockopen ("192.168.0.1", 8080, $errno, $errstr, 30);
        if (!$fp) {
           die ("$errstr ($errno)");
        }
        $point=false;
        $status="";
        $fix=0;
        while (!$point) {
                $string=@fgets($fp, 4096);
                switch (substr($string,0,6)) {
                        case "\$GPRMC" :
                                list($sentence, $time, $status, $latitude, $NS, $longitude, $EW, $speed, $course, $date, $magvar, $magvarEW)= explode(",", trim($string));
                                $latd=convdec($latitude, $NS);
                                $lond=convdec($longitude, $EW);
                                break;
                        case "\$GPGGA" :
                                list($sentence, $time, $latitude, $NS, $longitude, $EW, $fix, $nbsat, $HDOP, $altitude,,,,,)= explode(",", trim($string));
                                $latd=convdec($latitude, $NS);
                                $lond=convdec($longitude, $EW);
                                break;
                        default :
                        break;
                }

                if ($status=="A" and $fix == 1){
                        $point=true;
                }
        }
        ...

?>
ZZ Coder