annotate service/bedroomArduino/bedroomArduino.py @ 37:8e55a6a9c425

add shiftbrites to bedroom arduino Ignore-this: c0ce65f20b52679af58b9e31bcc3cf2e
author drewp@bigasterisk.com
date Sat, 25 Aug 2012 14:20:45 -0700
parents 5fef0b7db346
children 1fada3a61c5f
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
29
5fef0b7db346 bedroomarduino project started. web server isn't ready
drewp@bigasterisk.com
parents:
diff changeset
1 #!bin/python
5fef0b7db346 bedroomarduino project started. web server isn't ready
drewp@bigasterisk.com
parents:
diff changeset
2 """
5fef0b7db346 bedroomarduino project started. web server isn't ready
drewp@bigasterisk.com
parents:
diff changeset
3 talks to bed.pde on an arduino
5fef0b7db346 bedroomarduino project started. web server isn't ready
drewp@bigasterisk.com
parents:
diff changeset
4 """
5fef0b7db346 bedroomarduino project started. web server isn't ready
drewp@bigasterisk.com
parents:
diff changeset
5
5fef0b7db346 bedroomarduino project started. web server isn't ready
drewp@bigasterisk.com
parents:
diff changeset
6 from __future__ import division
5fef0b7db346 bedroomarduino project started. web server isn't ready
drewp@bigasterisk.com
parents:
diff changeset
7
37
8e55a6a9c425 add shiftbrites to bedroom arduino
drewp@bigasterisk.com
parents: 29
diff changeset
8 import cyclone.web, json, traceback, os, sys, time, logging, bitstring
29
5fef0b7db346 bedroomarduino project started. web server isn't ready
drewp@bigasterisk.com
parents:
diff changeset
9 from twisted.internet import reactor, task
5fef0b7db346 bedroomarduino project started. web server isn't ready
drewp@bigasterisk.com
parents:
diff changeset
10 from twisted.web.client import getPage
5fef0b7db346 bedroomarduino project started. web server isn't ready
drewp@bigasterisk.com
parents:
diff changeset
11 sys.path.append("/my/proj/house/frontdoor")
5fef0b7db346 bedroomarduino project started. web server isn't ready
drewp@bigasterisk.com
parents:
diff changeset
12 from loggingserial import LoggingSerial
5fef0b7db346 bedroomarduino project started. web server isn't ready
drewp@bigasterisk.com
parents:
diff changeset
13 sys.path.append("/my/proj/homeauto/lib")
5fef0b7db346 bedroomarduino project started. web server isn't ready
drewp@bigasterisk.com
parents:
diff changeset
14 from cycloneerr import PrettyErrorHandler
5fef0b7db346 bedroomarduino project started. web server isn't ready
drewp@bigasterisk.com
parents:
diff changeset
15 from logsetup import log
5fef0b7db346 bedroomarduino project started. web server isn't ready
drewp@bigasterisk.com
parents:
diff changeset
16
37
8e55a6a9c425 add shiftbrites to bedroom arduino
drewp@bigasterisk.com
parents: 29
diff changeset
17 sys.path.append("/my/proj/pixel/shiftweb")
8e55a6a9c425 add shiftbrites to bedroom arduino
drewp@bigasterisk.com
parents: 29
diff changeset
18 from shiftweb import hexFromRgb, rgbFromHex
8e55a6a9c425 add shiftbrites to bedroom arduino
drewp@bigasterisk.com
parents: 29
diff changeset
19
29
5fef0b7db346 bedroomarduino project started. web server isn't ready
drewp@bigasterisk.com
parents:
diff changeset
20 sys.path.append("/my/site/magma")
5fef0b7db346 bedroomarduino project started. web server isn't ready
drewp@bigasterisk.com
parents:
diff changeset
21 from stategraph import StateGraph
5fef0b7db346 bedroomarduino project started. web server isn't ready
drewp@bigasterisk.com
parents:
diff changeset
22 from rdflib import Namespace
5fef0b7db346 bedroomarduino project started. web server isn't ready
drewp@bigasterisk.com
parents:
diff changeset
23
5fef0b7db346 bedroomarduino project started. web server isn't ready
drewp@bigasterisk.com
parents:
diff changeset
24 ROOM = Namespace("http://projects.bigasterisk.com/room/")
5fef0b7db346 bedroomarduino project started. web server isn't ready
drewp@bigasterisk.com
parents:
diff changeset
25 DEV = Namespace("http://projects.bigasterisk.com/device/")
5fef0b7db346 bedroomarduino project started. web server isn't ready
drewp@bigasterisk.com
parents:
diff changeset
26
5fef0b7db346 bedroomarduino project started. web server isn't ready
drewp@bigasterisk.com
parents:
diff changeset
27 class ArduinoBedroom(object):
5fef0b7db346 bedroomarduino project started. web server isn't ready
drewp@bigasterisk.com
parents:
diff changeset
28 def __init__(self, port='/dev/ttyACM0'):
5fef0b7db346 bedroomarduino project started. web server isn't ready
drewp@bigasterisk.com
parents:
diff changeset
29 self.ser = LoggingSerial(port=port, baudrate=115200, timeout=1)
5fef0b7db346 bedroomarduino project started. web server isn't ready
drewp@bigasterisk.com
parents:
diff changeset
30 self.ser.flush()
5fef0b7db346 bedroomarduino project started. web server isn't ready
drewp@bigasterisk.com
parents:
diff changeset
31
5fef0b7db346 bedroomarduino project started. web server isn't ready
drewp@bigasterisk.com
parents:
diff changeset
32 def ping(self):
5fef0b7db346 bedroomarduino project started. web server isn't ready
drewp@bigasterisk.com
parents:
diff changeset
33 self.ser.write("\x60\x00\x00")
5fef0b7db346 bedroomarduino project started. web server isn't ready
drewp@bigasterisk.com
parents:
diff changeset
34 msg = self.ser.readJson()
5fef0b7db346 bedroomarduino project started. web server isn't ready
drewp@bigasterisk.com
parents:
diff changeset
35 assert msg == {"ok":True}, msg
5fef0b7db346 bedroomarduino project started. web server isn't ready
drewp@bigasterisk.com
parents:
diff changeset
36
5fef0b7db346 bedroomarduino project started. web server isn't ready
drewp@bigasterisk.com
parents:
diff changeset
37 def poll(self):
5fef0b7db346 bedroomarduino project started. web server isn't ready
drewp@bigasterisk.com
parents:
diff changeset
38 self.ser.write("\x60\x01\x00")
5fef0b7db346 bedroomarduino project started. web server isn't ready
drewp@bigasterisk.com
parents:
diff changeset
39 ret = self.ser.readJson()
37
8e55a6a9c425 add shiftbrites to bedroom arduino
drewp@bigasterisk.com
parents: 29
diff changeset
40 ret['motion'] = int(ret['motion'] > 100)
29
5fef0b7db346 bedroomarduino project started. web server isn't ready
drewp@bigasterisk.com
parents:
diff changeset
41 return ret
5fef0b7db346 bedroomarduino project started. web server isn't ready
drewp@bigasterisk.com
parents:
diff changeset
42
5fef0b7db346 bedroomarduino project started. web server isn't ready
drewp@bigasterisk.com
parents:
diff changeset
43 def setSpeakerChoice(self, pillow):
5fef0b7db346 bedroomarduino project started. web server isn't ready
drewp@bigasterisk.com
parents:
diff changeset
44 self.ser.write("\x60\x02" + chr(pillow))
37
8e55a6a9c425 add shiftbrites to bedroom arduino
drewp@bigasterisk.com
parents: 29
diff changeset
45 return self.ser.readJson()
8e55a6a9c425 add shiftbrites to bedroom arduino
drewp@bigasterisk.com
parents: 29
diff changeset
46
8e55a6a9c425 add shiftbrites to bedroom arduino
drewp@bigasterisk.com
parents: 29
diff changeset
47 def setLeds(self, colors):
8e55a6a9c425 add shiftbrites to bedroom arduino
drewp@bigasterisk.com
parents: 29
diff changeset
48 """
8e55a6a9c425 add shiftbrites to bedroom arduino
drewp@bigasterisk.com
parents: 29
diff changeset
49 shift out this sequence of (r,g,b) triples of 10-bit ints
8e55a6a9c425 add shiftbrites to bedroom arduino
drewp@bigasterisk.com
parents: 29
diff changeset
50 The nearest led gets color[0], etc.
8e55a6a9c425 add shiftbrites to bedroom arduino
drewp@bigasterisk.com
parents: 29
diff changeset
51 """
8e55a6a9c425 add shiftbrites to bedroom arduino
drewp@bigasterisk.com
parents: 29
diff changeset
52 resetCurrent = "".join(bitstring.pack("0b01, uint:10, uint:10, uint:10",
8e55a6a9c425 add shiftbrites to bedroom arduino
drewp@bigasterisk.com
parents: 29
diff changeset
53 127, 127, 127).bytes
8e55a6a9c425 add shiftbrites to bedroom arduino
drewp@bigasterisk.com
parents: 29
diff changeset
54 for loop in range(len(colors)))
8e55a6a9c425 add shiftbrites to bedroom arduino
drewp@bigasterisk.com
parents: 29
diff changeset
55 out = "".join(bitstring.pack("0b00, uint:10, uint:10, uint:10",
8e55a6a9c425 add shiftbrites to bedroom arduino
drewp@bigasterisk.com
parents: 29
diff changeset
56 b, r, g).bytes
8e55a6a9c425 add shiftbrites to bedroom arduino
drewp@bigasterisk.com
parents: 29
diff changeset
57 for r,g,b in reversed(colors))
8e55a6a9c425 add shiftbrites to bedroom arduino
drewp@bigasterisk.com
parents: 29
diff changeset
58 out = resetCurrent + out
8e55a6a9c425 add shiftbrites to bedroom arduino
drewp@bigasterisk.com
parents: 29
diff changeset
59 self.ser.write("\x60\x03" + chr(len(out)) + out)
8e55a6a9c425 add shiftbrites to bedroom arduino
drewp@bigasterisk.com
parents: 29
diff changeset
60 msg = self.ser.readJson()
8e55a6a9c425 add shiftbrites to bedroom arduino
drewp@bigasterisk.com
parents: 29
diff changeset
61 assert msg == {"ok":1}, msg
29
5fef0b7db346 bedroomarduino project started. web server isn't ready
drewp@bigasterisk.com
parents:
diff changeset
62
5fef0b7db346 bedroomarduino project started. web server isn't ready
drewp@bigasterisk.com
parents:
diff changeset
63 class Index(PrettyErrorHandler, cyclone.web.RequestHandler):
5fef0b7db346 bedroomarduino project started. web server isn't ready
drewp@bigasterisk.com
parents:
diff changeset
64 def get(self):
5fef0b7db346 bedroomarduino project started. web server isn't ready
drewp@bigasterisk.com
parents:
diff changeset
65 """
5fef0b7db346 bedroomarduino project started. web server isn't ready
drewp@bigasterisk.com
parents:
diff changeset
66 this is an acceptable status check since it makes a round-trip
5fef0b7db346 bedroomarduino project started. web server isn't ready
drewp@bigasterisk.com
parents:
diff changeset
67 to the arduino before returning success
5fef0b7db346 bedroomarduino project started. web server isn't ready
drewp@bigasterisk.com
parents:
diff changeset
68 """
5fef0b7db346 bedroomarduino project started. web server isn't ready
drewp@bigasterisk.com
parents:
diff changeset
69 self.settings.arduino.ping()
5fef0b7db346 bedroomarduino project started. web server isn't ready
drewp@bigasterisk.com
parents:
diff changeset
70
5fef0b7db346 bedroomarduino project started. web server isn't ready
drewp@bigasterisk.com
parents:
diff changeset
71 self.set_header("Content-Type", "application/xhtml+xml")
5fef0b7db346 bedroomarduino project started. web server isn't ready
drewp@bigasterisk.com
parents:
diff changeset
72 self.write(open("index.html").read())
5fef0b7db346 bedroomarduino project started. web server isn't ready
drewp@bigasterisk.com
parents:
diff changeset
73
5fef0b7db346 bedroomarduino project started. web server isn't ready
drewp@bigasterisk.com
parents:
diff changeset
74 class SpeakerChoice(PrettyErrorHandler, cyclone.web.RequestHandler):
5fef0b7db346 bedroomarduino project started. web server isn't ready
drewp@bigasterisk.com
parents:
diff changeset
75 def put(self):
5fef0b7db346 bedroomarduino project started. web server isn't ready
drewp@bigasterisk.com
parents:
diff changeset
76 ret = self.settings.arduino.setSpeakerChoice(int(self.get_argument('pillow')))
5fef0b7db346 bedroomarduino project started. web server isn't ready
drewp@bigasterisk.com
parents:
diff changeset
77 self.write(ret)
5fef0b7db346 bedroomarduino project started. web server isn't ready
drewp@bigasterisk.com
parents:
diff changeset
78
37
8e55a6a9c425 add shiftbrites to bedroom arduino
drewp@bigasterisk.com
parents: 29
diff changeset
79 class Brite(PrettyErrorHandler, cyclone.web.RequestHandler):
8e55a6a9c425 add shiftbrites to bedroom arduino
drewp@bigasterisk.com
parents: 29
diff changeset
80 def put(self, which):
8e55a6a9c425 add shiftbrites to bedroom arduino
drewp@bigasterisk.com
parents: 29
diff changeset
81 which = int(which)
8e55a6a9c425 add shiftbrites to bedroom arduino
drewp@bigasterisk.com
parents: 29
diff changeset
82 brites = self.settings.brites
8e55a6a9c425 add shiftbrites to bedroom arduino
drewp@bigasterisk.com
parents: 29
diff changeset
83 if which + 1 > len(brites):
8e55a6a9c425 add shiftbrites to bedroom arduino
drewp@bigasterisk.com
parents: 29
diff changeset
84 brites.extend([(0,0,0)] * (which + 1 - len(brites)))
8e55a6a9c425 add shiftbrites to bedroom arduino
drewp@bigasterisk.com
parents: 29
diff changeset
85 brites[which] = rgbFromHex(self.request.body)
8e55a6a9c425 add shiftbrites to bedroom arduino
drewp@bigasterisk.com
parents: 29
diff changeset
86
8e55a6a9c425 add shiftbrites to bedroom arduino
drewp@bigasterisk.com
parents: 29
diff changeset
87 self.settings.arduino.setLeds(brites)
8e55a6a9c425 add shiftbrites to bedroom arduino
drewp@bigasterisk.com
parents: 29
diff changeset
88 self.set_header("Content-Type", "text/plain")
8e55a6a9c425 add shiftbrites to bedroom arduino
drewp@bigasterisk.com
parents: 29
diff changeset
89 self.write("ok")
8e55a6a9c425 add shiftbrites to bedroom arduino
drewp@bigasterisk.com
parents: 29
diff changeset
90
29
5fef0b7db346 bedroomarduino project started. web server isn't ready
drewp@bigasterisk.com
parents:
diff changeset
91 class GraphPage(PrettyErrorHandler, cyclone.web.RequestHandler):
5fef0b7db346 bedroomarduino project started. web server isn't ready
drewp@bigasterisk.com
parents:
diff changeset
92 def get(self):
5fef0b7db346 bedroomarduino project started. web server isn't ready
drewp@bigasterisk.com
parents:
diff changeset
93 self.set_header("Content-Type", "application/x-trig")
5fef0b7db346 bedroomarduino project started. web server isn't ready
drewp@bigasterisk.com
parents:
diff changeset
94 g = StateGraph(ROOM['bedroomArduino'])
5fef0b7db346 bedroomarduino project started. web server isn't ready
drewp@bigasterisk.com
parents:
diff changeset
95 self.settings.poller.assertIsCurrent()
5fef0b7db346 bedroomarduino project started. web server isn't ready
drewp@bigasterisk.com
parents:
diff changeset
96 g.add((DEV['bedroomMotion'], ROOM['state'],
5fef0b7db346 bedroomarduino project started. web server isn't ready
drewp@bigasterisk.com
parents:
diff changeset
97 ROOM['motion'] if self.settings.poller.lastValues['motion'] else
5fef0b7db346 bedroomarduino project started. web server isn't ready
drewp@bigasterisk.com
parents:
diff changeset
98 ROOM['noMotion']))
5fef0b7db346 bedroomarduino project started. web server isn't ready
drewp@bigasterisk.com
parents:
diff changeset
99 self.write(g.asTrig())
5fef0b7db346 bedroomarduino project started. web server isn't ready
drewp@bigasterisk.com
parents:
diff changeset
100
5fef0b7db346 bedroomarduino project started. web server isn't ready
drewp@bigasterisk.com
parents:
diff changeset
101 class Poller(object):
5fef0b7db346 bedroomarduino project started. web server isn't ready
drewp@bigasterisk.com
parents:
diff changeset
102 """
5fef0b7db346 bedroomarduino project started. web server isn't ready
drewp@bigasterisk.com
parents:
diff changeset
103 Watches sensor values
5fef0b7db346 bedroomarduino project started. web server isn't ready
drewp@bigasterisk.com
parents:
diff changeset
104 """
5fef0b7db346 bedroomarduino project started. web server isn't ready
drewp@bigasterisk.com
parents:
diff changeset
105 def __init__(self, config, ard, period):
5fef0b7db346 bedroomarduino project started. web server isn't ready
drewp@bigasterisk.com
parents:
diff changeset
106 self.config, self.ard = config, ard
5fef0b7db346 bedroomarduino project started. web server isn't ready
drewp@bigasterisk.com
parents:
diff changeset
107 self.period = period
5fef0b7db346 bedroomarduino project started. web server isn't ready
drewp@bigasterisk.com
parents:
diff changeset
108 self.lastValues = None
5fef0b7db346 bedroomarduino project started. web server isn't ready
drewp@bigasterisk.com
parents:
diff changeset
109 self.lastPollTime = 0
37
8e55a6a9c425 add shiftbrites to bedroom arduino
drewp@bigasterisk.com
parents: 29
diff changeset
110 self.lastMotion = None
29
5fef0b7db346 bedroomarduino project started. web server isn't ready
drewp@bigasterisk.com
parents:
diff changeset
111
5fef0b7db346 bedroomarduino project started. web server isn't ready
drewp@bigasterisk.com
parents:
diff changeset
112 def assertIsCurrent(self):
5fef0b7db346 bedroomarduino project started. web server isn't ready
drewp@bigasterisk.com
parents:
diff changeset
113 """raise an error if the poll data is not fresh"""
5fef0b7db346 bedroomarduino project started. web server isn't ready
drewp@bigasterisk.com
parents:
diff changeset
114 dt = time.time() - self.lastPollTime
5fef0b7db346 bedroomarduino project started. web server isn't ready
drewp@bigasterisk.com
parents:
diff changeset
115 if dt > period * 2:
5fef0b7db346 bedroomarduino project started. web server isn't ready
drewp@bigasterisk.com
parents:
diff changeset
116 raise ValueError("last poll time was too old: %.1f sec ago" % dt)
5fef0b7db346 bedroomarduino project started. web server isn't ready
drewp@bigasterisk.com
parents:
diff changeset
117
5fef0b7db346 bedroomarduino project started. web server isn't ready
drewp@bigasterisk.com
parents:
diff changeset
118 def poll(self):
5fef0b7db346 bedroomarduino project started. web server isn't ready
drewp@bigasterisk.com
parents:
diff changeset
119 now = time.time()
5fef0b7db346 bedroomarduino project started. web server isn't ready
drewp@bigasterisk.com
parents:
diff changeset
120 try:
5fef0b7db346 bedroomarduino project started. web server isn't ready
drewp@bigasterisk.com
parents:
diff changeset
121 try:
5fef0b7db346 bedroomarduino project started. web server isn't ready
drewp@bigasterisk.com
parents:
diff changeset
122 newData = ard.poll()
5fef0b7db346 bedroomarduino project started. web server isn't ready
drewp@bigasterisk.com
parents:
diff changeset
123 except ValueError, e:
5fef0b7db346 bedroomarduino project started. web server isn't ready
drewp@bigasterisk.com
parents:
diff changeset
124 print e
5fef0b7db346 bedroomarduino project started. web server isn't ready
drewp@bigasterisk.com
parents:
diff changeset
125 else:
37
8e55a6a9c425 add shiftbrites to bedroom arduino
drewp@bigasterisk.com
parents: 29
diff changeset
126 #print newData # for testing
8e55a6a9c425 add shiftbrites to bedroom arduino
drewp@bigasterisk.com
parents: 29
diff changeset
127
29
5fef0b7db346 bedroomarduino project started. web server isn't ready
drewp@bigasterisk.com
parents:
diff changeset
128 self.lastPollTime = now
5fef0b7db346 bedroomarduino project started. web server isn't ready
drewp@bigasterisk.com
parents:
diff changeset
129 self.lastValues = newData # for other data besides the blinks
5fef0b7db346 bedroomarduino project started. web server isn't ready
drewp@bigasterisk.com
parents:
diff changeset
130 self.processMotion(newData['motion'])
5fef0b7db346 bedroomarduino project started. web server isn't ready
drewp@bigasterisk.com
parents:
diff changeset
131
5fef0b7db346 bedroomarduino project started. web server isn't ready
drewp@bigasterisk.com
parents:
diff changeset
132 except (IOError, OSError):
5fef0b7db346 bedroomarduino project started. web server isn't ready
drewp@bigasterisk.com
parents:
diff changeset
133 os.abort()
5fef0b7db346 bedroomarduino project started. web server isn't ready
drewp@bigasterisk.com
parents:
diff changeset
134 except Exception, e:
5fef0b7db346 bedroomarduino project started. web server isn't ready
drewp@bigasterisk.com
parents:
diff changeset
135 print "poll error", e
5fef0b7db346 bedroomarduino project started. web server isn't ready
drewp@bigasterisk.com
parents:
diff changeset
136 traceback.print_exc()
5fef0b7db346 bedroomarduino project started. web server isn't ready
drewp@bigasterisk.com
parents:
diff changeset
137
5fef0b7db346 bedroomarduino project started. web server isn't ready
drewp@bigasterisk.com
parents:
diff changeset
138 def processMotion(self, state):
5fef0b7db346 bedroomarduino project started. web server isn't ready
drewp@bigasterisk.com
parents:
diff changeset
139 if state == self.lastMotion:
5fef0b7db346 bedroomarduino project started. web server isn't ready
drewp@bigasterisk.com
parents:
diff changeset
140 return
5fef0b7db346 bedroomarduino project started. web server isn't ready
drewp@bigasterisk.com
parents:
diff changeset
141 self.lastMotion = state
5fef0b7db346 bedroomarduino project started. web server isn't ready
drewp@bigasterisk.com
parents:
diff changeset
142 msg = json.dumps(dict(board=self.config['boardName'],
5fef0b7db346 bedroomarduino project started. web server isn't ready
drewp@bigasterisk.com
parents:
diff changeset
143 name="bedroomMotion", state=state))
5fef0b7db346 bedroomarduino project started. web server isn't ready
drewp@bigasterisk.com
parents:
diff changeset
144 getPage('http://bang.bigasterisk.com:9069/inputChange',
5fef0b7db346 bedroomarduino project started. web server isn't ready
drewp@bigasterisk.com
parents:
diff changeset
145 method="POST",
5fef0b7db346 bedroomarduino project started. web server isn't ready
drewp@bigasterisk.com
parents:
diff changeset
146 postdata=msg,
5fef0b7db346 bedroomarduino project started. web server isn't ready
drewp@bigasterisk.com
parents:
diff changeset
147 headers={'Content-Type' : 'application/json'}
5fef0b7db346 bedroomarduino project started. web server isn't ready
drewp@bigasterisk.com
parents:
diff changeset
148 ).addErrback(self.reportError, msg)
5fef0b7db346 bedroomarduino project started. web server isn't ready
drewp@bigasterisk.com
parents:
diff changeset
149
5fef0b7db346 bedroomarduino project started. web server isn't ready
drewp@bigasterisk.com
parents:
diff changeset
150 def reportError(self, msg, *args):
5fef0b7db346 bedroomarduino project started. web server isn't ready
drewp@bigasterisk.com
parents:
diff changeset
151 print "post error", msg, args
5fef0b7db346 bedroomarduino project started. web server isn't ready
drewp@bigasterisk.com
parents:
diff changeset
152
5fef0b7db346 bedroomarduino project started. web server isn't ready
drewp@bigasterisk.com
parents:
diff changeset
153 if __name__ == '__main__':
5fef0b7db346 bedroomarduino project started. web server isn't ready
drewp@bigasterisk.com
parents:
diff changeset
154
5fef0b7db346 bedroomarduino project started. web server isn't ready
drewp@bigasterisk.com
parents:
diff changeset
155 config = { # to be read from a file
37
8e55a6a9c425 add shiftbrites to bedroom arduino
drewp@bigasterisk.com
parents: 29
diff changeset
156 'arduinoPort': '/dev/serial/by-id/usb-FTDI_FT232R_USB_UART_A4001nIu-if00-port0',
29
5fef0b7db346 bedroomarduino project started. web server isn't ready
drewp@bigasterisk.com
parents:
diff changeset
157 'servePort' : 9088,
5fef0b7db346 bedroomarduino project started. web server isn't ready
drewp@bigasterisk.com
parents:
diff changeset
158 'pollFrequency' : 6,
5fef0b7db346 bedroomarduino project started. web server isn't ready
drewp@bigasterisk.com
parents:
diff changeset
159 'boardName' : 'bedroom', # gets sent with updates
5fef0b7db346 bedroomarduino project started. web server isn't ready
drewp@bigasterisk.com
parents:
diff changeset
160 }
5fef0b7db346 bedroomarduino project started. web server isn't ready
drewp@bigasterisk.com
parents:
diff changeset
161
5fef0b7db346 bedroomarduino project started. web server isn't ready
drewp@bigasterisk.com
parents:
diff changeset
162 from twisted.python import log as twlog
5fef0b7db346 bedroomarduino project started. web server isn't ready
drewp@bigasterisk.com
parents:
diff changeset
163 #twlog.startLogging(sys.stdout)
5fef0b7db346 bedroomarduino project started. web server isn't ready
drewp@bigasterisk.com
parents:
diff changeset
164
5fef0b7db346 bedroomarduino project started. web server isn't ready
drewp@bigasterisk.com
parents:
diff changeset
165 log.setLevel(logging.DEBUG)
5fef0b7db346 bedroomarduino project started. web server isn't ready
drewp@bigasterisk.com
parents:
diff changeset
166
5fef0b7db346 bedroomarduino project started. web server isn't ready
drewp@bigasterisk.com
parents:
diff changeset
167 ard = ArduinoBedroom(port=config['arduinoPort'])
5fef0b7db346 bedroomarduino project started. web server isn't ready
drewp@bigasterisk.com
parents:
diff changeset
168
5fef0b7db346 bedroomarduino project started. web server isn't ready
drewp@bigasterisk.com
parents:
diff changeset
169 period = 1/config['pollFrequency']
5fef0b7db346 bedroomarduino project started. web server isn't ready
drewp@bigasterisk.com
parents:
diff changeset
170 p = Poller(config, ard, period)
5fef0b7db346 bedroomarduino project started. web server isn't ready
drewp@bigasterisk.com
parents:
diff changeset
171 task.LoopingCall(p.poll).start(period)
5fef0b7db346 bedroomarduino project started. web server isn't ready
drewp@bigasterisk.com
parents:
diff changeset
172
5fef0b7db346 bedroomarduino project started. web server isn't ready
drewp@bigasterisk.com
parents:
diff changeset
173 reactor.listenTCP(config['servePort'], cyclone.web.Application([
5fef0b7db346 bedroomarduino project started. web server isn't ready
drewp@bigasterisk.com
parents:
diff changeset
174 (r"/", Index),
5fef0b7db346 bedroomarduino project started. web server isn't ready
drewp@bigasterisk.com
parents:
diff changeset
175 (r"/graph", GraphPage),
5fef0b7db346 bedroomarduino project started. web server isn't ready
drewp@bigasterisk.com
parents:
diff changeset
176 (r'/speakerChoice', SpeakerChoice),
37
8e55a6a9c425 add shiftbrites to bedroom arduino
drewp@bigasterisk.com
parents: 29
diff changeset
177 (r'/brite/(\d+)', Brite),
8e55a6a9c425 add shiftbrites to bedroom arduino
drewp@bigasterisk.com
parents: 29
diff changeset
178 ], arduino=ard, poller=p, brites=[]))
29
5fef0b7db346 bedroomarduino project started. web server isn't ready
drewp@bigasterisk.com
parents:
diff changeset
179 reactor.run()