AmolV
Posts: 13
Joined: Thu Jun 26, 2014 6:10 pm

PIR Sensor + Email

Thu Aug 21, 2014 5:10 pm

Hello Everyone,
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

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()
Both these programs work perfectly individually.
Now all I need is a little help in integrating these two codes together as all my previous attempts at doing so have failed.
Please help :roll:

bartv
Posts: 35
Joined: Thu Aug 14, 2014 7:28 pm

Re: PIR Sensor + Email

Thu Aug 21, 2014 9:46 pm

Didn't test it, but i think something like this should work?

Code: Select all

# Import required Python libraries
import RPi.GPIO as GPIO
import time
import smtplib
 
#GMAIL mail setup
TO = '[email protected]'
SUBJECT = 'PIR'
TEXT = 'Motion is detected'

#GMAIL user setup
gmail_sender = '[email protected]'
gmail_passwd = 'mypwd'

#GMAIL stuff to login
server = smtplib.SMTP('smtp.gmail.com', 587)
server.ehlo()
server.starttls()
server.ehlo
server.login(gmail_sender, gmail_passwd)

#GMAIL Message
BODY = '\r\n'.join([
       'TO: %s' % TO,
       'From: %s' % gmail_sender ,
       'Subject: %s' % SUBJECT ,
       '',
       TEXT
       ])

# 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!"
      #Send mail
      server.sendmail(gmail_sender, [TO], BODY)
      # 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()
  server.quit()

AmolV
Posts: 13
Joined: Thu Jun 26, 2014 6:10 pm

Re: PIR Sensor + Email

Sat Aug 23, 2014 6:41 am

This was the first thing I tried but it shows invalid syntax for server.send mail :(

User avatar
joan
Posts: 14191
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: PIR Sensor + Email

Sat Aug 23, 2014 7:43 am

AmolV wrote:This was the first thing I tried but it shows invalid syntax for server.send mail :(
You now have a learning opportunity. Study the documentation and use the correct syntax.

User avatar
davef21370
Posts: 897
Joined: Fri Sep 21, 2012 4:13 pm
Location: Earth But Not Grounded

Re: PIR Sensor + Email

Sat Aug 23, 2014 2:09 pm

I may be missing something but why is TO in square brackets in the server.sendmail function?

Dave.
Apple say... Monkey do !!

AmolV
Posts: 13
Joined: Thu Jun 26, 2014 6:10 pm

Re: PIR Sensor + Email

Mon Aug 25, 2014 3:06 pm

It worked. All I did was declared the email data after the pir gpio data.

memo-chan
Posts: 5
Joined: Tue Mar 10, 2015 6:51 am

Re: PIR Sensor + Email

Tue Apr 21, 2015 7:54 am

You mean after declaring the pin?

Return to “Python”