目錄表

自建 OwnTrack 追蹤個人 GPS 軌跡服務

架構圖

graph TB %% External Components Mobile[📱 OwnTracks Mobile App
MQTT + WebSocket] Browser[🌐 Web Browser] External[🌍 External MQTT Clients] WebApp[📱 Mobile Web App
WebSocket Only] %% Docker Services subgraph "Docker Environment" subgraph "Mosquitto MQTT Broker" MQTT[Eclipse Mosquitto
Port: 1883, 9001] end subgraph "OwnTracks Recorder" Recorder[OwnTracks Recorder
Port: 8083
HTTP API] end subgraph "Frontend Web Interface" Frontend[OwnTracks Frontend
Port: 80
Nginx Web Server] end end %% Storage subgraph "Data Storage" MosquittocConf[(Mosquitto Config
./mosquitto/config)] MosquittoData[(Mosquitto Data
./mosquitto/data)] MosquittoLog[(Mosquitto Logs
./mosquitto/log)] RecorderStore[(Location Data
./store)] AuthFile[(Authentication
./auth/users.auth)] end %% Data Flow - Location Publishing Mobile -.->|MQTT Publish
Location Data
Port 1883| MQTT Mobile -.->|WebSocket Publish
Location Data
Port 9001| MQTT External -.->|MQTT Publish
Location Data
Port 1883| MQTT WebApp -.->|WebSocket Publish
Location Data
Port 9001| MQTT %% Internal MQTT Subscription MQTT -->|MQTT Subscribe
Location Topics| Recorder %% Data Storage Flow Recorder --> RecorderStore MQTT --> MosquittoData MQTT --> MosquittoLog %% Web Interface Flow Browser -->|HTTP Request
Port 80| Frontend Frontend -->|HTTP API Calls
Port 8083| Recorder Recorder -->|JSON Response
Location Data| Frontend Frontend -->|HTML/JS/CSS| Browser %% Configuration and Authentication MQTT -.-> MosquittocConf Recorder -.-> AuthFile %% WebSocket for Real-time (optional) MQTT -.->|WebSocket
Port 9001| Browser %% Styling classDef service fill:#e1f5fe,stroke:#01579b,stroke-width:2px classDef storage fill:#f3e5f5,stroke:#4a148c,stroke-width:2px classDef external fill:#e8f5e8,stroke:#1b5e20,stroke-width:2px class MQTT,Recorder,Frontend service class MosquittoData,MosquittoLog,RecorderStore,AuthFile,ConfigFile storage class Mobile,Browser,External external

安裝服務

Server 端

  1. 到 .env 內設定 recoder 存取 MQTT 的帳號密碼

    vi .env

  2. 產生 MQTT 的設定檔 mosquitto/config/mosquitto.conf

    mkdir -p mosquitto/config/
    vi mosquitto/config/mosquitto.conf

    • mosquitto/config/mosquitto.conf
      listener 1883
      protocol mqtt
       
      listener 9001
      protocol websockets
       
      allow_anonymous true
      #allow_anonymous false
      #password_file /mosquitto/config/passwd
       
      log_dest file /mosquitto/log/mosquitto.log
      log_type error
      log_type warning
      log_type notice
      log_type information
      #log_type debug
      #log_type subscribe
      #log_type unsubscribe
       
      persistence true
      persistence_location /mosquitto/data/
       
      max_inflight_messages 20
      max_queued_messages 1000
       
      allow_zero_length_clientid true
       
      connection_messages true
      log_timestamp true
       

      https://raw.githubusercontent.com/tryweb/docker-compose/refs/heads/main/owntracks/mosquitto/config/mosquitto.conf

  3. 產生可存取 MQTT 的帳號密碼檔

    touch mosquitto/config/passwd
    chmod 0770 mosquitto/config/passwd

  4. 設定直接存取 OTRecorder 的帳號密碼檔 auth/users.auth (不透過 MQTT 直接存取)

    mkdir -p auth
    vi auth/users.auth

  5. 第一次啟動服務

    docker compose pull
    docker compose up -d

  6. 查看相關紀錄
    • docker compose 紀錄

      docker compose logs -f

    • MQTT 紀錄

      tail -f mosquitto/log/mosquitto.log

  7. 建立 Recoder 存取 MQTT 的帳號密碼(定義在 .env 內) Exp. recorder / recorderpass

    docker compose exec mosquitto chown root:root /mosquitto/config/passwd
    docker compose exec mosquitto mosquitto_passwd /mosquitto/config/passwd recorder

    輸入兩次密碼 recorderpass 即可

  8. 建立 jonathan 存取 MQTT 的帳號密碼 Exp. jonathan / mypassword

    docker compose exec mosquitto mosquitto_passwd /mosquitto/config/passwd jonathan

    輸入兩次密碼 mypassword 即可

手機端

直接連 OTRecoder

連 MQTT (才可看到 Friends)

管理端可以看到畫面

設定 Reverse Proxy 方式

透過 Cloudflare Tunnel 設定 OTRecorder 對外服務

目前 MQTT + WebSocket 服務還無法成功透過 Cloudflare Tunnel 設定方式提供對外服務

透過 Nginx Proxy Manager 設定 MQTT + WebSocket 對外服務

常見問題

1. 合併兩個 rec 作法

2. 刪除特定 user 或 device 作法

  1. 針對這檔案型的作法就是直接刪除檔案 Exp. 刪除 jonathan/jonathan

    cd
    rm -rf store/rec/jonathan/jonathan/
    rm -rf store/last/jonathan/jonathan/
    rm -f store/ghash/data.mdb

  2. 重起 otrecorder 服務

    docker compose restart otrecorder

  3. 刪除 MQTT 的資料後, 重起服務

    rm mosquitto/data/mosquitto.db
    docker compose restart mosquitto

  4. 在 Android 需要進入 Friends→ 點選要刪除的項目 Exp. jt → 將畫面底下訊息上拉 → 點選 CLEAR

參考網址