-
Notifications
You must be signed in to change notification settings - Fork 12
/
Rss.py
556 lines (413 loc) · 20 KB
/
Rss.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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
#!/usr/bin/python
# -*- coding: iso-8859-1 -*-
#Importieren der Module
import os;
import json;
import glob;
import time;
import Adafruit_DHT;
import time;
import RPi.GPIO as gpio;
import rrdtool;
from sht_sensor import Sht
# Konstanten ############################################################################################################
########################################################################################################################
PATH = '/var/www/html/';
SETTINGS_FILE = PATH+'settings.json';
CURRENT_FILE = PATH+'current.json';
PICPATH = '/var/www/html/pic/';
# Sensor should be set to Adafruit_DHT.DHT11,
# Adafruit_DHT22, or Adafruit_AM2302.
sensor = Adafruit_DHT.AM2302
# Sainsmart Relais vereinfachung 0 aktiv
RELAY_ON = False;
RELAY_OFF = (not RELAY_ON);
DELAY = 4; #Wartezeit in der Schleife
count = 0; #Zähler für die Verzögerung der Befeuchtung
evac = True
# Pinbelegung
BOARD_MODE = gpio.BCM; # GPIO board mode
PIN_DHT = 17; # Pin für Temp/Hum Sensor
PIN_HEATER = 27; # Pin für Heizkabel
PIN_COOL = 22; # Pin für Kühlschrankkompressor
PIN_FAN = 18; # Pin für Umluftventilator
PIN_FAN1 = 23; # Pin für Austauschlüfter
PIN_HUM = 24; # Pin für Luftbefeuchter
sht = Sht(21, 20)
VERBOSE = True;
#RRD-Tool konfiguration
dbname = 'dht22' # Name fuer Grafiken etc
filename = dbname +'.rrd' # Dateinamen mit Endung
steps = 10 # Zeitintervall fuer die Messung in Sekunden
i = 0
z = 0
# Funktionen ############################################################################################################
########################################################################################################################
def goodbye():
cleanup();
writeVerbose('Goodbye!');
def cleanup():
writeVerbose('Running cleanup script...');
gpio.cleanup(); # GPIO zurücksetzen
writeVerbose('Cleanup complete.', True);
def setupGPIO():
global BOARD_MODE;
global PIN_HEATER;
global PIN_COOL;
global PIN_FAN;
global PIN_HUM;
global PIN_FAN1;
writeVerbose('Setting up GPIO...');
gpio.setwarnings(False);
# Board mode wird gesetzt
gpio.setmode(BOARD_MODE);
# Einstellen der GPIO PINS
gpio.setup(PIN_HEATER, gpio.OUT);
gpio.setup(PIN_COOL, gpio.OUT);
gpio.setup(PIN_FAN, gpio.OUT);
gpio.setup(PIN_HUM, gpio.OUT);
gpio.setup(PIN_FAN1, gpio.OUT);
gpio.output(PIN_HEATER, RELAY_OFF);
gpio.output(PIN_COOL, RELAY_OFF);
gpio.output(PIN_FAN, RELAY_OFF);
gpio.output(PIN_HUM, RELAY_OFF);
gpio.output(PIN_FAN1, RELAY_OFF);
writeVerbose('GPIO setup complete.',True);
def writeVerbose(s, newLine=False):
global VERBOSE;
if(VERBOSE):
print(s);
if(newLine is True):
print('');
def write_current(sensortemp, sensorhum):
global CURRENT_FILE;
s = json.dumps({"temperatur":sensortemp, "heizung":gpio.input(PIN_HEATER), "luftaustausch":gpio.input(PIN_FAN1), "kuhlung":gpio.input(PIN_COOL), "umluft":gpio.input(PIN_FAN),"luftfeuchtigkeit":sensorhum, 'date':int(time.time())});
with open(CURRENT_FILE, 'w') as file:
file.write(s);
def readSettings():
global SETTINGS_FILE;
s = None;
with open(SETTINGS_FILE, 'r') as file:
s = file.read();
data = json.loads(s);
return data;
def plotten(a):#, b, c, d):
# Funktion zum Plotten der Grafiken
# a: Wert, der geplottet werden soll
# Beschriftung für die Grafiken festlegen
if a == 'sensortemp':
title = 'Temperatur'
label = 'in C'
elif a == 'sensorhum':
title = 'Luftfeuchtigkeit'
label = 'in %'
elif a == "lat":
title = 'Luftaustausch'
label = 'ein oder aus'
elif a == "uml":
title = 'Luftumwelzung'
label = 'ein oder aus'
elif a == "heat":
title = 'Heizung'
label = 'ein oder aus'
elif a == "cool":
title = 'Kuehlung'
label = 'ein oder aus'
elif a == "lbf":
title = 'Luftbefeuchter'
label = 'ein oder aus'
# Aufteilung in drei Plots
for plot in ['daily' , 'weekly', 'monthly', 'hourly']:
if plot == 'weekly':
period = 'w'
elif plot == 'daily':
period = 'd'
elif plot == 'monthly':
period = 'm'
elif plot == 'hourly':
period = 'h'
# Grafiken erzeugen
ret = rrdtool.graph("%s%s_%s-%s.png" %(PICPATH,dbname,a,plot),
"--start",
"-1%s" %(period),
"--title=%s (%s)" %(title,plot),
"--vertical-label=%s" %(label),
'--watermark=leben-zwo-punkt-null.de',
"-w 800",
#"-h 600",
"--alt-autoscale",
"--slope-mode",
"DEF:%s=%s:%s_%s:AVERAGE" %(a, filename, dbname, a),
"DEF:durch=dht22.rrd:dht22_sensortemp:AVERAGE",
"DEF:durchhum=dht22.rrd:dht22_sensorhum:AVERAGE",
#"DEF:%s=%s:%s_%s:AVERAGE" %(c, filename, dbname, c),
#"DEF:%s=%s:%s_%s:AVERAGE" %(d, filename, dbname, d),
"GPRINT:durch:AVERAGE:Temperatur\: %3.2lf C",
"GPRINT:durchhum:AVERAGE:Luftfeuchtigkeit\: %3.2lf",
"LINE1:%s#0000FF:%s_%s" %(a, dbname, a))
#"LINE1:%s#00FF00:%s_%s" %(b, dbname, b),
#"LINE1:%s#FF0000:%s_%s" %(c, dbname, c),
#"LINE1:%s#000000:%s_%s" %(d, dbname, d))
def doMainLoop():
global value
global tempon;
global tempoff;
global tempstart;
global tempon1;
global tempoff1;
global tempstart1;
global sensortemp;
global sensorhum;
global temphyston;
global temphystoff;
global humhyston;
global humhystoff;
global temperature
global settings
global uml;
global lat;
global heat;
global cool;
global z;
global lbf;
global count;
global humdelay;
global evac;
while True:
sensortemp1 = sht.read_t()
sensorhum1 = sht.read_rh()
dew_point = sht.read_dew_point(sensortemp1, sensorhum1)
dew_point = round (dew_point,1)
# sensorhum1, sensortemp1 = Adafruit_DHT.read_retry(sensor, PIN_DHT)
if sensorhum1 is not None and sensortemp1 is not None:
sensortemp = round (sensortemp1,2)
sensorhum = round (sensorhum1,2)
else:
print 'Failed to get reading. Try again!'
try:
settings = readSettings();
except:
# An exception occurred and settings file cannot be reached.
writeVerbose('Unable to read settings file, checking if in the blind.');
continue;
mod = settings['mod'];
temp = settings['temp'];
hum = settings['hum'];
tempoff = settings['tempoff'];
tempon = settings['tempon'];
tempoff1 = settings['tempoff1'];
tempon1 = settings['tempon1'];
temphyston = settings['temphyston'];
temphystoff = settings['temphystoff'];
humhyston = settings['humhyston'];
humhystoff = settings['humhystoff'];
humdelay = settings ['humdelay'];
humdelay = humdelay*10;
# At this point, the settings have been read.
# However we still need to ensure that the last update time isn't out of our emergency range.
lastSettingsUpdate = settings['date'];
os.system('clear'); # Clears the terminal
t = int(time.time());
writeVerbose(' ');
writeVerbose('************************************************************');
writeVerbose(' ');
writeVerbose('Main loop... ('+str(t)+')');
print ('Eingestellter Temperaturwert in Celcius :')+str (temp);
print ('Aktuelle Temperatur :')+str (sensortemp);
print ('Eingestellter Luftfeuchtigwert in % :')+str (hum);
print ('Aktuelle Luftfeuchtigkeit :')+str (sensorhum);
print ('Aktuelle Taupunkt :')+str (dew_point);
write_current(sensortemp, sensorhum);
#Timer für Luftumwelzung
if tempoff > 0:
t = int(time.time())
if t < tempstart + tempoff:
#gpio.output(PIN_FAN, RELAY_OFF);
vent=True
if t >= tempstart + tempoff:
#gpio.output(PIN_FAN, RELAY_ON);
vent=False
if t >= tempstart + tempoff + tempon:
tempstart = int(time.time())
#Timer für Luftaustausch
if tempoff1 > 0:
#t = int(time.time())
if t < tempstart1 + tempoff1:
#gpio.output(PIN_FAN1, RELAY_OFF);
vent1=True
#print ('Timer umluft aus')
if t >= tempstart1 + tempoff1:
#gpio.output(PIN_FAN1, RELAY_ON);
vent1=False
#print ('Timer umluft ein')
if t >= tempstart1 + tempoff1 + tempon1:
tempstart1 = int(time.time())
if mod == 1: #Kühlmodus
evac=True
gpio.output(PIN_HEATER, RELAY_OFF);
if sensortemp >= temp + temphyston:
gpio.output(PIN_COOL, RELAY_ON);
if sensortemp <= temp + temphystoff :
gpio.output(PIN_COOL, RELAY_OFF);
if sensorhum <= hum - humhyston:
gpio.output(PIN_HUM, RELAY_ON);
if sensorhum >= hum - humhystoff:
gpio.output(PIN_HUM, RELAY_OFF);
if mod == 2: #Heizmodus
evac=True
gpio.output(PIN_COOL, RELAY_OFF);
if sensortemp <= temp - temphyston:
gpio.output(PIN_HEATER, RELAY_ON);
if sensortemp >= temp - temphystoff:
gpio.output(PIN_HEATER, RELAY_OFF);
if sensorhum <= hum - humhyston:
gpio.output(PIN_HUM, RELAY_ON);
if sensorhum >= hum - humhystoff:
gpio.output(PIN_HUM, RELAY_OFF);
if mod == 3: #Automodus
evac=True
if sensortemp >= temp + temphyston: #Kühlung ein
gpio.output(PIN_COOL, RELAY_ON);
if sensortemp <= temp + temphystoff:
gpio.output(PIN_COOL, RELAY_OFF);
if sensortemp <= temp - temphyston: #Heizung ein
gpio.output(PIN_HEATER, RELAY_ON);
if sensortemp >= temp - temphystoff:#Heizung aus
gpio.output(PIN_HEATER, RELAY_OFF);
if sensorhum <= hum - humhyston: #Luftbefeuchter ein
count = count+1
if count >= humdelay:
gpio.output(PIN_HUM, RELAY_ON);
if sensorhum >= hum - humhystoff: # Luftbefeuchter aus
gpio.output(PIN_HUM, RELAY_OFF);
count = 0;
if mod == 4: #Automodus erweitert
#Temperaturreglung
if sensortemp >= temp + temphyston: #Kühlung ein
gpio.output(PIN_COOL, RELAY_ON);
if sensortemp <= temp + temphystoff: #Kühlung aus
gpio.output(PIN_COOL, RELAY_OFF);
if sensortemp <= temp - temphyston: #Heizung ein
gpio.output(PIN_HEATER, RELAY_ON);
if sensortemp >= temp - temphystoff:#Heizung aus
gpio.output(PIN_HEATER, RELAY_OFF);
#Luftfeuctigkeitsreglung
if sensorhum <= hum - humhyston: #Luftbefeuchter ein
count = count+1
if count >= humdelay:
gpio.output(PIN_HUM, RELAY_ON);
if sensorhum >= hum - humhystoff: # Luftbefeuchter aus
gpio.output(PIN_HUM, RELAY_OFF);
count = 0;
if sensorhum >= hum + humhyston: #Luftaustausch ein
#gpio.output(PIN_FAN1, RELAY_ON)
evac = False
if sensorhum <= hum + humhystoff: #Luftaustausch aus
evac = True
#gpio.output(PIN_FAN1, RELAY_OFF)
#Einschaltung und Abschaltung der Be und Entlüftung
#Umluft
if gpio.input(PIN_HEATER) or gpio.input(PIN_COOL) or gpio.input(PIN_HUM) or vent == False:
gpio.output(PIN_FAN, RELAY_ON)
if gpio.input(PIN_HEATER) and gpio.input(PIN_COOL) and gpio.input(PIN_HUM) and vent == True:
gpio.output(PIN_FAN, RELAY_OFF);
#Luftaustausch
if vent1 ==False or evac == False:
gpio.output(PIN_FAN1, RELAY_ON);
#print('umluft ein.........')
if evac and vent1 == True:
gpio.output(PIN_FAN1, RELAY_OFF)
#print('Umluft aus.........')
if gpio.input(PIN_HEATER) == False:
writeVerbose('Heizung ein');
heat = 10
else:
writeVerbose('Heizung aus');
heat = 0
if gpio.input(PIN_COOL) == False:
writeVerbose('Kühlung ein');
cool = 10
else:
writeVerbose('Kühlung aus');
cool = 0
if gpio.input(PIN_HUM) == False:
writeVerbose('Luftbefeuchter ein');
lbf = 10
else:
writeVerbose('Luftbefeuchter aus');
lbf = 0;
if gpio.input(PIN_FAN) == False:
writeVerbose('Umluft ein');
uml = 10
else:
writeVerbose('Umluft aus');
uml = 0
if gpio.input(PIN_FAN1) == False:
writeVerbose('Luftaustausch ein');
lat = 10
else:
writeVerbose('Luftaustausch aus');
lat = 0
#print ('evac')+str (evac);
#print ('vent1')+str (vent1);
# Messwerte in die RRD-Datei schreiben
from rrdtool import update as rrd_update
ret = rrd_update('%s' %(filename), 'N:%s:%s:%s:%s:%s:%s:%s' %(sensortemp, sensorhum, lat, uml, heat, cool, lbf));
#array für graph
# Grafiken erzeugen
if z >= 2:
print "Erzeuge Grafiken"
plotten('sensortemp')#', 'heat', 'cool', 'uml')
plotten('sensorhum')#, 'lbf', 'uml', 'lat')
plotten('uml')#, 'lat')
plotten('lat')
plotten('heat')
plotten('cool')
plotten('lbf')
z = 0;
else:
z = z+1
time.sleep(1)
# Mainloop fertig
writeVerbose('Loop complete.');
time.sleep(3);
# Hauptprogramm #################################################################################################################
########################################################################################################################
os.system('clear'); # Bildschirm löschen
writeVerbose('************************************************************');
setupGPIO(); # GPIO initialisieren
# RRD-Datenbank anlegen, wenn nicht vorhanden
try:
with open(filename): pass
print "Datenbankdatei gefunden: " + filename
i=1
except IOError:
print "Ich erzeuge eine neue Datenbank: " + filename
ret = rrdtool.create("%s" %(filename),
"--step","%s" %(steps),
"--start",'0',
"DS:dht22_sensortemp:GAUGE:2000:U:U",
"DS:dht22_sensorhum:GAUGE:2000:U:U",
"DS:dht22_lat:GAUGE:2000:U:U",
"DS:dht22_uml:GAUGE:2000:U:U",
"DS:dht22_heat:GAUGE:2000:U:U",
"DS:dht22_cool:GAUGE:2000:U:U",
"DS:dht22_lbf:GAUGE:2000:U:U",
"RRA:AVERAGE:0.5:1:2160",
"RRA:AVERAGE:0.5:5:2016",
"RRA:AVERAGE:0.5:15:2880",
"RRA:AVERAGE:0.5:60:8760",)
i=1
writeVerbose('************************************************************');
settings = readSettings();
tempstart = int(time.time())
tempstart1 = tempstart
try:
doMainLoop();
except KeyboardInterrupt:
pass;
except Exception, e:
writeVerbose('Exception occurred!!!', True);
writeVerbose(str(e), True);
pass;
goodbye();