-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
238 lines (199 loc) · 7.02 KB
/
main.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
import smbus2 # I2C-Kommunikation
import bme280 # BME280-Sensor-Funktionalität
import sqlite3 # Datenbank Integration
import time as t # Zeitverzögerungen
import datetime # UnixTime
import Adafruit_ADS1x15 # Für die ADC to I2C Schnittstelle / Bodenfeuchtigkeitssensor
import RPi.GPIO as GPIO # Für das ansteuern der GPIO
import threading # Für Multithreading
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM) # GPIO-Nummerierungsschema : https://raspi.tv/2013/rpi-gpio-basics-4-setting-up-rpi-gpio-numbering-systems-and-inputs
# BME280
address = 0x77 # I2C-Adresse des BME280-Sensors
bus_number = 1 # I2C-Busnummer (1 bei Raspberry Pi 3B+)
bus = smbus2.SMBus(bus_number) # Öffnen des I2C-Busses
calibrationParameter = bme280.load_calibration_params(bus, address) # Laden der Kalibrierungsparameter des Sensors
# Bodenfeuchtigkeit Sensor / ADC to I2C
adc = Adafruit_ADS1x15.ADS1015()
GAIN = 1 # 1 = Analog kann zwischen 0-4.096V sein: https://joy-it.net/en/products/SEN-Moisture
# Wasserpumpe
PinWaterPump = 20
waterpumpStatus = False
waterpumpLastAktive = None
GPIO.setup(PinWaterPump, GPIO.OUT)
# Servomotor/ lüftung
servoStatus = False
PinServomotor = 26
servomotorLastAktive = None
GPIO.setup(PinServomotor, GPIO.OUT)
pwm = GPIO.PWM(PinServomotor, 50)
# Beleuchtung
PinLight = 10
isLightInit = False
GPIO.setup(PinLight, GPIO.OUT)
lichtStatus = False
# sqllite
sqliteConnection = sqlite3.connect("AutoGrowing.db")
cursor = sqliteConnection.cursor()
plantData = None
currentPlantID = None
#Datenbankabfrage, welches Pflanzenprofil gerade aktiv ist und welche Daten es enthält.
def InitialSetup():
global currentPlantID, plantData
sqlGetPlantID = "SELECT plantID FROM PlantLog Order by dateTime DESC Limit 1;"
sqlGetPlantData = "SELECT growingTemperature, lightDurationInMinutesPerDay, growingSoilMoisture, growingAirHumidity, waterRequirementPerDay, actOnSoilMoisture FROM Plant WHERE plantID = ?;"
cursor.execute(sqlGetPlantID)
(currentPlantID,) = cursor.fetchone()
if currentPlantID:
cursor.execute(sqlGetPlantData, (currentPlantID,))
row = cursor.fetchone()
if row:
plantData = dict(
zip(
[
"growingTemperature",
"lightDurationInMinutesPerDay",
"growingSoilHumidity",
"humidityTreshold",
"waterRequirementPerDay",
"actOnSoilMoisture",
],
row,
)
)
else:
plantData = None
else:
currentPlantID = None
plantData = None
# Konsolen ausgabe
for key, value in plantData.items():
print(f"{key} {value}")
return
# Auslesen der Daten des BME280 Sensors
# Rückgabe der Werte in Form eines Arrays
def bme280_read():
data = bme280.sample(bus, address, calibrationParameter)
bmeReadouts = [
round(data.temperature, 2),
round(data.humidity, 2),
round(data.pressure, 2),
]
return (
bmeReadouts
)
# Einfügen der Sensorwerte in die Datenbank
def insertIntoTable(
bme280Readouts, soilMoisture, servoStatus, waterpumpStatus, lichtStatus
):
dateTime = datetime.datetime.now()
query = "INSERT INTO sensorReadout (dateTime, temperature, airHumidity, soilMoisture, doorIsOpen, waterIsOn, lightIsOn) values (?,?,?,?,?,?,?)"
cursor.execute(
query,
(
str(dateTime),
str(bme280Readouts[0]),
str(bme280Readouts[1]),
str(soilMoisture),
str(servoStatus),
str(waterpumpStatus),
str(lichtStatus),
),
)
sqliteConnection.commit()
# Betrieb der Wasserpumpe
def runWaterpump(time):
global waterpumpStatus, waterpumpLastAktive
if GPIO.input(PinWaterPump) or waterpumpStatus:
return
waterpumpLastAktive = datetime.datetime.now()
waterpumpStatus = True
GPIO.output(PinWaterPump, GPIO.HIGH) # Schalte die Wasserpumpe ein
t.sleep(time)
GPIO.output(PinWaterPump, GPIO.LOW) # Schalte die Wasserpupe aus
waterpumpStatus = False
return
def SwitchLight():
if (isLightInit):
return
l
isLightInit = True
if (GPIO.input(PinLight)):
lichtStatus = True
GPIO.output(PinLight, GPIO.HIGH)
else:
lichtStatus = False
GPIO.output(PinWaterPump, GPIO.LOW)
t.sleep(300)
isLightInit = False
return
# Betrieb des Servomotors
def servoMotor(time):
global servoStatus, servomotorLastAktive
if GPIO.input(PinServomotor) or servoStatus:
return
servomotorLastAktive = datetime.datetime.now()
servoStatus = True
duty = (180 / 18) + 2
pwm.start(duty)
t.sleep(0.5)
pwm.stop
t.sleep(time)
pwm.start(duty)
duty = (1 / 18) + 2
pwm.start(duty)
t.sleep(0.5)
pwm.start(0)
pwm.stop
servoStatus = False
return
# Hauptcode, der den Codefluss steuert
try:
while True:
InitialSetup()
print("\nongoing")
# BME280
bme280Readouts = bme280_read()
# Bodenfeuchtigkeit/ADC to i2c
soilMoisture = adc.read_adc(0, gain=GAIN)
insertIntoTable(
bme280Readouts, soilMoisture, servoStatus, waterpumpStatus, lichtStatus
)
print("Insert into table")
print(
f"\nBME: {bme280Readouts}\nSoil Moisture: {soilMoisture}\nServo: {servoStatus}\nWaterpump: {waterpumpStatus}\nlight: {lichtStatus}"
)
# Run water pump and servo motor in separate threads
water_pump_thread = threading.Thread(target=runWaterpump, args=[plantData["waterRequirementPerDay"]])
servo_motor_thread = threading.Thread(target=servoMotor, args=[12])
switch_light_thread = threading.Thread(target=SwitchLight)
if waterpumpLastAktive is None:
waterpumpShouldWork = True
else:
waterpumpShouldWork = (datetime.datetime.now() - waterpumpLastAktive).days >= 1
if servomotorLastAktive is None:
ServoMotorShouldWork = True
else:
ServoMotorShouldWork = bme280Readouts[1] >= plantData["humidityTreshold"]
if not waterpumpStatus and waterpumpShouldWork:
print("start waterpump")
water_pump_thread.start()
if not servoStatus and ServoMotorShouldWork:
print("start servo")
servo_motor_thread.start()
currentTime = datetime.datetime.now().time()
if not isLightInit and currentTime >= datetime.time(10, 0) and currentTime <= datetime.time(10, 1):
print("switch light")
switch
t.sleep(10) # 2 Sekunden vor der nächsten Messung
except KeyboardInterrupt:
print(
"Messung wurde vom Benutzer gestoppt."
) # Ausgabe, wenn der Benutzer die Messung abbricht
finally:
GPIO.output(PinWaterPump, GPIO.LOW)
pwm.stop()
GPIO.output(PinLight, GPIO.LOW)
bus.close() # Schließen des I2C-Busses nach Beendigung der Messung
sqliteConnection.close()
GPIO.cleanup()