Tornado uses WebSockets on Raspberry Pi and monitors serial Arduino communication

Basically, what I want to achieve is a canvas-based web interface to control an Arduino through Raspberry Pi. The use case is that a user navigates to raspberrypi:8080 to display a canvas. Then after moving the slider, the Websocket message will be sent to the Tornado server on the Raspberry Pi. Tornado then sends a serial message to the Arduino, which changes the RGB value of RGB. So far, I have been able to do this with the help of the documentation from the developer Raspberry Pi Android HTML5 Realtime Servo Control.

However, communication is only one-way communication from Raspberry Pi to Arduino. I want tornado to also monitor the serial port to return any sensor data to the front end. Here is where I am not sure how to proceed. I can use Node.js to accomplish similar operations, which can asynchronously monitor Websocket messages and serial messages.

Should there be an asynchronous process that constantly monitors the port? I have seen several options for this solution.

>Some people suggest tornado.gen.Task, but for a single HTTP request, it is not used for constant serial data.
> tornado.ioloop.PeriodicCallback, I can set to check serial data every millisecond, but this sounds like a lot of overhead.
>I have also seen separate tools such as Swirl. (Swirl is outdated according to it’s Github repo)

Or should I set up a separate Python application to monitor the serial number, and then interact with the Tornado application Communication, can you understand the following content?

>Websocket message using websocket client
> ZeroMQ (Working example: pyzmq/examples/eventloop/web.py)

So there are many options… Any suggestions and some Reasons to try or avoid any of the above options?

This is what I have and need to add serial monitoring:

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()

I have found a solution that involves using the Python multiprocessing library. I just wrote a detailed blog post:

Raspberry Pi + Tornado + Arduino

I hope others find it useful…

Basically, what I want to achieve is a canvas-based web interface to control an Arduino through Raspberry Pi. The use case is a user navigates to raspberrypi:8080 to display a canvas. Then after moving the slider, the Websocket message will be sent to the Tornado server on the Raspberry Pi. Tornado then sends a serial message to the Arduino, which changes the RGB value of RGB. So far, I have been able to do this with the help of the documentation from the developer Raspberry Pi Android HTML5 Realtime Servo Control.

However, communication is only one-way communication from Raspberry Pi to Arduino. I want tornado to also monitor the serial port to return any sensor data to the front end. Here is where I am not sure how to proceed. I can use Node.js to accomplish similar operations, which can asynchronously monitor Websocket messages and serial messages.

Should there be an asynchronous process that constantly monitors the port? I have seen several options for this solution.

>Some people suggest tornado.gen.Task, but for a single HTTP request, it is not used for constant serial data.
> tornado.ioloop.PeriodicCallback, I can set to check serial data every millisecond, but this sounds like a lot of overhead.
>I have also seen separate tools such as Swirl. (Swirl is outdated according to it’s Github repo)

Or should I set up a separate Python application to monitor the serial number, and then interact with the Tornado application Communication, can you understand the following content?

>Websocket message using websocket client
> ZeroMQ (Working example: pyzmq/examples/eventloop/web.py)

So there are many options… Any suggestions and some Reasons to try or avoid any of the above options?

This is what I have and need to add serial monitoring:

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()

I have found a solution that involves using the Python multiprocessing library. I just wrote a detailed blog post:

Raspberry Pi + Tornado + Arduino

I hope others find it useful…

Leave a Comment

Your email address will not be published.