Geolocalization with LM based on Traccar service
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
3. Configure your client
- Device identifier: “56721” (or e.g. “4ruhru4rme”, “Car1532”)
- Server address: “demo.traccar.org”
- Server port: 5055
- Encryption: no
- Frequency(in seconds): e.g. 300 = 5 minutes
- Service status: yes
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.
4. Add your client on a server
- Name: “Andreas”
- Identifier: “56721” (the same as in the client)
- Group: empty or e.g. (Kids, Parents, Others or company divisions etc.)
5. Create “user.traccar” in Scripting -> User libraries and paste the following code
- --======================================================================================================
- --*********************************** INTEGRATION WITH TRACCAR PLATFORM ******************************--
- --************************* Version 1.0 Created by Sławomir Budzyński 11-11-2016 *********************--
- --======================================================================================================
-
- traccar = {
- update = function()
- -- load required modules
- http = require("socket.http")
- mime = require("mime")
- ltn12 = require("ltn12")
- json = require('json')
-
- -- edit parameters
- -- server account
- login = 'login'
- password = 'password'
-
- -- UTC zone of Logic Machine e.g. 1 or -1
- utc = 1
-
- -- Daylight Saving Time e.g. true or false
- dst = true
-
- -- sending request to Traccar server
- t = {}
- r, c, d, e = http.request {
- url = "http://demo.traccar.org/api/positions/",
- headers = { authorization = "Basic " .. (mime.b64(login .. ':' .. password)) },
- sink = ltn12.sink.table(t)
- }
-
- if (c ~= 200) then
- alert("traccar auth error")
- return
- end
-
- -- decoding received data
- response = table.concat(t)
- response = json.decode(response)
-
- if (type(response) ~= 'table') then
- alert("traccar decode error")
- return
- end
-
- -- compare the time from last data received from client to now
- date = response[1].fixTime
- d = {}
- d.year, d.month, d.day, d.hour, d.min, d.sec = date:match('(%d+)-(%d+)-(%d+)T(%d+):(%d+):(%d+)')
- d.hour = d.hour + utc
- if dst == true and os.date("*t")[istdst] then d.hour = d.hour + 1 end
- time = os.time(d)
-
- -- how much second elapsed from the last data received from a client
- delta = os.microtime() - time
-
- last_delta = storage.get('Traccar_Last update', 100000)
-
- -- update GAs when thera was some update from client or update the
- if delta < last_delta then
- storage.set('Traccar_Last update', delta)
- grp.update('Traccar_Last update', delta)
- grp.update('Longitude', response[1].longitude)
- grp.update('Latitude', response[1].latitude)
- grp.update('Speed', response[1].speed)
- grp.update('Altitude', response[1].altitude)
- else
- storage.set('Traccar_Last update', delta)
- end
- end,
-
- howfar = function( from, to )
- if from.latitude == nil or from.longitude == nil or to.latitude == nil or to.longitude == nil then
- return nil
- end
- local dlat = math.rad(to.latitude-from.latitude)
- local dlon = math.rad(to.longitude-from.longitude)
- local sin_dlat = math.sin(dlat/2)
- local sin_dlon = math.sin(dlon/2)
- local a = sin_dlat * sin_dlat + math.cos(math.rad(from.latitude)) * math.cos(math.rad(to.latitude)) * sin_dlon * sin_dlon
- local c = 2 * math.atan2(math.sqrt(a), math.sqrt(1-a))
- -- 6378 km is the earth's radius at the equator.
- -- 6357 km would be the radius at the poles (earth isn't a perfect circle).
- -- Thus, high latitude distances will be slightly overestimated
- -- To get miles, use 3963 as the constant (equator again)
- local d = 6378 * c
- return d
- end,
-
- tocoordinates = function(name)
- if traccar.locations[name] then
- return traccar.locations[name].longitude, traccar.locations[name].latitude
- end
- return nil
- end,
-
- locations = {
- home = {longitude = 23.170, latitude = 50.983},
- work = {longitude = 22.968, latitude = 51.246},
- }
- }
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:
- -- Read the data from the traccar server
-
- 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.
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