Skip to content

Instantly share code, notes, and snippets.

@mbolli
Last active April 11, 2023 06:56
Show Gist options
  • Save mbolli/fba44156cf668940e325f98cb62483f7 to your computer and use it in GitHub Desktop.
Save mbolli/fba44156cf668940e325f98cb62483f7 to your computer and use it in GitHub Desktop.
GPS/NMEA collector setup (gpsd/prometheus/grafana)

GPS/NMEA collector setup

Grafana screenshot

Hardware

  • GPS Receiver: Teltonika RUTX11 Router with enabled NMEA Forwarding (set to the IP of the Raspberry and TCP port 2948)
  • GPS Antenna: TE Connectivity 2x2 MIMO 2G/3G/4G WLAN GPS/GNSS Antenna
  • Collector: Raspberry Pi 4 2GB with latest Raspberry Pi OS

Software

  • gpsd, for interpreting the NMEA messages (apt install gpsd)
    • ncat, a much better/resilient netcat implementation (apt install ncat) - I had issues with the original nc and had to restart it frequently.
    • Also see the systemd service file nmea_forwarding.service, the gpsd systemd service is not used.
  • Prometheus, time-series database for storing the GPS coordinates, speed and sat count (systemd service file prometheus.service)
    • gpsd exporter for storing the gpsd data (pip install gpsd-prometheus-exporter, systemd service file gpsd_exporter.service)
  • Grafana, for the neat display of the Prometheus data (installation instructions)
    • TrackMap panel for the map
    • Dashboard config file: see grafana-gps-dashboard.json

How does it work

  • The Raspberry receives NMEA messages on TCP port 2948
  • So we listen via ncat on said port and pass it directly to gpsd stdin
  • gpsd parses the messages (use gpsmon to see if it works)
  • gpsd-exporter reads from gpsd and writes to the Prometheus time-series database
  • We use Grafana to show the collected data in a nice way

That’s it. Seems simple, now that it works…

To do

  • Grafana: Show the distance driven for the current time frame. I know how to calculate the distance from coordinates, but not how to do it in Grafana and how to show the result in the best way possible. Any help appreciated.
  • Grafana: Apparently the TrackMap panel "places a dot on the map at the current time as you mouse over other panels" but that doesn’t work for me
