himani18
Posts: 44
Joined: Fri Feb 19, 2016 4:52 pm

error in sending sms on motion detecting

Mon Apr 04, 2016 1:03 pm

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

Code: Select all

import RPi.GPIO as GPIO
import time
import picamera
import datetime
from twilio.rest import TwilioRestClient

def get_file_name():
        return datetime.datetime.now().strftime("%Y-%m-%d_%H.%M.%S.h264")
def sms():
		ACCOUNT_SID = "my_Acc_Sid"
		AUTH_TOKEN = "my_auth_token"
		client = TwilioRestClient(ACCOUNT_SID, AUTH_TOKEN)
		message = client.messages.create(
		body="Hello Motion Detected.... !",  # Message body, if any
		to="+91xxxxx",
		from_="+121xxxxx",
		)
		print message.sid
		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: ", sms()
		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 my getting this error:
File "t4.py", line 34
print "Values outside the function: ", sms()
^
IndentationError: expected an indented block


please solve this error for me... :(
Last edited by himani18 on Mon Apr 04, 2016 2:10 pm, edited 2 times in total.

DirkS
Posts: 10497
Joined: Tue Jun 19, 2012 9:46 pm
Location: Essex, UK

Re: error in sending sms on motion detecting

Mon Apr 04, 2016 1:25 pm

Are those '' bits in your code or did you try to make them bold for posting on the forum (which does not work in a code block)?

Your code should look like this:

Code: Select all

   if current_state!=previous_state:
      print "Values outside the function: ", sms()
      new_state="HIGH" if current_state else "LOW"
      print("GPIO pin %s is %s"%(sensor,new_state))

himani18
Posts: 44
Joined: Fri Feb 19, 2016 4:52 pm

Re: error in sending sms on motion detecting

Mon Apr 04, 2016 2:05 pm

i have used that for bold fonts... :lol:

DirkS
Posts: 10497
Joined: Tue Jun 19, 2012 9:46 pm
Location: Essex, UK

Re: error in sending sms on motion detecting

Mon Apr 04, 2016 2:10 pm

himani18 wrote:i have used that for bold fonts... :lol:
Ok, but how does the code on your Pi look like?
NB: Python can even trip over a combination of space and TAB characters, so the code can look as if it's properly indented, but it isn't.

himani18
Posts: 44
Joined: Fri Feb 19, 2016 4:52 pm

Re: error in sending sms on motion detecting

Mon Apr 04, 2016 2:23 pm

yaa...I have indented properly...

DirkS
Posts: 10497
Joined: Tue Jun 19, 2012 9:46 pm
Location: Essex, UK

Re: error in sending sms on motion detecting

Mon Apr 04, 2016 3:19 pm

himani18 wrote:yaa...I have indented properly...
Are you sure? The print statement in your code above is still at the wrong level...

gordon77
Posts: 5374
Joined: Sun Aug 05, 2012 3:12 pm

Re: error in sending sms on motion detecting

Mon Apr 04, 2016 3:21 pm

DirkS wrote:
himani18 wrote:yaa...I have indented properly...
Are you sure? The print statement in your code above is still at the wrong level...
I thought that but when I cut and pasted it it was OK.

DirkS
Posts: 10497
Joined: Tue Jun 19, 2012 9:46 pm
Location: Essex, UK

Re: error in sending sms on motion detecting

Mon Apr 04, 2016 3:32 pm

gordon77 wrote:
DirkS wrote:
himani18 wrote:yaa...I have indented properly...
Are you sure? The print statement in your code above is still at the wrong level...
I thought that but when I cut and pasted it it was OK.
Not in my editor. It still shows up as

Code: Select all

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: ", sms()
      new_state="HIGH" if current_state else "LOW"
      print("GPIO pin %s is %s"%(sensor,new_state))
      if current_state:
with the print on the same level as the 'if' above it.

himani18
Posts: 44
Joined: Fri Feb 19, 2016 4:52 pm

Re: error in sending sms on motion detecting

Mon Apr 04, 2016 4:08 pm

Code: Select all

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: ",sms()
		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()  	
thats how i intended it..

Magic Knight
Posts: 33
Joined: Sat Jan 12, 2013 5:14 pm

Re: error in sending sms on motion detecting

Mon Apr 04, 2016 4:50 pm

Whats does sms() return, is it a string ?

himani18
Posts: 44
Joined: Fri Feb 19, 2016 4:52 pm

Re: error in sending sms on motion detecting

Mon Apr 04, 2016 4:58 pm

naa...

i am calling a method...

Code: Select all

def sms():
        ACCOUNT_SID = "myaccountsid"
	AUTH_TOKEN = "myauthtoken"
	client = TwilioRestClient(ACCOUNT_SID, AUTH_TOKEN)
	message = client.messages.create(
	body="Hello Motion Detected.... !",  # Message body, if any
	to="+91xxxxxxx",
	from_="+121xxxxx",
	)
	print message.sid
	return	

User avatar
elParaguayo
Posts: 1943
Joined: Wed May 16, 2012 12:46 pm
Location: London, UK

Re: error in sending sms on motion detecting

Mon Apr 04, 2016 6:26 pm

himani18 wrote:

Code: Select all

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: ",sms()
		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()  	
thats how i intended it..
Ok - but this line is indented wrongly

Code: Select all

print "Values outside the function: ",sms()
and the "else:" line.
Last edited by elParaguayo on Mon Apr 04, 2016 6:29 pm, edited 1 time in total.
RPi Information Screen: plugin based system for displaying weather, travel information, football scores etc.

User avatar
elParaguayo
Posts: 1943
Joined: Wed May 16, 2012 12:46 pm
Location: London, UK

Re: error in sending sms on motion detecting

Mon Apr 04, 2016 6:28 pm

himani18 wrote:naa...

i am calling a method...

Code: Select all

def sms():
        ACCOUNT_SID = "myaccountsid"
	AUTH_TOKEN = "myauthtoken"
	client = TwilioRestClient(ACCOUNT_SID, AUTH_TOKEN)
	message = client.messages.create(
	body="Hello Motion Detected.... !",  # Message body, if any
	to="+91xxxxxxx",
	from_="+121xxxxx",
	)
	print message.sid
	return	
You're mixing tabs and spaces. More often than not, this will lead to problems. You probably thought this looked ok when pasting the code, but look at the ACCOUNT_SID line, the indentation is different from the next line.

Also, you probably want to return message.sid otherwise printing "sms()" will print "None".
RPi Information Screen: plugin based system for displaying weather, travel information, football scores etc.

himani18
Posts: 44
Joined: Fri Feb 19, 2016 4:52 pm

Re: error in sending sms on motion detecting

Tue Apr 05, 2016 1:09 pm

Code: Select all

def sms():
   ACCOUNT_SID = "myaccountsid"
   AUTH_TOKEN = "myauthtoken"
   client = TwilioRestClient(ACCOUNT_SID, AUTH_TOKEN)
   message = client.messages.create(
   body="Hello Motion Detected.... !",  # Message body, if any
   to="+91xxxxxxx",
   from_="+121xxxxx",
   )
   print message.sid
   return   message.sid
is it ok now??? :P

User avatar
elParaguayo
Posts: 1943
Joined: Wed May 16, 2012 12:46 pm
Location: London, UK

Re: error in sending sms on motion detecting

Tue Apr 05, 2016 4:14 pm

himani18 wrote:

Code: Select all

def sms():
   ACCOUNT_SID = "myaccountsid"
   AUTH_TOKEN = "myauthtoken"
   client = TwilioRestClient(ACCOUNT_SID, AUTH_TOKEN)
   message = client.messages.create(
   body="Hello Motion Detected.... !",  # Message body, if any
   to="+91xxxxxxx",
   from_="+121xxxxx",
   )
   print message.sid
   return   message.sid
is it ok now??? :P
Why ask me? Try running it!
RPi Information Screen: plugin based system for displaying weather, travel information, football scores etc.

himani18
Posts: 44
Joined: Fri Feb 19, 2016 4:52 pm

Re: error in sending sms on motion detecting

Thu Apr 07, 2016 2:36 pm

my final code is this:

Code: Select all

import RPi.GPIO as GPIO
import time
import picamera
import datetime
from twilio.rest import TwilioRestClient
def get_file_name():
    return datetime.datetime.now().strftime("%Y-%m-%d_%H.%M.%S.h264")
def sms():
    ACCOUNT_SID = "Accountsid"
    AUTH_TOKEN = "accounttoken"
    client = TwilioRestClient(ACCOUNT_SID, AUTH_TOKEN)
    message = client.messages.create(
    body="Hello Motion Detected.... !",  # Message body, if any
    to="+9197xxxxxxx",
    from_="+121xxxxxxxx",
    )
    print message.sid
    return message.sid
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 "sent ", sms
        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()                     
 
It records videos
but still it is not sending sms..
can anyone help me for this?? :!:

and i am getting this type of o/p
sent <function sms at 0x76373fb0>
GPIO pin 7 is HIGH
sent <function sms at 0x76373fb0>
GPIO pin 7 is LOW
sent <function sms at 0x76373fb0>
GPIO pin 7 is HIGH
sent <function sms at 0x76373fb0>
GPIO pin 7 is LOW

DirkS
Posts: 10497
Joined: Tue Jun 19, 2012 9:46 pm
Location: Essex, UK

Re: error in sending sms on motion detecting

Thu Apr 07, 2016 2:47 pm

Code: Select all

        print "sent ", sms
This just prints the address of the function 'sms', but doesn't run it.
Try

Code: Select all

        print "sent ", sms()

himani18
Posts: 44
Joined: Fri Feb 19, 2016 4:52 pm

Re: error in sending sms on motion detecting

Thu Apr 07, 2016 2:53 pm

so how can I call the function sms?? :idea:

i have tried this code

Code: Select all

import RPi.GPIO as GPIO
import time
import picamera
import datetime
from twilio.rest import TwilioRestClient
def get_file_name():
    return datetime.datetime.now().strftime("%Y-%m-%d_%H.%M.%S.h264")
def sms():
    ACCOUNT_SID = "Accountsid"
    AUTH_TOKEN = "accounttoken"
    client = TwilioRestClient(ACCOUNT_SID, AUTH_TOKEN)
    message = client.messages.create(
    body="Hello Motion Detected.... !",  # Message body, if any
    to="+9197xxxxxxx",
    from_="+121xxxxxxxx",
    )
    print message.sid
    return message.sid
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:
        sms
        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()                     
 

DirkS
Posts: 10497
Joined: Tue Jun 19, 2012 9:46 pm
Location: Essex, UK

Re: error in sending sms on motion detecting

Thu Apr 07, 2016 3:03 pm

himani18 wrote:so how can I call the function sms?? :idea:
That's what should happen when the print statement is called (my modified version above)

Alternatively use:

Code: Select all

        smsReturn =  sms()
        print "sent ", smsReturn

gordon77
Posts: 5374
Joined: Sun Aug 05, 2012 3:12 pm

Re: error in sending sms on motion detecting

Thu Apr 07, 2016 3:03 pm

similar to how you call get_file_name() ?

use sms() ?

Code: Select all

if current_state:
            fileName=get_file_name()
            sms()
            cam.start_preview()
            cam.start_recording(fileName)               
        else:
            cam.stop_preview()
            cam.stop_recording()

Return to “Python”