-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpiponics.py
150 lines (123 loc) · 9.44 KB
/
piponics.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
#!/usr/bin/env python 3
# Copyright (c) 2020 PiPonics, Inc.
# Author: 0rion5 B3lt
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
import datetime as dt
import logging
import logging.handlers
from time import sleep
from os import system
import serial
class PiPonics:
def __init__(self, log_file, max_bytes, backup_count, pins):
self.log_file = log_file # set log file attribute
self.max_bytes = max_bytes # set max bytes attribute
self.backup_count = backup_count # set backup count attribute
self.valve_one_time = valve_one_time # set valve one attribute
self.valve_two_time = valve_two_time # set valve two attribute
self.wait_time = wait_time # set wait time attribute
self.cycle_count = cycle_count # set cycle count attribute
self.pins = pins # set the pins to be used
for i in self.pins:
system('gpio -1 mode '+str(i)+' out') # Execute the command (a string) in a subshell
@property
def time(self):
return dt.datetime.now().strftime(' %Y-%m-%d %I:%M:%S %p ') # a string representing the date and time
@property
def soil_moisture_sensor(self):
ser = serial.Serial('/dev/ttyACM0', 9600)
serial_read = str(ser.readline()).replace("b'","").replace("\\r\\n'","")
return serial_read
def start_logger(self, file_name, max_bytes, backup_count):
logger = logging.getLogger(__name__) # return logger of the current module
logger.setLevel(logging.INFO) # set the logging level of this logger
handler = logging.handlers.RotatingFileHandler(
file_name, 'w', max_bytes, backup_count) # The specified file is opened and used as the stream for logging.
handler.setLevel(logging.INFO) # set the logging level for this handler
formatter = logging.Formatter(
'%(levelname)s - %(name)s - %(message)s') # create formatter and add it to the handlers
handler.setFormatter(formatter) # set the Formatter for this handler
logger.addHandler(handler) # adds the Specified handler to this logger
return logger # return logger
def valve_one_open(self): # valve one open
for i in self.pins[0::2]: # for pins[0] and pins[2]
system('gpio -1 write '+str(i)+' 0') # write gpio pins[i] on
def valve_one_closed(self): # valve one closed
for i in self.pins[0::2]: # for pins[0] and pins[2]
system('gpio -1 write '+str(i)+' 1') # write gpio pins[i] off
def valve_two_open(self): # valve two open
for i in self.pins[1::1]: # for pins[0] and pins[1]
system('gpio -1 write '+str(i)+' 1') # write gpio pins[i] on
def valve_two_closed(self): # valve two closed
for i in self.pins[1::1]: # for pins[0] and pins[1]
system('gpio -1 write '+str(i)+' 0') # write gpio pins[i] off
def watering_cycle(self,valve_one_time, valve_two_time, wait_time, cycle_count):
logger = self.start_logger(
self.log_file, self.max_bytes, self.backup_count) # Create logging instance
for i in range(1, self.cycle_count+1):
# Cycle Start
print('Cycle: '+str(i)) # Print Cycle Count
logger.info(self.time + ' Starting Cycle ' + str(i)) # Log Cycle Count
logger.info(self.time + ' Valve One Opened')
logger.info(self.time + ' ' + self.soil_moisture_sensor) # Log Valve One Opened
self.valve_one_open() # OPEN VALVE ONE HERE
sleep(self.valve_one_time*60) # Valve One Timer
logger.info(self.time + ' Valve One Closed') # Log Valve One Closed
self.valve_one_closed() # CLOSE VALVE ONE HERE
sleep(self.wait_time*60) # Wait For Growbed to Drain
logger.info(self.time + ' Valve Two Opened') # Log Valve Two Opened
self.valve_two_open() # OPEN VALVE TWO HERE
sleep(self.valve_two_time*60) # Valve Two Timer
logger.info(self.time + ' Valve Two Closed') # Log Valve Two Closed
self.valve_two_closed() # CLOSE VALVE TWO HERE
sleep(self.wait_time*60) # Wait For Growbed To Drain
print("Done\n") # Print Cycle Is Done
logger.info(self.time + ' Cycle ' + str(i) + ' Complete\n') # Log Cycle Complete
def soil_moisture_cycle(self):
try:
logger = self.start_logger(
self.log_file, self.max_bytes, self.backup_count)
while True:
try:
soil_moisture = int(self.soil_moisture_sensor)
except ValueError:
soil_moisture = str(self.soil_moisture_sensor).replace("\\n'","")
if soil_moisture < 35:
self.valve_one_open()
print(self.time+" Soil Moisture "+self.soil_moisture_sensor + " Watering")
logger.warning(self.time+" Soil Moisture "+self.soil_moisture_sensor + "% Watering")
elif soil_moisture > 35:
self.valve_one_closed()
print(self.time+" Soil Moisture "+self.soil_moisture_sensor + " Not Watering")
#logger.info(self.time+" Soil Moisture "+self.soil_moisture_sensor + "% Not Watering")
sleep(5)
except KeyboardInterrupt:
for i in ponics.pins:
system('gpio -1 write '+str(i)+' 1')
print('not cycling water')
if __name__ == "__main__":
log_file = '/home/pi/Documents/projects/PiPonics/logs/PiPonics.log' # log file directory
max_bytes = 500000 # max bytes
backup_count = 5 # backup count
valve_one_time = 0.05 # valve one minutes
valve_two_time = 0.05 # valve two minutes
wait_time = 0.05 # wait time minutes
cycle_count = 9999 # cycle count
pins = [36, 38, 40] # gpio pins physical pin numbering
ponics = PiPonics(log_file, max_bytes, backup_count,pins)
ponics.soil_moisture_cycle()
#ponics.watering_cycle(valve_one_time, valve_two_time, wait_time, cycle_count)