Creating a script for tracking my router GPS location

Hi all,
I recently purchased a MikroTik LtAP router and put it in my van. Idea is to use it as a portable hotspot and car tracker.
My scripting skills are very limited and this is what me and AI came up with:

# Define the log function
:global logMessage do={
    :local message ($1)
    :log info $message
}

# Log script start
$logMessage "Starting GPS script"

# Initialize variables for current and previous coordinates
:global lat
:global lon
:global prevLat
:global prevLon

# Fetch GPS coordinates
/system gps monitor once do={
    :set $lat $"latitude"
    :set $lon $"longitude"
    $logMessage ("Fetched GPS coordinates: lat=" . $lat . ", lon=" . $lon)
}

# Check if lat and lon were fetched correctly
:if ([:typeof $lat] = "nothing" or [:typeof $lon] = "nothing") do={
    $logMessage ("Failed to fetch GPS coordinates.")
    :error "Failed to fetch GPS coordinates."
}

# Ensure previous coordinates are initialized
:if ([:typeof $prevLat] = "nothing" or [:typeof $prevLon] = "nothing") do={
    :set $prevLat $lat
    :set $prevLon $lon
    $logMessage ("Initialized previous coordinates: prevLat=" . $prevLat . ", prevLon=" . $prevLon)
}

# Log current and previous coordinates for debugging
$logMessage ("Current coordinates: lat=" . $lat . ", lon=" . $lon)
$logMessage ("Previous coordinates: prevLat=" . $prevLat . ", prevLon=" . $prevLon)

# Calculate the change in degrees
:local deltaLat ($lat - $prevLat)
:local deltaLon ($lon - $prevLon)

# Log the changes
$logMessage ("Change in degrees: deltaLat=" . $deltaLat . ", deltaLon=" . $deltaLon)

There is some more code afterward but it breaks because the output changes from DD to DMS (if I am not mistaken)

Here is the log output:

Starting GPS script
Fetched GPS coordinates: lat=43.610883, lon=15.982591
Current coordinates: lat=43.610883, lon=15.982591
Previous coordinates: prevLat=43.610875, prevLon=15.982556
Change in degrees: deltaLat=00:00:00.000008, deltaLon=00:00:00.000035

Any tips on how to solve this would be greatly appreciated

I managed to get the script working. If anyone needs it, feel free to copy it

# Define global variables for current and previous coordinates
:global lat
:global lon
:global prevLat
:global prevLon

# Fetch GPS coordinates
/system gps monitor once do={
    :set $lat $"latitude"
    :set $lon $"longitude"
}

# Check if lat and lon were fetched correctly
:if ([:typeof $lat] = "nothing" or [:typeof $lon] = "nothing") do={
    :error "Failed to fetch GPS coordinates."
}

# Ensure previous coordinates are initialized
:if ([:typeof $prevLat] = "nothing" or [:typeof $prevLon] = "nothing") do={
    :set $prevLat $lat
    :set $prevLon $lon
}

# Extract integer and fractional parts of the coordinates
:local latInt [:pick $lat 0 [:find $lat "."]]
:local latFrac [:pick $lat ([:find $lat "."] + 1) [:len $lat]]
:local lonInt [:pick $lon 0 [:find $lon "."]]
:local lonFrac [:pick $lon ([:find $lon "."] + 1) [:len $lon]]

:local prevLatInt [:pick $prevLat 0 [:find $prevLat "."]]
:local prevLatFrac [:pick $prevLat ([:find $prevLat "."] + 1) [:len $prevLat]]
:local prevLonInt [:pick $prevLon 0 [:find $prevLon "."]]
:local prevLonFrac [:pick $prevLon ([:find $prevLon "."] + 1) [:len $prevLon]]

# Convert to numbers and combine parts
:local latScaled ([:tonum $latInt] * 1000000 + [:tonum $latFrac])
:local lonScaled ([:tonum $lonInt] * 1000000 + [:tonum $lonFrac])
:local prevLatScaled ([:tonum $prevLatInt] * 1000000 + [:tonum $prevLatFrac])
:local prevLonScaled ([:tonum $prevLonInt] * 1000000 + [:tonum $prevLonFrac])

# Calculate the change in scaled degrees
:local deltaLat ($latScaled - $prevLatScaled)
:local deltaLon ($lonScaled - $prevLonScaled)

# Define the threshold in scaled degrees (approximately 20 meters)
:local threshold 300

# Calculate absolute values manually
:local absDeltaLat ($deltaLat)
:if ($deltaLat < 0) do={
    :set absDeltaLat ($deltaLat * -1)
}

:local absDeltaLon ($deltaLon)
:if ($deltaLon < 0) do={
    :set absDeltaLon ($deltaLon * -1)
}

# Threshold check
:if ($absDeltaLat > $threshold or $absDeltaLon > $threshold) do={
    :set $prevLat $lat
    :set $prevLon $lon

    # Send data to the URL
    :local url "http://yourwebsite.com/index.php"
    :local data ("{\"lat\":\"" . $lat . "\",\"lon\":\"" . $lon . "\"}")
    /tool fetch mode=http url=$url http-method=post http-data=$data http-header-field="Content-Type: application/json"
    :put ("{\"lat\":\"" . $lat . "\",\"lon\":\"" . $lon . "\"}")
}

You will, of course, need a server (you can use Google Cloud free tier) to log and display the coordinates, there is a decent tutorial here:https://www.youtube.com/watch?v=2EVXUhf4ZEU