Update gps00.py

This commit is contained in:
Adolfo Delorenzo 2024-07-16 02:55:00 +00:00
parent 553be55ce0
commit 58caa74b23

View File

@ -18,11 +18,13 @@ 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])
def convert_to_decimal(coord_str):
# Split the coordinate into degrees, minutes, and direction
coord = coord_str[:-2]
direction = coord_str[-1]
degrees = float(coord[:2] if direction in ['N', 'S'] else coord[:3])
minutes = float(coord[2:] if direction in ['N', 'S'] else coord[3:])
# Convert to decimal degrees
decimal = degrees + (minutes / 60)
@ -35,11 +37,11 @@ def convert_to_decimal(coord, direction):
def parse_gps_data(gps_string):
parts = gps_string.split(',')
if len(parts) < 8:
if len(parts) != 4:
return "Invalid GPS data"
lat = convert_to_decimal(parts[0], parts[1])
lon = convert_to_decimal(parts[2], parts[3])
lat = convert_to_decimal(parts[0] + ',' + parts[1])
lon = convert_to_decimal(parts[2] + ',' + parts[3])
return f"{lat:.6f}, {lon:.6f}"
@ -56,12 +58,11 @@ def send_at(command,back,timeout):
print(command + ' back:\t' + rec_buff.decode())
return 0
else:
# gps_data = rec_buff.decode()[25:-36]
gps_data = rec_buff.decode()[13:-36]
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)
client.publish("iiot/"+ myhost +"/gps", converted_gps)
return 1
else:
print('GPS no está listo')