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

help needed for clubbing programs.. of SMS and email

Sat Apr 02, 2016 7:57 am

I am making a project on Motion detecting camera
I have used two PIR sensors..
whenever pir1 is detected it will record video and whenever pir2 is detected it will capture images...
I have used the following code..

Code: Select all

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

def get_file_name():
        return datetime.datetime.now().strftime("%Y-%m-%d_%H.%M.%S.h264")
     

sensor1=40
sensor2=7
video = 0

GPIO.setmode(GPIO.BOARD)
GPIO.setup(sensor1, GPIO.IN, GPIO.PUD_DOWN)
GPIO.setup(sensor2, GPIO.IN, GPIO.PUD_DOWN)

previous_state1 = False
current_state1  = False
previous_state2 = False
current_state2  = False

cam=picamera.PiCamera()

while True:
   time.sleep(0.1)
   previous_state1=current_state1
   current_state1=GPIO.input(sensor1)
   previous_state2=current_state2
   current_state2=GPIO.input(sensor2)
   
   if current_state1!=previous_state1:
      new_state1="HIGH" if current_state1 else "LOW"
      print("GPIO pin %s is %s"%(sensor1,new_state1))
      if current_state1:
         video = 1
         fileName=get_file_name()
         cam.start_preview()
         cam.start_recording(fileName)
      else:
         cam.stop_preview()
         cam.stop_recording()
         video = 0
         
   if current_state2!=previous_state2 and video == 0:
      new_state2="HIGH" if current_state2 else "LOW"
      print("GPIO pin %s is %s"%(sensor2,new_state2))
      if current_state2:
         fileName=get_file_name()
         cam.start_preview()
         time.sleep(2)
         cam.capture(fileName + '.jpg')
         cam.stop_preview()
now i want whenever pir1 is detected it should send a sms.. & whenever pir2 is detected it should send me an email..
sms code is as follow:

Code: Select all


from twilio.rest import TwilioRestClient

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="+9199999999xx"

    from_="+129872000",
)
print message.sid

and my email code is as follow:

Code: Select all

import smtplib

content='PIR motion detected'
mail=smtplib.SMTP('smtp.gmail.com',587)
mail.ehlo()
mail.starttls()
mail.login('email@domain.com','mypassword')
mail.sendmail('email-id',content)
mail.close()   

I have performed both the above codes individually and they worked perfectly :twisted: ...now I want to club above 3 codes.in 1 program...

so plz can anyone club this codes ... :mrgreen:

Return to “Beginners”