Raspberry Pi GPS tracker – Converting Code to PHP – Part 2

In the last post I looked at converting the original Python code to PHP. This all ran without issue but I quickly found that because the Pi wasn’t connected to the internet the date and time of the device never got updated. This meant that the log files always had the wrong timestamp when they were created making it difficult to find the one I needed.

Turns out that there is a simple answer to this problem. As the GPS satellites include the current date and time as part of the detail that is sent along with the location we can extract this information and then update the Pi using the Linux date command. The code for this is below.

    $resp = explode(",", $line);
    if ($first_time){
        // set the pi time
        $dt = substr($resp[9],4,2).'-'.substr($resp[9],2,2).'-'.substr($resp[9],0,2).' '.
              substr($resp[1],0,2).':'.substr($resp[1],2,2).':'.substr($resp[1],4,2);
        if ($set_time) exec('date -s "'.$dt.'"');
        $first_time = FALSE;
    }

You only need to do this once on start-up hence the $first_time boolean.

This is part of the code, the rest of which you can find on my Github page here.

Next up I will be looking at how you can access your Raspberry Pi from another device without having to connect it to the Internet.