Mikrotik GPS

Maksim3 years ago

My current working version:

:local serverurl "SERVER";
:local serverport "5055";
:local id "IMEI";

:global Lat
:global Lon
:global Altitude
:global Bearing
:global Speed
:global Satellites
:global Valid

/system gps monitor once do={
:set $Lat $("latitude")
:set $Lon $("longitude")
:local altitudetxt $("altitude")
:set $Altitude [:pick $altitudetxt 0 5]
:local bearinval $("true-bearing")
:set $Bearing [:pick $bearinval 0 9]
:local speedtxt $("speed")
:set $Speed [:pick $speedtxt 0 8]
:set $Satellites $("satellites")
:set $Valid $("valid")
}

:local longdegree;
:set longdegree "+";
:local latdegree;
:set latdegree "+";

:set Lon ($longdegree . $Lon);
:set Lat ($latdegree . $Lat);

:global fncJD do={
:local months [:toarray "jan,feb,mar,apr,may,jun,jul,aug,sep,oct,nov,dec"];
:local jd
:local M [:pick $1 0 3];
:local D [:pick $1 4 6];
:local Y [:pick $1 7 11];
:for x from=0 to=([:len $months] - 1) do={
   :if ([:tostr [:pick $months $x]] = $M) do={:set M ($x + 1) }
   }
:if ( $M = 1 || $M = 2) do={
    :set Y ($Y-1);
    :set M ($M+12);
}
:local A ($Y/100)
:local B ($A/4)
:local C (2-$A+$B)
:local E ((($Y+4716) * 36525)/100)
:local F ((306001*($M+1))/10000)
:local jd ($C+$D+$E+$F-1525)
:return $jd
};

:global timestamp do={
:global fncJD $fncJD
:local currtime [/system clock get time];
:local jdnow [$fncJD [/system clock get date]]
:local days ($jdnow - 2440587)
:local ore [:pick $currtime 0 2]
:local minute [:pick $currtime 3 5]
:local secunde [:pick $currtime 6 8]
:return (($days * 86400) + ($ore * 3600) + ($minute * 60) + $secunde - [/system clock get gmt-offset]);
}

:local mystamp [$timestamp];

# :log info message="GPS Script: Posting update. lt=$Lat, lon=$Lon timestamp=$mystamp altitude=$Altitude speed=$Speed course=$Bearing satelites=$Satellites gps=$Satellites valid=$Valid";

:do {/tool fetch keep-result=no check-certificate=no url=("http://" . $serverurl . ":" . $serverport ."/\?id=$id&lat=$Lat&lon=$Lon×tamp=$mystamp&altitude=$Altitude&speed=$Speed&course=$Bearing&satelites=$Satellites&gps=$Satellites&valid=$Valid")
} on-error={:log error message="Fetch failed..."}

# :log info message="GPS Script: Posting update DONE";
Sasha3 years ago

You are the best! Thank you so much!

Sasha3 years ago

Hey @Maksim,

I've started testing the system and getting errors, hope you can point me to the right direction.

I started test with a simple curl command run-in locally.

curl --request POST --url 'http://local-lan-ip:5055/?lat=20&lon=20&id=1234567'

It worked, got 200 and the ID appears in traccar interface.

Next tried running curl remotely. It worked as well.

curl --request POST --url 'http://remote-ip:5055/?lat=20&lon=20&id=1234567'

After changing the variables in the script I ran it and got Fetch failed...

From the LTAP terminal I've tried:

/tool fetch "http://remote-ip:5055/?lat=20&lon=20&id=1234567"

And that retuned status: failed, 400 Bad Request.

What do you think is it?

Maksim3 years ago

Have you checked the script on Mikrotik itself?

Sasha3 years ago

Yes. And the log said Fetch failed…

Maksim3 years ago

Sorry, unfortunately I don't know what it might be.

Sasha3 years ago

Fixed it with different script. Not sure why the original didn't work, maybe version difference?

So things I've learned:

In GPS settings, change the Coordinate Format to: dd
Use port 5055
And HTTP, HTTPS is the next step

