GPS_read 1.59 KB
import serial
import RPi.GPIO as GPIO
import time
import os

out_a = 6
out_b = 13
in_a = 19
in_b = 26

GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)

GPIO.setup(out_a, GPIO.OUT, initial = 0)
GPIO.setup(out_b, GPIO.OUT, initial = 0)

GPIO.setup(in_a, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(in_b, GPIO.IN, pull_up_down=GPIO.PUD_UP)

ser = serial.Serial("/dev/ttyS0")

while(1):
	d_in=ser.readline().decode('ascii')
	d_in = d_in.split(',')
	
	if d_in[0]==("$GPRMC"):#GPS
		if d_in[2]=='A':
			now = d_in[9]
			tid = d_in[1]
			time = tid[:2]
			min = tid[2:4]
			ar = now[4:]
			mane=now[2:4]
			dag = now[:2]
			timezone = ("CET")
			print('{}.{}.{}-{}:{} {}'.format(ar,mane,dag,time,min,timezone))
			oclock = ('{}.{}.{}-{}:{} {}'.format(ar,mane,dag,time,min,timezone))

			latitude=d_in[3]
			ltdg =latitude[:2]
			ldg=float(ltdg)
			lmdg=(latitude[2:])
			limdg=float(lmdg)
			lfddg=ldg+(limdg/60)
			print('{},{}'.format(lfddg, d_in[4]))
			lat = ('{}'.format(lfddg))

			longtitude=d_in[5]
			lotdg =longtitude[:3]
			lodg=float(lotdg)
			lomdg=longtitude[3:]
			loimdg=float(lomdg)
			lofddg=lodg+(loimdg/60)
			print('{},{}'.format(lofddg, d_in[6]))
			lon = ('{}'.format(lofddg))

			GPIO.output(out_b, 0)
			GPIO.output(out_a, 0)
			if GPIO.input(in_a)==0:
				f = open ('/var/www/html/data.txt','a')
				locInfo = oclock + '\n' + lat + '\t' + lon + '\t' + 'star3' +'\t' + '\n'
				f.write(locInfo)
				f.close()
				print("Location saved!")
				GPIO.output(out_b, 1)
			elif GPIO.input(in_b)==0:
				os.system("shutdown -h now")
				GPIO.output(out_a, 1)
		else:
			print("ERROR: Data is invalid")

ser.close()