Example: Geolocalization with LM based on Traccar service

Task

Traccar is a free and Open Source GPS Tracking Platform. Clients (dedicated devices, smartphones, tablets) send its current positions and other geo-data to server (Traccar server on PC or TCP server on LogicMachine). This tutorial describes how to configure Traccar platform (iPhone and demo server) and how LogicMachine can read the data from this server.

Traccar pages:
https://www.traccar.org
http://traccar.litvak.su/index.html
 

1.Register new account on one of a few demo servers e.g. http://demo.traccar.org

See https://www.traccar.org/demo-server

 

2. Install “Traccar client” app on your iPhone

 
traccar_client_settings_logicmachine

3. Configure your client

After the configuration is done, change the page to “Status”.
If you see a message like “16:09 – Location update” it works good.

Note! *I’ve checked this on iPhone and I saw some situations when client stopped sending data to the server. I don’t know if this was iPhone app problem or some standby issues. But I’ve checked that when I open a client and go to “Status page” and then minimize the app it works.
 
traccar_server_logicmachine

4. Add your client on a server

 

5. Create “user.traccar” in Scripting -> User libraries and paste the following code

Source code    
  1. --======================================================================================================
  2. --*********************************** INTEGRATION WITH TRACCAR PLATFORM ******************************--
  3. --************************* Version 1.0 Created by Sławomir Budzyński 11-11-2016 *********************--
  4. --======================================================================================================
  5.  
  6. traccar = {
  7. update = function()
  8. -- load required modules
  9. http = require("socket.http")
  10. mime = require("mime")
  11. ltn12 = require("ltn12")
  12. json = require('json')
  13.  
  14. -- edit parameters
  15. -- server account
  16. login = 'login'
  17. password = 'password'
  18.  
  19. -- UTC zone of Logic Machine e.g. 1 or -1
  20. utc = 1
  21.  
  22. -- Daylight Saving Time e.g. true or false
  23. dst = true
  24.  
  25. -- sending request to Traccar server
  26. t = {}
  27. r, c, d, e = http.request {
  28. url = "http://demo.traccar.org/api/positions/",
  29. headers = { authorization = "Basic " .. (mime.b64(login .. ':' .. password)) },
  30. sink = ltn12.sink.table(t)
  31. }
  32.  
  33. if (c ~= 200) then
  34. alert("traccar auth error")
  35. return
  36. end
  37.  
  38. -- decoding received data
  39. response = table.concat(t)
  40. response = json.decode(response)
  41.  
  42. if (type(response) ~= 'table') then
  43. alert("traccar decode error")
  44. return
  45. end
  46.  
  47. -- compare the time from last data received from client to now
  48. date = response[1].fixTime
  49. d = {}
  50. d.year, d.month, d.day, d.hour, d.min, d.sec = date:match('(%d+)-(%d+)-(%d+)T(%d+):(%d+):(%d+)')
  51. d.hour = d.hour + utc
  52. if dst == true and os.date("*t")[istdst] then d.hour = d.hour + 1 end
  53. time = os.time(d)
  54.  
  55. -- how much second elapsed from the last data received from a client
  56. delta = os.microtime() - time
  57.  
  58. last_delta = storage.get('Traccar_Last update', 100000)
  59.  
  60. -- update GAs when thera was some update from client or update the
  61. if delta < last_delta then
  62. storage.set('Traccar_Last update', delta)
  63. grp.update('Traccar_Last update', delta)
  64. grp.update('Longitude', response[1].longitude)
  65. grp.update('Latitude', response[1].latitude)
  66. grp.update('Speed', response[1].speed)
  67. grp.update('Altitude', response[1].altitude)
  68. else
  69. storage.set('Traccar_Last update', delta)
  70. end
  71. end,
  72.  
  73. howfar = function( from, to )
  74. if from.latitude == nil or from.longitude == nil or to.latitude == nil or to.longitude == nil then
  75. return nil
  76. end
  77. local dlat = math.rad(to.latitude-from.latitude)
  78. local dlon = math.rad(to.longitude-from.longitude)
  79. local sin_dlat = math.sin(dlat/2)
  80. local sin_dlon = math.sin(dlon/2)
  81. local a = sin_dlat * sin_dlat + math.cos(math.rad(from.latitude)) * math.cos(math.rad(to.latitude)) * sin_dlon * sin_dlon
  82. local c = 2 * math.atan2(math.sqrt(a), math.sqrt(1-a))
  83. -- 6378 km is the earth's radius at the equator.
  84. -- 6357 km would be the radius at the poles (earth isn't a perfect circle).
  85. -- Thus, high latitude distances will be slightly overestimated
  86. -- To get miles, use 3963 as the constant (equator again)
  87. local d = 6378 * c
  88. return d
  89. end,
  90.  
  91. tocoordinates = function(name)
  92. if traccar.locations[name] then
  93. return traccar.locations[name].longitude, traccar.locations[name].latitude
  94. end
  95. return nil
  96. end,
  97.  
  98. locations = {
  99. home = {longitude = 23.170, latitude = 50.983},
  100. work = {longitude = 22.968, latitude = 51.246},
  101. }
  102. }

Type your traccar server login, password and LM UTC zone. You can also add your known locations e.g. home and work.

 

6. Add Scheduled script to your LogicMachine with interval = 1 minute or more

Interval depends on how often your clients sends the data to your server. Paste below script:

Source code    
  1. -- Read the data from the traccar server
  2.  
  3. traccar.update()

 

7. Use this data to creating some new interesting features

I’ve added some tools for this purpose like below functions which are included in the library:

tocoordinates(name) – return coordinates from the known name(see point 5).

howfar(from, to) – calculates the distance between 2 points on the Earth in km.

 

Some additional screenshots from Traccar server to show how it looks

It is possible to prepare very good with optimal price geolocalisation system.

traccar_server_1traccar_server_3

traccar_server_5

traccar_server_6

traccar_server_7

traccar_server_8

traccar_server_9

 
 
 
 
 
 
 
 
  
 
 
 
 
 
 
 
 
 
 
 
 
 

You can also expand possibilities of Traccar by using one of following APIs:
– Google Places API
– Google Geolocation API
– Google Geocoding API
– Google Distance Matrix API

 
Example created by Sławomir Budzyński