annotate service/theaterArduino/theaterArduino.py @ 438:d453cdd8a86f

start dev mode nginx config. not working yet Ignore-this: d18b11cd5566d844bf126b8a9e50b912
author drewp@bigasterisk.com
date Sat, 13 Apr 2019 20:56:30 -0700
parents 84af13435de7
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
4
be855a111619 move a bunch of services into this tree, give them all web status pages
drewp@bigasterisk.com
parents:
diff changeset
1 """
be855a111619 move a bunch of services into this tree, give them all web status pages
drewp@bigasterisk.com
parents:
diff changeset
2 arduino example sketches, 'StandardFirmata'.
be855a111619 move a bunch of services into this tree, give them all web status pages
drewp@bigasterisk.com
parents:
diff changeset
3
be855a111619 move a bunch of services into this tree, give them all web status pages
drewp@bigasterisk.com
parents:
diff changeset
4 ####easy_install http://github.com/lupeke/python-firmata/tarball/master
be855a111619 move a bunch of services into this tree, give them all web status pages
drewp@bigasterisk.com
parents:
diff changeset
5
be855a111619 move a bunch of services into this tree, give them all web status pages
drewp@bigasterisk.com
parents:
diff changeset
6 Now using http://code.google.com/p/pyduino, modified to run at 57600
be855a111619 move a bunch of services into this tree, give them all web status pages
drewp@bigasterisk.com
parents:
diff changeset
7 baud like my arduino's code does. pyduino is better than the lupeke
be855a111619 move a bunch of services into this tree, give them all web status pages
drewp@bigasterisk.com
parents:
diff changeset
8 one in that you can read your settings off the output pins
be855a111619 move a bunch of services into this tree, give them all web status pages
drewp@bigasterisk.com
parents:
diff changeset
9
be855a111619 move a bunch of services into this tree, give them all web status pages
drewp@bigasterisk.com
parents:
diff changeset
10 Note that there are some startup delays and you may not hear about
be855a111619 move a bunch of services into this tree, give them all web status pages
drewp@bigasterisk.com
parents:
diff changeset
11 input changes for a few seconds.
be855a111619 move a bunch of services into this tree, give them all web status pages
drewp@bigasterisk.com
parents:
diff changeset
12 """
be855a111619 move a bunch of services into this tree, give them all web status pages
drewp@bigasterisk.com
parents:
diff changeset
13 from __future__ import division
be855a111619 move a bunch of services into this tree, give them all web status pages
drewp@bigasterisk.com
parents:
diff changeset
14 import sys, cyclone.web, time, simplejson, os
be855a111619 move a bunch of services into this tree, give them all web status pages
drewp@bigasterisk.com
parents:
diff changeset
15 from twisted.web.client import getPage
be855a111619 move a bunch of services into this tree, give them all web status pages
drewp@bigasterisk.com
parents:
diff changeset
16 from twisted.internet import reactor, task
be855a111619 move a bunch of services into this tree, give them all web status pages
drewp@bigasterisk.com
parents:
diff changeset
17
be855a111619 move a bunch of services into this tree, give them all web status pages
drewp@bigasterisk.com
parents:
diff changeset
18 sys.path.append("/my/proj/homeauto/lib")
be855a111619 move a bunch of services into this tree, give them all web status pages
drewp@bigasterisk.com
parents:
diff changeset
19 from cycloneerr import PrettyErrorHandler
be855a111619 move a bunch of services into this tree, give them all web status pages
drewp@bigasterisk.com
parents:
diff changeset
20 from logsetup import log
be855a111619 move a bunch of services into this tree, give them all web status pages
drewp@bigasterisk.com
parents:
diff changeset
21
be855a111619 move a bunch of services into this tree, give them all web status pages
drewp@bigasterisk.com
parents:
diff changeset
22 sys.path.append("pyduino-read-only")
be855a111619 move a bunch of services into this tree, give them all web status pages
drewp@bigasterisk.com
parents:
diff changeset
23 import pyduino
be855a111619 move a bunch of services into this tree, give them all web status pages
drewp@bigasterisk.com
parents:
diff changeset
24
be855a111619 move a bunch of services into this tree, give them all web status pages
drewp@bigasterisk.com
parents:
diff changeset
25 def _num(name):
be855a111619 move a bunch of services into this tree, give them all web status pages
drewp@bigasterisk.com
parents:
diff changeset
26 if name.startswith('d'):
be855a111619 move a bunch of services into this tree, give them all web status pages
drewp@bigasterisk.com
parents:
diff changeset
27 return int(name[1:])
be855a111619 move a bunch of services into this tree, give them all web status pages
drewp@bigasterisk.com
parents:
diff changeset
28 raise ValueError(name)
be855a111619 move a bunch of services into this tree, give them all web status pages
drewp@bigasterisk.com
parents:
diff changeset
29
be855a111619 move a bunch of services into this tree, give them all web status pages
drewp@bigasterisk.com
parents:
diff changeset
30 class pin(PrettyErrorHandler, cyclone.web.RequestHandler):
be855a111619 move a bunch of services into this tree, give them all web status pages
drewp@bigasterisk.com
parents:
diff changeset
31 def get(self, name):
be855a111619 move a bunch of services into this tree, give them all web status pages
drewp@bigasterisk.com
parents:
diff changeset
32 self.set_header("Content-Type", "text/plain")
be855a111619 move a bunch of services into this tree, give them all web status pages
drewp@bigasterisk.com
parents:
diff changeset
33 arduino = self.settings.arduino
be855a111619 move a bunch of services into this tree, give them all web status pages
drewp@bigasterisk.com
parents:
diff changeset
34 arduino.iterate()
be855a111619 move a bunch of services into this tree, give them all web status pages
drewp@bigasterisk.com
parents:
diff changeset
35 self.write(str(int(arduino.digital[_num(name)].read())))
be855a111619 move a bunch of services into this tree, give them all web status pages
drewp@bigasterisk.com
parents:
diff changeset
36
be855a111619 move a bunch of services into this tree, give them all web status pages
drewp@bigasterisk.com
parents:
diff changeset
37 def put(self, name):
be855a111619 move a bunch of services into this tree, give them all web status pages
drewp@bigasterisk.com
parents:
diff changeset
38 t1 = time.time()
be855a111619 move a bunch of services into this tree, give them all web status pages
drewp@bigasterisk.com
parents:
diff changeset
39 self.settings.arduino.digital[_num(name)].write(int(self.request.body))
be855a111619 move a bunch of services into this tree, give them all web status pages
drewp@bigasterisk.com
parents:
diff changeset
40 log.debug("arduino write in %.1f ms" % (1000 * (time.time() - t1)))
be855a111619 move a bunch of services into this tree, give them all web status pages
drewp@bigasterisk.com
parents:
diff changeset
41
be855a111619 move a bunch of services into this tree, give them all web status pages
drewp@bigasterisk.com
parents:
diff changeset
42
be855a111619 move a bunch of services into this tree, give them all web status pages
drewp@bigasterisk.com
parents:
diff changeset
43 class pinMode(PrettyErrorHandler, cyclone.web.RequestHandler):
be855a111619 move a bunch of services into this tree, give them all web status pages
drewp@bigasterisk.com
parents:
diff changeset
44 def get(self, name):
be855a111619 move a bunch of services into this tree, give them all web status pages
drewp@bigasterisk.com
parents:
diff changeset
45 self.set_header("Content-Type", "text/plain")
be855a111619 move a bunch of services into this tree, give them all web status pages
drewp@bigasterisk.com
parents:
diff changeset
46 mode = self.settings.arduino.digital[_num(name)].get_mode()
be855a111619 move a bunch of services into this tree, give them all web status pages
drewp@bigasterisk.com
parents:
diff changeset
47 self.write({pyduino.DIGITAL_INPUT : "input",
be855a111619 move a bunch of services into this tree, give them all web status pages
drewp@bigasterisk.com
parents:
diff changeset
48 pyduino.DIGITAL_OUTPUT : "output"}[mode])
be855a111619 move a bunch of services into this tree, give them all web status pages
drewp@bigasterisk.com
parents:
diff changeset
49
be855a111619 move a bunch of services into this tree, give them all web status pages
drewp@bigasterisk.com
parents:
diff changeset
50 def put(self, name):
be855a111619 move a bunch of services into this tree, give them all web status pages
drewp@bigasterisk.com
parents:
diff changeset
51 mode = {
be855a111619 move a bunch of services into this tree, give them all web status pages
drewp@bigasterisk.com
parents:
diff changeset
52 "input" : pyduino.DIGITAL_INPUT,
be855a111619 move a bunch of services into this tree, give them all web status pages
drewp@bigasterisk.com
parents:
diff changeset
53 "output" : pyduino.DIGITAL_OUTPUT}[self.request.body.strip()]
be855a111619 move a bunch of services into this tree, give them all web status pages
drewp@bigasterisk.com
parents:
diff changeset
54 self.settings.arduino.digital[_num(name)].set_mode(mode)
be855a111619 move a bunch of services into this tree, give them all web status pages
drewp@bigasterisk.com
parents:
diff changeset
55
be855a111619 move a bunch of services into this tree, give them all web status pages
drewp@bigasterisk.com
parents:
diff changeset
56 class Pid(PrettyErrorHandler, cyclone.web.RequestHandler):
be855a111619 move a bunch of services into this tree, give them all web status pages
drewp@bigasterisk.com
parents:
diff changeset
57 def get(self):
be855a111619 move a bunch of services into this tree, give them all web status pages
drewp@bigasterisk.com
parents:
diff changeset
58 self.set_header("Content-Type", "text/plain")
be855a111619 move a bunch of services into this tree, give them all web status pages
drewp@bigasterisk.com
parents:
diff changeset
59 self.write(str(os.getpid()))
be855a111619 move a bunch of services into this tree, give them all web status pages
drewp@bigasterisk.com
parents:
diff changeset
60
be855a111619 move a bunch of services into this tree, give them all web status pages
drewp@bigasterisk.com
parents:
diff changeset
61 class index(PrettyErrorHandler, cyclone.web.RequestHandler):
be855a111619 move a bunch of services into this tree, give them all web status pages
drewp@bigasterisk.com
parents:
diff changeset
62 def get(self):
be855a111619 move a bunch of services into this tree, give them all web status pages
drewp@bigasterisk.com
parents:
diff changeset
63 """
be855a111619 move a bunch of services into this tree, give them all web status pages
drewp@bigasterisk.com
parents:
diff changeset
64 this is a suitable status check; it does a round-trip to arduino
be855a111619 move a bunch of services into this tree, give them all web status pages
drewp@bigasterisk.com
parents:
diff changeset
65 """
be855a111619 move a bunch of services into this tree, give them all web status pages
drewp@bigasterisk.com
parents:
diff changeset
66 # this would be a good ping() call for pyduino
be855a111619 move a bunch of services into this tree, give them all web status pages
drewp@bigasterisk.com
parents:
diff changeset
67 self.settings.arduino.sp.write(chr(pyduino.REPORT_VERSION))
be855a111619 move a bunch of services into this tree, give them all web status pages
drewp@bigasterisk.com
parents:
diff changeset
68 self.settings.arduino.iterate()
be855a111619 move a bunch of services into this tree, give them all web status pages
drewp@bigasterisk.com
parents:
diff changeset
69
be855a111619 move a bunch of services into this tree, give them all web status pages
drewp@bigasterisk.com
parents:
diff changeset
70 self.set_header("Content-Type", "application/xhtml+xml")
be855a111619 move a bunch of services into this tree, give them all web status pages
drewp@bigasterisk.com
parents:
diff changeset
71 self.write(open('index.html').read())
be855a111619 move a bunch of services into this tree, give them all web status pages
drewp@bigasterisk.com
parents:
diff changeset
72
be855a111619 move a bunch of services into this tree, give them all web status pages
drewp@bigasterisk.com
parents:
diff changeset
73 class Application(cyclone.web.Application):
be855a111619 move a bunch of services into this tree, give them all web status pages
drewp@bigasterisk.com
parents:
diff changeset
74 def __init__(self, arduino):
be855a111619 move a bunch of services into this tree, give them all web status pages
drewp@bigasterisk.com
parents:
diff changeset
75 handlers = [
be855a111619 move a bunch of services into this tree, give them all web status pages
drewp@bigasterisk.com
parents:
diff changeset
76 (r"/", index),
be855a111619 move a bunch of services into this tree, give them all web status pages
drewp@bigasterisk.com
parents:
diff changeset
77 (r'/pin/(.*)/mode', pinMode),
be855a111619 move a bunch of services into this tree, give them all web status pages
drewp@bigasterisk.com
parents:
diff changeset
78 (r'/pin/(.*)', pin),
be855a111619 move a bunch of services into this tree, give them all web status pages
drewp@bigasterisk.com
parents:
diff changeset
79 (r'/pid', Pid),
be855a111619 move a bunch of services into this tree, give them all web status pages
drewp@bigasterisk.com
parents:
diff changeset
80 # web refresh could benefit a lot from a json resource that
be855a111619 move a bunch of services into this tree, give them all web status pages
drewp@bigasterisk.com
parents:
diff changeset
81 # gives all the state
be855a111619 move a bunch of services into this tree, give them all web status pages
drewp@bigasterisk.com
parents:
diff changeset
82 ]
be855a111619 move a bunch of services into this tree, give them all web status pages
drewp@bigasterisk.com
parents:
diff changeset
83 settings = {"arduino" : arduino,}
be855a111619 move a bunch of services into this tree, give them all web status pages
drewp@bigasterisk.com
parents:
diff changeset
84 cyclone.web.Application.__init__(self, handlers, **settings)
be855a111619 move a bunch of services into this tree, give them all web status pages
drewp@bigasterisk.com
parents:
diff changeset
85
be855a111619 move a bunch of services into this tree, give them all web status pages
drewp@bigasterisk.com
parents:
diff changeset
86 class WatchPins(object):
be855a111619 move a bunch of services into this tree, give them all web status pages
drewp@bigasterisk.com
parents:
diff changeset
87 def __init__(self, arduino, conf):
be855a111619 move a bunch of services into this tree, give them all web status pages
drewp@bigasterisk.com
parents:
diff changeset
88 self.arduino, self.conf = arduino, conf
be855a111619 move a bunch of services into this tree, give them all web status pages
drewp@bigasterisk.com
parents:
diff changeset
89 self.lastState = {}
be855a111619 move a bunch of services into this tree, give them all web status pages
drewp@bigasterisk.com
parents:
diff changeset
90 self.pins = conf['watchPins']
be855a111619 move a bunch of services into this tree, give them all web status pages
drewp@bigasterisk.com
parents:
diff changeset
91 if self.pins == 'allInput':
be855a111619 move a bunch of services into this tree, give them all web status pages
drewp@bigasterisk.com
parents:
diff changeset
92 self.watchAllInputs()
be855a111619 move a bunch of services into this tree, give them all web status pages
drewp@bigasterisk.com
parents:
diff changeset
93 for pin in self.pins:
be855a111619 move a bunch of services into this tree, give them all web status pages
drewp@bigasterisk.com
parents:
diff changeset
94 arduino.digital_ports[pin >> 3].set_active(1)
be855a111619 move a bunch of services into this tree, give them all web status pages
drewp@bigasterisk.com
parents:
diff changeset
95 arduino.digital[pin].set_mode(pyduino.DIGITAL_INPUT)
be855a111619 move a bunch of services into this tree, give them all web status pages
drewp@bigasterisk.com
parents:
diff changeset
96
be855a111619 move a bunch of services into this tree, give them all web status pages
drewp@bigasterisk.com
parents:
diff changeset
97 def watchAllInputs(self):
be855a111619 move a bunch of services into this tree, give them all web status pages
drewp@bigasterisk.com
parents:
diff changeset
98 raise NotImplementedError("this needs to be updated whenever the modes change")
be855a111619 move a bunch of services into this tree, give them all web status pages
drewp@bigasterisk.com
parents:
diff changeset
99 self.pins = [p for p in range(2, 13+1) if
be855a111619 move a bunch of services into this tree, give them all web status pages
drewp@bigasterisk.com
parents:
diff changeset
100 self.arduino.digital[p].get_mode() ==
be855a111619 move a bunch of services into this tree, give them all web status pages
drewp@bigasterisk.com
parents:
diff changeset
101 pyduino.DIGITAL_INPUT]
be855a111619 move a bunch of services into this tree, give them all web status pages
drewp@bigasterisk.com
parents:
diff changeset
102
be855a111619 move a bunch of services into this tree, give them all web status pages
drewp@bigasterisk.com
parents:
diff changeset
103 def reportPostError(self, fail, pin, value, url):
be855a111619 move a bunch of services into this tree, give them all web status pages
drewp@bigasterisk.com
parents:
diff changeset
104 log.error("failed to send pin %s update (now %s) to %r: %r" % (pin, value, url, fail))
be855a111619 move a bunch of services into this tree, give them all web status pages
drewp@bigasterisk.com
parents:
diff changeset
105
be855a111619 move a bunch of services into this tree, give them all web status pages
drewp@bigasterisk.com
parents:
diff changeset
106 def poll(self):
be855a111619 move a bunch of services into this tree, give them all web status pages
drewp@bigasterisk.com
parents:
diff changeset
107 try:
be855a111619 move a bunch of services into this tree, give them all web status pages
drewp@bigasterisk.com
parents:
diff changeset
108 self._poll()
be855a111619 move a bunch of services into this tree, give them all web status pages
drewp@bigasterisk.com
parents:
diff changeset
109 except Exception, e:
be855a111619 move a bunch of services into this tree, give them all web status pages
drewp@bigasterisk.com
parents:
diff changeset
110 log.error("during poll:", exc_info=1)
be855a111619 move a bunch of services into this tree, give them all web status pages
drewp@bigasterisk.com
parents:
diff changeset
111
be855a111619 move a bunch of services into this tree, give them all web status pages
drewp@bigasterisk.com
parents:
diff changeset
112 def _poll(self):
be855a111619 move a bunch of services into this tree, give them all web status pages
drewp@bigasterisk.com
parents:
diff changeset
113 # this can IndexError for a port number being out of
be855a111619 move a bunch of services into this tree, give them all web status pages
drewp@bigasterisk.com
parents:
diff changeset
114 # range. I'm not sure how- maybe error data coming in the
be855a111619 move a bunch of services into this tree, give them all web status pages
drewp@bigasterisk.com
parents:
diff changeset
115 # port?
be855a111619 move a bunch of services into this tree, give them all web status pages
drewp@bigasterisk.com
parents:
diff changeset
116 arduino.iterate()
be855a111619 move a bunch of services into this tree, give them all web status pages
drewp@bigasterisk.com
parents:
diff changeset
117 for pin in self.pins:
be855a111619 move a bunch of services into this tree, give them all web status pages
drewp@bigasterisk.com
parents:
diff changeset
118 current = arduino.digital[pin].read()
be855a111619 move a bunch of services into this tree, give them all web status pages
drewp@bigasterisk.com
parents:
diff changeset
119 if current != self.lastState.get(pin, None):
be855a111619 move a bunch of services into this tree, give them all web status pages
drewp@bigasterisk.com
parents:
diff changeset
120 d = getPage(
be855a111619 move a bunch of services into this tree, give them all web status pages
drewp@bigasterisk.com
parents:
diff changeset
121 self.conf['post'],
be855a111619 move a bunch of services into this tree, give them all web status pages
drewp@bigasterisk.com
parents:
diff changeset
122 method="POST",
be855a111619 move a bunch of services into this tree, give them all web status pages
drewp@bigasterisk.com
parents:
diff changeset
123 postdata=simplejson.dumps(dict(board=self.conf['boardName'], pin=pin, level=int(current))),
be855a111619 move a bunch of services into this tree, give them all web status pages
drewp@bigasterisk.com
parents:
diff changeset
124 headers={'Content-Type' : 'application/json'})
be855a111619 move a bunch of services into this tree, give them all web status pages
drewp@bigasterisk.com
parents:
diff changeset
125 d.addErrback(self.reportPostError, pin, current, self.conf['post'])
be855a111619 move a bunch of services into this tree, give them all web status pages
drewp@bigasterisk.com
parents:
diff changeset
126
be855a111619 move a bunch of services into this tree, give them all web status pages
drewp@bigasterisk.com
parents:
diff changeset
127 self.lastState[pin] = current
be855a111619 move a bunch of services into this tree, give them all web status pages
drewp@bigasterisk.com
parents:
diff changeset
128
be855a111619 move a bunch of services into this tree, give them all web status pages
drewp@bigasterisk.com
parents:
diff changeset
129 if __name__ == '__main__':
be855a111619 move a bunch of services into this tree, give them all web status pages
drewp@bigasterisk.com
parents:
diff changeset
130
be855a111619 move a bunch of services into this tree, give them all web status pages
drewp@bigasterisk.com
parents:
diff changeset
131 config = { # to be read from a file
18
84af13435de7 move more ports to use /dev/serial/by-id/
drewp@bigasterisk.com
parents: 4
diff changeset
132 'arduinoPort': '/dev/serial/by-id/usb-FTDI_FT232R_USB_UART_A900cepU-if00-port0',
4
be855a111619 move a bunch of services into this tree, give them all web status pages
drewp@bigasterisk.com
parents:
diff changeset
133 'servePort' : 9056,
be855a111619 move a bunch of services into this tree, give them all web status pages
drewp@bigasterisk.com
parents:
diff changeset
134 'pollFrequency' : 20,
be855a111619 move a bunch of services into this tree, give them all web status pages
drewp@bigasterisk.com
parents:
diff changeset
135 'post' : 'http://bang:9069/pinChange',
be855a111619 move a bunch of services into this tree, give them all web status pages
drewp@bigasterisk.com
parents:
diff changeset
136 'boardName' : 'theater', # gets sent with updates
be855a111619 move a bunch of services into this tree, give them all web status pages
drewp@bigasterisk.com
parents:
diff changeset
137 'watchPins' : [9, 10], # or 'allInput' (not yet working)
be855a111619 move a bunch of services into this tree, give them all web status pages
drewp@bigasterisk.com
parents:
diff changeset
138 # todo: need options to preset inputs/outputs at startup
be855a111619 move a bunch of services into this tree, give them all web status pages
drewp@bigasterisk.com
parents:
diff changeset
139 }
be855a111619 move a bunch of services into this tree, give them all web status pages
drewp@bigasterisk.com
parents:
diff changeset
140
be855a111619 move a bunch of services into this tree, give them all web status pages
drewp@bigasterisk.com
parents:
diff changeset
141 arduino = pyduino.Arduino(config['arduinoPort'])
be855a111619 move a bunch of services into this tree, give them all web status pages
drewp@bigasterisk.com
parents:
diff changeset
142 wp = WatchPins(arduino, config)
be855a111619 move a bunch of services into this tree, give them all web status pages
drewp@bigasterisk.com
parents:
diff changeset
143 task.LoopingCall(wp.poll).start(1/config['pollFrequency'])
be855a111619 move a bunch of services into this tree, give them all web status pages
drewp@bigasterisk.com
parents:
diff changeset
144 reactor.listenTCP(config['servePort'], Application(arduino))
be855a111619 move a bunch of services into this tree, give them all web status pages
drewp@bigasterisk.com
parents:
diff changeset
145 reactor.run()