Created
May 1, 2023 13:46
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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'×tamp='$(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