I am doing a project using a PIR sensor and the raspberry pi.
All I want to do is that when the PIR pin goes high i.e when the motion is detected , it should send an email suggesting that the motion is detected .
I have following code for the PIR sensor from raspberry spy http://www.raspberrypi-spy.co.uk/2013/0 ... pi-part-1/
Code: Select all
# Import required Python libraries import RPi.GPIO as GPIO import time # Use BCM GPIO references # instead of physical pin numbers GPIO.setmode(GPIO.BCM) # Define GPIO to use on Pi GPIO_PIR = 7 print "PIR Module Test (CTRL-C to exit)" # Set pin as input GPIO.setup(GPIO_PIR,GPIO.IN) # Echo Current_State = 0 Previous_State = 0 try: print "Waiting for PIR to settle ..." # Loop until PIR output is 0 while GPIO.input(GPIO_PIR)==1: Current_State = 0 print " Ready" # Loop until users quits with CTRL-C while True : # Read PIR state Current_State = GPIO.input(GPIO_PIR) if Current_State==1 and Previous_State==0: # PIR is triggered print " Motion detected!" # Record previous state Previous_State=1 elif Current_State==0 and Previous_State==1: # PIR has returned to ready state print " Ready" Previous_State=0 # Wait for 10 milliseconds time.sleep(0.01) except KeyboardInterrupt: print " Quit" # Reset GPIO settings GPIO.cleanup()
And this is the code for sending email using smtp
Both these programs work perfectly individually.
Code: Select all
#!/usr/bin/python import smtplib TO = '[email protected]' SUBJECT = 'PIR' TEXT = 'Motion is detected' gmail_sender = '[email protected]' gmail_passwd = 'mypwd' server = smtplib.SMTP('smtp.gmail.com', 587) server.ehlo() server.starttls() server.ehlo server.login(gmail_sender, gmail_passwd) BODY = '\r\n'.join([ 'TO: %s' % TO, 'From: %s' % gmail_sender , 'Subject: %s' % SUBJECT , '', TEXT ]) try: server.sendmail(gmail_sender, [TO], BODY) print 'email sent' except: print 'error' server.quit()
Now all I need is a little help in integrating these two codes together as all my previous attempts at doing so have failed.