The script that works for me is from the Mikrotik forum.
https://forum.mikrotik.com/viewtopic.php?f=23&t=155106&p=791295&hilit=traccar#p791295

{
    :local devid
    :local spd
    :local alti
    :local lat
    :local lon
    :local vld

    :set $devid "UNIQUEDEVICEID"

    :local sendurl  ("https://YOUR.TRACCAR.SERVER.URL/")

    /system gps monitor once do={
        :set $vld $("valid")
        :set $lat $("latitude")
        :set $lon $("longitude")
        :set $spd $("speed")
        :set $alti $("altitude")
        
    }

    if ($vld = true) do {

        :set $alti [:pick $alti 0 [:find $alti "m" -1]]
        :set $spd [:pick $spd 0 [:find $spd " km/h" -1]]
     
        :local senddata ("\?id=" . $devid. "&lat=" . $lat . "&lon=" . $lon . "&altitude=" . $alti . "&speed=" . $spd)

        /tool fetch mode=https url=$sendurl port=40445 http-method=put http-data=$senddata http-header-field="Content-Type: application/x-www-form-urlencoded" output=none
    }

}
Snoop3 years ago

Hello,
I've tried i think every script on the internet and i cannot get this working.
Any chance of some help>?
I'm using multiple scripts to try and find the right one and they're all scheduled to run every ten seconds on my ltap mini.
So far nothing going to the demo server at all despite me matching the IMEI of my lte connection.
Any help would be appreciated.

:local serverport "5055";
:local id "355654093968367";

:global Lat
:global Lon
:global Altitude
:global Bearing
:global Speed
:global Satellites
:global Valid

/system gps monitor once do={
:set $Lat $("latitude")
:set $Lon $("longitude")
:local altitudetxt $("altitude")
:set $Altitude [:pick $altitudetxt 0 5]
:local bearinval $("true-bearing")
:set $Bearing [:pick $bearinval 0 9]
:local speedtxt $("speed")
:set $Speed [:pick $speedtxt 0 8]
:set $Satellites $("satellites")
:set $Valid $("valid")
}

:local longdegree;
:set longdegree "+";
:local latdegree;
:set latdegree "+";

:set Lon ($longdegree . $Lon);
:set Lat ($latdegree . $Lat);

:global fncJD do={
:local months [:toarray "jan,feb,mar,apr,may,jun,jul,aug,sep,oct,nov,dec"];
:local jd
:local M [:pick $1 0 3];
:local D [:pick $1 4 6];
:local Y [:pick $1 7 11];
:for x from=0 to=([:len $months] - 1) do={
   :if ([:tostr [:pick $months $x]] = $M) do={:set M ($x + 1) }
   }
:if ( $M = 1 || $M = 2) do={
    :set Y ($Y-1);
    :set M ($M+12);
}
:local A ($Y/100)
:local B ($A/4)
:local C (2-$A+$B)
:local E ((($Y+4716) * 36525)/100)
:local F ((306001*($M+1))/10000)
:local jd ($C+$D+$E+$F-1525)
:return $jd
};

:global timestamp do={
:global fncJD $fncJD
:local currtime [/system clock get time];
:local jdnow [$fncJD [/system clock get date]]
:local days ($jdnow - 2440587)
:local ore [:pick $currtime 0 2]
:local minute [:pick $currtime 3 5]
:local secunde [:pick $currtime 6 8]
:return (($days * 86400) + ($ore * 3600) + ($minute * 60) + $secunde - [/system clock get gmt-offset]);
}

:local mystamp [$timestamp];

# :log info message="GPS Script: Posting update. lt=$Lat, lon=$Lon timestamp=$mystamp altitude=$Altitude speed=$Speed course=$Bearing satelites=$Satellites gps=$Satellites valid=$Valid";

:do {/tool fetch keep-result=no check-certificate=no url=("http://" . $serverurl . ":" . $serverport ."/\?id=$id&lat=$Lat&lon=$LonĂ—tamp=$mystamp&altitude=$Altitude&speed=$Speed&course=$Bearing&satelites=$Satellites&gps=$Satellites&valid=$Valid")
} on-error={:log error message="Fetch failed..."}

# :log info message="GPS Script: Posting update DONE";