Part of my project involves having a doorbell, I figured a simple doorbell whereby a visitor just presses a button and then it rings/ notify whoever is it the house, That's not really creative in my point of view.
So I then decided why not have a doorbell that not only rings when the button is pressed but also tell you someone is at the door and then sends you an email notification just in case you might be away(Not able to hear the bell.) as well as log the time that the button was pressed.
IDEA: It would be really cool if I had a USB Webcam that would take a picture of the person visiting and emails it.
first things first, we'll need to create the wav file ie TextToSpeech. Using this simple tool below.
[sourcecode language="bash"]
pico2wave -w DoorNotify.wav "Someone is ringing the doorbell. Please Go Check"
[/sourcecode]
Next, create a python script that will watch the state of GPIO pin 18 on the RPi and take some action when the doorbell brings it to ‘HIGH’.
[sourcecode language="python"]
#!/usr/bin/env python2.7
__author__ = "Mpho Mphego"
__version__ = "$Revision: 1.1 $"
__description__ = "Smart Doorbell Notifier with voice and email notification : Interrupt driven"
__date__ = "$Date: 2015/01/11 02:23 $"
__copyright__ = "Copyright (c) 2015 Mpho Mphego"
__url__ = "mmphego.wordpress.com"
__license__ = "Python"
import RPi.GPIO as GPIO
import time
import os
# Connect to Relay which will control the 12V Buzzer.
led = 17 #GPIO0
button = 18 #GPIO1
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)
GPIO.setup(led, GPIO.OUT)
time.sleep(0.1)
GPIO.output(led, False)
# GPIO 1 set up as inputs, pulled up to avoid false detection.
# Both ports are wired to connect to GND on button press.
# So we'll be setting up falling edge detection for both
GPIO.setup(button, GPIO.IN, pull_up_down=GPIO.PUD_UP)
# define callback functions
# this will run when an event are detected
def buttonHandler(channel):
print "falling edge detected on 18"
GPIO.output(led, True)
time.sleep(1)
GPIO.output(led, False)
os.system("aplay /home/pi/Scripts/Smart_DoorBell/DoorNotify.wav")
os.system("python /home/pi/Scripts/Smart_DoorBell/DoorBellLogger.py")
time.sleep(1)
GPIO.output(led, True)
time.sleep(1)
GPIO.output(led, False)
# when a falling edge is detected on port 1, regardless of whatever
# else is happening in the program, the function buttonHandler will be run
GPIO.add_event_detect(button, GPIO.FALLING, callback=buttonHandler, bouncetime=5000)
try:
print "Waiting for button to be pressed"
while True:
# To ease the CPU Usage have a 10s delay on while loop.
time.sleep(10)
continue
except:
GPIO.cleanup() # clean up GPIO on CTRL+C exit
GPIO.cleanup() # clean up GPIO on normal exit
print "Clean up by resetting all GPIO"
[/sourcecode]
Now for the fun part, the email notification and data log.
[sourcecode language="python"]
#!/usr/bin/env python
__author__ = "Mpho Mphego"
__description__ = "Doorbell notifier time logger and email notifier."
__version__ = "$Revision: 1.0 $"
__date__ = "$Date: 2015/01/10 02:09 $"
__copyright__ = "Copyright (c) 2015 Mpho Mphego"
__license__ = "Python"
import time
import datetime
import os
import smtplib
from email.mime.text import MIMEText
#-----------------------Data Logger-----------------
f=open('/home/pi/Logs/DoorBell_Data_Log.txt','a')
now = datetime.datetime.now()
timestamp = now.strftime("%H:%M on %Y/%m/%d")
outstring1 = " Someone was at the door at " + str(timestamp)
outstring2 = "\n********************************************* \n "
outstring = outstring1 + outstring2
f.write(outstring)
f.close()
# -----------------------Email Notifier----------------------
# Replace with your details.
USERNAME = "*******@gmail.com"
PASSWORD = "*********"
MAILTO = "******@gmail.com"
if os.system("ping -c 1 www.google.com >> /dev/null 2&>1") == 0 :
print "Sending Email Notification"
msg = MIMEText('Someone is ringing the doorbell! \nGo Check!')
msg['Subject'] = 'Doorbell notification!'
msg['From'] = USERNAME
msg['To'] = MAILTO
server = smtplib.SMTP('smtp.gmail.com:587')
server.ehlo_or_helo_if_needed()
server.starttls()
server.ehlo_or_helo_if_needed()
server.login(USERNAME,PASSWORD)
server.sendmail(USERNAME, MAILTO, msg.as_string())
server.quit()
else:
print "Unable to connect to Internet."
[/sourcecode]
To have this run upon booting, we need to edit /etc/rc.local (as root since this is the owner).
[sourcecode language="bash"]
sudo nano /etc/rc.local
[/sourcecode]
At the bottom, just above exit 0 we’ll add a call to our script.
[sourcecode language="bash"]
python /home/pi/Scripts/Smart_DoorBell/WaitDoorbell.py
[/sourcecode]