-
Notifications
You must be signed in to change notification settings - Fork 1
/
mybbq.py
130 lines (111 loc) · 3.46 KB
/
mybbq.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
from twisted.web.server import Site
from twisted.web.resource import Resource
from twisted.internet import reactor
from twisted.web.static import File
from twisted.internet import task
import sys
import os
import time
import json
import RPi.GPIO as GPIO
from bbq import *
sys.path.insert(0, os.getcwd() + "/max31855")
from max31855 import MAX31855, MAX31855Error
class MyTempSensor(TempSensor):
def __init__(self):
cs_pin = 24
clock_pin = 11
data_pin = 10
units = "f"
self.thermocouple = MAX31855(cs_pin, clock_pin, data_pin, units)
self.currentTemp = 0
self.previousCurrentTemp = 0
self.ambientTemp = 0
self.previousAmbientTemp = 0
def getTemp(self):
self.previousAmbientTemp = self.ambientTemp
self.previousCurrentTemp = self.currentTemp
try:
self.ambientTemp = self.thermocouple.get_rj()
self.currentTemp = self.thermocouple.get()
self.thermocoupleStatus = "normal"
except MAX31855Error as e:
self.status = "" + e.value
self.ambientTemp = self.previousAmbientTemp
self.currentTemp = self.previousCurrentTemp
return self.currentTemp
class MyFan(AirControl):
def __init__(self):
super(MyFan, self).__init__()
GPIO.setmode(GPIO.BCM)
self.pin = 18
self.value = 0
self.step = 5
GPIO.setup(self.pin, GPIO.OUT)
GPIO.output(self.pin, 0)
self.p = GPIO.PWM(self.pin, 50)
self.p.start(0)
self.value = 0
def getValue(self):
return self.value
def setValue(self, value):
self.value = value
self.p.ChangeDutyCycle(value)
def __del__(self):
self.p.stop()
GPIO.cleanup()
logging.basicConfig(stream=sys.stdout)
logger = logging.getLogger()
logger.setLevel(logging.INFO)
logger.debug("starting up!")
myBBQ = BBQ()
tempSensor = MyTempSensor()
airControl = MyFan()
myBBQ.tempSensor = tempSensor
myBBQ.airControl = airControl
myBBQ.algorithm = LinearControl(myBBQ, airControl, tempSensor)
myBBQ.setMode(BBQ.MODES.MAINTAIN)
myBBQ.goal = 250
class BBQStatus(Resource):
def render_GET(self, request):
print("get BBQ called\n")
request.responseHeaders.addRawHeader(b"content-type", b"application/json")
info = { "pit" : myBBQ.temp,
"goal" : myBBQ.goal,
"fan" : myBBQ.airValue,
"history" : list(myBBQ.history)}
return json.dumps(info, default=lambda o: o.__dict__, sort_keys=True, indent=4)
def render_PUT(self, request):
print("I got a PUT!");
jsonString = request.content.getvalue()
decoded = json.loads(jsonString)
goal = decoded[u'goal']
print("goal: %d" % goal)
myBBQ.goal = int(goal)
request.setResponseCode(200)
# FIXME: better response
return ""
class BBQResource(Resource):
def getChild(self, name, request):
return BBQStatus()
root = File("static/html")
root.putChild("jq", File("jq"))
root.putChild("js", File("static/js"))
root.putChild("css", File("static/css"))
root.putChild("BBQ", BBQResource())
factory = Site(root)
reactor.listenTCP(8880, factory)
l = task.LoopingCall(myBBQ.runIteration)
l.start(1)
reactor.run()
#
# Editor modelines - https://www.wireshark.org/tools/modelines.html
#
# Local variables:
# c-basic-offset: 4
# indent-tabs-mode: nil
# End:
#
# vi: set shiftwidth=4 expandtab:
# :indentSize=4:noTabs=true:
#