Tulisan ini sebagai bahan referensi saja bagi kalian yang akan membuat GPS Tracer yang terpasang di Mikrotik, dimana titik koordinat akan di post ke database apabila titik koordinat ditemukan atau berpindah lalu hasil koordinat tersebut ditampilkan dalam sebuah marker peta.

Data titik koordinat diambil dari Mikrotik yang dipasang script untuk perintah post ke mikrotikgps.php, kemudian dari mikrotikgps.php berfungsi untuk menyimpan koordinat ke database. Data koordinat di database akan ditampilkan dalam format xml yang di generate oleh mikrotik_genxml.php.

Kemudian akan ditampilkan marker pada peta lokasi Mikrotik oleh mikrotiklocation.html yang data koordinatnya diambil dari hasil generate mikrotik_genxml.php.

Langkah

Langkahnya kita install dulu webserver localhost misalnya xampp lalu buat database untuk menyimpan data koordinat.

Database

Buat sebuah database dengan nama gps

CREATE TABLE `tbl_gps` (
  `identity` varchar(20) NOT NULL,
  `mac` varchar(20) NOT NULL,
  `latitude` varchar(30) NOT NULL,
  `longitude` varchar(30) NOT NULL,
  `lat` varchar(30) NOT NULL,
  `lng` varchar(30) NOT NULL,
  `date` date NOT NULL,
  `dbm` varchar(30) NOT NULL,
  `technology` varchar(11) NOT NULL,
  `speed` varchar(11) NOT NULL,
  `altitude` varchar50) NOT NULL,
  `satellites` varchar(11) NOT NULL
)

Script untuk Post ke mikrotikgps.php

Buat sebuah scheduler untuk menjalankan script secara berkala/interval

:local server "IP MIKROTIK";

# Get GPS info and find variables
:system gps monitor file="gps.txt";
:global gpstext [/file get gps.txt contents];
:local datestart [:find $gpstext "time: "];
:local dateend [:find $gpstext "latitude"];
:local date [:pick $gpstext ($datestart +6) ($dateend -14)]
:local longstart [:find $gpstext "longitude" -1];
:local longend [:find $gpstext "\n" $longstart];
:local latstart [:find $gpstext "latitude" -1];
:local latend [:find $gpstext "\n" $latstart];
:local validstart [:find $gpstext "valid" -1];
:local validend [:find $gpstext "\n" $validstart];
:local speedstart [:find $gpstext "speed"];
:local speedend [:find $gpstext "." $speedstart];
:local speed [;pick $gpstext ($speedstart +7) ($speedend +3)];
:local altitudestart [:find $gpstext "altitude"];
:local altitudeend [:find $gpstext "." $altitudestart];
:local altitude [;pick $gpstext ($altitudestart +10) ($altitudeend +3)];
:local satellitesstart [:find $gpstext "satellites"];
:local satellitesend [:find $gpstext "\n" $satellitesstart];
:local satellites [;pick $gpstext ($satellitesstart +12) ($satellitesend)];
:local valid false;
:local zeros "";

# Get Ethernet details and set mac variable
:interface ethernet print detail from=ether1 file=eth1details.txt
:global eth1details [/file get "eth1details.txt" contents];
:local macstart [:find $eth1details "orig"];
:local mac [:pick $eth1details ($macstart +17) ($macstart +34)];

# Get identity variable
:local identity [:system identity get value-name=name];

# Get LTE1 info, set signal and technology variables
:interface lte info lte1 file="lte1info.txt"
:local lte1info [:file get lte1info.txt contents];
:local sigstart [:find $lte1info "signal"];
:local sigend [:find $lte1info "dBm" $sigstart];
:local signal [:pick $lte1info ($sigstart +16) ($sigend -1)];
:local techstart [:find $lte1info "access"];
:local techend [:find $lte1info "\n" $techstart];
:local techno [:pick $lte1info ($techstart +19) ($techend)];

:local technology;
:if ($techno ="GSM compact") do={[:set technology "GSM";]}
:if ($techno ="3G") do={:set technology "UMTS";}
:if ($techno ="Evolved 3G (LTE)") do={[:set technology "LTE";]}

:if ([:find $gpstext "yes" $validstart] > 0) do={:set valid true;};

:global longitude [:pick $gpstext ($longstart + 11) $longend];
:local degreestart [:find $longitude " " -1];
:local minutestart [:find $longitude " " $degreestart];
:local secondstart [:find $longitude "'" $minutestart];

:local secondend;
:local secfract;

:if ([:len [:find $longitude "." 0]] < 1) do={
    :set secondend [:find $longitude "'" $secondstart];
    :set secfract "0";
} else={
    :set secondend [:find $longitude "." $secondstart];
    :set secfract [:pick $longitude ($secondend + 1) ($secondend + 2)];
};

