Send data to traccar from php

Leif Neland7 years ago

Here is a function for loading data into traccar from php (I use this to feed locations from Endomondo to traccar)

It emulates a TK101 when sending data to port 5006

    function sendToTraccar($devid,$time,$latitude,$longitude); 
      $date=date_create($time); 
  
      $gpmrc=sprintf("GPRMC,%s,A,%09.4f,N,%010.4f,E,0.00,0.00,%s,,,A", 
        date_format($date,"His.v"), 
        toNmea($latitude), 
        toNmea($longitude), 
        date_format($date,"dmy"));
      $xs=xorsum($gpmrc);
      $part1=sprintf("%s,%s*%X,F,imei:%015s","12345",$gpmrc,$xs,$devid);
      $str=sprintf("%s,%s,%d",date_format($date,"ymdHis"),$part1,strlen($part1));

      $fp = fsockopen("tracker.example.com", 5006, $errno, $errstr, 30);
      if (!$fp) {
          echo "$errstr ($errno)\n";
      } else {
          fwrite($fp, $str);
          fclose($fp); // To close the connection
      }
    }

    function toNmea($deg) {
      $i=floor($deg);
      $p=$deg-$i;
      return(100*$i+60*$p);
     }
     function xorsum($data)
     {
       $crc = 0x00;
       for ($i = 0; $i < strlen($data); $i++)
       {
         $crc = ($crc ^ ord($data[$i])) & 0xFF;
       }
       return $crc;
     }
Hussah Saleh6 years ago

Hi Leif, This is such an amazing code that save my time. Thank you for sharing the code.

Something you might miss, on the first line at the end of the function signature is " { "

Thank you,

Leif Neland6 years ago

Thanks.
But look at the osmand protocol, that's only a fopen with coordinates as parameters, even easier.

Can't supply code, I'm on mobile this week.

Himanshu4 years ago

Dear Leif Neland
Can you provide the code using fopen? I am unable to get this working.

Hussah Saleh4 years ago

Hi Himanshu,
It is working perfect, just make sure to change ";" at the end of the first line to "{".

Also, make sure to allow port 5006 in firewall rules.

Himanshu4 years ago

I am looking for code using php and osmand protocol as suggested by Leif Neland. I am trying to send data using osmand protocol on 5055 using fopen and CURL (tried both) in PHP. The problem is that fopen or CURL is immediately closing the connection right after data being send. The online/offline status get updated but traccar is not storing the lat/long/speed and other parameters. When I add sleep function before closing the curl so that curl keep the connection open. With sleep function of 1 second, data is getting stored by traccar. Now the challenge is that I want to send 200 packets every minute and it is taking more than 200 second to get the complete 200 data entered into traccar. Is there any other way to resolve this issue?