Hi All,
My project consists of one RPi 3 with motion sensor and camera attached. All this is linked to my gmail account. Once motion is detected, it will take a recording and email me saying that motion has been detected and it should also attach the image. All this happens automatically. I have ssmtp all set up for my gmail and everything works perfect, apart from the image is not attached to the email. So, I get the notification, but with no image attached. Please help. Please see my script below and please advice as I am stuck.
#!/usr/bin/python
import RPi.GPIO as GPIO
import time
import picamera
import datetime
import subprocess
import smtplib
import os
AuthUser = 'myemail@gmail.com'
AuthPass = 'mypassword'
toAdd = 'myemail@gmail.com'
fromAdd = AuthUser
subject = 'Motion Detected'
def getFileName():
return datetime.datetime.now().strftime("%Y-%m-%d_%H.%M.%S.h264")
mail = smtplib.SMTP('smtp.gmail.com',587)
gmailUpload = ()
mail.ehlo()
mail.starttls()
mail.login(AuthUser, AuthPass)
mail.send_mail = (fromAdd, toAdd)
#sensor setup
sensorPin = 7
GPIO.setmode(GPIO.BOARD)
GPIO.setup(sensorPin, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
prevState = False
currState = False
camera = picamera.PiCamera()
while True:
time.sleep(0.1)
prevState = currState
currState = GPIO.input(sensorPin)
if currState != prevState:
newState = "HIGH" if currState else "LOW"
print ("GPIO pin %s is %s" % (sensorPin, newState))
if currState:
fileName = getFileName()
print ("Starting Recording...")
camera.start_preview()
camera.start_recording(fileName)
print (fileName)
else:
camera.stop_preview()
camera.stop_recording()
print ("Stopped Recording")
print ("Sending Mail Notification...")
subprocess.call("mail -s 'Motion Detected' myemail@gmail.com < /home/pi/mail/images", shell=True)
print ("Complete")