IoT based Smart Wi-Fi doorbell using Raspberry Pi and PiCamera

In the current era, security is a major concern, and the market offers a wide range of surveillance and security systems. However, these solutions can be quite expensive and may come with their own set of problems that are difficult to resolve. In a previous project, we constructed a surveillance camera capable of streaming live video to an IoT cloud. Today, we will embark on building a cost-effective Smart Wi-Fi doorbell using a Raspberry Pi.

This system is designed to capture and send a picture of the visitor via email when the doorbell switch is pressed. A PiCamera is connected to the Raspberry Pi to capture the image, although a USB webcam can also be utilized if a PiCamera is unavailable. This system can be installed at the main entrance of your home or office and can be remotely monitored from anywhere in the world via the internet.

Requirements

To build the Raspberry Pi-based Smart Wi-Fi doorbell, you will need the following components:

– Raspberry Pi with Raspbian OS installed
– Pi Camera or USB webcam
– Push button
– Jumper wires

In this project, we will utilize SSH to access our Raspberry Pi from a laptop. If you have a monitor available, starting the setup process will be straightforward. However, if you don't have a monitor, you can set up the Raspberry Pi in headless mode or use a VNC server to access the Raspberry Pi's desktop on your laptop.

Raspberry Pi is widely acclaimed for building IoT-based projects due to its comprehensive support for Internet of Things applications. It is a compact computer that comes equipped with built-in features such as Wi-Fi, Bluetooth, USB ports, audio/video ports, HDMI port, camera port, and more. You can explore a variety of Raspberry Pi-based IoT projects by referring to the available resources.

Connection Diagram

The circuit diagram for the Raspberry Pi Smart Doorbell is straightforward. All you need to do is connect a button to one of the GPIO pins on the Raspberry Pi and attach the Pi Camera to the camera slot.

Setup Pi Camera or USB webcam with Raspberry Pi

PiCamera:

1. If you are using picam then you have to enable camera interfacing from raspi-config. Run the sudo raspi-config and go to Interfacing options.

2. Then select the Camera option and Enable it on the next window and restart the Pi.

3. Now, test the camera by capturing a photo using below command.

raspistill -o image.jpg

If you got an image in Pi directory then you are ready to go else check your camera strip and camera module.

USB Camera:

If you are using USB webcam then you have to install some packages to enable the webcam functionalities. Install the package using below command

sudo apt-get install fswebcam

Now, check for the working of the camera by capturing a photo using following command

fswebcam image.jpg

Use above command to replace the picamera functionality in final code.

Now, we are ready on hardware side. Its time to install SMTP libraries and setup Google account.

Installing SMTP on Raspberry Pi to send Emails

To send emails using Simple Message Transfer Protocol (SMTP), which is a communication protocol for email transfer, we need to install certain libraries and packages on the Raspberry Pi. This allows us to easily send emails using command line or Python scripts.

Installing SMTP libraries

If you are using a newer version of Raspbian, the SMTP library packages are likely already installed. However, you can reinstall them to ensure that all packages are properly installed. To do this, follow these steps:

sudo apt-get update
sudo apt-get upgrade

2. Now, install SMTP library packages using  following commands

sudo apt-get install ssmtp
sudo apt-get install mailutils

Modify Security Settings in Google Mail account

Google does not allow to send and receive e-mails which contains Python code. So, we have to update some security settings in Google account. Follow below steps to enable “Allow less secure apps” permission.

1. Login to your Gmail account by entering your login credentials.

2. Click on profile picture and then click on “Google account”.

3. Under Security tab you will find Less secure app access. Turn it ON by Clicking on “Allow less secure apps”.

Code and Explanation

To understand the working of the code, we will explain the Python script used to send an email with a photo attachment when the doorbell switch is pressed. The Pi camera captures the photo, which is then sent to the owner of the house via email.

1. Open your preferred text editor on the Raspberry Pi and import the necessary libraries for the PiCamera, RPi GPIOs, SMTP, and time.

import os
import picamera
import RPi.GPIO as GPIO
import smtplib
from time import sleep

2. Next, import all the required modules for sending an email. To compose the email with plain text, attachments, and a subject, we need a separate module that facilitates the composition of the complete email.

from email.MIMEMultipart import MIMEMultipart
from email.MIMEText import MIMEText
from email.MIMEBase import MIMEBase
from email import encoders

3. Assign your e-mail id, receiver e-mail id and password in a variable as shown.

sender = '[email protected]'
password = 'gmail password'
receiver = '[email protected]'

4. In order to save the captured photo in a directory and give different names to them assign a folder and a prefix name.

dir = './visitors/'
prefix = 'photo'

5. Set pin mode and pin number to attach a push button which act as a doorbell switch.

GPIO.setwarnings(False)
GPIO.setmode(GPIO.BOARD)
GPIO.setup(15, GPIO.IN)

6. Now, make a function to capture photos. In this function we have to check for the directory if it already exists or not. If not then make the directory.

def capture_img():
if not os.path.exists(dir):
        os.makedirs(dir)

Assign a file name and sort it using glob and find the largest ID of existing images and Start new images after this ID value.

 files = sorted(glob.glob(os.path.join(dir, prefix + '[0-9][0-9][0-9].jpg')))
 count = 0

 Grab the count from the last filename.

 if len(files) > 0:
     count = int(files[-1][-7:-4])+1

Now, capture the photo and give it a unique name and save it in the defined folder.

filename = os.path.join(dir, prefix + '%03d.jpg' % count)
with picamera.PiCamera() as camera:
pic = camera.capture(filename)

7. Now, make another function to send mail. In this function we will attach subject, body and attachments and then send all the content to the receiver using SMTP.

def send_mail():
    msg = MIMEMultipart()
    msg['From'] = sender
    msg['To'] = receiver
    msg['Subject'] = New Visitor'
    body = 'Picture is Attached.'
    msg.attach(MIMEText(body, 'plain'))
    attachment = open(filename, 'rb')
..
..
    msg.attach(part)
    server = smtplib.SMTP('smtp.gmail.com', 587)
    server.starttls()
    server.login(sender, password)
    text = msg.as_string()
    server.sendmail(sender, receiver, text)
    server.quit()

Now finally, read the push button value and when its goes high, Raspberry Pi calls the capture_img() function to capture the image of visitor and send a alert email with the picture of visitor as an attachment. Here send_mail() is used inside the capture_img() function for sending the mail.

Testing the PiCamera Doorbell

After wiring the complete code save the file with .py extension and execute this script using below command

python filename.py

If there is no error in the code, press the push button. After 4-5 seconds you should receive a mail with the photo as attachment.


About The Author

Ibrar Ayyub

I am an experienced technical writer holding a Master's degree in computer science from BZU Multan, Pakistan University. With a background spanning various industries, particularly in home automation and engineering, I have honed my skills in crafting clear and concise content. Proficient in leveraging infographics and diagrams, I strive to simplify complex concepts for readers. My strength lies in thorough research and presenting information in a structured and logical format.

Follow Us:
LinkedinTwitter
Scroll to Top