#! /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, os, time, serial class Arduino() : def loop(self, verbose) : # 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) while True : print "Sleeping ..." time.sleep(2) self.ser.flushInput() data = self.ser.readline().strip() if not data : continue print "data str = '" + data + "'" data = int(data) print "data int =", data if verbose : print data if data < 45 : if verbose : print "Snapping photo!" os.system("fswebcam --device /dev/video0 -S 1 output.jpg") #os.system('fswebcam --device /dev/video0 -s "Exposure, Auto"=4 -D 1.5 output.jpg') webfile = open("arduino.html", "w") print >>webfile, """ Proximity webcam

Proximity webcam

%s

""" % data webfile.close() # If -v, run in verbose mode if len (sys.argv) > 1 and sys.argv[1] == '-v' : verbose = True else : # For the demo, just always make it true. In real life, set to False. verbose = True arduino = Arduino() arduino.loop(verbose)