A self-hosted GPS pet tracking solution — part 1

A self-hosted GPS pet tracking solution — part 1
Photo by Svetlana Gumerova / Unsplash

As a pet owner, keeping track of your furry friend can provide invaluable peace of mind. Whether you’re dealing with an adventurous pup or simply want to ensure your pet’s safety during walks, a GPS tracker can be a game-changer. In this two-part series, I’ll share my journey of building a self-hosted GPS pet tracking solution that’s reliable, customizable, and free from ongoing subscription costs.

Why Track Your Pet?

Dogs are curious by nature. Even a slight distraction can lead to them wandering off. Having a GPS tracker allows you to locate them more quickly. This not only ensures their safety but also offers peace of mind.

Defining the Requirements

To build a robust GPS tracking system, I had a few must-haves for both the tracker device and the server:

For the tracker device:

  • Small and lightweight: Comfortable for the dog to wear.
  • Good battery life: To minimize frequent recharging, and to ensure it does not run out of battery when the dog needs to be found
  • Cellular connectivity: To work virtually anywhere
  • Data efficiency: Keeps operational costs low.
  • Waterproof (IP68): Durable in all weather conditions.
  • Easy to charge: Convenient for regular use.
  • Attachment-friendly: Easily secures to a collar or harness.

For the tracking server:

  • Cross-platform support: Accessible on both desktop and mobile.
  • Geofence capabilities: For setting virtual boundaries.
  • Home Assistant integration: To fit into my existing smart home ecosystem.
  • Comprehensive mapping: Includes both street map and satellite imagery.
  • Cost-effective: Free of charge and preferably open-source.

The Solution

After researching numerous options, I settled on the following:

  • Server: Traccar — an open-source GPS tracking system with robust features and a user-friendly interface.
  • Device: Mictrack MT710 — a compact, waterproof GPS tracker meeting all the criteria I outlined. It's $75 for the tracker and the silicon case, shipped directly from the OEM.

I’m thrilled with how this setup turned out:

Here’s how you can set up Traccar for your own self-hosted GPS tracking solution.

Setting Up Traccar

For this project, I decided to host Traccar on a public cloud server. This ensures that the tracking system remains accessible even during home network or power outages. Alternatively, there is a hosted offering for ~$10/month, and it's a good way to support the open-source project.

I decided to deploy it in a docker container, but bare-metal installation is also supported.

To deploy Traccar, you'll need:

  • a read-only mount for the config
  • a writable mount for logs
  • open ports for the tracker to connect to and for the web UI
  • a database (MariaDB in my case)
  • optional, but recommended: a reverse proxy (Caddy in my case) to put in front of the UI for security and convenient SSL termination with Let's Encrypt

My docker compose looks like this:

version: "3.3"
services:
  traccar:
    container_name: traccar
    hostname: traccar
    restart: unless-stopped
    ports:
      - 8082:8082
      - 5030:5030
      - 5055:5055
      - 5030:5030/udp
      - 5055:5055/udp
    volumes:
      - /opt/traccar/logs:/opt/traccar/logs:rw
      - /opt/traccar/conf/traccar.xml:/opt/traccar/conf/traccar.xml:ro
    image: traccar/traccar:latest
    networks:
      - caddy
      - default
    depends_on:
      mariadb:
        condition: service_healthy
  mariadb:
    image: mariadb:latest
    container_name: mariadb
    restart: unless-stopped
    environment:
      MYSQL_ROOT_PASSWORD: ${MARIADB_ROOT_PASSWORD}
      MYSQL_DATABASE: ${MARIADB_DATABASE}
      MYSQL_USER: ${MARIADB_USER}
      MYSQL_PASSWORD: ${MARIADB_PASSWORD}
    volumes:
      - data:/var/lib/mysql
    ports:
      - 3306:3306
    networks:
      - default
    healthcheck:
      test:
        - CMD
        - healthcheck.sh
        - --connect
        - --innodb_initialized
      interval: 10s
      timeout: 5s
      retries: 5
      start_period: 30s
networks:
  default: null
  caddy:
    external: true
volumes:
  data: null

docker-compose.yaml

With the following env file so that I don't store my secrets in the compose yaml.

MARIADB_ROOT_PASSWORD=<root_password_here>
MARIADB_DATABASE=traccar_db
MARIADB_USER=traccar_db_user
MARIADB_PASSWORD=<user_password_here>

.env

And this is my Traccar config.xml, pointing to my MariaDB and with a few other options configure. The only thing required to start is the database. You'll need to ensure the database exists, and Traccar will create the tables for you and populate them.

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE properties SYSTEM 'http://java.sun.com/dtd/properties.dtd'>
<properties>
    <entry key='database.driver'>com.mysql.cj.jdbc.Driver</entry>
    <entry key='database.url'>jdbc:mysql://mariadb:3306/traccar_db?zeroDateTimeBehavior=round&amp;serverTimezone=UTC&amp;allowPublicKeyRetrieval=true&amp;useSSL=false&amp;allowMultiQueries=true&amp;autoReconnect=true&amp;useUnicode=yes&amp;characterEncoding=UTF-8&amp;sessionVariables=sql_mode=''</entry>
    <entry key='database.user'>traccar_db_user</entry>
    <entry key='database.password'>myPassword</entry>

    <entry key='geocoder.enable'>true</entry>
    <entry key='geocoder.type'>nominatim</entry>
    <entry key='geocoder.url'>https://us1.locationiq.com/v1/reverse.php</entry>
    <entry key='geocoder.key'>myKeyHere</entry>
    <entry key='geocoder.onRequest'>false</entry>
    <entry key='geocoder.ignorePositions'>false</entry>
    <entry key='geocoder.reuseDistance'>25</entry>
</properties>

/opt/traccar/conf/traccar.xml

Integration with Home Assistant

Integrating Traccar with Home Assistant is very easy. Simply add a new integration and point it to the UI port (in my case, 8082)

Coming Up Next

With Traccar up and running, the next step is to configure the Mictrack MT710 device and link it to the server. In Part 2, I’ll guide you through setting up the tracker.