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