2024-07-22 -

This commit is contained in:
Adolfo Delorenzo 2024-07-22 21:02:56 -06:00
parent 51ff47cdee
commit fddcb5ad31
5 changed files with 2056 additions and 2 deletions

View File

@ -110,6 +110,7 @@ if not os.path.exists(csv_filename):
try: try:
get_gps_position() get_gps_position()
time.sleep(10)
except Exception as e: except Exception as e:
print(f"Error: {e}") print(f"Error: {e}")
finally: finally:

1
gps_data.csv Normal file
View File

@ -0,0 +1 @@
Timestamp,Latitude,Longitude
1 Timestamp Latitude Longitude

File diff suppressed because it is too large Load Diff

52
gps_new.py Normal file
View File

@ -0,0 +1,52 @@
import serial
import csv
import time
from datetime import datetime
# Configure the serial port
ser = serial.Serial('/dev/ttyS0', baudrate=9600, timeout=1)
# Open the CSV file for writing
csv_filename = 'gps_data.csv'
csv_file = open(csv_filename, 'w', newline='')
csv_writer = csv.writer(csv_file)
# Write the header row
csv_writer.writerow(['Timestamp', 'Latitude', 'Longitude'])
try:
while True:
# Read a line from the GPS device
line = ser.readline().decode('ascii', errors='replace').strip()
# Check if it's a GPRMC sentence (which contains coordinate data)
if line.startswith('$GPRMC'):
parts = line.split(',')
if len(parts) >= 7 and parts[2] == 'A': # 'A' means data is valid
# Extract latitude and longitude
lat = float(parts[3][:2]) + float(parts[3][2:]) / 60
if parts[4] == 'S':
lat = -lat
lon = float(parts[5][:3]) + float(parts[5][3:]) / 60
if parts[6] == 'W':
lon = -lon
# Get current timestamp
timestamp = datetime.now().isoformat()
# Write to CSV
csv_writer.writerow([timestamp, lat, lon])
csv_file.flush() # Ensure data is written to file
print(f"Recorded: {timestamp}, Lat: {lat}, Lon: {lon}")
time.sleep(1) # Wait for 1 second before reading next line
except KeyboardInterrupt:
print("Script terminated by user")
finally:
# Close the serial port and CSV file
ser.close()
csv_file.close()
print(f"Data saved to {csv_filename}")

View File

@ -12,9 +12,15 @@ myhost = os.uname()[1]
client = mqtt.Client(myhost) client = mqtt.Client(myhost)
client.connect(mqttBroker, 1883) client.connect(mqttBroker, 1883)
preferred_device = '/dev/ttyUSB5'
fallback_device = '/dev/ttyUSB0'
device_path = preferred_device if os.path.exists(preferred_device) else fallback_device
# Modbus configuration # Modbus configuration
mb_address = 1 # Modbus address mb_address = 1 # Modbus address
sensy_boi = minimalmodbus.Instrument('/dev/ttyUSB0', mb_address) sensy_boi = minimalmodbus.Instrument(device_path, mb_address)
sensy_boi.serial.baudrate = 9600 sensy_boi.serial.baudrate = 9600
sensy_boi.serial.bytesize = 8 sensy_boi.serial.bytesize = 8
sensy_boi.serial.parity = minimalmodbus.serial.PARITY_NONE sensy_boi.serial.parity = minimalmodbus.serial.PARITY_NONE
@ -78,7 +84,7 @@ try:
GPIO.output(LuzPuerta, GPIO.LOW) GPIO.output(LuzPuerta, GPIO.LOW)
GPIO.output(LuzEncendido, GPIO.HIGH) GPIO.output(LuzEncendido, GPIO.HIGH)
time.sleep(5) time.sleep(15)
except Exception as e: except Exception as e:
print(f"Error: {str(e)}") print(f"Error: {str(e)}")