This repository has been archived by the owner on Apr 25, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
waterpi.py
134 lines (110 loc) · 3.69 KB
/
waterpi.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
#!/usr/bin/python3
#coding: utf8
#waterpi.py
import RPi.GPIO as GPIO
import sys
import spidev
from spidev import SpiDev
import time
from time import localtime, strftime
# CLASS & FUNCTIONS
class MCP3008:
def __init__(self, bus = 0, device = 0):
self.bus, self.device = bus, device
self.spi = SpiDev()
self.open()
def open(self):
self.spi.open(self.bus, self.device)
self.spi.max_speed_hz = 1000000 # ab Raspbian-Version "Buster" erforderlich!
def read(self, channel = 0):
adc = self.spi.xfer2([1,(8+channel)<<4,0])
if 0<=adc[1]<=3:
data = ((adc[1]&3)<<8)+adc[2]
per = (918 - data) / 918 * 100 # maximalen Wert testen und 2x eintragen, zB 918; Wert kann nie mehr als 1023 sein!
return per
else:
return 0
def close(self):
self.spi.close()
def watering(relay,pump):
GPIO.output(relay, False)
time.sleep(pump)
GPIO.output(relay, True)
time.sleep(1)
# LOG
zeitpunkt = strftime("%Y-%m-%d %H:%M:00", time.localtime())
sys.stdout = open("datenlog.log", "a")
# PINS FESTLEGEN
strom_sensoren = 5
relais1 = 11
relais2 = 13
relais3 = 15
relais4 = 16
#relais5 = x
# BEZEICHNUNGEN FESTLEGEN (bezogen auf die Sensoren-Nummer)
text1 = "Buchsbaum"
text2 = "Paprika"
text3 = "Tomaten"
text4 = "Petersilie [Kräuter]" # Teil der Gruppe: Kräuter
text5 = "Schnittlauch [Kräuter]" # Teil der Gruppe: Kräuter
#text6 = "x"
# PROZENTWERTE (Minimum,Maximum) und PUMPENDAUER (in Sekunden) FESTLEGEN - [MIN,MAX,DAUER]
duerr = [5,15,15]
trocken = [15,25,10]
feucht = [25,35,5]
# GPIO SETUP
GPIO.setwarnings(False) # Fehlermeldungen deaktivieren
GPIO.setmode(GPIO.BOARD)
GPIO.setup(strom_sensoren, GPIO.OUT)
GPIO.setup(relais1, GPIO.OUT)
GPIO.setup(relais2, GPIO.OUT)
GPIO.setup(relais3, GPIO.OUT)
GPIO.setup(relais4, GPIO.OUT)
#GPIO.setup(relais5, GPIO.OUT)
# SENSOREN ABFRAGEN
GPIO.output(strom_sensoren, GPIO.HIGH)
time.sleep(1)
adc = MCP3008()
sensor1 = adc.read( channel = 0 )
sensor2 = adc.read( channel = 1 )
sensor3 = adc.read( channel = 2 )
sensor4 = adc.read( channel = 3 )
sensor5 = adc.read( channel = 4 )
#sensor6 = adc.read( channel = 5 )
time.sleep(1)
GPIO.output(strom_sensoren, GPIO.LOW)
time.sleep(0.5)
# PUMPE 1 (Buchsbaum)
print(zeitpunkt+";"+text1+";{:.1f}%".format(sensor1))
if duerr[0] <= sensor1 <= duerr[1]:
watering(relais1,duerr[2])
elif trocken[0] <= sensor1 <= trocken[1]:
watering(relais1,trocken[2])
elif feucht[0] <= sensor1 <= feucht[1]:
watering(relais1,feucht[2])
# PUMPE 2 (Paprika)
print(zeitpunkt+";"+text2+";{:.1f}%".format(sensor2))
if duerr[0] <= sensor2 <= duerr[1]:
watering(relais2,duerr[2])
elif trocken[0] <= sensor2 <= trocken[1]:
watering(relais2,trocken[2])
elif feucht[0] <= sensor2 <= feucht[1]:
watering(relais2,feucht[2])
# PUMPE 3 (Tomaten)
print(zeitpunkt+";"+text3+";{:.1f}%".format(sensor3))
if duerr[0] <= sensor3 <= duerr[1]:
watering(relais3,18) # manuelle Eingabe
elif trocken[0] <= sensor3 <= trocken[1]:
watering(relais3,15) # manuelle Eingabe
elif feucht[0] <= sensor3 <= feucht[1]:
watering(relais3,12) # manuelle Eingabe
# PUMPE 4 [Kräuter]
print(zeitpunkt+";"+text4+";{:.1f}%".format(sensor4))
print(zeitpunkt+";"+text5+";{:.1f}%".format(sensor5))
if (duerr[0] <= sensor4 <= duerr[1]) or (duerr[0] <= sensor5 <= duerr[1]):
watering(relais4,duerr[2])
elif (trocken[0] <= sensor4 <= trocken[1]) or (trocken[0] <= sensor5 <= trocken[1]):
watering(relais4,trocken[2])
elif (feucht[0] <= sensor4 <= feucht[1]) or (feucht[0] <= sensor5 <= feucht[1]):
watering(relais4,feucht[2])
# PUMPE 5 .....