Friday 28 December 2012

Door Bell


First attempt to build a raspberry Pi door bell

Having just bought a Raspberry Pi for my 10 year old son, I thought I'd better get one step ahead of him and find out how it works.

So I have set out to create a Python program that emails my Iphone when a GPIO input is grounded.

So far I have broken that task down into 5 sections

  1. Email
  2. GPIO
  3. Play doorbell sound via audio jack
  4. Join all sections tidy like
  5. Auto start python in background mode

Email


My first simple python program to send a single msg from the Raspi






#!/usr/bin/python

import smtplib


gmail_user = <gmail user name>
gmail_pwd = <gmail password>
fromaddrs="doorbell"
toaddrs = <to my iphone email address>

msg="Subject: doorbell, there is somebody at the door"
server = smtplib.SMTP("smtp.gmail.com:587")
server.starttls()
server.login(gmail_user,gmail_pwd)
server.sendmail(fromaddrs, toaddrs, msg)
server.quit()
print("Sent: ", msg)




GPIO



Uses wiringpi



#!/usr/bin/python
import wiringpi
from time import sleep
##ensure that pin 17 is set as input
## and pull up is set
call (["gpio", "export 17 in"])
call (["gpio","-g","mode 17 up"])
# to use Raspberry Pi board pin numbers
#GPIO.setmode(GPIO.BOARD)
io = wiringpi.GPIO(wiringpi.GPIO.WPI_MODE_SYS)
io.pinMode(17,io.INPUT)
#Read input pin and print current value
while True:
    # input from RPi board pin 17
    input_value = io.digitalRead(17)
    print(input_value)
    sleep(0.1)


AUDIO OUT 


#!/usr/bin/python

from subprocess import call

print('ding dong')call(["mplayer","Ding Dong.mp3"])  


Final tidy version



#!/usr/bin/python
## Changed call to Popen to get the mp3 player to run as a subprocess
## Opened a google account just for my doorbell
## Set up a the doorbell as a VIP on my Iphone with doorbell sound
import wiringpi
from time import sleep
import smtplib
import string
from subprocess import Popen
msg="There is somebody at the door"
##ensure that pin 17 is set as input
## and pull up is set
call(['gpio','export','17','in'])call(['gpio','-g','mode','17','up'])
     
# to use Raspberry Pi board pin numbers
io = wiringpi.GPIO(wiringpi.GPIO.WPI_MODE_SYS)
io.pinMode(17,io.INPUT)

#This function sends an email message to toaddrs
def sendmsg(msg):
    gmail_user = "**********"
    gmail_pwd = "**********"    fromaddrs = "frontdoor"
    toaddrs ="iPhone1, iPhone2, Ipod3"
    subject = "Doorbell"    body=[]
    body='\n'.join([
        "From: %s" % fromaddrs,
        "To: %s" % toaddrs,
        "Subject: %s" % subject,
        " ",
        msg
        ,'\r\n'])
            
    server = smtplib.SMTP("smtp.gmail.com:587")
    server.starttls()
    server.login(gmail_user,gmail_pwd)
    server.sendmail(fromaddrs, toaddrs.split(","), body)
    server.quit()
    return

#Run a continuous loop checking for bell push
while True:
   # get input from GPIO
    input_value = io.digitalRead(17)
   #Let RasPi rest for a bit  
    sleep(0.1)
    if input_value==False:
        # if pin is grounded sound bell and send email        p=Popen(["mpg123","-q","Ding Dong.mp3"])        sendmsg(msg)


RUNNING PYTHON SCRIPT AT START UP



No comments:

Post a Comment