A Python doorbell application for the Raspberry Pi
My goal of this project was to create a doorbell system using Python (DoorPy) which would alert me when someone is at my door. In addition, I would like to notify people waiting at the door. As I live in an apartment, people (especially delivery guys) would leave in a moment even before I could get to my door. My hopes are that a little screen would keep them mesmerised for long enough, so I can go downstairs. Or simply deny them with a message that I am not around.
This project is a work in progress, below is indicated how far I currently am:
I am not at all a developer, I just like Python and use this project as something to learn from. Especially GPIO got my interest (physical computing, yay ), as I have never worked with electronics in combination with a Raspberry Pi this seemed like a nice learning experience. I have programmed before, but like I said, not on an epic scale. If you have any comments/advice/questions, just let me know.
What you will need for this project:
Wel that was easy... Connect the button to the pins, import the Python library and off you go!
Okay, hold you horses before you fry your Raspberry! Let us get some things straight:
The initial script turned out to be extremely easy. Also GPIO is easy once you read a bit into it. It boils down to knowing your pin layout ('pinout') and having some basic knowledge of electronics.
For the RPi2 the pinout is easily found online and looks like this:
A more interactive and really useful website with more information on the pins is Pinout.xyz, make sure to check their site out as it is really useful.
DISCLAIMER: Please triple-check you pinout before connecting anything. Read on, understand the different state a pin can have, and also note that there is a different pinout for the RPi1 and RPi2!
continue reading...
The initial script ended up being something similar to this:
#!/usr/bin/python
import RPi.GPIO as GPIO
import time, os, sys
# Set GPIO
gpio_bell = 21 #Doorbell button GPIO name (NOT pin#)
GPIO.setmode(GPIO.BCM)
GPIO.setup(gpio_bell, GPIO.IN , pull_up_down=GPIO.PUD_DOWN)
# Set an even detection on the channel to detect voltage/button presses
GPIO.add_event_detect(gpio_bell,GPIO.FALLING)
# Loop code, on detect do something
try:
while True:
if GPIO.input(gpio_bell):
print "Danger, danger... High voltage!"
time.sleep(10) # Add
else:
pass # No input on GPIO pin, so pass
except KeyboardInterrupt:
print "Keyboard interrupt (ctrl+c)"
except Exception as e:
print "Exception caught, terminating. Reason:"
print str(e)
finally:
# Reset GPIO pin states
GPIO.cleanup()
A few things might be new if you are -like me- are just starting out with GPIO on the RPi, but it is actually pretty easy. This Python script is rather simple, it does something with GPIO pins and the keeps running until it detects 'high voltage' on the GPIO ping. Sleeps for a bit and then loops again, until interrupted. But let me elaborate on a few things:
...
import RPi.GPIO as GPIO
...
# Set GPIO
GPIO.setmode(GPIO.BCM)
gpio_bell = 21 #Doorbell button GPIO name (NOT pin#)
GPIO.setup(gpio_bell, GPIO.IN , pull_up_down=GPIO.PUD_DOWN)
...
First you will need to import all GPIO functions, luckily these are included on any Raspbian image. If not you can get the package from here.
Then you set the mode to 'GPIO.BCM', which means that you are referring to the pins by the "BroadCoM" number. This is the number you see defined in gpio_bell = 21
-part, which is the name for the pin you find on the PinoutXYZ website. This allows us to define GPIO pins by their number instead of their pin number (but you can change this if you would like otherwise, of course).
GPIO.setup(gpio_bell, GPIO.IN , pull_up_down=GPIO.PUD_DOWN)
defines the BCM21-pin we just defined to become a so called 'pull-down'-pin. Each pin can be configured as either a 'pull-up' or 'pull-down' pin. What this boils down to an internal resitor being created to measure the state of the port. The difference between the up or down setting are simply said the following:
Therefore it makes sense to pick 'Pull-down' for this setup, as we only want to know if someone rings the doorbell. And we are certainly not interested in a floating state, where you cannot do much about with logically, as it is no boolean value.
...
# Set an even detection on the channel to detect voltage/button presses
GPIO.add_event_detect(gpio_bell,GPIO.FALLING)
...
Simply set the GPIO pin to detect changes, where FALLING
means that is detected when a button is pressed (i.e. current is going through the pin to ground).
...
# Loop code, on detect do something
try:
while True:
if GPIO.input(gpio_bell):
print "Danger, danger... High voltage!"
time.sleep(10) # Add
else:
pass # No input on GPIO pin, so pass
...
Keep looping the code pass
, until you measure a 'pull-down' on GPIO BCM21-pin.
See, GPIO is pretty simple! A few lines of code and BOOM you are tinkering with electronics connect you your Raspberry Pi!
Bacon ipsum dolor amet rump pork chop short loin brisket spare ribs sausage ham. Doner biltong shank leberkas, tenderloin strip steak sausage frankfurter flank pastrami pork loin tongue picanha. Filet mignon pork belly pig turkey turducken doner rump drumstick picanha shoulder meatball pork leberkas. Filet mignon leberkas porchetta beef ribs. Short loin frankfurter t-bone pork belly bacon beef beef ribs boudin tenderloin meatball chicken rump ham.
Shankle shoulder ham pork chop, ham hock venison porchetta. Short ribs biltong kielbasa, hamburger shankle tri-tip salami sirloin beef turkey kevin. Tongue flank bacon, drumstick sirloin doner pork loin hamburger kevin porchetta bresaola chicken spare ribs kielbasa ground round. Shank ham hock flank short ribs doner. Prosciutto venison pork, alcatra cupim tri-tip bresaola. Cow ham beef swine pork loin, jerky pork chop biltong strip steak andouille jowl.
Bacon ipsum dolor amet rump pork chop short loin brisket spare ribs sausage ham. Doner biltong shank leberkas, tenderloin strip steak sausage frankfurter flank pastrami pork loin tongue picanha. Filet mignon pork belly pig turkey turducken doner rump drumstick picanha shoulder meatball pork leberkas. Filet mignon leberkas porchetta beef ribs. Short loin frankfurter t-bone pork belly bacon beef beef ribs boudin tenderloin meatball chicken rump ham.
Shankle shoulder ham pork chop, ham hock venison porchetta. Short ribs biltong kielbasa, hamburger shankle tri-tip salami sirloin beef turkey kevin. Tongue flank bacon, drumstick sirloin doner pork loin hamburger kevin porchetta bresaola chicken spare ribs kielbasa ground round. Shank ham hock flank short ribs doner. Prosciutto venison pork, alcatra cupim tri-tip bresaola. Cow ham beef swine pork loin, jerky pork chop biltong strip steak andouille jowl.
Bacon ipsum dolor amet rump pork chop short loin brisket spare ribs sausage ham. Doner biltong shank leberkas, tenderloin strip steak sausage frankfurter flank pastrami pork loin tongue picanha. Filet mignon pork belly pig turkey turducken doner rump drumstick picanha shoulder meatball pork leberkas. Filet mignon leberkas porchetta beef ribs. Short loin frankfurter t-bone pork belly bacon beef beef ribs boudin tenderloin meatball chicken rump ham.
Shankle shoulder ham pork chop, ham hock venison porchetta. Short ribs biltong kielbasa, hamburger shankle tri-tip salami sirloin beef turkey kevin. Tongue flank bacon, drumstick sirloin doner pork loin hamburger kevin porchetta bresaola chicken spare ribs kielbasa ground round. Shank ham hock flank short ribs doner. Prosciutto venison pork, alcatra cupim tri-tip bresaola. Cow ham beef swine pork loin, jerky pork chop biltong strip steak andouille jowl.
Bacon ipsum dolor amet rump pork chop short loin brisket spare ribs sausage ham. Doner biltong shank leberkas, tenderloin strip steak sausage frankfurter flank pastrami pork loin tongue picanha. Filet mignon pork belly pig turkey turducken doner rump drumstick picanha shoulder meatball pork leberkas. Filet mignon leberkas porchetta beef ribs. Short loin frankfurter t-bone pork belly bacon beef beef ribs boudin tenderloin meatball chicken rump ham.
Shankle shoulder ham pork chop, ham hock venison porchetta. Short ribs biltong kielbasa, hamburger shankle tri-tip salami sirloin beef turkey kevin. Tongue flank bacon, drumstick sirloin doner pork loin hamburger kevin porchetta bresaola chicken spare ribs kielbasa ground round. Shank ham hock flank short ribs doner. Prosciutto venison pork, alcatra cupim tri-tip bresaola. Cow ham beef swine pork loin, jerky pork chop biltong strip steak andouille jowl.
Bacon ipsum dolor amet rump pork chop short loin brisket spare ribs sausage ham. Doner biltong shank leberkas, tenderloin strip steak sausage frankfurter flank pastrami pork loin tongue picanha. Filet mignon pork belly pig turkey turducken doner rump drumstick picanha shoulder meatball pork leberkas. Filet mignon leberkas porchetta beef ribs. Short loin frankfurter t-bone pork belly bacon beef beef ribs boudin tenderloin meatball chicken rump ham.
Shankle shoulder ham pork chop, ham hock venison porchetta. Short ribs biltong kielbasa, hamburger shankle tri-tip salami sirloin beef turkey kevin. Tongue flank bacon, drumstick sirloin doner pork loin hamburger kevin porchetta bresaola chicken spare ribs kielbasa ground round. Shank ham hock flank short ribs doner. Prosciutto venison pork, alcatra cupim tri-tip bresaola. Cow ham beef swine pork loin, jerky pork chop biltong strip steak andouille jowl.
Of course, there is more to make/hack/break than this! I still have some wishes for my beloved DoorPy:
Credits where credits are due: