-
Notifications
You must be signed in to change notification settings - Fork 1
/
run_greenhouse.py
71 lines (53 loc) · 1.58 KB
/
run_greenhouse.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
import RPi.GPIO as GPIO
import time
from datetime import datetime
from datetime import time as dt_time
import signal, sys
#sudo pip install adafruit-mcp30008
import Adafruit_GPIO.SPI as SPI
import Adafruit_MCP3008
light_channel = 24
water_channel = 23
GPIO.setmode(GPIO.BCM)
GPIO.setup(light_channel, GPIO.OUT)
GPIO.setup(water_channel, GPIO.OUT)
# Software SPI configuration:
SPI_PORT = 0
SPI_DEVICE = 0
mcp = Adafruit_MCP3008.MCP3008(spi=SPI.SpiDev(SPI_PORT, SPI_DEVICE))
# Handler for Ctrl+C halt
def signal_hander(sig, frame):
print('\nHard exit time, closing connections...')
print('1/3 Forcing sunset...')
GPIO.output(light_channel, False)
print('2/3 Prohibiting rain...')
GPIO.output(water_channel, False)
print('3/3 Cleaning up environmental connections...')
GPIO.cleanup()
sys.exit(0)
sunrise = dt_time(8,30)
sunset = dt_time(20,00)
signal.signal(signal.SIGINT, signal_hander)
now = datetime.now()
current_time = now.strftime("%H:%M:%S")
print('Starting life cycle at {}'.format(current_time))
light_on = False
water_flowing = False
while True:
now = datetime.now()
current_time = now.strftime("%H:%M:%S")
if sunrise < now.time() < sunset:
if not light_on:
print('Sun up at {}'.format(current_time))
GPIO.output(light_channel, True)
light_on = True
elif light_on: #Sun isn't up and light is on
print('Sun down at {}'.format(current_time))
GPIO.output(light_channel, False)
light_on = False
#AD converter inputs
readings = [0]*8
for i in range(8):
readings[i] = mcp.read_adc(i)
print('\t {} Water level: {}'.format(current_time, readings[0]))
time.sleep(60)