annotate service/garageArduino/garageArduino.py @ 44:55b68d1e8212

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