title: Tornado on Raspberry Pi to use websockets as well as monitor serial port Arduino communication
body: <p>Essentially, what I'm hoping to achieve is a canvas based web interface to control an <a href="http://en.wikipedia.org/wiki/Arduino#ArduinoUno" rel="noreferrer">Arduino</a>, via a <a href="http://en.wikipedia.org/wiki/Raspberry_Pi" rel="noreferrer">Raspberry Pi</a>. The <a href="http://en.wikipedia.org/wiki/Use_case" rel="noreferrer">use case</a> is that a user navigates to <code>raspberrypi:8080</code> which displays a canvas. Then upon moving a slider, a websocket message is sent to the <a href="http://en.wikipedia.org/wiki/Tornado_%28web_server%29" rel="noreferrer">Tornado</a> server on the Raspberry Pi. Tornado then sends a serial message to the Arduino which changes the <a href="http://en.wikipedia.org/wiki/RGB_color_model" rel="noreferrer">RGB</a> value of an <a href="http://en.wikipedia.org/wiki/Light-emitting_diode" rel="noreferrer">LED</a>. So far so good, I've been able to do this with the help of the documentation by a developer, <em><a href="http://martinsant.net/?page_id=479" rel="noreferrer">Raspberry Pi Android HTML5 Realtime Servo Control</a></em>.</p>
  
  <p>However, the communication is only one-way from Raspberry Pi to Arduino. I'd like Tornado to also monitor the serial port to get any sensor data back to the front-end. Here's where I'm unsure about how to proceed. I was able to accomplish something like this using <a href="http://en.wikipedia.org/wiki/Node.js" rel="noreferrer">Node.js</a>, which monitors for both websocket messages as well as serial messages asynchronously.  </p>
  
  <p>Should an asynchronous process be spawned which constantly monitors the port?  I've seen a couple of options for this sort of solution.</p>
  
  <ol>
  <li>Some people suggest <code>tornado.gen.Task</code>, but for single HTTP requests, not for constant serial data.</li>
  <li><code>tornado.ioloop.PeriodicCallback</code> which I could set up to check for serial data every millisecond, but that sounds like a lot of overhead.</li>
  <li><strike>I've also seen separate tools such as <a href="http://code.naeseth.com/swirl/" rel="noreferrer">Swirl</a>.</strike> (Swirl is outdated according to <a href="https://github.com/enaeseth/swirl" rel="noreferrer">it's Github repo</a>)</li>
  </ol>
  
  <p>Or should I set up a separate <a href="http://en.wikipedia.org/wiki/Python_%28programming_language%29" rel="noreferrer">Python</a> application which monitors serial and then communicates to the Tornado application on something it can understand like the following?</p>
  
  <ol start="4">
  <li>websocket messages using a <a href="https://github.com/liris/websocket-client" rel="noreferrer">websocket client</a></li>
  <li>ZeroMQ (working example: <em><a href="https://github.com/zeromq/pyzmq/blob/master/examples/eventloop/web.py" rel="noreferrer">pyzmq / examples / eventloop / web.py</a></em>)</li>
  </ol>
  
  <p>So there are lots of options... What are some recommendations and some reasons to try out or avoid any of the above options?</p>
  
  <p>Here's what I have and need to add serial monitoring to:</p>
  
  <pre class="lang-py prettyprint-override"><code>import tornado.httpserver
  import tornado.ioloop
  import tornado.options
  import tornado.web
  import tornado.websocket
  
  from tornado.options import define, options
  define("port", default=8080, help="run on the given port", type=int)
  
  class IndexHandler(tornado.web.RequestHandler):
      def get(self):
          self.render('index.html')
  
  class WebSocketHandler(tornado.websocket.WebSocketHandler):
      def open(self):
          print 'new connection'
          self.write_message("connected")
  
      def on_message(self, message):
          print 'message received %s' % message
          self.write_message('message received %s' % message)
  
      def on_close(self):
          print 'connection closed'
  
  if __name__ == "__main__":
      tornado.options.parse_command_line()
      app = tornado.web.Application(
          handlers=[
              (r"/", IndexHandler),
              (r"/ws", WebSocketHandler)
          ]
      )
      httpServer = tornado.httpserver.HTTPServer(app)
      httpServer.listen(options.port)
      print "Listening on port:", options.port
      tornado.ioloop.IOLoop.instance().start()
  </code></pre>