I am a bit new to python and i'm trying to accomplish a task.
What I am trying to accomplish
I have this code which gathers GPS data from a thread in a while loop and I post that data to my webserver.
Problem
The problem here is, on first run the latitude/longitude is always zero and correct values come after that. The while loop fetches the gps coordinates a random amount of times from 5-10 and they are all written in my database.
This is not correct behavior since I just want ONE instance of results being written to the database at any point in time. I tried putting an IF statement in the while loop to say "If latitude > 0 upload results to database and break else continue looping but I seem to be getting the syntax wrong.
As you can see the code that post to my database is inside the while loop so all
try:
gpsp.start() # start it up
print ' GPS'
while True:
print ' GPS reading'
print 'latitude ' , gpsd.fix.latitude
print 'longitude ' , gpsd.fix.longitude
if gpsd.fix.latitude > 0:
payload={'id':"P8",'lat':gpsd.fix.latitude,'lon':gpsd.fix.longitude}
r= requests.post("http://my-server.com/Pi/Loc.php",data=payload)
break
elif gpsd.fix.latutde=0:
except (KeyboardInterrupt, SystemExit): #when you press ctrl+c
print "\nKilling Thread..."
gpsp.running = False
gpsp.join() # wait for the thread to finish what it's doing
print "Done.\nExiting."
Any assistance would be appreciated.
My issue is not "Capturing" the coordinates, however I'm looking for assistance in correcting the syntax to log just one record, I am aware the code I have shown is a bit incorrect, this is just what I have for now.
This is the result from running the code (I Fixed the indent and removed the if). There was actually many more lines but i deleted them for simplicity. I'm just trying to find a way to select the last one to my webserver
GPS
GPS reading
latitude 0.0
longitude 0.0
GPS reading
latitude **.292548333
longitude **.379363333
GPS reading
latitude **.292548333
longitude **.379363333
GPS reading
latitude **.292546667
longitude **.379365
GPS reading
latitude **.292546667
longitude **.379365
GPS reading
latitude **.292546667
longitude **.379365
GPS reading
latitude **.292546667
longitude **.379365
GPS reading
latitude **.292503333
longitude **.379376667
GPS reading
latitude **.292498333
longitude **.379376667