-
Notifications
You must be signed in to change notification settings - Fork 0
/
database.py
294 lines (256 loc) · 8.72 KB
/
database.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
import urllib.request, json
from sense_hat import SenseHat, ACTION_PRESSED, ACTION_HELD, ACTION_RELEASED
from time import sleep
import sys, getopt
import argparse
import time
import mysql.connector as mariadb
from mysql.connector import errorcode
sensor_name = "Temperatuur"
sensor_name1 = "Image"
# 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)
#verbinden met de database
try:
mariadb_connection = mariadb.connect(**dbconfig)
if verbose:
print("Database connected")
except mariadb.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()
# turn on autocommit
#cursor.autocommit = True
# Define some colours
Y = (255, 247, 0) # Yellow
B = (0, 0, 255) # Blue
G = (105, 105, 105) # Grey
W = (255, 255, 255) # White
E = (0, 0, 0) # Empty
sense = SenseHat()
delay_img = 2
delay_press = 1
# Demo url to test, so the key doesn't expire
demo_url = "http://weerlive.nl/api/json-data-10min.php?key=demo&locatie=Amsterdam"
basis_url = "http://weerlive.nl/api/json-data-10min.php?key="
# Key invullen
key = "27a31f3737"
rest_url = "&locatie="
lat = 52
long = 5
# Change (demo_url) to (basis_url + key + rest_url + str(lat) + "," + str(long)) for normal code
response = urllib.request.urlopen(basis_url + key + rest_url + str(lat) + "," + str(long))
data = json.loads(response.read().decode("utf-8"))
x = data['liveweer']
y = x[0]
weertype = y['image']
temp = y['temp']
# A def so you can call it when you press a button
def press(event):
if event.action != ACTION_RELEASED:
if weertype == "bewolkt" or weertype == "zwaarbewolkt" or weertype == "wolkennacht":
# Set up where the image displays
bewolkt = [
G, G, E, E, E, E, E, G,
G, G, G, E, E, E, G, G,
E, E, E, E, E, E, E, E,
E, E, G, G, E, G, E, E,
E, G, G, G, G, G, G, E,
G, G, G, G, G, G, G, G,
G, G, G, G, G, G, G, G,
E, E, E, E, E, E, E, E
]
# Display these colours on the LED matrix
sense.set_pixels(bewolkt)
elif weertype == "halfbewolkt":
# Set up where the image displays
halfbewolkt = [
G, G, E, E, E, E, E, G,
G, G, G, E, E, E, G, G,
E, E, E, E, E, E, E, E,
E, E, G, G, E, Y, E, Y,
E, G, G, G, G, G, G, E,
G, G, G, G, Y, G, Y, Y,
G, G, G, G, G, G, Y, Y,
E, E, E, E, Y, E, Y, Y
]
# Display these colours on the LED matrix
sense.set_pixels(halfbewolkt)
elif weertype == "zonnig":
# Set up where the image displays
zonnig = [
Y, E, Y, E, Y, E, Y, E,
E, E, E, E, E, E, E, Y,
Y, E, E, Y, Y, E, E, E,
E, E, Y, Y, Y, Y, E, Y,
Y, E, Y, Y, Y, Y, E, E,
E, E, E, Y, Y, E, E, Y,
Y, E, E, E, E, E, E, E,
E, Y, E, Y, E, Y, E, Y
]
# Display these colours on the LED matrix
sense.set_pixels(zonnig)
elif weertype == "bliksem":
# Set up where the image displays
bliksem = [
G, G, E, E, E, E, E, G,
B, G, G, E, E, E, G, B,
B, E, E, E, E, E, E, B,
E, E, G, G, E, G, E, E,
E, G, G, G, G, G, G, E,
G, G, Y, G, G, G, Y, G,
E, Y, Y, G, G, Y, Y, G,
E, Y, E, E, E, Y, E, E
]
# Display these colours on the LED matrix
sense.set_pixels(bliksem)
elif weertype == "regen" or weertype == "buien":
# Set up where the image displays
regen = [
G, G, E, E, E, E, E, G,
B, G, G, E, E, E, G, B,
B, E, E, E, E, E, E, B,
E, E, G, G, E, G, E, E,
E, G, G, G, G, G, G, E,
G, G, G, G, G, G, G, G,
B, G, B, G, B, G, B, G,
B, E, B, E, B, E, B, E
]
# Display these colours on the LED matrix
sense.set_pixels(regen)
elif weertype == "hagel" or weertype == "sneeuw":
# Set up where the image displays
sneeuw = [
E, E, G, G, E, G, E, E,
E, G, G, G, G, G, G, E,
G, G, G, G, G, G, G, G,
E, W, E, W, E, W, E, W,
W, E, W, E, W, E, W, E,
E, W, E, W, E, W, E, W,
W, E, W, E, W, E, W, E,
E, W, E, W, E, W, E, W
]
# Display these colours on the LED matrix
sense.set_pixels(sneeuw)
elif weertype == "mist" or weertype == "nachtmist":
# Set up where the image displays
mist = [
E, E, E, E, E, E, E, E,
E, E, E, E, E, E, E, E,
E, E, E, E, E, E, E, E,
E, E, E, E, E, E, E, E,
E, E, E, E, E, E, E, E,
E, G, G, E, G, G, G, E,
G, G, G, G, G, G, G, G,
G, G, G, G, G, G, G, G
]
# Display these colours on the LED matrix
sense.set_pixels(mist)
elif weertype == "helderenacht":
# Set up where the image displays
nacht = [
E, E, E, E, E, E, E, E,
E, E, E, G, G, E, E, E,
E, E, G, G, G, G, E, E,
E, G, G, G, G, G, G, E,
E, G, G, G, G, G, G, E,
E, E, G, G, G, G, E, E,
E, E, E, G, G, E, E, E,
E, E, E, E, E, E, E, E,
]
# Display these colours on the LED matrix
sense.set_pixels(nacht)
else:
# Yeah Earth is gone...
print("There is no weather, Earth is gone")
sleep(delay_img)
sense.clear()
sense.show_message(temp + "C", text_colour=[255, 255, 0])
sleep(delay_press)
# If middle button is pressed then it will show weather and tempature
sense.stick.direction_middle = press
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]))
#Temperatuur
t = temp
# verbose
if verbose:
print("Temperature: %s C" % t)
# store measurement in database
try:
cursor.execute('INSERT INTO meting (waarde, sensor_id) VALUES (%s, %s);', (t, sensor_id[0]))
except mariadb.connector.Error as err:
print("Error: {}".format(err))
else:
# commit measurements
mariadb_connection.commit();
if verbose:
print("Temperature committed");
# determine the sensor_id for image
try:
cursor.execute("SELECT id FROM sensor WHERE naam=%s", [sensor_name1])
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_name1)
sys.exit(2)
if verbose:
print("Reading data from sensor %s with id %s" % (sensor_name1, sensor_id[0]))
#Temperatuur
image = weertype
# verbose
if verbose:
print("Image: %s" % image)
# store measurement in database
try:
cursor.execute('INSERT INTO meting (waarde, sensor_id) VALUES (%s, %s);', (image, sensor_id[0]))
except mariadb.connector.Error as err:
print("Error: {}".format(err))
else:
# commit measurements
mariadb_connection.commit();
if verbose:
print("Image committed");
time.sleep(interval)
# close db connection
mariadb_connection.close()
# done