:local longdegree;
:local longdegreelink;

:if ([:pick $longitude 0 1] = "W") do={
    :set longdegree "-";
    :set longdegreelink "W";
} else={
    :set longdegree "+";
    :set longdegreelink "E";
};

:set longdegree ($longdegree . [:pick $longitude 2 $minutestart]);
:set longdegreelink ($longdegreelink . [:pick $longitude 2 $minutestart]);
:local longmin [:pick $longitude ($minutestart + 1) $secondstart];
:local longsec [:pick $longitude ($secondstart + 2) $secondend];
:local longfract ((([:tonum $longmin] * 6000) + ([:tonum $longsec] * 100) + ([:tonum $secfract] * 10) ) / 36);

:while (([:len $zeros] + [:len $longfract]) < 4) do={
    :set zeros ($zeros . "0");
};

:global newlong ($longdegree . "." . $zeros . $longfract);
:global newlonglink ($longdegreelink . "." . $zeros . $longfract);

:global latitude [:pick $gpstext (latstart + 10) $latend];
:set degreestart [:find $latitude " " -1];
:set minutestart [:find $latitude " " $degreestart];
:set secondstart [:find $latitude "'" $minutestart];

:if ([:len [:find $latitude "." 0]] < 1) do={
    :set secondend [:find $latitude "'" $secondstart];
    :set secfract "0";
} else={
    :set secondend [:find $latitude "." $secondstart];
    :set secfract [:pick $latitude ($secondend + 1) ($secondend +2)];
};

:local latdegree;
:local latdegreelink;

:if ([:pick $latitude 0 1] = "N") do={
    :set latdegree "+";
    :set latdegreelink "N";
} else={
    :set latdegree "-";
    :set latdegreelink "S";
};

:set latdegree ($latdegree . [:pick $latitude 2 $minutestart]);
:set latdegreelink ($latdegreelink . [:pick $latitude 2 $minutestart]);
:local latmin [:pick $latitude ($minutestart + 1) $secondstart];
:local latsec [:pick $latitude ($secondstart + 2) $secondend];
:local latfract ((([:tonum $latmin] * 6000) + ([:tonum $latsec] * 100) +([:tonum $secfract] * 10)) / 36);

:set zeros "";

:while (([:len $zeros] + [:len $latfract]) < 4) do={
    :set zeros ($zeros . "0");
};

:global newlat ($latdegree . "." . $zeros . $latfract);
:global newlatlink ($latdegreelink . "." . $zeros . $latfract);

:global coordinates ($newlong . "," . $newlat);

