Reputation: 33
I am looking for a way to modify the Tornado Websockets Chat Demo in order to communicate with a serial port on the server over the chat. Shown below is the method(?) called on when a message is sent. I can write to serial fine sending the body of the message to the serial.write(). But the problem is I am only able to get the serial feedback once. However in my current application the serial may send in data more than just when spoken to.
...
import serial
serial = serial.Serial("COM3", 57600)
...
def on_message(self, message):
test = ""
logging.info("got message %r", message)
parsed = tornado.escape.json_decode(message)
serial.write(parsed["body"] + "\n")
logging.info("SENT " + parsed["body"] + "\n")
test = serial.readline()
chat = {
"id": str(uuid.uuid4()),
"body": test,
}
chat["html"] = self.render_string("message.html", message=chat)
ChatSocketHandler.update_cache(chat)
ChatSocketHandler.send_updates(chat)
Is there a way to constantly poll the serial and handle the websockets IO at the same time? Also if there is an easier alternative to Tornado with realtime multiple page updates I would not be opposed. Although I do like Tornado.
Upvotes: 3
Views: 1353
Reputation: 839
I've used a different approach using Tornado and Python's multiprocessing library. Details are available here:
Raspberry Pi + Tornado + Arduino
Upvotes: 1
Reputation: 22051
Since you are open for other approaches, you might be interested in this complete working example, demonstrating real-time charts in browser instances driven from an Arduino sensor board via serial and WebSocket. This uses Autobahn and Twisted.
Disclosure: I'm original author of Autobahn and work for Tavendo.
Upvotes: 1