Want to start Innovating with Arduino, Raspberry Pi and Internet of things (IoT) ? Click here to learn about our July 2017 training

IoT: Smart Doorbell

Posted by Momen 20/06/2017 0 Comment(s)

Welcome to Zeroohm Raspberry Pi Training series. In this series of Raspberry Pi Training, Zeroohm will focus its efforts on helping the community learn more about Raspberry Pi fundamentals and basics starting with basic control of LED's, motors, sensors and utilizing of wirless communication. We hope that you find this useful, Please, leave a comment for any questions or comments or reviews!

We provide all our trainings for groups from schools, universities, public sector and private sector in Abu Dhabi, Dubai, Sharjah, Fujirah, RAK or anywhere in UAE. Please,click here to contact us directly for any training inquires.

Our Raspberry Pi training series:

1. Introduction to Raspberry Pi

2. Analog Inputs/Output In Raspberry Pi

3. Wireless Communication in Raspberry Pi

4. How to connect a new Raspberry Pi to your Laptop without a screen

5. Communication between Raspberry Pi and Arduino

- See more at: https://www.zeroohm.com/blog/Communication-between-raspberry-pi-and-arduino/#sthash.bffyLEJA.dpuf

Welcome to Zeroohm Raspberry Pi Training series. In this series of Raspberry Pi Training, Zeroohm will focus its efforts on helping the community learn more about Raspberry Pi fundamentals and basics starting with basic control of LED's, motors, sensors and utilizing of wirless communication. We hope that you find this useful, Please, leave a comment for any questions or comments or reviews!

We provide all our trainings for groups from schools, universities, public sector and private sector in Abu Dhabi, Dubai, Sharjah, Fujirah, RAK or anywhere in UAE. Please,click here to contact us directly for any training inquires.

Our Raspberry Pi training series:

1. Introduction to Raspberry Pi

2. Analog Inputs/Output In Raspberry Pi

3. Wireless Communication in Raspberry Pi

4. How to connect a new Raspberry Pi to your Laptop without a screen

5. Communication between Raspberry Pi and Arduino

- See more at: https://www.zeroohm.com/blog/Communication-between-raspberry-pi-and-arduino/#sthash.bffyLEJA.dpuf

Welcome to Zeroohm Raspberry Pi Training series. In this series of Raspberry Pi Training, Zeroohm will focus its efforts on helping the community learn more about Raspberry Pi fundamentals and basics starting with basic control of LED's, motors, sensors and utilizing of wirless communication. We hope that you find this useful, Please, leave a comment for any questions or comments or reviews!

We provide all our trainings for groups from schools, universities, public sector and private sector in Abu Dhabi, Dubai, Sharjah, Fujirah, RAK or anywhere in UAE. Please,click here to contact us directly for any training inquires.

Our Raspberry Pi training series:

1. Introduction to Raspberry Pi

2. Analog Inputs/Output In Raspberry Pi

3. Wireless Communication in Raspberry Pi

4. How to connect a new Raspberry Pi to your Laptop without a screen

5. Communication between Raspberry Pi and Arduino

- See more at: https://www.zeroohm.com/blog/Communication-between-raspberry-pi-and-arduino/#sthash.bffyLEJA.dpuf

Welcome to Zeroohm Raspberry Pi Training series. In this series of Raspberry Pi Training, Zeroohm will focus its efforts on helping the community learn more about Raspberry Pi fundamentals and basics starting with basic control of LED's, motors, sensors and utilizing of wirless communication. We hope that you find this useful, Please, leave a comment for any questions or comments or reviews!

We provide all our trainings for groups from schools, universities, public sector and private sector in Abu Dhabi, Dubai, Sharjah, Fujirah, RAK or anywhere in UAE. Please,click here to contact us directly for any training inquires.

- See more at: https://www.zeroohm.com/blog/wireless-communication-in-raspberry-pi/#sthash.R0bZiNyf.dpuf

Welcome to Zeroohm Raspberry Pi Training series. In this series of Raspberry Pi Training, Zeroohm will focus its efforts on helping the community learn more about Raspberry Pi fundamentals and basics starting with basic control of LED's, motors, sensors and utilizing of wirless communication. We hope that you find this useful, Please, leave a comment for any questions or comments or reviews!

We provide all our trainings for groups from schools, universities, public sector and private sector in Abu Dhabi, Dubai, Sharjah, Fujirah, RAK or anywhere in UAE. Please,click here to contact us directly for any training inquires.

Our Raspberry Pi training series:

1. Introduction to Raspberry Pi

2. Analog Inputs/Output In Raspberry Pi

3. Wireless Communication in Raspberry Pi

4. How to connect a new Raspberry Pi to your Laptop without a screen

5. Communication between Raspberry Pi and Arduino

6. IoT: Smart Doorbell

 

 

In this tutorial, I will try to explain how to implement a smart doorbell which takes a photo of whoever rings it and emails it to the specified email address, and keeps all the photos in a folder. Below is a list of all the hardware components used in this tutorial:

Hardware Components:

  • Raspberry Pi
  • Raspberry Pi case for protection
  • Mini breadboard for wiring
  • Pushbutton which should be a normally open switch
  • 10K resistor required for pushbutton
  • Lengthy wire to connect Pi to doorbell.
  • Jumper cables for breadboard
  • Webcam
  • A box to hold the entire circuit if you want it to look good

