Parking Finder
The Parking Finder system is a smart parking solution designed to help drivers efficiently locate available
Loading...
Searching...
No Matches
gps-sensor-data.py
Go to the documentation of this file.
1
23
24# pip install pyserial
25
26import serial
27import time
28import pynmea2
29
30
35ser = serial.Serial(
36 port='/dev/ttyTHS1', # Adjust this for your specific setup
37 baudrate=9600,
38 parity=serial.PARITY_NONE,
39 stopbits=serial.STOPBITS_ONE,
40 bytesize=serial.EIGHTBITS,
41 timeout=1 # Timeout in seconds
42)
43
44print("Serial port opened. Waiting for data...")
45
46
47try:
48 while True:
49 if ser.in_waiting > 0:
50 # Read data from the serial port
51 # ser.readline() reads until a newline character is encountered
52 # ser.read(num_bytes) reads a specified number of bytes
53 data = ser.readline().decode().strip() # Decode and remove whitespace
54 #'utf-8'
55 if data:
56 if data.find('GGA') > 0:
57 try:
58 msg = pynmea2.parse(data)
59 print(msg.timestamp,'Lat:',round(msg.latitude,6),'Lon:',round(msg.longitude,6),'Alt:',msg.altitude,'Sats:',msg.num_sats)
60 #print(f"Received: {data}")
61 except Exception as e:
62 print(e)
63
64 else:
65 print("No data recieved")
66 time.sleep(0.1) # Small delay to prevent busy-waiting
67
68except Exception as e:
69 print(f"An error occurred: {e}")
70finally:
71 ser.close()
72 print("Serial port closed.")