Update gps00.py
This commit is contained in:
parent
5d709fbe8f
commit
3a2beffb41
33
gps00.py
33
gps00.py
@ -18,12 +18,37 @@ ser.flushInput()
|
||||
rec_buff = ''
|
||||
time_count = 0
|
||||
|
||||
def convert_to_decimal(coord, direction):
|
||||
# Split the coordinate into degrees and minutes
|
||||
parts = coord.split('.')
|
||||
degrees = float(parts[0][:-2])
|
||||
minutes = float(parts[0][-2:] + '.' + parts[1])
|
||||
|
||||
# Convert to decimal degrees
|
||||
decimal = degrees + (minutes / 60)
|
||||
|
||||
# Make negative if South or West
|
||||
if direction in ['S', 'W']:
|
||||
decimal = -decimal
|
||||
|
||||
return decimal
|
||||
|
||||
def parse_gps_data(gps_string):
|
||||
parts = gps_string.split(',')
|
||||
if len(parts) < 6:
|
||||
return "Invalid GPS data"
|
||||
|
||||
lat = convert_to_decimal(parts[0], parts[1])
|
||||
lon = convert_to_decimal(parts[2], parts[3])
|
||||
|
||||
return f"{lat:.6f}, {lon:.6f}"
|
||||
|
||||
def send_at(command,back,timeout):
|
||||
rec_buff = ''
|
||||
ser.write((command+'\r\n').encode())
|
||||
time.sleep(timeout)
|
||||
if ser.inWaiting():
|
||||
time.sleep(0.01 )
|
||||
time.sleep(0.01)
|
||||
rec_buff = ser.read(ser.inWaiting())
|
||||
if rec_buff != '':
|
||||
if back not in rec_buff.decode():
|
||||
@ -33,8 +58,10 @@ def send_at(command,back,timeout):
|
||||
else:
|
||||
# gps_data = rec_buff.decode()[25:-36]
|
||||
gps_data = rec_buff.decode()[13:-36]
|
||||
print(gps_data)
|
||||
client.publish("iiot/"+ myhost +"/gps", gps_data)
|
||||
print("Raw GPS data:", gps_data)
|
||||
converted_gps = parse_gps_data(gps_data)
|
||||
print("Converted GPS data for Google Maps:", converted_gps)
|
||||
# client.publish("iiot/"+ myhost +"/gps", gps_data)
|
||||
return 1
|
||||
else:
|
||||
print('GPS no está listo')
|
||||
|
Loading…
Reference in New Issue
Block a user