-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
66 lines (58 loc) · 2.15 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
import os
# Change into the src directory if this script is run outside of it
# (so the config file import works - relative import did not work for some reason?)
if "src" in os.listdir():
os.chdir("src")
from irrigationsystem import IrrigationSystem
from smartplant import SmartPlant
from connections import connect_to_wifi
from logger import log_boot_time
import time
import config
import btree
def remove_inactive_plants_from_db(plants):
try:
f = open("smartplantdb", "r+b")
except OSError:
f = open("smartplantdb", "w+b")
db = btree.open(f)
names = [plant.name for plant in plants]
for key in db.keys():
if key.decode("utf-8") not in names:
del db[b"" + key]
db.flush()
db.close()
f.close()
def main():
if config.SSID is not None:
connect_to_wifi(config.SSID, config.WIFI_PASSWORD)
if config.LOGGING_ENABLED:
log_boot_time(config.DEVICE_NAME, time.time())
smart_plants = [
SmartPlant("plant_1",
pump_pin=33,
moisture_sensor_pin=34,
dry_reference=4000,
wet_reference=2900,
dry_treshold=20,
watered_threshold=70,
filter_window_size=config.MOVING_AVERAGE_WINDOW_SIZE,
watering_time=5),
SmartPlant("plant_2",
pump_pin=32,
moisture_sensor_pin=35,
dry_reference=4000,
wet_reference=2900,
dry_treshold=20,
watered_threshold=70,
filter_window_size=config.MOVING_AVERAGE_WINDOW_SIZE,
watering_time=5)
]
remove_inactive_plants_from_db(smart_plants)
irrigation_system = IrrigationSystem(smart_plants,
monitor_interval_seconds=config.MONITOR_INTERVAL_S,
watering_cooldown_seconds=config.WATERING_COOLDOWN_S,
logging_enabled=config.LOGGING_ENABLED)
irrigation_system.start_monitoring()
if __name__ == "__main__":
main()