annotate service/garageArduino/garageArduino.py @ 997:b24885725f59

support for pwm board Ignore-this: 4bf59699c590c75c79e6ae72d719343a darcs-hash:20150830081929-312f9-45f7134e807c5de2bb0dad13a5a9d1fa032bf471
author drewp <drewp@bigasterisk.com>
date Sun, 30 Aug 2015 01:19:29 -0700
parents 7d4dec166822
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
805
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
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
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
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
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
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
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)
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
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
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
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
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
drewp <drewp@bigasterisk.com>
parents: 839
diff changeset
76 resetCurrent = "".join(bitstring.pack("0b01, uint:10, uint:10, uint:10",
drewp <drewp@bigasterisk.com>
parents: 839
diff changeset
77 127, 127, 127).bytes
drewp <drewp@bigasterisk.com>
parents: 839
diff changeset
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
drewp <drewp@bigasterisk.com>
parents: 839
diff changeset
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
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
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
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
100 class Index(PrettyErrorHandler, cyclone.web.RequestHandler):
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
101 def get(self):
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
102 """
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
103 this is an acceptable status check since it makes a round-trip
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
104 to the arduino before returning success
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
105 """
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
106 self.settings.arduino.ping()
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
107
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
108 self.set_header("Content-Type", "application/xhtml+xml")
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
109 self.write(open("index.html").read())
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
110
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
111 class GraphPage(PrettyErrorHandler, cyclone.web.RequestHandler):
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
112 def get(self):
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
113 self.set_header("Content-Type", "application/x-trig")
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
114 g = StateGraph(ROOM['garageArduino'])
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
115 self.settings.poller.assertIsCurrent()
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
116 g.add((DEV['frontDoorMotion'], ROOM['state'],
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
117 ROOM['motion'] if self.settings.poller.lastValues['motion'] else
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
118 ROOM['noMotion']))
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
119 g.add((ROOM['house'], ROOM['usingPower'],
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
120 Literal(self.settings.poller.lastWatts, datatype=ROOM["#watts"])))
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
121 self.write(g.asTrig())
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
122
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
123 class FrontDoorMotion(PrettyErrorHandler, cyclone.web.RequestHandler):
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
124 def get(self):
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
125 self.set_header("Content-Type", "application/javascript")
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
126 self.settings.poller.assertIsCurrent()
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
127 self.write(json.dumps({"frontDoorMotion" :
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
128 self.settings.poller.lastValues['motion']}))
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
129
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
130 class HousePower(PrettyErrorHandler, cyclone.web.RequestHandler):
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
131 def get(self):
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
132 self.set_header("Content-Type", "application/javascript")
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
133 self.settings.poller.assertIsCurrent()
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
134 w = self.settings.poller
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
135 self.write(json.dumps({
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
136 "currentWatts" : round(w.lastWatts, 2) if isinstance(w.lastWatts, float) else w.lastWatts,
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
137 "lastPulseAgo" : "%.1f sec ago" % (time.time() - w.lastBlinkTime) if w.lastBlinkTime is not None else "unknown",
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
138 "kwhPerBlink" : w.kwhPerBlink}))
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
139
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
140 class HousePowerRaw(PrettyErrorHandler, cyclone.web.RequestHandler):
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
141 """
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
142 raw data from the analog sensor, for plotting or picking a noise threshold
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
143 """
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
144 def get(self):
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
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
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
152
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
153 class HousePowerThreshold(PrettyErrorHandler, cyclone.web.RequestHandler):
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
154 """
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
155 the level that's between between an IR pulse and the noise
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
156 """
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
157 def get(self):
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
158 self.set_header("Content-Type", "application/javascript")
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
159 self.settings.poller.assertIsCurrent()
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
160 self.write(json.dumps({"threshold" : thr}))
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
161
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
162 def put(self):
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
163 pass
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
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
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
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
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
197 class Application(cyclone.web.Application):
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
198 def __init__(self, ard, poller):
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
199 handlers = [
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
200 (r"/", Index),
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
201 (r"/graph", GraphPage),
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
202 (r"/frontDoorMotion", FrontDoorMotion),
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
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
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
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
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
212 cyclone.web.Application.__init__(self, handlers, **settings)
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
213
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
214 class Poller(object):
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
215 """
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
216 times the blinks to estimate power usage. Captures the other
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
217 returned sensor values too in self.lastValues
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
218 """
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
219 def __init__(self, ard, period):
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
220 self.ard = ard
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
221 self.period = period
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
222 self.carbon = CarbonClient(serverHost='bang')
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
223 self.lastBlinkTime = None
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
224 self.lastValues = None
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
225 self.lastPollTime = 0
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
226 self.lastWatts = "(just restarted; wait no data yet)"
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
227 self.kwhPerBlink = 1.0 # unsure
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
228 self.lastMotion = False
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
229
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
230 def assertIsCurrent(self):
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
231 """raise an error if the poll data is not fresh"""
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
232 dt = time.time() - self.lastPollTime
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
233 if dt > period * 2:
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
234 raise ValueError("last poll time was too old: %.1f sec ago" % dt)
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
235
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
236 def poll(self):
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
237 now = time.time()
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
238 try:
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
239 try:
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
240 newData = ard.poll()
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
241 except ValueError, e:
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
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
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
244 else:
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
245 self.lastPollTime = now
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
246 self.lastValues = newData # for other data besides the blinks
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
247 self.processBlinks(now, newData['newBlinks'])
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
248 self.processMotion(newData['motion'])
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
249
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
250 except (IOError, OSError):
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
251 os.abort()
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
252 except Exception, e:
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
253 print "poll error", e
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
254 traceback.print_exc()
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
255
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
256 def processBlinks(self, now, b):
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
257 if b > 0:
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
258 if b > 1:
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
259 # todo: if it's like 1,1,2,2,2,2,1,1 then we
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
260 # need to subdivide those inner sample periods
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
261 # since there might really be two blinks. But
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
262 # if it's like 0,0,0,2,0,0, that should be
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
263 # treated like b=1 since it's probably noise
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
264 pass
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
265
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
266 if self.lastBlinkTime is not None:
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
267 dt = now - self.lastBlinkTime
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
268 dth = dt / 3600.
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
269 watts = self.kwhPerBlink / dth
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
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
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
272 # this pulse (or the previous one) is
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
273 # likely noise. Too late for the previous
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
274 # one, but we're going to skip this one
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
275 return
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
276 else:
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
277 self.lastWatts = watts
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
278
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
279 # todo: remove this; a separate logger shall do it
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
280 self.carbon.send('system.house.powerMeter_w', watts, now)
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
281
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
282 self.lastBlinkTime = now
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
283
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
284 def processMotion(self, state):
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
285 if state == self.lastMotion:
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
286 return
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
287 self.lastMotion = state
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
288 msg = json.dumps(dict(board='garage',
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
289 name="frontDoorMotion", state=state))
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
290 getPage('http://bang.bigasterisk.com:9069/inputChange',
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
291 method="POST",
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
292 postdata=msg,
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
293 headers={'Content-Type' : 'application/json'}
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
294 ).addErrback(self.reportError, msg)
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
295
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
296 def reportError(self, msg, *args):
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
297 print "post error", msg, args
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
298
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
299 if __name__ == '__main__':
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
300
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
301 config = { # to be read from a file
818
ccbff2a4b8ce port change
drewp <drewp@bigasterisk.com>
parents: 810
diff changeset
302 'arduinoPort': '/dev/serial/by-id/usb-Arduino__www.arduino.cc__Arduino_Uno_6493534323335161A2F1-if00',
805
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
303 'servePort' : 9050,
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
304 'pollFrequency' : 5,
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
305 'boardName' : 'garage', # gets sent with updates
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
306 }
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
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
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
312
818
ccbff2a4b8ce port change
drewp <drewp@bigasterisk.com>
parents: 810
diff changeset
313 ard = ArduinoGarage(port=config['arduinoPort'])
805
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
314
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
315 period = 1/config['pollFrequency']
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
316 p = Poller(ard, period)
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
317 task.LoopingCall(p.poll).start(period)
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
318 reactor.listenTCP(config['servePort'], Application(ard, p))
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
319 reactor.run()