Page 1 of 1

Motion detection, email notification

Posted: Thu Feb 09, 2017 5:29 pm
by dannyV
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 = '[email protected]'
AuthPass = 'mypassword'

toAdd = '[email protected]'
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' [email protected] < /home/pi/mail/images", shell=True)
print ("Complete")

Re: Motion detection, email notification

Posted: Thu Feb 09, 2017 9:33 pm
by pcmanbob
Hi.

I use ssmtp to send my daily speed test results file from my broadband speed testing Pi.

To do this I found I had to use mpack with ssmtp

http://ozzmaker.com/send-email-from-the ... tachments/

then I send the file like this

Code: Select all

mpack -s "Broadband Speed Testing Results with txt file attached" -d /home/pi/message.txt /home/pi/speed_test_results.txt $email_addresses
so the bit between " " is the subject line
-d /home/pi/message.txt his file contains the actual message text.
the last bit /home/pi/speed_test_results.txt is the file you want to send.
after this you have the address you want to send too.

hope this helps

Re: Motion detection, email notification

Posted: Thu Feb 09, 2017 10:26 pm
by VAXpascal
One of my to do list items, email send from RPi, thanks for posting!!!

Python code for motion detection and mail sending

Posted: Sun Oct 15, 2017 2:26 pm
by AnithaU
Please give me the python code to detect motion using PIR sensor, capture image and send mail with the image captured.
Kindly do the needful.

Re: Motion detection, email notification

Posted: Sun Oct 15, 2017 5:47 pm
by tpylkko
Here is something I can recommend
The goal here is to make it as simple and painless as possible to send emails
You can basically send email with gmail.with one line. It also does not save your password in the script, but uses the keyring

https://github.com/kootenpv/yagmail/blo ... /README.md