# File: /etc/systemd/system/gspd_exporter.service
[Unit]
Description=gpsd Exporter
Wants=network-online.target
After=nmea_forwarding
[Service]
User=pi
Group=pi
Type=simple
Restart=always
ExecStart=/home/pi/.local/bin/gpsd-exporter -p 9110 -s kph
[Install]
WantedBy=multi-user.target
{
"__inputs": [],
"__requires": [
{
"type": "grafana",
"id": "grafana",
"name": "Grafana",
"version": "8.2.5"
},
{
"type": "panel",
"id": "pr0ps-trackmap-panel",
"name": "TrackMap",
"version": "2.1.2"
},
{
"type": "panel",
"id": "timeseries",
"name": "Time series",
"version": ""
}
],
"annotations": {
"list": [
{
"builtIn": 1,
"datasource": "-- Grafana --",
"enable": true,
"hide": true,
"iconColor": "rgba(0, 211, 255, 1)",
"name": "Annotations & Alerts",
"target": {
"limit": 100,
"matchAny": false,
"tags": [],
"type": "dashboard"
},
"type": "dashboard"
}
]
},
"editable": true,
"fiscalYearStartMonth": 0,
"gnetId": null,
"graphTooltip": 0,
"id": null,
"links": [],
"liveNow": false,
"panels": [
{
"autoZoom": true,
"datasource": null,
"defaultLayer": "OpenStreetMap",
"gridPos": {
"h": 9,
"w": 12,
"x": 0,
"y": 0
},
"id": 4,
"lineColor": "red",
"maxDataPoints": 5000,
"pointColor": "royalblue",
"scrollWheelZoom": false,
"showLayerChanger": true,
"targets": [
{
"exemplar": true,
"expr": "latitude{}",
"interval": "",
"legendFormat": "",
"refId": "A"
},
{
"exemplar": true,
"expr": "longitude{}",
"hide": false,
"interval": "",
"legendFormat": "",
"refId": "B"
}
],
"title": "Position",
"type": "pr0ps-trackmap-panel"
},
{
"datasource": null,
"fieldConfig": {
"defaults": {
"color": {
"mode": "palette-classic"
},
"custom": {
"axisLabel": "[müM]",
"axisPlacement": "auto",
"barAlignment": 0,
"drawStyle": "line",
"fillOpacity": 30,
"gradientMode": "hue",
"hideFrom": {
"legend": false,
"tooltip": false,
"viz": false
},
"lineInterpolation": "linear",
"lineStyle": {
"fill": "solid"
},
"lineWidth": 2,
"pointSize": 5,
"scaleDistribution": {
"type": "linear"
},
"showPoints": "auto",
"spanNulls": false,
"stacking": {
"group": "A",
"mode": "none"
},
"thresholdsStyle": {
"mode": "off"
}
},
"decimals": 0,
"mappings": [],
"thresholds": {
"mode": "absolute",
"steps": [
{
"color": "green",
"value": null
}
]
}
},
"overrides": []
},
"gridPos": {
"h": 9,
"w": 12,
"x": 12,
"y": 0
},
"id": 2,
"options": {
"legend": {
"calcs": [],
"displayMode": "list",
"placement": "bottom"
},
"tooltip": {
"mode": "single"
}
},
"targets": [
{
"exemplar": true,
"expr": "altitude{}",
"instant": false,
"interval": "",
"legendFormat": "",
"refId": "A"
}
],
"title": "Altitude",
"type": "timeseries"
},
{
"datasource": null,
"fieldConfig": {
"defaults": {
"color": {
"mode": "palette-classic"
},
"custom": {
"axisLabel": "km/h",
"axisPlacement": "auto",
"barAlignment": 0,
"drawStyle": "line",
"fillOpacity": 0,
"gradientMode": "none",
"hideFrom": {
"legend": false,
"tooltip": false,
"viz": false
},
"lineInterpolation": "linear",
"lineWidth": 2,
"pointSize": 5,
"scaleDistribution": {
"type": "linear"
},
"showPoints": "auto",
"spanNulls": false,
"stacking": {
"group": "A",
"mode": "none"
},
"thresholdsStyle": {
"mode": "off"
}
},
"mappings": [],
"thresholds": {
"mode": "absolute",
"steps": [
{
"color": "green",
"value": null
},
{
"color": "red",
"value": 80
}
]
}
},
"overrides": []
},
"gridPos": {
"h": 8,
"w": 12,
"x": 0,
"y": 9
},
"id": 8,
"options": {
"legend": {
"calcs": [],
"displayMode": "list",
"placement": "bottom"
},
"tooltip": {
"mode": "single"
}
},
"targets": [
{
"exemplar": true,
"expr": "speed{}",
"interval": "",
"legendFormat": "",
"refId": "A"
}
],
"title": "Speed",
"type": "timeseries"
},
{
"datasource": null,
"fieldConfig": {
"defaults": {
"color": {
"mode": "palette-classic"
},
"custom": {
"axisLabel": "",
"axisPlacement": "auto",
"barAlignment": 0,
"drawStyle": "line",
"fillOpacity": 0,
"gradientMode": "none",
"hideFrom": {
"legend": false,
"tooltip": false,
"viz": false
},
"lineInterpolation": "linear",
"lineWidth": 1,
"pointSize": 5,
"scaleDistribution": {
"type": "linear"
},
"showPoints": "auto",
"spanNulls": false,
"stacking": {
"group": "A",
"mode": "none"
},
"thresholdsStyle": {
"mode": "off"
}
},
"mappings": [],
"thresholds": {
"mode": "absolute",
"steps": [
{
"color": "green",
"value": null
},
{
"color": "red",
"value": 80
}
]
}
},
"overrides": []
},
"gridPos": {
"h": 8,
"w": 12,
"x": 12,
"y": 9
},
"id": 6,
"options": {
"legend": {
"calcs": [],
"displayMode": "list",
"placement": "bottom"
},
"tooltip": {
"mode": "single"
}
},
"targets": [
{
"exemplar": true,
"expr": "satcount",
"interval": "",
"legendFormat": "",
"refId": "A"
}
],
"title": "Sat Count",
"type": "timeseries"
},
],
"refresh": false,
"schemaVersion": 32,
"style": "dark",
"tags": [],
"templating": {
"list": []
},
"time": {
"from": "2021-11-23T23:00:00.000Z",
"to": "2021-12-03T22:59:59.000Z"
},
"timepicker": {},
"timezone": "",
"title": "GPS",
"uid": "iaIUZfkgk",
"version": 6
}
# File: /etc/systemd/system/nmea_forwarding.service
[Unit]
Description=NMEA forwarder
Wants=network-online.target
After=network-online.target
[Service]
User=pi
Group=pi
Type=simple
ExecStart=/home/pi/start_gps_daemon
[Install]
WantedBy=multi-user.target
# File: /etc/systemd/system/prometheus.service
[Unit]
Description=Prometheus
Wants=network-online.target
After=network-online.target
[Service]
User=prometheus
Group=prometheus
Type=simple
Restart=always
RestartSec=5
ExecStart=/opt/prometheus-2.31.1.linux-armv7/prometheus \
--config.file /etc/prometheus/prometheus.yml \
--storage.tsdb.path /opt/prometheus-data \
--storage.tsdb.retention.time=2y
[Install]
WantedBy=multi-user.target
# File: /etc/prometheus/prometheus.yml
global:
scrape_interval: 15s # Set the scrape interval to every 15 seconds. Default is every 1 minute.
evaluation_interval: 15s # Evaluate rules every 15 seconds. The default is every 1 minute.
# scrape_timeout is set to the global default (10s).
# Alertmanager configuration
alerting:
alertmanagers:
- static_configs:
- targets:
# - alertmanager:9093
# Load rules once and periodically evaluate them according to the global 'evaluation_interval'.
rule_files:
scrape_configs:
# The job name is added as a label `job=<job_name>` to any timeseries scraped from this config.
- job_name: 'prometheus'
static_configs:
- targets: ['localhost:9090']
- job_name: 'gps'
scrape_interval: 30s
static_configs:
- targets: ['localhost:9110']
labels:
group: 'gps'
location: 'Björn'
#!/bin/bash
# file: /home/pi/start_gps_daemon
# listens on port 2948 for NMEA messages
# which are immediately forwarded to gpsd
ncat -k -l 2948 | gpsd -n -N /dev/stdin
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment