-
Notifications
You must be signed in to change notification settings - Fork 0
/
liveloc.py
164 lines (137 loc) · 4.69 KB
/
liveloc.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
import os
import sys, getopt
from gps import *
from time import *
import time
import threading
import argparse
import mysql.connector as mariadb
from mysql.connector import errorcode
import mysql.connector
from ftplib import FTP
import os
import fileinput
gpsd = None #seting the global variable
sensor1 = 0
sensor2 = 0
sensorname = ['Lengtegraad', 'Breedtegraad']
sensor= [sensor1, sensor2]
os.system('clear') #clear the terminal (optional)
# database connection configuration
dbconfig = {
'user': 'sensem',
'password': 'h@',
'host': 'localhost',
'database': 'pibike',
'raise_on_warnings': True,
}
# parse arguments
verbose = True
interval = 10 # second
try:
opts, args = getopt.getopt(sys.argv[1:], "vt:")
except getopt.GetoptError as err:
print(str(err))
print('measure.py -v -t <interval>')
print('-v: be verbose')
print('-t <interval>: measure each <interval> seconds (default: 10s)')
sys.exit(2)
for opt, arg in opts:
if opt == '-v':
verbose = True
elif opt == '-t':
interval = int(arg)
try:
mariadb_connection = mariadb.connect(**dbconfig)
if verbose:
print("Database connected")
except mysql.connector.Error as err:
if err.errno == errorcode.ER_ACCESS_DENIED_ERROR:
print("Something is wrong with your user name or password")
elif err.errno == errorcode.ER_BAD_DB_ERROR:
print("Database does not exist")
else:
print("Error: {}".format(err))
sys.exit(2)
# create the database cursor for executing SQL queries
cursor = mariadb_connection.cursor()
class GpsPoller(threading.Thread):
def __init__(self):
threading.Thread.__init__(self)
global gpsd #bring it in scope
gpsd = gps(mode=WATCH_ENABLE) #starting the stream of info
self.current_value = None
self.running = True #setting the thread running to true
def run(self):
global gpsd
while gpsp.running:
gpsd.next() #this will continue to loop and grab EACH set of gpsd info to clear the buffer
if __name__ == '__main__':
gpsp = GpsPoller() # create the thread
try:
gpsp.start() # start it up
while True:
#It may take a second or two to get good data
#print gpsd.fix.latitude,', ',gpsd.fix.longitude,' Time: ',gpsd.utc
#######################
# Getting GPS readings#
#######################
sensor1 = gpsd.fix.latitude
sensor2 = gpsd.fix.longitude
os.system('clear')
print ()
print (' GPS reading')
print ('----------------------------------------')
print ('latitude ' , sensor1)
print ('longitude ' , sensor2)
print ('mode ' , gpsd.fix.mode)
print ()
ftp = FTP()
ftp.set_debuglevel(2)
ftp.connect('185.182.57.89', 22)
ftp.login('[email protected]','pibike123')
ftp.cwd('/public_html')
lat=open("latitude.txt","rb")
lat.write(str(sensor1))
ftp.storbinary('STOR %s' % os.path.basename(localfile), fp, 1024)
lat.close
lon=open("longitude.txt","rb")
lon.write(str(sensor2))
ftp.storbinary('STOR %s' % os.path.basename(localfile), fp, 1024)
lon.close
session.quit()
x = 0
while (x <= 1):
############################
# Determine the sensor_id's#
############################
sensor_name = sensorname[x]
try:
cursor.execute("SELECT id FROM sensor WHERE naam=%s", [sensor_name])
except mariadb.Error as err:
print("Error: {}".format(err))
sys.exit(2)
sensor_id = cursor.fetchone()
if sensor_id == None:
print("Error: no sensor found with naam = %s" % sensor_name)
sys.exit(2)
if verbose:
print("Reading data from sensor %s with id %s" % (sensor_name, sensor_id[0]))
# store measurement in database
try:
cursor.execute('INSERT INTO meting (waarde, sensor_id) VALUES (%s, %s);', (sensor[x], sensor_id[0]))
except mysql.connector.Error as err:
print("Error: {}".format(err))
else:
# commit measurements
mariadb_connection.commit();
if verbose:
print(str(sensor_name) + " committed");
x += 1
time.sleep(1) #set to whatever
except (KeyboardInterrupt, SystemExit): #when you press ctrl+c
print ("\nKilling Thread...")
mariadb_connection.close()
gpsp.running = False
gpsp.join() # wait for the thread to finish what it's doing
print ("Done.\nExiting.")