I have a gps sensor that outputs latitude and longitude. Along with it I am calling the current time in timec(). I want to output these values into a text file that I can upload into excel for extrapolation. My outputs currently look like
14:48:48,"(36.12306, -97.06177333333332)"
And I need my outputs to look like
14:48:48, 36.12306, -97.06177333333332
I have tried what is below:
import time
import board
import busio
import adafruit_gps
import csv
import serial
import time
from datetime import datetime
def timec():
now = datetime.now().time()
current_time = now.strftime("%H:%M:%S")
return current_time
def GPS():
uart = serial.Serial("/dev/ttyUSB0", baudrate=9600, timeout=10)
gps = adafruit_gps.GPS(uart, debug=False) # Use UART/pyserial
gps.send_command(b"PMTK314,0,1,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0")
gps.send_command(b"PMTK220,1000")
last_print = time.monotonic()
while True:
gps.update()
current = time.monotonic()
if current - last_print >= 1.0:
last_print = current
if not gps.has_fix:
print("Waiting for fix...")
continue
lat = gps.latitude
lon = gps.longitude
return lat,lon
if __name__ == '__main__':
while True:
ctime = timec()
coord = GPS()
Rows = (ctime,coord)
with open('Text FILE69', 'a', newline='') as f:
write = csv.writer(f)
write.writerow(Rows)
time.sleep(1)
I know my issue lies with how I call lat and lon. in 'main' and the bbottom of GPS()
CodePudding user response:
Spread the result of GPS
in to separate variables.
You should just open the file once, not every time through the loop. You can use f.flush()
to flush the line to the file immediately.
if __name__ == '__main__':
with open('Text FILE69', 'a', newline='') as f:
write = csv.writer(f)
while True:
ctime = timec()
lat, lon = GPS()
Rows = (ctime, lat, lon)
write.writerow(Rows)
f.flush()
time.sleep(1)