-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest.py
More file actions
175 lines (152 loc) · 4.78 KB
/
test.py
File metadata and controls
175 lines (152 loc) · 4.78 KB
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
from gpiozero import Button
import RPi.GPIO as GPIO
import time
import redis
import sched
import board
import digitalio
import socket
from PIL import Image, ImageDraw, ImageFont
import adafruit_ssd1306
WIDTH = 128
HEIGHT = 64
BORDER = 5
i2c = board.I2C() # uses board.SCL and board.SDA
oled = adafruit_ssd1306.SSD1306_I2C(WIDTH, HEIGHT, i2c, addr=0x3C)
oled.fill(0)
oled.show()
image = Image.new("1", (oled.width, oled.height))
draw = ImageDraw.Draw(image)
font = ImageFont.load_default()
padding = -2
top = padding
bottom = HEIGHT-padding
x = 0
time.sleep(60)
try:
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.connect(('8.8.8.8', 1))
IP = s.getsockname()[0]
except:
IP = "UNKNOWN"
text1 = "AeroGarden Pi"
text2 = "v 1.04"
text3 = "bit.ly/AeroPiCode"
draw.text((x, top), text1, font=font, fill=255)
draw.text((x, top+16), text2, font=font, fill=255)
draw.text((x, top+32), text3, font=font, fill=255)
draw.text((x, top+56), "IP: " + str(IP), font=font, fill=255)
oled.image(image)
oled.show()
s = sched.scheduler(time.time, time.sleep)
r = redis.Redis(host='localhost', port=6379, db=0, decode_responses=True)
# Default values are stored in 'aerogardenInit.py', which is run when the Pi boots
# Verify the values below in that file if you believe they have been changed from the defaults shown here
#
# r.set('pumpPIN', '27')
# r.set('pumpState', 'Off')
# r.set('pumpTimer', '300')
# r.set('lightPIN', '22')
# r.set('lightState', 'Off')
# r.set('lightTimer', '3600')
# r.set('pumpLastAction', time.time())
# r.set('lightsLastAction', time.time())
pumpPIN = int(r.get('pumpPIN'))
lightPIN = int(r.get('lightPIN'))
buttonPIN = int(r.get('buttonPIN'))
button = Button(buttonPIN)
prevState = 1
currState = 0
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
GPIO.setup(pumpPIN,GPIO.OUT)
GPIO.setup(lightPIN,GPIO.OUT)
GPIO.output(pumpPIN,1)
GPIO.output(lightPIN,1)
def updateLCD():
draw.rectangle((0,0,oled.width,oled.height), outline=0, fill=0)
pumpState = r.get('pumpState')
if( int(pumpState) == 0 ):
pumpStateString = "Off"
pumpTimer = float(r.get('pumpOffTime'))
elif( int(pumpState) == 1 ):
pumpStateString = "On"
pumpTimer = float(r.get('pumpOnTime'))
pumpLastAction = float(r.get('pumpLastAction'))
lightState = r.get('lightState')
if( int(lightState) == 0 ):
lightStateString = "Off"
lightTimer = float(r.get('lightOffTime'))
elif( int(lightState) == 1 ):
lightStateString = "On"
lightTimer = float(r.get('lightOnTime'))
lightLastAction = float(r.get('lightLastAction'))
pumpStateOutput = "P: " + pumpStateString + " " + str(time.strftime("%I:%M %p", time.localtime(pumpLastAction + pumpTimer)))
lightStateOutput = "L: " + lightStateString + " " + str(time.strftime("%I:%M %p", time.localtime(lightLastAction + lightTimer)))
updateText1 = str(time.strftime("%I:%M:%S %p %a %m/%d"))
draw.text((x, top), updateText1, font=font, fill=255)
draw.text((x, top+16), pumpStateOutput, font=font, fill=255)
draw.text((x, top+32), lightStateOutput, font=font, fill=255)
draw.text((x, top+56), "IP: " + str(IP), font=font, fill=255)
oled.image(image)
oled.show()
s.enter(1, 1, updateLCD)
def updateDevices():
pumpLastAction = float(r.get('pumpLastAction'))
pumpState = int(r.get('pumpState'))
if( int(pumpState) == 0 ):
pumpTimer = int(r.get('pumpOffTime'))
else:
pumpTimer = int(r.get('pumpOnTime'))
pumpOnTime = int(r.get('pumpOnTime'))
pumpOffTime = int(r.get('pumpOffTime'))
if( pumpLastAction + pumpTimer <= time.time() ):
if( pumpState == 1):
r.set('pumpState', 0)
r.set('pumpTimer', pumpOffTime)
r.set('pumpLastAction', time.time())
GPIO.output(pumpPIN, 0)
elif ( pumpState == 0 ):
r.set('pumpState', 1)
r.set('pumpTimer', pumpOnTime)
r.set('pumpLastAction', time.time())
GPIO.output(pumpPIN, 1)
lightLastAction = float(r.get('lightLastAction'))
lightState = int(r.get('lightState'))
if( int(lightState) == 0 ):
lightTimer = int(r.get('lightOffTime'))
else:
lightTimer = int(r.get('lightOnTime'))
lightOnTime = int(r.get('lightOnTime'))
lightOffTime = int(r.get('lightOffTime'))
if( lightLastAction + lightTimer <= time.time() ):
if( lightState == 1):
r.set('lightState', 0)
r.set('lightTimer', lightOffTime)
r.set('lightLastAction', time.time())
GPIO.output(lightPIN, 0)
elif ( lightState == 0 ):
r.set('lightState', 1)
r.set('lightTimer', lightOnTime)
r.set('lightLastAction', time.time())
GPIO.output(lightPIN, 1)
s.enter(10, 1, updateDevices)
def buttonPress():
if currState == 1:
print("Button Pressed!")
if currState == 0:
print("Button Released!")
s.enter(10, 1, updateLCD)
s.enter(30, 1, updateDevices)
while(True):
if button.is_pressed:
print("Button!!!")
if prevState != currState:
currState = 1
buttonPress()
time.sleep(0.15)
else:
currState = 0
buttonPress()
time.sleep(0.15)
s.run()