-
Notifications
You must be signed in to change notification settings - Fork 0
/
SwamPi.py
213 lines (193 loc) · 7.24 KB
/
SwamPi.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
import time
import rpyc
import sqlite3
import logging
from threading import Thread
####Settings
boardType = 1 #1 = Raspberrypi; 2 = Le Potato
CtrlMethod = 1 #1: masterstat; 2: Relay Board; NOTE: Not yet able to run masterstat with Le Potato
PreWet = 75 #time in seconds to prewet pads before starting pump
postWet = 75
loopTime = 20 #time in seconds to wait before rechecking for thermostat control
purgePumpEnable = 1 #1 or 0 if using purge pump
purgePumpFreq = 6 #amount of runtime in hours that elapses between purge pump runs
purgePumpRunTime = 4 #amount of time in minutes purge pump runs during cycle
####Initialize system parameters, Don't Change
serverMode = 0; # 0 = off; 1 = auto; 2 = override controls that auto and manual cannot run at the same time
switch = [0,0,0,0]
running = 0
runTime = 0
startTime = 0
purging = 0
if boardType == 1:
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM)
elif boardType == 2:
import LePotatoPi.GPIO.GPIO as GPIO
pinCoolHi = 17
pinCoolLo = 27
pinFan = 4
GPIO.setup(pinCoolHi,GPIO.IN,GPIO.PUD_DOWN)
GPIO.setup(pinCoolLo,GPIO.IN,GPIO.PUD_DOWN)
GPIO.setup(pinFan,GPIO.IN,GPIO.PUD_DOWN)
if CtrlMethod == 1:
import os
os.system("sudo pigpiod")
import pigpio
pi = pigpio.pi()
pi.wave_tx_stop()
pin = 23
GPIO.setwarnings(False)
GPIO.setup(pin,GPIO.OUT)
def control(switches):
totalOn = sum(switches)
remainingTime = 885600-totalOn*5600
pulse = []
pulse.append(pigpio.pulse(1<<pin,0,31200))
pulse.append(pigpio.pulse(0,1<<pin,2000))
pulse.append(pigpio.pulse(1<<pin,0,11200))
pulse.append(pigpio.pulse(0,1<<pin,2000))
pulse.append(pigpio.pulse(1<<pin,0,11200))
pulse.append(pigpio.pulse(0,1<<pin,2000))
pulse.append(pigpio.pulse(1<<pin,0,11200))
pulse.append(pigpio.pulse(0,1<<pin,2000))
pulse.append(pigpio.pulse(1<<pin,0,11200))
pulse.append(pigpio.pulse(0,1<<pin,2000))
pulse.append(pigpio.pulse(1<<pin,0,5600+5600*switches[0]))
pulse.append(pigpio.pulse(0,1<<pin,2000))
pulse.append(pigpio.pulse(1<<pin,0,5600+5600*switches[1]))
pulse.append(pigpio.pulse(0,1<<pin,2000))
pulse.append(pigpio.pulse(1<<pin,0,5600+5600*switches[2]))
pulse.append(pigpio.pulse(0,1<<pin,2000))
pulse.append(pigpio.pulse(1<<pin,0,5600+5600*switches[3]))
pulse.append(pigpio.pulse(0,1<<pin,remainingTime))
pi.wave_clear()
pi.wave_add_new()
pi.wave_add_generic(pulse)
wave = pi.wave_create()
pi.wave_send_repeat(wave)
def stop():
pi.wave_tx_stop()
elif CtrlMethod == 2:
GPIO.setup(12,GPIO.OUT) #FanHi
GPIO.setup(16,GPIO.OUT) #FanLo
GPIO.setup(21,GPIO.OUT) #PurgePump
GPIO.setup(20,GPIO.OUT) #Pump
def control(switches):
GPIO.output(12, switches[0])
GPIO.output(16, switches[1])
GPIO.output(21, switches[2]) #PurgePump
GPIO.output(20, switches[3])
#print("setup Complete")
class ServerService(rpyc.Service):
def exposed_manual(self,fanHi,fanLo,purgePump,pump):
global serverMode
global running
serverMode = 2
toggle = [fanHi,fanLo,purgePump,pump].index(1)
if switch[toggle] == 1:
switch[toggle] = 0
else:
switch[toggle] = 1
sendCmd()
if switch[3]:
if switch[0]:
running = 2
elif switch[1]:
running = 1
else: running = 0
logging.info(["Manual Override, switch = ", switch])
def exposed_auto(self):
global serverMode
serverMode = 1
global running #0 = off; 1 = coolLo; 2 = coolHi
global switch
return 0
while serverMode == 1:
nestcmd = [GPIO.input(pinCoolHi),GPIO.input(pinCoolLo),GPIO.input(pinFan)]
#print("reading", nestcmd, running)
logging.debug(["reading ; nest cmd = ", nestcmd,"running = ", running])
if running == 0:
if nestcmd[1] == 1 or nestcmd[0] == 1:
switch = ([0,0,0,1])
sendCmd()
time.sleep(PreWet)
if nestcmd[0] == 1: #High
switch = ([1,0,0,1])
sendCmd()
running = 2
elif nestcmd[1] == 1: #Low
switch = ([0,1,0,1])
sendCmd()
running = 1
elif running != 0:
if nestcmd[1] == 0 and nestcmd[0] == 0: #was running last loop but not anymore
#print("shutting down")
switch = ([0,0,0,1])
sendCmd()
#print("and again")
time.sleep(postWet)
switch = ([0,0,0,0])
sendCmd()
running = 0
if (nestcmd[1] == 1 or nestcmd[0] == 1):
if nestcmd[0] == 1 and running == 1: #Changing to High
switch = ([1,0,0,1])
sendCmd()
running = 2
elif nestcmd[1] == 1 and running == 2: #Changing to Low
switch = ([0,1,0,1])
sendCmd()
running = 1
time.sleep(loopTime)
def exposed_getSwitch(self):
global switch
return switch
def sendCmd():
global switch
global running
global startTime
global runTime
global purging
if switch[0] == 1 and switch[1] == 1: #Disable running Low and High at the same time
switch[1] = 0
if switch[0] or switch[1]:
if running == 0:
startTime = time.time()
else:
runTime = runTime + time.time() - startTime
else:
if running != 0:
runTime = runTime + time.time() - startTime
if purging == 1: switch[2] = 1 #in case sendCmd() is called during purging
#print("running control ",switch,running)
logging.info(["Sending Command, switch = ",switch])
control(switch)
def runPurge():
global purgePumpFreq
global purgePumpRunTime
global runTime
global purging
while True:
if runTime > purgePumpFreq*3600:
global switch
logging.info("Starting Purge Cycle")
purging = 1
switch[2] = 1
sendCmd()
time.sleep(purgePumpRunTime*60)
switch[2] = 0
purging = 0
sendCmd()
runTime = 0
time.sleep(60*10)
logging.basicConfig(filename='/home/pi/SwamPi/log.log',
filemode='a',
format='%(asctime)s,%(msecs)d %(name)s %(levelname)s %(message)s',
datefmt='%m-%d-%y %H:%M:%S',
level=logging.INFO)
if purgePumpEnable == 1: Thread(target=runPurge).start()
from rpyc.utils.server import ThreadedServer
t = ThreadedServer(ServerService, port = 12345)
logging.info("Setup Complete, starting Server")
t.start()