-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.py
164 lines (121 loc) · 3.95 KB
/
app.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
from flask import Flask
from flask import session
from flask import render_template
from flask import request
from random import randint
from random import uniform
from subprocess import Popen, PIPE, call
from multiprocessing import Process, Pipe, Queue, current_process, Value
import time
import pidypi as PIDController
#import RPi.GPIO as GPIO
import logging
app = Flask(__name__)
logging.basicConfig(filename='example.log',level=logging.DEBUG)
tt = Value('i', 50)
kp = Value('i', 102)
ki = Value('i', 100)
kd = Value('i', 5)
cycle_duty = Value('d', 0)
job = None
w1_sensorid = "28-03146215acff"
testMode = True
pinNum = 17
cycle_time=5
@app.route("/")
def index():
if job is None:
automode = "Off"
else:
automode = "On"
return render_template('index.html', targetTemp=tt.value, kp=kp.value , ki=ki.value , kd=kd.value, automode=automode )
@app.route("/auto")
def auto():
global job
if job is None:
print("AUTO MODE Started")
startAutoMode(tt, kp, ki, kd)
else:
print("AUTO MODE Stopped")
job.terminate()
job = None
return "OK"
@app.route("/setData")
def setData():
print("SET DATA")
tt.value = int(request.args.get('tt', ''))
kp.value = int(request.args.get('kp', ''))
ki.value = int(request.args.get('ki', ''))
kd.value = int(request.args.get('kd', ''))
return "OK"
@app.route("/getTemp")
def getTemp():
t = float(tempData1Wire(w1_sensorid))
i = str(round(t,1))
return "{\"temp\":\"%s\",\"heat\":\"%s\"}" % (i,cycle_duty.value,)
@app.route("/heat")
def heatOn():
print("HEAT ON")
return "ON"
def startAutoMode(tt, kp, ki, kd):
print("START JOB")
global job
job = Process(name = "gettempProc1", target=gettempProc, args=(tt,kp,ki,kd,cycle_duty,))
job.start()
def gettempProc(targetTemp, kp, ki, kd, dc):
## SETUP PID
pid = PIDController.pidpy(cycle_time, kp.value, ki.value, kd.value)
## SETUP GPIO
if testMode == False:
GPIO.setmode(GPIO.BCM)
GPIO.setup(pinNum, GPIO.OUT)
GPIO.output(pinNum, 1)
while (True):
print("TT", targetTemp.value)
print("KP", kp.value)
print("KI", ki.value)
print("KD", kd.value)
temp_ma = float(tempData1Wire(w1_sensorid))
duty_cycle = pid.calcPID_reg4(temp_ma, targetTemp.value, True)
#print 'DUTY CYCLE', duty_cycle
print(cycle_time)
dc.value = float(duty_cycle)
if duty_cycle == 0:
print "--> 100% OFF"
#GPIO.output(pinNum, 0)
time.sleep(5)
elif duty_cycle == 100:
print "--> 100% ON"
#GPIO.output(pinNum, 1)
time.sleep(5)
else:
print "--> INDIVIDUAL"
on_time, off_time = getonofftime(cycle_time, duty_cycle)
#GPIO.output(pinNum, 1)
time.sleep(on_time)
#GPIO.output(pinNum, 0)
time.sleep(off_time)
def getonofftime(cycle_time, duty_cycle):
duty = duty_cycle/100.0
on_time = cycle_time*(duty)
off_time = cycle_time*(1.0-duty)
return [on_time, off_time]
def tempData1Wire(tempSensorId):
if testMode == True:
pipe = Popen(["cat","w1_slave"], stdout=PIPE)
else:
pipe = Popen(["cat","/sys/bus/w1/devices/w1_bus_master1/" + tempSensorId + "/w1_slave"], stdout=PIPE)
result = pipe.communicate()[0]
if (result.split('\n')[0].split(' ')[11] == "YES"):
temp_C = float(result.split("=")[-1])/1000 # temp in Celcius
else:
temp_C = -99 #bad temp reading
return temp_C
if __name__ == "__main__":
#start thermometer
if testMode == False:
call(["modprobe", "w1-gpio"])
call(["modprobe", "w1-therm"])
#session setup
app.secret_key = 'A0Zr98j/3yX R~XHH!jmN]LWX/,?RT'
app.run(debug=False, host='0.0.0.0')