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
1 Answer 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.
-
so can you suggest me a proper indent...Himani– Himani2016年04月04日 14:49:55 +00:00Commented Apr 4, 2016 at 14:49
-
and where should i have to write calling statement as i am defining function??Himani– Himani2016年04月04日 14:50:36 +00:00Commented Apr 4, 2016 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.Steve Robillard– Steve Robillard2016年04月04日 14:56:13 +00:00Commented Apr 4, 2016 at 14:56
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 yourwhile True
loop except sleeping for hours, have all the action in a callback function and only wake up the script when motion is detected.