From 3a2beffb41a04d098360fa5e806b48f09c331343 Mon Sep 17 00:00:00 2001 From: Adolfo Delorenzo Date: Tue, 16 Jul 2024 02:48:35 +0000 Subject: [PATCH] Update gps00.py --- gps00.py | 33 ++++++++++++++++++++++++++++++--- 1 file changed, 30 insertions(+), 3 deletions(-) diff --git a/gps00.py b/gps00.py index 02c212a..0e80101 100644 --- a/gps00.py +++ b/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')