import sys import time as t import RPi.GPIO as gpio import smtplib gpio.setwarnings(False) gpio.setmode(gpio.BOARD) gpio.setup(7,gpio.IN) gpio.setup(11,gpio.OUT) #See pictures of setup at http://ncpcs.com/gallery.html this will be featured soon in PiPower Thanks again hope this helps #Sincerely, ElectronLux # PIR Voltage to pin 4, Gnd to pin 6, Signal to pin 11... Transistor Base to pin 11, Emitter to pin 14, Collector to Relay Gnd. # 9v Battery Gnd to pin 39, Voltage to Relay + (Note 470 Ohm resistor between Transistor Base and Pin 11) print ''' for monitoring and recording motion with timestamp on Raspberry utilizes PIR HC-SR501 detector. Message on screen logs motion times, when motion has been detected..> Option provided to activate Relay \nwhen motion is detected ''' y =input('Do you want to start monitoring Motion with PIR sensor if yes press 1\n then Enter or any other number then Enter to exit ') y = int(y) if y == 1: print('\nOk then, Please choose if you would like the Relay \nand/or E-Mail notification active') k = input('\nPressing 1 No Relay No E-Mail, \nPressing 2 Relay activated No Email, \nPressing 3 Relay activated E-Mail notification, \nPressing 4 E-Mail notification No Relay \nType a number (1 - 4) and press Enter\n') k = int(k) if k == 1: print('Well, ok then you will not get an E-Mail or any Relay when Motion is Detected') elif k == 2: print('Ok then, When Motion is detected I will turn on a Relay') elif k == 3: print('Ok then, You will recieve an E-Mail notification anytime motion is detected And I will activate a relay') else: gpio.output(11, False) print('Ok then, You will recieve an E-Mail notification anytime motion is detected, I will NOT activate a relay') else: quit() def motion_detect(): ryme=t.localtime(t.time()) name='%d_%d_%d'%(ryme[1],ryme[2],ryme[0]) try: while True: if gpio.input(7)==1: if k == 2: gpio.output(11, True) elif k == 3: server=smtplib.SMTP('smtp.gmail.com',587) server.starttls() server.login("youracount@gmail.com", "thePasswordForthataccountHERE") msg = "Motion Detection Responses recieved from PIR HC-SR501------- Remember to check the Log on the Local machine for Time Stamps upon your return" server.sendmail("youracount@gmail.com",'OutGoingToEmail@wherever.com', msg) server.quit() print ('Email message has been sent \n End Program with Ctrl C') gpio.output(11, True) elif k == 4: server=smtplib.SMTP('smtp.gmail.com',587) server.starttls() server.login("youracount@gmail.com", "thePasswordForthataccountHERE") msg = "Motion Detection Responses recieved from PIR HC-SR501------- Remember to check the Log on the Local machine for Time Stamps upon your return" server.sendmail('youracount@gmail.com' ,'OutGoingToEmail@wherever.com', msg) server.quit() gpio.output(11, False) else: gpio.output(11, False) print'1 Second of motion detected @ ----> PIR HC-SR501 sensor ' t.sleep(3.5) if gpio.input(7)==0: gpio.output(11, False) print'\n actively monitoring with PIR501 the current machine time is' print name+(t.strftime('_%I;%M;%S')) print '\n Terminate Program with Ctrl C' except KeyboardInterrupt: gpio.cleanup() print'\n Terminated by User' sys.exit() if __name__ == '__main__': motion_detect()