Raspberry Pi, Python, GPSD

AllanL2 years ago

So I'm new to all this Raspberry pie stuff...

There did not seem to be any tracker clients that worked with GPSD on currant 64bit OS.. my previous 2G china made trackers no longer work or battery exploded..

So I present my work in progress...

requires:
working GPSD
GPS3 package https://github.com/wadda/gps3/
other stuff...
skilful operator

Welcome all input, comment... be kind it's my first time...


#Code from AGPS3 example messed around with by Allan
from gps3.agps3threaded import AGPS3mechanism
import time
import requests

agps_thread = AGPS3mechanism()  # Instantiate AGPS3 Mechanisms
agps_thread.stream_data()  # From localhost (), or other hosts, by example, (host='gps.ddns.net')
agps_thread.run_thread()  # Throttle time to sleep after an empty lookup, default '()' 0.2 two tenths of a second

#what the Traccar server is expecting
#http://demo.traccar.org:5055/?id=123456&lat={0}&lon={1}&timestamp={2}&hdop={3}&altitude={4}&speed={5}

#the ID for the device you want to present to the traccar server
id = <Your Traccar ID>

#output if no GPS signal
notavail = "n/a"

#other variables that Traccar will possiblly accept
wifiap = "69:69:69:00:00 screwnicorn" #need to find out what value server is expecting here and provide it from OS
accuracy = 100 #to be done..
batt = 98 #battery charge value.. waiting on parts to arrive.. server accepts value 0-100
driverUniqueId = 'allan' #TBD
#setup update rate to change depending on speed
uberfast = 120
fastud = 60
slowud = 40
#traccar server
server = 'http://demo.traccar.org:5055'

while True:  # All data is available via instantiated thread data stream attribute.
    # line #140-ff of /usr/local/lib/python3.5/dist-packages/gps3/agps.py
    print('---------------------')
    print(                   agps_thread.data_stream.time)
    print('Lat:{}   '.format(agps_thread.data_stream.lat))
    print('Lon:{}   '.format(agps_thread.data_stream.lon))
    print('Speed:{} '.format(agps_thread.data_stream.speed))
    print('Alt:{}'.format(agps_thread.data_stream.alt))
    print('HDOP:{}'.format(agps_thread.data_stream.hdop))
    print('---------------------')
    print(id)
 #you can remove the above print lines if you don't want output    
#now check if GPS is active and if so post it to the server   
    if agps_thread.data_stream.time != notavail:
        requests.post((server), data = {
            'id': (id),
            'lat': (agps_thread.data_stream.lat),
            'lon': (agps_thread.data_stream.lon),
            'timestamp': (agps_thread.data_stream.time),
            'hdop': (agps_thread.data_stream.hdop),
            'altitude': (agps_thread.data_stream.alt),
            'speed': (agps_thread.data_stream.speed),
            'batt': (batt)
        })
    
    time.sleep(10) # Sleep, or do other things for as long as you like.