For my birthday, I wanted to return in a cozy restaurant in Florence, where I had a memorable dinner years ago.
I remembered the delicious food and the warm atmosphere, but I couldn’t remember the name or the address of the place. I knew it was somewhere in the city center, but that was all I could remember.
I decided to use the location application on my smartphone to try to find the restaurant: I zoomed in on the center of Florence and within seconds I had found it!
I couldn’t believe it: I had found the restaurant after all these years. I booked immediately and couldn’t wait to return to relive that special dinner.
Since that day, I have made sure to keep my location tracking active at all times.
What is location tracking
Location tracking is a function common to most smartphones that allows you to record information about your location over a certain period of time, which you can consult later to retrace your steps and remember where you have been.
There are several benefits in tracking your location history:
- Memory aid: it can serve as a memory aid to remember places they have visited, things they have done, and where you have been.
- Reflect on past habits: it can help you to see your past habits and location patterns, which can be useful for self-reflection or personal growth.
- Share location history with others: you can share your location history with friends, family or partner to track your movements.
- Personal safety: it can help you keep track of your movements, especially if you are traveling alone or in an unfamiliar area.
Google Maps Timeline is the most common location tracking platform, as it is integrated into the popular Google Maps application.
Unfortunately, Google Maps Timeline hides a major privacy problem, as the location history is analysed by Google and used for targeted advertisements.
A privacy-oriented alternative for location tracking
OwnTracks is a great open source alternative for people who are looking for more control over their location data compare to commercial solutions like Google Maps Timeline.
OwnTracks platform consists of two components:
- A open-source mobile app that runs in the background on your mobile device and sends out your current coordinates to your server
- Your server where your backend runs to listen to the data broadcasted by the mobile app and processes your received data, to store in a database and show your position history on a map.
You see that there is no commercial entity collecting your data and processing it for its own purposes.
Your data is yours and remains in your hands!
Since we love coding and self-hosted solutions here, let’s have fun creating your own self-hosted location system to provide the same functions as Google Map Timeline, but in a privacy-oriented way.
First we need to set up the server to listen to the messages the application sends and to process the data, then we will configure the mobile application on your smartphone.
Setup the backend on your server
Before we can configure the mobile app, we need to setup the self-hosted backend on your server, which is made up of four components:
- the MQTT broker
- the Database
- the Listener
- the Location Diary
The MQTT broker
MQTT (Message Queue Telemetry Transport) is a protocol for communication between devices and a central server called “broker”.
MQTT uses a “publish-subscribe” model, in which devices publish messages to a specific topic, and other devices can subscribe that topic to receive the messages.
In OwnTracks platform, the MQTT broker allows the Listener to subscribe to the topic that we will later configure in the mobile app.
We choose Eclipse Mosquitto broker, an easy to install open source MQTT broker. Here is a quick guide to install on Linux:
Install the Mosquitto package:
sudo apt-get update sudo apt-get install mosquitto mosquitto-clients
To start, stop or restart the broker you can use the following command:
sudo systemctl start mosquitto sudo systemctl stop mosquitto sudo systemctl restart mosquitto
Check the status of the service by running:
systemctl status mosquitto
The mosquitto_sub tool can be used to subscribe to a topic and receive messages.
mosquitto_sub -h localhost -t "test"
The mosquitto_pub tool can be used to publish a message to a specific topic
mosquitto_pub -h localhost -t "test" -m "hello world"
To secure the broker, create a password file:
sudo mosquitto_passwd -c /etc/mosquitto/passwd <username>
edit the mosquitto.conf file and add the following options:
allow_anonymous false password_file /etc/mosquitto/passwd
Restart the service
sudo systemctl restart mosquitto
The MQTT broker must be accessible from the internet, as the mobile app will periodically publish its location by connecting to your server.
You will need to set up port forwarding on your router. This will allow incoming traffic on a port 1883 for MQTT to be directed to the internal IP address of the device running the MQTT broker.
If you have static IP address, you will use it in the configuration of the mobile app.
If you do not have a static IP address, you can use a Dynamic DNS service (e.g., Duck DNS) that will allow you to access your broker by using a hostname instead of an IP address. The service will automatically update the DNS record with your current IP address, so you don’t have to worry about updating it manually.
We need to setup the database and table to store the data that the Listener will receive from the mobile app.
Here is the proper configuration for the MySql database:
CREATE DATABASE owntracks; USE owntracks; CREATE TABLE owntracks ( id INT AUTO_INCREMENT PRIMARY KEY, timestamp TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, lat FLOAT NOT NULL, lon FLOAT NOT NULL);
The Listener is a Python script that runs in the background, receives the location from the mobile app and stores the data in a database.
The script connects to the MQTT broker, subscribes to the Owntracks topic, and listens for incoming messages. The topic will be defined later when setting up the mobile app, so remember to return to the Listener configuration to insert the topic.
When a message is received, it decodes the payload that contains latitude and longitude, converts it to a JSON object, and it inserts those values into a MySQL database table.
The Location Diary web application
Finally, the Location Diary is the web application that you will use to see you location history.
Two view options are available:
- Markers view: a marker is displayed for each location point tracked, with a line connecting them to show your route. In marker view, you can filters by date using the date picker.
- Heatmap view: colored areas represent the density of points in a given area, from warmer to cooler colors. This allows you to see where you have spent the most time.
How to setup OwnTracks mobile app
Now it’s time to install and setup the OwnTracks application on your mobile phone!
Remember that all components of this project are open source, and also the app guarantees your privacy as it sends your location only to your server and not to any commercial cloud.
- Download the OwnTracks app from the App Store (iOS) or the Google Play Store (Android)
- In the app settings (info icon on top left corner), set the connection mode to MQTT and configure the connection settings with your self-hosted MQTT broker’s hostname, port number and credentials that you set up in the configuration of the broker
- Go back to the map, click on your marker and retrieve your personal topic (“
owntracks/mqtt/<token>”) that you need to insert in the configuration of the Listener on your server.
In the standard configuration, the OwnTracks app sends the data unencrypted to your MQTT broker.
For your security and privacy, consider to configure both Mosquitto and the mobile app to use TSL. I plan to write a dedicated post on securing OwnTracks!
Your private location tracker is now live!
And here we are! The mobile app will start sending your position to your server (especially when you move, to save your device battery) and the Listener will constantly listen to the messages sent over MQTT protocol and save your location to your personal database. The Location Diary will retrieve the locations from the database and show on a map.
And the Location Diary will do the magic!
Relive your trips, navigate in the past with the date picker, and discover where you spent the most of your time with the heatmap!
Thank you for being interested in this project!
If you have installed the project on your server, please leave a comment to share your experience!