Tornado Websockets realtime bidirectional info.

Howdy,

I'm trying to get information back to the web browser via websockets. I have been able to do it by requesting it every time I send a message to the server. The problem is it is old info from the last time it sent a string.

I'm not very smart with python and even looked how to put an "if serial is available, write_message to client" but could not get it down. Any ideas or anyone who has been able to get real time from the arduino to the browser?

Here's my python/tornado code:

import tornado.ioloop
import tornado.web
import tornado.websocket
import serial

from tornado.options import define, options, parse_command_line

define("port", default=8888, help="run on the given port", type=int)

clients = dict()

ser = serial.Serial('/dev/ttyATH0', 115200, timeout=0)

class WebSocketHandler(tornado.websocket.WebSocketHandler):
	print "Waiting for Connection..."

	def check_origin(self, origin):
		return True

	def open(self):
		print "Websocket Connection Open"
		ser.flushInput()
		ser.flushOutput()
		ser.write("thdfONx")

	def on_message(self, message):
		dato_1 = str(message)
		print (dato_1)
		ser.write(dato_1)
		dato_2 = str(ser.readline())
		print (dato_2)
		self.write_message(dato_2)
		ser.flushInput()
		ser.flushOutput()
		
	def on_close(self):
		print "WebSocket Connection Closed"
		ser.flushInput()
		ser.flushOutput()
		ser.write("thdfOFFx")

app = tornado.web.Application([
    (r'/', WebSocketHandler),
])

if __name__ == '__main__':
    parse_command_line()
    app.listen(options.port)
    tornado.ioloop.IOLoop.instance().start()

Thanks in advance!

@dfronin,
I'm looking at your code. I am NOT familiar with tornado. It appears to be some type of webserver/webapp.
What does on_message do?

This section of code, appears to be the critical portion:

	def on_message(self, message):
		dato_1 = str(message)
		print (dato_1)
		ser.write(dato_1)
		dato_2 = str(ser.readline())
		print (dato_2)
		self.write_message(dato_2)
		ser.flushInput()
		ser.flushOutput()

As such, it appears that ser.readline() will stall, if there is nothing to read. But it also appears you have been debugging this.

Jesse

on_message executes when the browser sends a message to the websockets server (tornado).

dfronin:
on_message executes when the browser sends a message to the websockets server (tornado).

I got that part. What does the rest of the code do?

Jesse