This week Bob shows us his real world project to monitor activity in a room to turn on a light using a Raspberry Pi.
Segment 1 #!/usr/bin/python import math import time import RPi.GPIO as GPIO global TRIGPIN global ECHOPIN global RELAY1PIN # Pin numbers are based on functions, # not physical pins. TRIGPIN = 25 # physical pin 22 ECHOPIN = 24 # physical pin 18 RELAY1PIN = 23 # physical pin 16 global DEBUG DEBUG = True def readHCSR04(): # Disable warning GPIO.setwarnings(False) GPIO.setmode(GPIO.BCM) distance = 0 # Setup Trig pin, and turn it off GPIO.setup(TRIGPIN,GPIO.OUT) GPIO.output(TRIGPIN,GPIO.LOW) # Setup Echo pin GPIO.setup(ECHOPIN,GPIO.IN) # Turn on Trig pin for 10 microseconds, then turn it off GPIO.output(TRIGPIN, True) time.sleep(0.00001) GPIO.output(TRIGPIN, False) # Listen to the input pin and wait for the pin to go high. #========================================================== # The signalOff keeps being reset until the pin goes high startTime = time.time() signalOff = 0 while (GPIO.input(ECHOPIN) == 0 and distance == 0): signalOff = time.time() elapsedTime = signalOff - startTime if elapsedTime > 1: distance = 999 # The signalOn keeps being reset until the pin goes low. startTime = time.time() signalOn = 0 while (GPIO.input(ECHOPIN) == 1 and distance == 0): signalOn = time.time() elapsedTime = signalOn - startTime if elapsedTime > 1: distance = 998 # Calculate the time the pin was on if (distance == 0): timeOn = signalOn - signalOff if (DEBUG == True): buffer = 'timeOn = ' + repr(timeOn) print buffer # convert the time in seconds to cm or inches # speed of sound is approx. 34000 cm/sec # speed of sound is approx. 13500 inch/sec # The value must be divided by 2 because we only need the # return value. The timeOn includes the time it takes to # get to the surface in addition to the time it took to return. tempDistance = timeOn * (34000/2) # round to 2 significant digits distance = math.ceil(tempDistance * 100) / 100 GPIO.cleanup() # return the distance, or the error code return distance #end of readHCSR04() # # Loop through reading the sensor forever # count = 0 errors = 0 beginTime = time.time() timeNow = time.time() startingTime = time.strftime("%c") GPIO.setwarnings(False) GPIO.setmode(GPIO.BCM) GPIO.setup(23,GPIO.OUT) GPIO.output(23,GPIO.HIGH) whileLoop = True while (whileLoop == True): try: count += 1 sensorReading = readHCSR04() if (sensorReading == 0) or (sensorReading == 998) or (sensorReading == 999): errors += 1 if (DEBUG == True): buffer = 'Sensor reading ' + repr(count) + ' = ' + repr(sensorReading) + 'cm' print buffer buffer = 'Error count = ' + repr(errors) print buffer GPIO.setwarnings(False) GPIO.setmode(GPIO.BCM) GPIO.setup(RELAY1PIN,GPIO.OUT) # if the sensor reads something within # 10cm, then turn on the light if (sensorReading <= 10) and (sensorReading >= 0.1): GPIO.output(RELAY1PIN,GPIO.LOW) beginTime = time.time() if (DEBUG == True): print 'Valid Reading' timeNow = time.time() elapsedTime = math.ceil((timeNow - beginTime) * 100) / 100 if (DEBUG == True): buffer = 'elapsedTime = ' + repr(elapsedTime) + ' sec' print buffer # if time is greater than 10 seconds, # turn off the light if (elapsedTime > 10): GPIO.output(RELAY1PIN,GPIO.HIGH) if (DEBUG == True): buffer = 'Time has expired on lightOn cycle ' #+ repr(lightOn) print buffer # Wait 1 sec and start the loop over time.sleep(1) # capture interrupt nicely for a clean exit from program except KeyboardInterrupt: whileLoop = False # this code isn't reached until the program is exiting GPIO.output(RELAY1PIN,GPIO.HIGH) GPIO.cleanup() print '' print '' print 'Summary:' print '========' buffer = 'Starting Time = ' + repr(startingTime) print buffer buffer = 'Total count = ' + repr(count) print buffer buffer = 'Total errors = ' + repr(errors) print buffer endingTime = time.strftime("%c") buffer = 'Ending Time: ' + repr(endingTime) print buffer