NOTE: Raspberry Pi 3 will be used in this tutorial, earlier versions are also applicable but a WiFi adapter is needed if you want your project to be wireless. Ethernet cable can be used too but then again it will be a wired connection.

And of course, there are other tools such as wires stripers, side cutters, soldering iron and a PC with internet connection. Now let’s have a look at the hardware description.

Hardware Design

The hardware connection is pretty simple. All you need to do is plug in your webcam into your Raspberry Pi board along with your selected network interface. After that just connect the circuit as shown in the following diagrams below:

raspberry-pi-doorbell-connection

raspberry-pi-schematic-doorbell

NOTE: I have modelled the 10K resistor as a 220 Ohm resistor in the above diagrams. If you are using a B+ version of Raspberry Pi, then you must use pin GPIO 7 instead of 21. The pushbutton is modelled as the doorbell; in case you dint know.

Now we are done with the hardware part, now onto the software design! Plug your Raspberry Pi in your PC and let’s get started.

Software Design

Step 1- First you need to SSH into the Raspberry Pi (Raspberry Wheezy or later). I have assumed that you are aware how to do this and all of you are advanced users. If not there are many good tutorials on the net. Look it up!

Step 2- In the command line, make sure you are in your home directory or whatever directory you wish to install the program to, by the way it is all contained in one folder so it won’t go everywhere in your home directory. (If you are planning to copy-paste, remember to remove the $ sign)

$ cd ~

Step 3- Now you should download the folder containing all the code.

$ git clone https://github.com/Happypig123123/dbell.git

Step 4- Next, we need to CD into the directory.

$ cd dbell

$ mkdir photos

Step 5- Since we are using a non-B+ version, we need to change in the code to the appropriate pin. Open:

 $ nano bell.py

and change the Button = 21 line to Button = 7. The edited code is shown below:

import RPi.GPIO as GPIO

import time

import os

import glob

#GPIO SETUP

GPIO.setmode(GPIO.BCM)

Button = 7

n = 1

GPIO.setup(Button,GPIO.IN)

#loop

print("Program Running")

while 1 == 1:#loops forever till keyboard interupt (ctr + C)

if GPIO.input(Button) == False:#when button pressed:

print("Button Pressed")

#    ------|    photo & Bell    |------ #

#Get FileName

now = time.strftime("Date%m-%d-%yTime%H-%M-%S")

#Make command to run OCMMDS

command = "bash oscmds.sh " +  str(now)

# -- OSMC.sh is an Shell script that

# -- is responsible for taking the picture and

# -- Making the Doorbell Noise

# --- We insert the "Now" argument so the python

# --- script knows what the file name of the

# --- picture will be so it can pass it on into the

# --- email script (so it knows what file to email

#run command

os.system(command)

#diagnostics

print("Filename:", now)

# ----| Email     |---- #

print("Email")#email

emailcommand = 'python3 IOTNOTIFY2.py "Someone is at the door"' + ' "photos/' + now + '.jpg"'

os.system(emailcommand) #running the Email script with:

#-- the subject as "Someone is at the door" and the filename

#-- We made before at the -Photo & Bell- section

# -- End Diagnostic Info

print("Done Process")

#-space out for next "Press of Button"

print("")

print("")

We are done with the main software setup. Now we can focus on editing the email code!

Step 6- Have a Gmail account! Or make one right now. You will need it to send emails to your personal email.

gmail-logo

 

 

Step 7- After setting up the email account, we can now setup up the email script. I have not explained here how this script works since it diverts from the main topic and there are many tutorials in the net talking about it. Start by doing this:

$ nano IOTNOTIFY2.py

and change the following values for the fields in the mentioned comments.

from email.mime.text import MIMEText

from email.mime.application import MIMEApplication

from email.mime.multipart import MIMEMultipart

from smtplib import SMTP

import smtplib

import sys

recipients = ['<YourEmail>'] #Your personal email

emaillist = [elem.strip().split(',') for elem in recipients]

msg = MIMEMultipart()

msg['Subject'] = str(sys.argv[1])

msg['From'] = '<From Email>' #Your Gmail address

msg['Reply-to'] = 'abcxyz@gmail.com' #Gmail address again

msg.preamble = 'Multipart massage.\n'

part = MIMEText("Hello Sir/Ma'am, There is some one at your door. A picture of this person has been attached.")

msg.attach(part)

part = MIMEApplication(open(str(sys.argv[2]),"rb").read())

part.add_header('Content-Disposition', 'attachment', filename=str(sys.argv[2]))

msg.attach(part)

server = smtplib.SMTP("smtp.gmail.com:587")

server.ehlo()

server.starttls()

server.login('<From Email>','<From password>') #Your Gmail address & password

server.sendmail(msg['From'], emaillist , msg.as_string())

After you have changed all the values, press Ctr X, then Y to save the code.

Step 8- Now we can finally run the code by typing this in the command line.

$ python3 bell.py

The script will run forever until you press Ctrl Z or Ctrl C.

Signing Off Note: Now all you have to do is put your system inside a box, and make the necessary adjustments for example cutting holes and taping things.

Since I have nothing to add anymore, I’ll say my goodbyes and I hope this tutorial was useful and enjoyable for you.

 

Leave a Comment