Mercurial > code > home > repos > homeauto
annotate service/garageArduino/garageArduino.py @ 839:9cf01cee74f6
garage: videoselect and shiftbrite move from parallel to arduino
Ignore-this: 45c791caa87d4c054826020eeae8298b
darcs-hash:20120625075033-312f9-fe2b5088e638e008dd194bd450aa1bac3d48c233.gz
author | drewp <drewp@bigasterisk.com> |
---|---|
date | Mon, 25 Jun 2012 00:50:33 -0700 |
parents | ccbff2a4b8ce |
children | 62b3bb861e35 |
rev | line source |
---|---|
805 | 1 #!bin/python |
2 """ | |
3 talks to frontdoordriver.pde on an arduino | |
4 """ | |
5 | |
6 from __future__ import division | |
7 | |
839
9cf01cee74f6
garage: videoselect and shiftbrite move from parallel to arduino
drewp <drewp@bigasterisk.com>
parents:
818
diff
changeset
|
8 import cyclone.web, json, traceback, os, sys, time, logging, bitstring |
810
44e1ca03ddf1
move garage door opener from parport to arduino
drewp <drewp@bigasterisk.com>
parents:
809
diff
changeset
|
9 from twisted.internet import reactor, task, defer |
805 | 10 from twisted.web.client import getPage |
11 sys.path.append("/my/proj/house/frontdoor") | |
12 from loggingserial import LoggingSerial | |
810
44e1ca03ddf1
move garage door opener from parport to arduino
drewp <drewp@bigasterisk.com>
parents:
809
diff
changeset
|
13 sys.path.append("/my/proj/homeauto/lib") |
44e1ca03ddf1
move garage door opener from parport to arduino
drewp <drewp@bigasterisk.com>
parents:
809
diff
changeset
|
14 from cycloneerr import PrettyErrorHandler |
44e1ca03ddf1
move garage door opener from parport to arduino
drewp <drewp@bigasterisk.com>
parents:
809
diff
changeset
|
15 from logsetup import log |
805 | 16 sys.path.append("../../../room") |
17 from carbondata import CarbonClient | |
18 sys.path.append("/my/site/magma") | |
19 from stategraph import StateGraph | |
20 from rdflib import Namespace, RDF, Literal | |
839
9cf01cee74f6
garage: videoselect and shiftbrite move from parallel to arduino
drewp <drewp@bigasterisk.com>
parents:
818
diff
changeset
|
21 from webcolors import hex_to_rgb, rgb_to_hex |
9cf01cee74f6
garage: videoselect and shiftbrite move from parallel to arduino
drewp <drewp@bigasterisk.com>
parents:
818
diff
changeset
|
22 |
9cf01cee74f6
garage: videoselect and shiftbrite move from parallel to arduino
drewp <drewp@bigasterisk.com>
parents:
818
diff
changeset
|
23 def rgbFromHex(h): |
9cf01cee74f6
garage: videoselect and shiftbrite move from parallel to arduino
drewp <drewp@bigasterisk.com>
parents:
818
diff
changeset
|
24 """returns tuple of 0..1023""" |
9cf01cee74f6
garage: videoselect and shiftbrite move from parallel to arduino
drewp <drewp@bigasterisk.com>
parents:
818
diff
changeset
|
25 norm = hex_to_rgb(h) |
9cf01cee74f6
garage: videoselect and shiftbrite move from parallel to arduino
drewp <drewp@bigasterisk.com>
parents:
818
diff
changeset
|
26 return tuple([x * 4 for x in norm]) |
9cf01cee74f6
garage: videoselect and shiftbrite move from parallel to arduino
drewp <drewp@bigasterisk.com>
parents:
818
diff
changeset
|
27 |
9cf01cee74f6
garage: videoselect and shiftbrite move from parallel to arduino
drewp <drewp@bigasterisk.com>
parents:
818
diff
changeset
|
28 def hexFromRgb(rgb): |
9cf01cee74f6
garage: videoselect and shiftbrite move from parallel to arduino
drewp <drewp@bigasterisk.com>
parents:
818
diff
changeset
|
29 return rgb_to_hex(tuple([x // 4 for x in rgb])) |
810
44e1ca03ddf1
move garage door opener from parport to arduino
drewp <drewp@bigasterisk.com>
parents:
809
diff
changeset
|
30 |
805 | 31 |
32 ROOM = Namespace("http://projects.bigasterisk.com/room/") | |
33 DEV = Namespace("http://projects.bigasterisk.com/device/") | |
34 | |
35 class ArduinoGarage(object): | |
36 def __init__(self, port='/dev/ttyACM0'): | |
37 self.ser = LoggingSerial(port=port, baudrate=115200, timeout=1) | |
38 self.ser.flush() | |
39 | |
40 def ping(self): | |
41 self.ser.write("\x60\x00\x00") | |
42 msg = self.ser.readJson() | |
43 assert msg == {"ok":True}, msg | |
44 | |
45 def poll(self): | |
46 self.ser.write("\x60\x01\x00") | |
47 ret = self.ser.readJson() | |
48 return ret | |
49 | |
50 def lastLevel(self): | |
51 self.ser.write("\x60\x02\x00") | |
52 return self.ser.readJson()['z'] | |
53 | |
54 def setThreshold(self, t): | |
55 """set 10-bit threshold""" | |
56 self.ser.write("\x60\x03"+chr(max(1 << 2, t) >> 2)) | |
57 return self.ser.readJson()['threshold'] | |
58 | |
810
44e1ca03ddf1
move garage door opener from parport to arduino
drewp <drewp@bigasterisk.com>
parents:
809
diff
changeset
|
59 def setGarage(self, level): |
44e1ca03ddf1
move garage door opener from parport to arduino
drewp <drewp@bigasterisk.com>
parents:
809
diff
changeset
|
60 """set garage door opener pin""" |
44e1ca03ddf1
move garage door opener from parport to arduino
drewp <drewp@bigasterisk.com>
parents:
809
diff
changeset
|
61 self.ser.write("\x60\x04"+chr(int(bool(level)))) |
44e1ca03ddf1
move garage door opener from parport to arduino
drewp <drewp@bigasterisk.com>
parents:
809
diff
changeset
|
62 return self.ser.readJson()['garage'] |
44e1ca03ddf1
move garage door opener from parport to arduino
drewp <drewp@bigasterisk.com>
parents:
809
diff
changeset
|
63 |
839
9cf01cee74f6
garage: videoselect and shiftbrite move from parallel to arduino
drewp <drewp@bigasterisk.com>
parents:
818
diff
changeset
|
64 def setVideoSelect(self, chan): |
9cf01cee74f6
garage: videoselect and shiftbrite move from parallel to arduino
drewp <drewp@bigasterisk.com>
parents:
818
diff
changeset
|
65 """set video select bits from 0..3""" |
9cf01cee74f6
garage: videoselect and shiftbrite move from parallel to arduino
drewp <drewp@bigasterisk.com>
parents:
818
diff
changeset
|
66 self.ser.write("\x60\x05"+chr(chan)) |
9cf01cee74f6
garage: videoselect and shiftbrite move from parallel to arduino
drewp <drewp@bigasterisk.com>
parents:
818
diff
changeset
|
67 return self.ser.readJson()['videoSelect'] |
9cf01cee74f6
garage: videoselect and shiftbrite move from parallel to arduino
drewp <drewp@bigasterisk.com>
parents:
818
diff
changeset
|
68 |
9cf01cee74f6
garage: videoselect and shiftbrite move from parallel to arduino
drewp <drewp@bigasterisk.com>
parents:
818
diff
changeset
|
69 def shiftbrite(self, colors): |
9cf01cee74f6
garage: videoselect and shiftbrite move from parallel to arduino
drewp <drewp@bigasterisk.com>
parents:
818
diff
changeset
|
70 """ |
9cf01cee74f6
garage: videoselect and shiftbrite move from parallel to arduino
drewp <drewp@bigasterisk.com>
parents:
818
diff
changeset
|
71 shift out this sequence of (r,g,b) triples of 10-bit ints |
9cf01cee74f6
garage: videoselect and shiftbrite move from parallel to arduino
drewp <drewp@bigasterisk.com>
parents:
818
diff
changeset
|
72 """ |
9cf01cee74f6
garage: videoselect and shiftbrite move from parallel to arduino
drewp <drewp@bigasterisk.com>
parents:
818
diff
changeset
|
73 current = "".join(bitstring.pack("0b01, uint:10, uint:10, uint:10", |
9cf01cee74f6
garage: videoselect and shiftbrite move from parallel to arduino
drewp <drewp@bigasterisk.com>
parents:
818
diff
changeset
|
74 127, 127, 127).bytes |
9cf01cee74f6
garage: videoselect and shiftbrite move from parallel to arduino
drewp <drewp@bigasterisk.com>
parents:
818
diff
changeset
|
75 for loop in range(len(colors))) |
9cf01cee74f6
garage: videoselect and shiftbrite move from parallel to arduino
drewp <drewp@bigasterisk.com>
parents:
818
diff
changeset
|
76 out = "".join(bitstring.pack("0b00, uint:10, uint:10, uint:10", |
9cf01cee74f6
garage: videoselect and shiftbrite move from parallel to arduino
drewp <drewp@bigasterisk.com>
parents:
818
diff
changeset
|
77 b, r, g).bytes |
9cf01cee74f6
garage: videoselect and shiftbrite move from parallel to arduino
drewp <drewp@bigasterisk.com>
parents:
818
diff
changeset
|
78 for r,g,b in colors) |
9cf01cee74f6
garage: videoselect and shiftbrite move from parallel to arduino
drewp <drewp@bigasterisk.com>
parents:
818
diff
changeset
|
79 out = current + out |
9cf01cee74f6
garage: videoselect and shiftbrite move from parallel to arduino
drewp <drewp@bigasterisk.com>
parents:
818
diff
changeset
|
80 self.ser.write("\x60\x06" + chr(len(out)) + out) |
9cf01cee74f6
garage: videoselect and shiftbrite move from parallel to arduino
drewp <drewp@bigasterisk.com>
parents:
818
diff
changeset
|
81 msg = self.ser.readJson() |
9cf01cee74f6
garage: videoselect and shiftbrite move from parallel to arduino
drewp <drewp@bigasterisk.com>
parents:
818
diff
changeset
|
82 assert msg == {"ok":1}, msg |
805 | 83 |
84 class Index(PrettyErrorHandler, cyclone.web.RequestHandler): | |
85 def get(self): | |
86 """ | |
87 this is an acceptable status check since it makes a round-trip | |
88 to the arduino before returning success | |
89 """ | |
90 self.settings.arduino.ping() | |
91 | |
92 self.set_header("Content-Type", "application/xhtml+xml") | |
93 self.write(open("index.html").read()) | |
94 | |
95 class GraphPage(PrettyErrorHandler, cyclone.web.RequestHandler): | |
96 def get(self): | |
97 self.set_header("Content-Type", "application/x-trig") | |
98 g = StateGraph(ROOM['garageArduino']) | |
99 self.settings.poller.assertIsCurrent() | |
100 g.add((DEV['frontDoorMotion'], ROOM['state'], | |
101 ROOM['motion'] if self.settings.poller.lastValues['motion'] else | |
102 ROOM['noMotion'])) | |
103 g.add((ROOM['house'], ROOM['usingPower'], | |
104 Literal(self.settings.poller.lastWatts, datatype=ROOM["#watts"]))) | |
105 self.write(g.asTrig()) | |
106 | |
107 class FrontDoorMotion(PrettyErrorHandler, cyclone.web.RequestHandler): | |
108 def get(self): | |
109 self.set_header("Content-Type", "application/javascript") | |
110 self.settings.poller.assertIsCurrent() | |
111 self.write(json.dumps({"frontDoorMotion" : | |
112 self.settings.poller.lastValues['motion']})) | |
113 | |
114 class HousePower(PrettyErrorHandler, cyclone.web.RequestHandler): | |
115 def get(self): | |
116 self.set_header("Content-Type", "application/javascript") | |
117 self.settings.poller.assertIsCurrent() | |
118 w = self.settings.poller | |
119 self.write(json.dumps({ | |
120 "currentWatts" : round(w.lastWatts, 2) if isinstance(w.lastWatts, float) else w.lastWatts, | |
121 "lastPulseAgo" : "%.1f sec ago" % (time.time() - w.lastBlinkTime) if w.lastBlinkTime is not None else "unknown", | |
122 "kwhPerBlink" : w.kwhPerBlink})) | |
123 | |
124 class HousePowerRaw(PrettyErrorHandler, cyclone.web.RequestHandler): | |
125 """ | |
126 raw data from the analog sensor, for plotting or picking a noise threshold | |
127 """ | |
128 def get(self): | |
129 self.set_header("Content-Type", "application/javascript") | |
809
bebb8f7c5a3e
move a bunch of services into this tree, give them all web status pages
drewp <drewp@bigasterisk.com>
parents:
805
diff
changeset
|
130 pts = [] |
bebb8f7c5a3e
move a bunch of services into this tree, give them all web status pages
drewp <drewp@bigasterisk.com>
parents:
805
diff
changeset
|
131 for i in range(60): |
bebb8f7c5a3e
move a bunch of services into this tree, give them all web status pages
drewp <drewp@bigasterisk.com>
parents:
805
diff
changeset
|
132 level = self.settings.arduino.lastLevel() |
bebb8f7c5a3e
move a bunch of services into this tree, give them all web status pages
drewp <drewp@bigasterisk.com>
parents:
805
diff
changeset
|
133 pts.append((round(time.time(), 3), level)) |
bebb8f7c5a3e
move a bunch of services into this tree, give them all web status pages
drewp <drewp@bigasterisk.com>
parents:
805
diff
changeset
|
134 |
bebb8f7c5a3e
move a bunch of services into this tree, give them all web status pages
drewp <drewp@bigasterisk.com>
parents:
805
diff
changeset
|
135 self.write(json.dumps({"irLevels" : pts})) |
805 | 136 |
137 class HousePowerThreshold(PrettyErrorHandler, cyclone.web.RequestHandler): | |
138 """ | |
139 the level that's between between an IR pulse and the noise | |
140 """ | |
141 def get(self): | |
142 self.set_header("Content-Type", "application/javascript") | |
143 self.settings.poller.assertIsCurrent() | |
144 self.write(json.dumps({"threshold" : thr})) | |
145 | |
146 def put(self): | |
147 pass | |
148 | |
810
44e1ca03ddf1
move garage door opener from parport to arduino
drewp <drewp@bigasterisk.com>
parents:
809
diff
changeset
|
149 class GarageDoorOpen(PrettyErrorHandler, cyclone.web.RequestHandler): |
44e1ca03ddf1
move garage door opener from parport to arduino
drewp <drewp@bigasterisk.com>
parents:
809
diff
changeset
|
150 def post(self): |
44e1ca03ddf1
move garage door opener from parport to arduino
drewp <drewp@bigasterisk.com>
parents:
809
diff
changeset
|
151 self.set_header("Content-Type", "text/plain") |
44e1ca03ddf1
move garage door opener from parport to arduino
drewp <drewp@bigasterisk.com>
parents:
809
diff
changeset
|
152 self.settings.arduino.setGarage(True) |
44e1ca03ddf1
move garage door opener from parport to arduino
drewp <drewp@bigasterisk.com>
parents:
809
diff
changeset
|
153 self.write("pin high, waiting..\n") |
44e1ca03ddf1
move garage door opener from parport to arduino
drewp <drewp@bigasterisk.com>
parents:
809
diff
changeset
|
154 self.flush() |
44e1ca03ddf1
move garage door opener from parport to arduino
drewp <drewp@bigasterisk.com>
parents:
809
diff
changeset
|
155 d = defer.Deferred() |
44e1ca03ddf1
move garage door opener from parport to arduino
drewp <drewp@bigasterisk.com>
parents:
809
diff
changeset
|
156 def finish(): |
44e1ca03ddf1
move garage door opener from parport to arduino
drewp <drewp@bigasterisk.com>
parents:
809
diff
changeset
|
157 self.settings.arduino.setGarage(False) |
44e1ca03ddf1
move garage door opener from parport to arduino
drewp <drewp@bigasterisk.com>
parents:
809
diff
changeset
|
158 self.write("pin low. Done") |
44e1ca03ddf1
move garage door opener from parport to arduino
drewp <drewp@bigasterisk.com>
parents:
809
diff
changeset
|
159 d.callback(None) |
44e1ca03ddf1
move garage door opener from parport to arduino
drewp <drewp@bigasterisk.com>
parents:
809
diff
changeset
|
160 reactor.callLater(1.5, finish) # this time depends on the LP circuit |
44e1ca03ddf1
move garage door opener from parport to arduino
drewp <drewp@bigasterisk.com>
parents:
809
diff
changeset
|
161 return d |
805 | 162 |
839
9cf01cee74f6
garage: videoselect and shiftbrite move from parallel to arduino
drewp <drewp@bigasterisk.com>
parents:
818
diff
changeset
|
163 class VideoSelect(PrettyErrorHandler, cyclone.web.RequestHandler): |
9cf01cee74f6
garage: videoselect and shiftbrite move from parallel to arduino
drewp <drewp@bigasterisk.com>
parents:
818
diff
changeset
|
164 def post(self): |
9cf01cee74f6
garage: videoselect and shiftbrite move from parallel to arduino
drewp <drewp@bigasterisk.com>
parents:
818
diff
changeset
|
165 self.set_header("Content-Type", "application/javascript") |
9cf01cee74f6
garage: videoselect and shiftbrite move from parallel to arduino
drewp <drewp@bigasterisk.com>
parents:
818
diff
changeset
|
166 v = self.settings.arduino.setVideoSelect(int(self.request.body)) |
9cf01cee74f6
garage: videoselect and shiftbrite move from parallel to arduino
drewp <drewp@bigasterisk.com>
parents:
818
diff
changeset
|
167 self.write(json.dumps({"videoSelect" : v})) |
9cf01cee74f6
garage: videoselect and shiftbrite move from parallel to arduino
drewp <drewp@bigasterisk.com>
parents:
818
diff
changeset
|
168 |
9cf01cee74f6
garage: videoselect and shiftbrite move from parallel to arduino
drewp <drewp@bigasterisk.com>
parents:
818
diff
changeset
|
169 class Brite(PrettyErrorHandler, cyclone.web.RequestHandler): |
9cf01cee74f6
garage: videoselect and shiftbrite move from parallel to arduino
drewp <drewp@bigasterisk.com>
parents:
818
diff
changeset
|
170 def get(self, chan): |
9cf01cee74f6
garage: videoselect and shiftbrite move from parallel to arduino
drewp <drewp@bigasterisk.com>
parents:
818
diff
changeset
|
171 self.set_header("Content-Type", "text/plain") |
9cf01cee74f6
garage: videoselect and shiftbrite move from parallel to arduino
drewp <drewp@bigasterisk.com>
parents:
818
diff
changeset
|
172 self.write(hexFromRgb(self.settings.colors[int(chan)])) |
9cf01cee74f6
garage: videoselect and shiftbrite move from parallel to arduino
drewp <drewp@bigasterisk.com>
parents:
818
diff
changeset
|
173 |
9cf01cee74f6
garage: videoselect and shiftbrite move from parallel to arduino
drewp <drewp@bigasterisk.com>
parents:
818
diff
changeset
|
174 def put(self, chan): |
9cf01cee74f6
garage: videoselect and shiftbrite move from parallel to arduino
drewp <drewp@bigasterisk.com>
parents:
818
diff
changeset
|
175 s = self.settings |
9cf01cee74f6
garage: videoselect and shiftbrite move from parallel to arduino
drewp <drewp@bigasterisk.com>
parents:
818
diff
changeset
|
176 s.colors[int(chan)] = rgbFromHex(self.request.body) |
9cf01cee74f6
garage: videoselect and shiftbrite move from parallel to arduino
drewp <drewp@bigasterisk.com>
parents:
818
diff
changeset
|
177 s.arduino.shiftbrite(s.colors) |
9cf01cee74f6
garage: videoselect and shiftbrite move from parallel to arduino
drewp <drewp@bigasterisk.com>
parents:
818
diff
changeset
|
178 post = put |
9cf01cee74f6
garage: videoselect and shiftbrite move from parallel to arduino
drewp <drewp@bigasterisk.com>
parents:
818
diff
changeset
|
179 |
805 | 180 class Application(cyclone.web.Application): |
181 def __init__(self, ard, poller): | |
182 handlers = [ | |
183 (r"/", Index), | |
184 (r"/graph", GraphPage), | |
185 (r"/frontDoorMotion", FrontDoorMotion), | |
186 (r'/housePower', HousePower), | |
809
bebb8f7c5a3e
move a bunch of services into this tree, give them all web status pages
drewp <drewp@bigasterisk.com>
parents:
805
diff
changeset
|
187 (r'/housePower/raw', HousePowerRaw), |
bebb8f7c5a3e
move a bunch of services into this tree, give them all web status pages
drewp <drewp@bigasterisk.com>
parents:
805
diff
changeset
|
188 (r'/housePower/threshold', HousePowerThreshold), |
810
44e1ca03ddf1
move garage door opener from parport to arduino
drewp <drewp@bigasterisk.com>
parents:
809
diff
changeset
|
189 (r'/garageDoorOpen', GarageDoorOpen), |
839
9cf01cee74f6
garage: videoselect and shiftbrite move from parallel to arduino
drewp <drewp@bigasterisk.com>
parents:
818
diff
changeset
|
190 (r'/videoSelect', VideoSelect), |
9cf01cee74f6
garage: videoselect and shiftbrite move from parallel to arduino
drewp <drewp@bigasterisk.com>
parents:
818
diff
changeset
|
191 (r"/brite/(\d+)", Brite), |
805 | 192 ] |
839
9cf01cee74f6
garage: videoselect and shiftbrite move from parallel to arduino
drewp <drewp@bigasterisk.com>
parents:
818
diff
changeset
|
193 colors = [(0,0,0)] * 1 # stored 10-bit |
9cf01cee74f6
garage: videoselect and shiftbrite move from parallel to arduino
drewp <drewp@bigasterisk.com>
parents:
818
diff
changeset
|
194 settings = {"arduino" : ard, "poller" : poller, "colors" : colors} |
805 | 195 cyclone.web.Application.__init__(self, handlers, **settings) |
196 | |
197 class Poller(object): | |
198 """ | |
199 times the blinks to estimate power usage. Captures the other | |
200 returned sensor values too in self.lastValues | |
201 """ | |
202 def __init__(self, ard, period): | |
203 self.ard = ard | |
204 self.period = period | |
205 self.carbon = CarbonClient(serverHost='bang') | |
206 self.lastBlinkTime = None | |
207 self.lastValues = None | |
208 self.lastPollTime = 0 | |
209 self.lastWatts = "(just restarted; wait no data yet)" | |
210 self.kwhPerBlink = 1.0 # unsure | |
211 self.lastMotion = False | |
212 | |
213 def assertIsCurrent(self): | |
214 """raise an error if the poll data is not fresh""" | |
215 dt = time.time() - self.lastPollTime | |
216 if dt > period * 2: | |
217 raise ValueError("last poll time was too old: %.1f sec ago" % dt) | |
218 | |
219 def poll(self): | |
220 now = time.time() | |
221 try: | |
222 try: | |
223 newData = ard.poll() | |
224 except ValueError, e: | |
225 print e | |
226 else: | |
227 self.lastPollTime = now | |
228 self.lastValues = newData # for other data besides the blinks | |
229 self.processBlinks(now, newData['newBlinks']) | |
230 self.processMotion(newData['motion']) | |
231 | |
232 except (IOError, OSError): | |
233 os.abort() | |
234 except Exception, e: | |
235 print "poll error", e | |
236 traceback.print_exc() | |
237 | |
238 def processBlinks(self, now, b): | |
239 if b > 0: | |
240 if b > 1: | |
241 # todo: if it's like 1,1,2,2,2,2,1,1 then we | |
242 # need to subdivide those inner sample periods | |
243 # since there might really be two blinks. But | |
244 # if it's like 0,0,0,2,0,0, that should be | |
245 # treated like b=1 since it's probably noise | |
246 pass | |
247 | |
248 if self.lastBlinkTime is not None: | |
249 dt = now - self.lastBlinkTime | |
250 dth = dt / 3600. | |
251 watts = self.kwhPerBlink / dth | |
252 | |
253 if watts > 10000: | |
254 # this pulse (or the previous one) is | |
255 # likely noise. Too late for the previous | |
256 # one, but we're going to skip this one | |
257 return | |
258 else: | |
259 self.lastWatts = watts | |
260 | |
261 # todo: remove this; a separate logger shall do it | |
262 self.carbon.send('system.house.powerMeter_w', watts, now) | |
263 | |
264 self.lastBlinkTime = now | |
265 | |
266 def processMotion(self, state): | |
267 if state == self.lastMotion: | |
268 return | |
269 self.lastMotion = state | |
270 msg = json.dumps(dict(board='garage', | |
271 name="frontDoorMotion", state=state)) | |
272 getPage('http://bang.bigasterisk.com:9069/inputChange', | |
273 method="POST", | |
274 postdata=msg, | |
275 headers={'Content-Type' : 'application/json'} | |
276 ).addErrback(self.reportError, msg) | |
277 | |
278 def reportError(self, msg, *args): | |
279 print "post error", msg, args | |
280 | |
281 if __name__ == '__main__': | |
282 | |
283 config = { # to be read from a file | |
818 | 284 'arduinoPort': '/dev/serial/by-id/usb-Arduino__www.arduino.cc__Arduino_Uno_6493534323335161A2F1-if00', |
805 | 285 'servePort' : 9050, |
286 'pollFrequency' : 5, | |
287 'boardName' : 'garage', # gets sent with updates | |
288 } | |
289 | |
810
44e1ca03ddf1
move garage door opener from parport to arduino
drewp <drewp@bigasterisk.com>
parents:
809
diff
changeset
|
290 #from twisted.python import log as twlog |
44e1ca03ddf1
move garage door opener from parport to arduino
drewp <drewp@bigasterisk.com>
parents:
809
diff
changeset
|
291 #twlog.startLogging(sys.stdout) |
44e1ca03ddf1
move garage door opener from parport to arduino
drewp <drewp@bigasterisk.com>
parents:
809
diff
changeset
|
292 |
44e1ca03ddf1
move garage door opener from parport to arduino
drewp <drewp@bigasterisk.com>
parents:
809
diff
changeset
|
293 log.setLevel(logging.DEBUG) |
805 | 294 |
818 | 295 ard = ArduinoGarage(port=config['arduinoPort']) |
805 | 296 |
297 period = 1/config['pollFrequency'] | |
298 p = Poller(ard, period) | |
299 task.LoopingCall(p.poll).start(period) | |
300 reactor.listenTCP(config['servePort'], Application(ard, p)) | |
301 reactor.run() |