Smart Doorbell

Smart doorbell equipped with a motion sensor, which notifies the owner if any visitor is approaching via email, colorfoul LEDS and ringing.

BeginnerFull instructions provided8 hours596
Smart Doorbell

Things used in this project

Hardware components

Raspberry Pi 4 Model B
Raspberry Pi 4 Model B
×1
Ultrasonic Sensor - HC-SR04 (Generic)
Ultrasonic Sensor - HC-SR04 (Generic)
×1
Breadboard (generic)
Breadboard (generic)
×1
Buzzer
Buzzer
×1
Resistor 1k ohm
Resistor 1k ohm
×1
LED (generic)
LED (generic)
×3
General Purpose Transistor NPN
General Purpose Transistor NPN
×1
Through Hole Resistor, 150 ohm
Through Hole Resistor, 150 ohm
×3
Male/Female Jumper Wires
Male/Female Jumper Wires
×9
Male/Male Jumper Wires
×9
1N4007 – High Voltage, High Current Rated Diode
1N4007 – High Voltage, High Current Rated Diode
×1
Camera (generic)
×1

Software apps and online services

Raspbian
Raspberry Pi Raspbian
Python 3.8 programming language
Python modules Rpi.GPIO, time, picamera, os, board, adafruit_dht, pigpio
Simple Mail Transfer Protocol
Flask Server

Story

Read more

Schematics

5f8af2ab-0413-41f9-bf1e-bf67018ec1de_aYQV08MrOJ.png

Code

index.html

HTML
<html>
	<head>
		<title>Smart home door bell</title>
		<script src="https://code.jquery.com/jquery-3.3.1.min.js"></script>
	</head>
	
	<body>

		<div  align="center">
			<h1>Live feed</h1>
			<iframe src='http://localhost:8081' width="50%" height="485">
				<p>mama</p>
			</iframe>
		</div>
		
		<hr>
		
		<div align="center">
			<table style='font-size:35px'>
				<tr>
					<td>
						<div>Distance: </div>
					</td>
					<td>
						<div id='Distance'></div>
					</td>
				</tr>
			</table> 
		</div>
		
		
		<script>
			 $(document).ready(function(){
				setInterval(refreshFunction,100);
			 });
			 
			 function refreshFunction(){
				console.log('mama')
				$.getJSON('/refresh', function(result){
					$('#Distance').html(result.toString() + " m");
				});
			 }
		</script>
	</body>
</html>

smart_home_door_bell.py

Python
#import the necessary modules from the bottle library
from bottle import route, run, template, request
#we will import the RPi.GPIO library with the name of GPIO
import RPi.GPIO as GPIO
import smtplib, ssl
import time
import pigpio
#we will set the pin numbering to the GPIO.BOARD numbering
GPIO.setmode(GPIO.BOARD)

############### DISTANCE SENSOR
#declare the pins used by the ultrasonic module
trig = 16
echo = 18
#set the trigger pin as OUTPUT and the echo as INPUT
GPIO.setup(trig, GPIO.OUT)
GPIO.setup(echo, GPIO.IN)

################ BUZZER
#create an instance of the pigpio library
pi = pigpio.pi()

#define the pin used by the Buzzer
#this is GPIO18, which is pin 12
buzzer = 18

################ LEDS
#define the pin used by the Led
led_closest = 3
led_middle = 5
led_farthest = 7

#set the pin used by the Led as OUTPUT
GPIO.setup(led_closest, GPIO.OUT)
GPIO.setup(led_middle, GPIO.OUT)
GPIO.setup(led_farthest, GPIO.OUT)

smtp_server = "smtp.gmail.com"
port = 587  # For starttls
sender = "gabitim470@gmail.com"
password = 'dummy_passwd' # real passwd is requiered in order to work
receivers = ['gabitim470@gmail.com']
# Create a secure SSL context
context = ssl.create_default_context()

def calculate_distance():
 #set the trigger to HIGH
 GPIO.output(trig, GPIO.HIGH)
 #sleep 0.00001 s and the set the trigger to LOW
 time.sleep(0.00001)
 GPIO.output(trig, GPIO.LOW)
 #save the start and stop times
 start = time.time()
 stop = time.time()
 #modify the start time to be the last time until
 #the echo becomes HIGH
 
 while GPIO.input(echo) == 0:
  start = time.time()
  #modify the stop time to be the last time until
  #the echo becomes LOW
  
 while GPIO.input(echo) == 1:
  stop = time.time()
 
 #get the duration of the echo pin as HIGH
 duration = stop - start
 
 #calculate the distance
 distance = 34300/2 * duration
 
 if distance < 0.5 and distance > 400:
  return 0
 else:
  #return the distance
  if distance < 80:
   notification_message = "You have a visitor!"
   server.sendmail(sender, receivers, notification_message)
   buzzer_function('On')
   update_leds(GPIO.HIGH, GPIO.HIGH, GPIO.HIGH)
  elif distance < 200:
    update_leds(GPIO.LOW, GPIO.HIGH, GPIO.HIGH)
    buzzer_function('Off')
  else: 
    update_leds(GPIO.LOW, GPIO.LOW, GPIO.HIGH)
    buzzer_function('Off')
  
  return distance / 100
  
def buzzer_function(state):
 #if the state is "On", we will turn On the buzzer
 #else, we will turn Off the buzzer
 if state == 'On':
  #turn On the buzzer
  pi.hardware_PWM(buzzer, 1000, 500000)
 else:
  #turn off the buzzer
  pi.hardware_PWM(buzzer, 0, 0)
  
def update_leds(led_closest_state, led_middle_state, led_farthest_state):
 GPIO.output(led_closest, led_closest_state)
 GPIO.output(led_middle, led_middle_state)
 GPIO.output(led_farthest, led_farthest_state)
 
 #define the route for the main page
@route('/')
def index():
 #at the '/' route we will return the index.html
 #template that is in the views folder
 return template('index.html')
 
@route('/refresh')
#define a function to send data to client
def refresh():
 #calculate the distance
 dist = calculate_distance()
 #return the distance to client
 return f'{dist:.2f}' 
  
try:
 server = smtplib.SMTP(smtp_server,port) # starting the smpt server
 server.ehlo() 
 server.starttls(context=context)
 server.ehlo() # Can be omitted
 server.login(sender, password)
 #we will run the app on port 5000
 run(host = '0.0.0.0', port = '5000')
except KeyboardInterrupt:
 pass
#clean the used ports
# server.quit()
GPIO.cleanup()

Github Repo

The github repository for the following project.

Credits

Mihai-Silviu Hârțan
1 project • 1 follower
Gabriel Timofti
0 projects • 0 followers
Strugariu Vlad-Florin
0 projects • 0 followers

Comments