Introduction : In this tutorial, Iâll show you how to send emails containing pictures using a Raspberry
Introduction :
In this tutorial , Iâll show you how to send emails containing pictures using a Raspberry Pi4 and a motion detector and the Python programming language. We will use a PIR motion detection sensor and whenever motion is detected, the Raspberry Pi4 Camera will take a picture and send an email with the picture attached.
In this tutorial i divide this project some step if you flow this step with me i hope you can do this
Step-1:
You have to need some change in your google account flow this step
- Login to your Gmail account by entering your login credentials.
- Click on your profile picture and then click on âGoogle accountâ.
- Under âSign-in and Securityâ click âConnected apps and sitesâ.
- Click âAllow less secure appsâ to turn it on.(By Default its Turn off)
- Step-2
Circuit Diagram
The following Fritzing based images shows all the connections with respect to the PIR Motion Sensor using Raspberry Pi4How to Use the Raspberry Pi4 Camera And PIR Sensor to Send Emails
Circuit Design:
Connect the VCC and GND pins of the PIR Motion Sensor to +5V and GND pins of the Raspberry Pi4. Connect the DATA Pin of the PIR Sensor to GPIO24 of the Raspberry Pi4.
A 5V Buzzer is connected to GPIO20 And Raspberry pi4 Camera Connect to the PI Connector Slot. The other pin of the buzzer connected to GND.
The Python Code to Send Emails
Before running the code, make sure the sender and receiver email addresses and the password of the email sender are all entered.
The code will read the output from the sensor and, upon detecting motion, will capture an image and save it on the database folder.
When you run the code for the first time, it will automatically create the folder. Once the camera captures an image, the image will be attached in the email and sent to the senderâs address.
My Previous Project :
PIR Motion Sensor using Raspberry Pi4 | Interfacing Tutorial
Raspberry pi4 Workshop PIR Sensor -Email Sending Movement Detector using IFTTT
Controlling a DC Motor with Raspberry Pi4
Custom parts and enclosures
Code
import RPi.GPIO as GPIO | |
import time | |
import datetime | |
import picamera | |
import os | |
import smtplib | |
from email import encoders | |
from email.mime.base import MIMEBase | |
from email.mime.multipart import MIMEMultipart | |
camera = picamera.PiCamera() | |
GPIO.setmode(GPIO.BCM) | |
GPIO.setup(23, GPIO.IN) #PIR | |
GPIO.setup(24, GPIO.OUT) #BUzzer | |
ââ | |
ts = time.time() | |
st = datetime.datetime.fromtimestamp(ts).strftime(â%Y-%m-%d %H:%M:%Sâ) | |
ââ | |
COMMASPACE = â, â | |
def Send_Email(image): | |
sender = â###YOUREMAIL###â | |
gmail_password = â###YOURPASSWORD###â | |
recipients = [â##YOURRECIPENTEMAIL###â] | |
# Create the enclosing (outer) message | |
outer = MIMEMultipart() | |
outer[âSubjectâ] = âAttachment Testâ | |
outer[âToâ] = COMMASPACE.join(recipients) | |
outer[âFromâ] = sender | |
outer.preamble = âYou will not see this in a MIME-aware mail reader.\nâ | |
# List of attachments | |
attachments = [image] | |
# Add the attachments to the message | |
for file in attachments: | |
try: | |
with open(file, ârbâ) as fp: | |
msg = MIMEBase(âapplicationâ, âoctet-streamâ) | |
msg.set_payload(fp.read()) | |
encoders.encode_base64(msg) | |
msg.add_header(âContent-Dispositionâ, âattachmentâ, filename=os.path.basename(file)) | |
outer.attach(msg) | |
except: | |
print(âUnable to open one of the attachments. Error: â, sys.exc_info()[0]) | |
raise | |
composed = outer.as_string() | |
# Send the email | |
try: | |
with smtplib.SMTP(âsmtp.gmail.comâ, 587) as s: | |
s.ehlo() | |
s.starttls() | |
s.ehlo() | |
s.login(sender, gmail_password) | |
s.sendmail(sender, recipients, composed) | |
s.close() | |
print(âEmail sent!â) | |
except: | |
print(âUnable to send the email. Error: â, sys.exc_info()[0]) | |
raise | |
try: | |
time.sleep(2) # to stabilize sensor | |
while True: | |
##Timeloop | |
ts = time.time() | |
st = datetime.datetime.fromtimestamp(ts).strftime(â%Y-%m-%d %H:%M:%Sâ) | |
if GPIO.input(23): | |
##If loop | |
GPIO.output(24, True) | |
time.sleep(0.5) #Buzzer turns on for 0.5 sec | |
print(âMotion Detected at {}â.format(st)) | |
##Adds timestamp to image | |
camera.capture(âimage_Time_{}.jpgâ.format(st)) | |
image = (âimage_Time_{}.jpgâ.format(st)) | |
Send_Email(image) | |
time.sleep(2) | |
GPIO.output(24, False) | |
time.sleep(5) #to avoid multiple detection | |
time.sleep(0.1) #loop delay, should be less than detection delay | |
except: | |
GPIO.cleanup() |
Source: How to Use the Raspberry Pi4 Camera And PIR Sensor to Send E