-
Notifications
You must be signed in to change notification settings - Fork 0
/
spiracle.py
executable file
·215 lines (153 loc) · 5.08 KB
/
spiracle.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
import os
import sys
import time
import click
import RPi.GPIO as GPIO
from ADCDACPi import ADCDACPi as ADC
GPIO.setmode(GPIO.BOARD)
ADC_REFERENCE_VOLTAGE = 3.3
ADC_READ_MODE_SINGLE = 0
ADC_READ_INTERVAL = 0.25
SOIL_ADC_CHANNEL = 1
WATER_ADC_CHANNEL = 2
SOIL_DRY_THRESHOLD = 2.5
WATER_LEVEL_WARNING_THRESHOLD = 3.0
WATER_LEVEL_CRITICAL_THRESHOLD = 2.7
RELAY_PIN_NUMBER = 10
WATER_LEVEL_CRITICAL_MESSAGE = (
"The water level is critically low! "
"Refill the tank to continue watering."
)
WATER_LEVEL_WARNING_MESSAGE = "The water level is low."
SOIL_IS_WET_MESSAGE = "The soil is wet."
TIME_IS_UP_MESSAGE = "The time is up."
STOPPING_PUMP_MESSAGE = "Stopping the pump."
class Pin:
def __init__(self, number, direction):
self.number = number
self._direction = direction
GPIO.setup(self.number, self._direction)
@property
def _current_state(self):
raise NotImplementedError("Must be implemented in subclasses.")
@property
def is_high(self):
return self._current_state == GPIO.HIGH
@property
def is_low(self):
return self._current_state == GPIO.LOW
class InputPin(Pin):
def __init__(self, number):
super().__init__(number, GPIO.IN)
@property
def _current_state(self):
return GPIO.input(self.number)
@property
def value(self):
return self._current_state
class OutputPin(Pin):
def __init__(self, number):
super().__init__(number, GPIO.OUT)
self._current_output = GPIO.LOW
@property
def _current_state(self):
return self._current_output
def _set_output(self, value):
self._current_output = value
GPIO.output(self.number, self._current_output)
def set_high(self):
return self._set_output(GPIO.HIGH)
def set_low(self):
return self._set_output(GPIO.LOW)
def _echo(*args):
click.echo(" ".join(str(arg) for arg in args))
def cleanup():
GPIO.cleanup()
def to_stdout(s):
terminal_width = os.get_terminal_size().columns
number_of_extra_spaces = terminal_width - len(s)
full_line = s + " " * number_of_extra_spaces
sys.stdout.write(full_line)
sys.stdout.flush()
sys.stdout.write("\r")
sys.stdout.flush()
def read_adc(adc, channel):
return adc.read_adc_voltage(channel, ADC_READ_MODE_SINGLE)
def is_water_level_low(adc):
return read_adc(adc, WATER_ADC_CHANNEL) < WATER_LEVEL_WARNING_THRESHOLD
def is_water_level_critical(adc):
return read_adc(adc, WATER_ADC_CHANNEL) < WATER_LEVEL_CRITICAL_THRESHOLD
def is_soil_dry(adc):
return read_adc(adc, SOIL_ADC_CHANNEL) < SOIL_DRY_THRESHOLD
def run_pump(adc, timeout, check_water_level=True, check_moisture=True):
if check_moisture:
_echo(
"Running pump until the soil is wet or "
"for at most {} seconds.".format(timeout)
)
else:
_echo("Running pump for {} seconds.".format(timeout))
relay_pin = OutputPin(RELAY_PIN_NUMBER)
relay_pin.set_high()
stop_time = time.time() + timeout
while True:
if check_water_level and is_water_level_critical(adc):
_echo(WATER_LEVEL_CRITICAL_MESSAGE)
break
if check_moisture and not is_soil_dry(adc):
_echo(SOIL_IS_WET_MESSAGE)
break
if time.time() >= stop_time:
_echo(TIME_IS_UP_MESSAGE)
break
_echo(STOPPING_PUMP_MESSAGE)
relay_pin.set_low()
if check_water_level and is_water_level_low(adc):
_echo(WATER_LEVEL_WARNING_MESSAGE)
def check_sensors_and_run_pump(adc, timeout):
if is_water_level_critical(adc):
_echo(WATER_LEVEL_CRITICAL_MESSAGE)
exit(1)
if is_water_level_low(adc):
_echo(WATER_LEVEL_WARNING_MESSAGE)
if is_soil_dry(adc):
_echo("The soil is dry.")
run_pump(adc, timeout)
else:
_echo(SOIL_IS_WET_MESSAGE)
def run_and_cleanup(runner, *runner_args, **runner_kwargs):
try:
adc = ADC()
adc.set_adc_refvoltage(ADC_REFERENCE_VOLTAGE)
runner(adc, *runner_args, **runner_kwargs)
finally:
cleanup()
@click.group()
def spiracle():
pass
@spiracle.command()
@click.argument("channel", type=click.INT)
def debug(channel):
adc = ADC()
adc.set_adc_refvoltage(ADC_REFERENCE_VOLTAGE)
def output_line(value):
return "ADC channel {} is showing {} V".format(channel, value)
def read_adc():
return adc.read_adc_voltage(channel, ADC_READ_MODE_SINGLE)
while True:
voltage = read_adc()
line = output_line(voltage)
to_stdout(line)
time.sleep(ADC_READ_INTERVAL)
@spiracle.command()
@click.argument("timeout", type=click.FLOAT)
@click.option("--water-level-sensor", is_flag=True, default=False)
@click.option("--moisture-sensor", is_flag=True, default=False)
def pump(timeout, water_level_sensor, moisture_sensor):
run_and_cleanup(run_pump, timeout, water_level_sensor, moisture_sensor)
@spiracle.command()
@click.argument("timeout", type=click.FLOAT)
def run(timeout):
run_and_cleanup(check_sensors_and_run_pump, timeout)
if __name__ == "__main__":
spiracle()