0

I am building motion sensor camera,,, so whenever any motion will be detected it should send email.. i have tried following code for that..

import RPi.GPIO as GPIO
import time
import picamera
import datetime
import smtplib

def get_file_name():
    return datetime.datetime.now().strftime("%Y-%m-%d_%H.%M.%S.h264")
def email():
    content='PIR motion detected'
    mail=smtplib.SMTP('smtp.gmail.com',587)
    mail.ehlo()
    mail.starttls()
    mail.login('myemail','mypassword')
    mail.sendmail('myemail','myemail',content)
    mail.close()   
    return  

sensor=7
GPIO.setmode(GPIO.BOARD)
GPIO.setup(sensor, GPIO.IN, GPIO.PUD_DOWN)

previous_state=False
current_state=False

cam=picamera.PiCamera()

while True:
 time.sleep(0.01)
 previous_state=current_state
 current_state=GPIO.input(sensor)
 if current_state!=previous_state:
 print "Values outside the function: ", email()
    new_state="HIGH" if current_state else "LOW"
    print("GPIO pin %s is %s"%(sensor,new_state))
    if current_state:
                    fileName=get_file_name()
                    cam.start_preview()
                    cam.start_recording(fileName)

            else:
                    cam.stop_preview()
                    cam.stop_recording()

GPIO.cleanup()                                                  

but i am getting following error.. File "t5.py", line 33 print "Values outside the function: ", email() ^ IndentationError: expected an indented block

2
  • 1
    Hello and welcome. While it is not an issue wrt the question, something to consider for future coding: per PEP0008 Use 4 spaces per indentation level.
    – Ghanima
    Apr 4 '16 at 14:35
  • You could benefit from using GPIO interrupts rather than polling. RPI.GPIO has this feature and is easy to use. That way, you wouldn't have to run the script every 0.01 seconds (!) but could instead do nothing in your while True loop except sleeping for hours, have all the action in a callback function and only wake up the script when motion is detected.
    – jDo
    Apr 4 '16 at 19:47
1

You need to indent this line

print "Values outside the function: ", email()

because it is part of the if statement above it. Change it to this:

 if current_state!=previous_state:
     print "Values outside the function: ", email()

This line

new_state="HIGH" if current_state else "LOW"

looks wrong as well. Is this supposed to be two separate lines? It also has a non python if statement else and no :. shouldn't it be something like

new_state="HIGH" 
if not current_state:
    current_state = "LOW"

As @Ghanima mentioned above your indents should all be 4 spaces. You are using a variety of different indents.

3
  • so can you suggest me a proper indent...
    – Himani
    Apr 4 '16 at 14:49
  • and where should i have to write calling statement as i am defining function??
    – Himani
    Apr 4 '16 at 14:50
  • I updated my answer to show you the indent. To call a function you call it from your script outside of the function below its definition. Note comments should not be used to ask a continuing string of questions. Rather you should be asking a new question. However, questions on the basics of Python are off topic here and should be asked on our sister site stackoverflow.com. Apr 4 '16 at 14:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.