#! /usr/bin/env python # Read the output of an Arduino which is constantly printing sensor output. # See also # http://www.arcfn.com/2009/06/arduino-sheevaplug-cool-hardware.html import sys, serial, threading class Arduino(threading.Thread) : def run(self, interactive) : # Port may vary, so look for it: baseport = "/dev/ttyUSB" self.ser = None for i in xrange(0, 9) : try : self.ser = serial.Serial(baseport + str(i), 9600, timeout=10) break except : self.ser = None pass if not self.ser : print "Couldn't open a serial port" sys.exit(1) print "Opened /dev/ttyUSB" + str(i) self.ser.flushInput() while True : data = self.ser.readline().strip() if data : if interactive : print data else : webfile = open("arduino.html", "w") print >>webfile, """ Arduino sensor output

Arduino sensor output

%s

""" % data webfile.close() # If -i, run in interactive mode if len (sys.argv) > 1 and sys.argv[1] == '-i' : interactive = True else : interactive = False arduino = Arduino() arduino.run(interactive)