Upon testing, I am able to capture image using webca, and able to send email without attachment.
Now all i Need is the full coding to be edited in a way that when my PIR motion sensor detects motion, it must trigger the buzzer and trigger to webcam to snap picture. And then i want it to send attachment to the gmail account.
p/s: email provided is just a sample from internet.
Code: Select all
#!/usr/bin/env python
import smtplib
from email.MIMEMultipart import MIMEMultipart
from email.MIMEBase import MIMEBase
from email.MIMEText import MIMEText
from email.Utils import COMMASPACE, formatdate
from email import Encoders
import os
import RPi.GPIO as GPIO
import time
import picamera # new
USERNAME = "raspberrypipi795@gmail.com"
PASSWORD = "berry1993"
sensor = 24
led = 17
buzzer = 27
system_start = False
taken_image = False
hasSendMail = False
GPIO.setmode(GPIO.BCM)
GPIO.setup(sensor, GPIO.IN, GPIO.PUD_DOWN)
GPIO.setup(led, GPIO.OUT)
GPIO.setup(buzzer, GPIO.OUT)
previous_state = False
current_state = False
def sendMail(to, subject, text, files=[]):
assert type(to)==list
assert type(files)==list
msg = MIMEMultipart()
msg['From'] = USERNAME
msg['To'] = COMMASPACE.join(to)
msg['Date'] = formatdate(localtime=True)
msg['Subject'] = subject
msg.attach( MIMEText(text) )
for file in files:
part = MIMEBase('application', "octet-stream")
part.set_payload( open(file,"rb").read() )
Encoders.encode_base64(part)
part.add_header('Content-Disposition', 'attachment; filename="%s"'
% os.path.basename(file))
msg.attach(part)
server = smtplib.SMTP('smtp.gmail.com:587')
server.ehlo_or_helo_if_needed()
server.starttls()
server.ehlo_or_helo_if_needed()
server.login(USERNAME,PASSWORD)
server.sendmail(USERNAME, to, msg.as_string())
server.quit()
cam = picamera.PiCamera() # new
while True:
time.sleep(1)
openFile = open("data_cmd.txt","r")
operation = openFile.read()
if operation == "Activate":
print "Activate"
system_start = True
elif operation == "Deactivate":
print "Dectivate"
if hasSendMail == True:
GPIO.output(led, False)
GPIO.output(buzzer, False)
#cam.stop_preview()
hasSendMail = False
system_start = False
elif operation == "Capture_Image":
taken_image = True
openFile = open("data_cmd.txt","w")
appendFile = openFile.write("")
openFile.close()
if taken_image == True:
print "Capture Image"
cam.capture('/var/www/inmosis/image/image.jpg')
openFile = open("image_file.txt","w")
appendFile = openFile.write("image.jpg")
taken_image = False
if system_start == True:
previous_state = current_state
current_state = GPIO.input(sensor)
if current_state != previous_state:
new_state = "HIGH" if current_state else "LOW"
print("GPIO pin %s is %s" % (sensor, new_state))
if current_state: # new
print "Image"
GPIO.output(led, True)
GPIO.output(buzzer,1)
#cam.start_preview()
cam.capture('image.jpg')
sendMail( ["fuzza93@gmail.com"],"Motion detected!!!","Attached is the captured image",["image.jpg"] )
hasSendMail = True
print "Send Mail"
else:
GPIO.output(led, False)
GPIO.output(buzzer, False)
#cam.stop_preview()