From 58caa74b23ca7403976bdc3e53d898fbc282a231 Mon Sep 17 00:00:00 2001 From: Adolfo Delorenzo Date: Tue, 16 Jul 2024 02:55:00 +0000 Subject: [PATCH] Update gps00.py --- gps00.py | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/gps00.py b/gps00.py index e3de7c4..7134a41 100644 --- a/gps00.py +++ b/gps00.py @@ -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')