Skip to content

Instantly share code, notes, and snippets.

@jkoop
Created May 1, 2023 13:46
Show Gist options
  • Save jkoop/db64c0bce8d84b998af0a08642b592df to your computer and use it in GitHub Desktop.
Save jkoop/db64c0bce8d84b998af0a08642b592df to your computer and use it in GitHub Desktop.
Get location via WiFi (with Google API) and report it to your Traccar server
#!/bin/bash
# Run this as root
GOOGLE_API_KEY=AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
TRACCAR_URL='https://traccar.example.com'
INTERFACE=wlan0
DEVICE_ID=123456
json=$(sudo iwlist $INTERFACE scan | egrep -o 'Address: .*|Channel:.*|Signal level=.*|Last beacon:.*' | sed -E 's/Address: (.*)/\{"macAddress":"\1",/g' | sed -E 's/Channel:(.*)/"channel":\1,/g' | sed -E 's/Signal level=(.*) dBm/"signalStrength":\1,/g' | sed -E 's/Last beacon: (.*)ms ago/"age":\1}/g')
json=$(echo "$json" | head -n 1; echo "$json" | sed -E 's/\{/,{/g' | tail -n +2)
json=$(echo '{"considerIp":false,"wifiAccessPoints":['"$json"']}' | curl -d @/dev/stdin -H "Content-Type: application/json" "https://www.googleapis.com/geolocation/v1/geolocate?key=$GOOGLE_API_KEY")
lat=$(echo "$json" | grep lat | egrep -o '[.0-9-]+')
lng=$(echo "$json" | grep lng | egrep -o '[.0-9-]+')
acc=$(echo "$json" | grep accuracy | egrep -o '[.0-9-]+')
curl -i -XPOST $TRACCAR_URL'/?id='$DEVICE_ID'&lat='$lat'&lon='$lng'&timestamp='$(date +%s)'&hdop=0&altitude=0&speed=0&accuracy='$acc
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment