#!/usr/bin/env python2 from websocket_server import WebsocketServer import thread import serial import time import json measures = { 'pressure_bottom': 0, 'pressure_top': 0, 'flexion': 0, 'angle_x': 0, 'angle_y': 0, 'angle_z': 0, } def server_procedure(server): server.run_forever() def main(): server = WebsocketServer(1234, '0.0.0.0') thread.start_new_thread(server_procedure, (server,)) with serial.Serial('/dev/ttyACM0', 9600) as ser: #with open('/tmp/fifo') as ser: while True: line = ser.readline() if not line: time.sleep(0.01) continue line = line.strip() if line.startswith('PRESSURE_BOTTOM:'): measures['pressure_bottom'] = line.split('PRESSURE_BOTTOM:')[1] elif line.startswith('PRESSURE_TOP:'): measures['pressure_top'] = line.split('PRESSURE_TOP:')[1] elif line.startswith('FLEXION:'): measures['flexion'] = line.split('FLEXION:')[1] elif line.startswith('ANGLE_X:'): measures['angle_x'] = line.split('ANGLE_X:')[1] server.send_message_to_all(json.dumps(measures)) elif line.startswith('ANGLE_Y:'): measures['angle_y'] = line.split('ANGLE_Y:')[1] elif line.startswith('ANGLE_Z:'): measures['angle_z'] = line.split('ANGLE_Z:')[1] else: print(line) if __name__ == '__main__': main()