# SEND TO SERVER IF THE COORDINATES HAVE CHANGED
:if (valid) do={
    :global oldpos;

    :if ($oldpos != $coordinates) do={
        {
        :local urlstring "http://$server/mikrotikgps.php\?identity=$identity&mac=$mac&latitude=$latitude&longitude=$longitude&lat=$newlat&lng=$newlong&date=$date&dbm=$signal&technology=$technology&speed=$speed&altitude=$altitude&satellites=$satellites";
        :local urlEncoded;
        :for i from=0 to=([:len $urlstring] - 1) do={ 
          :local char [:pick $urlstring $i]
          :if ($char = " ") do={
            :set $char "%20"
          }
          :if ($char = "-") do={
            :set $char "%2D"
          }
          :if ($char = "+") do={
            :set $char "%2B"
          }
          :set urlEncoded ($urlEncoded . $char)
        }
        :tool fetch url="$urlEncoded" mode=http dst-path=gps-to-url;
        }
        :set oldpos $coordinates;
        :global counter;
        :set counter 0;
     };
} else={
         :global counter;
         :set counter ($counter + 1);
         :log info "GPS not valid, count = $counter";
         :if ($counter = 12) do={
         :system routerboard usb power-reset duration=1;
         :set counter 0;
};

Script untuk Post informasi koordinat Mikrotik ke email

Script ini akan mengirimkan titik koordinat ke email apabila koordinat Mikrotik berubah

# CHANGE BELOW E-MAIL ADDRESS
:local email "[email protected]";

:system gps monitor file="gps.txt";
:global gpstext [/file get gps.txt contents];
:local longstart [:find $gpstext "longitude" -1];
:local longend [:find $gpstext "\n" $longstart];
:local latstart [:find $gpstext "latitude" -1];
:local latend [:find $gpstext "\n" $latstart];
:local validstart [:find $gpstext "valid" -1];
:local validend [:find $gpstext "\n" $validstart];
:local valid false;
:local zeros "";

:if ([:find $gpstext "yes" $validstart] > 0) do={:set valid true;};

:global longitude [:pick $gpstext ($longstart + 11) $longend];
:local degreestart [:find $longitude " " -1];
:local minutestart [:find $longitude " " $degreestart];
:local secondstart [:find $longitude "'" $minutestart];

:local secondend;
:local secfract;

:if ([:len [:find $longitude "." 0]] < 1) do={
    :set secondend [:find $longitude "'" $secondstart];
    :set secfract "0";
} else={
    :set secondend [:find $longitude "." $secondstart];
    :set secfract [:pick $longitude ($secondend + 1) ($secondend + 2)];
};

:local longdegree;
:local longdegreelink;

:if ([:pick $longitude 0 1] = "W") do={
    :set longdegree "-";
    :set longdegreelink "W";
} else={
    :set longdegree "+";
    :set longdegreelink "E";
};

:set longdegree ($longdegree . [:pick $longitude 2 $minutestart]);
:set longdegreelink ($longdegreelink . [:pick $longitude 2 $minutestart]);
:local longmin [:pick $longitude ($minutestart + 1) $secondstart];
:local longsec [:pick $longitude ($secondstart + 2) $secondend];
:local longfract ((([:tonum $longmin] * 6000) + ([:tonum $longsec] * 100) + ([:tonum $secfract] * 10) ) / 36);

:while (([:len $zeros] + [:len $longfract]) < 4) do={
    :set zeros ($zeros . "0");
};

:global newlong ($longdegree . "." . $zeros . $longfract);
:global newlonglink ($longdegreelink . "." . $zeros . $longfract);

:global latitude [:pick $gpstext (latstart + 10) $latend];
:set degreestart [:find $latitude " " -1];
:set minutestart [:find $latitude " " $degreestart];
:set secondstart [:find $latitude "'" $minutestart];

:if ([:len [:find $latitude "." 0]] < 1) do={
    :set secondend [:find $latitude "'" $secondstart];
    :set secfract "0";
} else={
    :set secondend [:find $latitude "." $secondstart];
    :set secfract [:pick $latitude ($secondend + 1) ($secondend +2)];
};

:local latdegree;
:local latdegreelink;

:if ([:pick $latitude 0 1] = "N") do={
    :set latdegree "+";
    :set latdegreelink "N";
} else={
    :set latdegree "-";
    :set latdegreelink "S";
};

:set latdegree ($latdegree . [:pick $latitude 2 $minutestart]);
:set latdegreelink ($latdegreelink . [:pick $latitude 2 $minutestart]);
:local latmin [:pick $latitude ($minutestart + 1) $secondstart];
:local latsec [:pick $latitude ($secondstart + 2) $secondend];
:local latfract ((([:tonum $latmin] * 6000) + ([:tonum $latsec] * 100) +([:tonum $secfract] * 10)) / 36);

:set zeros "";

:while (([:len $zeros] + [:len $latfract]) < 4) do={
    :set zeros ($zeros . "0");
};

:global newlat ($latdegree . "." . $zeros . $latfract);
:global newlatlink ($latdegreelink . "." . $zeros . $latfract);

:global coordinates ($newlong . "," . $newlat);

:global linkout "http://maps.google.com?q=$newlatlink+$newlonglink";
:global SMlinkout "http://www.openstreetmap.org/?lat=$newlat&lon=$newlong&zoom=8&layers=M";

:global kmlout "

  
    My router
    My router's location
    
      $coordinates
    
  

";

:if (valid) do={
    :global oldpos;

    :if ($oldpos != $coordinates) do={
        /file set [/file find name=gps.kml] contents=$kmlout
        /tool e-mail
        send to=$email subject="Router move" body="Moved to $latitude $longitude\r\n$linkout\r\n$SMlinkout" file=gps.kml
        :set oldpos $coordinates;
     };
} else={
         /tool e-mail
         send to=$email subject="Router gps position invalid" body="Router gps position invalid"
};

Buat mikrotikgps.php

Mikrotikgps.php berfungsi untuk menyimpan ke database dari data yang dikirim oleh script Mikrotik. https://pastebin.com/jL47etPQ

Buat koneksi.php

Koneksi.php berfungsi untuk menghubungkan ke database gps https://pastebin.com/tkdDbXEv

Buat mikrotik_genxml.php

Mikrotik_genxml.php berfungsi untuk memparse/membuat data dengan format xml dari tabel tbl_gps https://pastebin.com/3F0pquJ7 . Seperti pada gambar dibawah ini:

Buat mikrotiklocation.html

File ini untuk menampilkan peta lokasi/marker posisi Mikrotik. https://pastebin.com/LWHCfNBN

Hasilnya

Hasilnya akan ditampilkan dalam sebuah marker/titik koordinat dalam peta seperti pada gambar dibawah ini:

Sumber :

https://wiki.mikrotik.com/wiki/Manual:GPS-tracking

https://github.com/timmay2/mikrotik-gps

2 COMMENTS

  1. ini semacam klo di Indihome untuk cek posisi odp terdekat dari pelanggan fiber optik seperti itu kan om ?

Leave a Reply