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