Raspberry Pi Internet Door Bell

Ok Ok so I know I’m way behind the curve with the Internet of Things, well that’s not quite true when the boiler was fitted at the back end of 2015 we did opt for the fancy internet connected thermostat for a number of reasons one of which being the ability to change the temperature here when we where away from the house, but apart from that we don’t really have any connected devices at the moment, not because I’m massively against having components of my home on the internet but more because there has just been no need.

Well all that has begun to change today! With my first toe dipping into IoT ūüôā

As you can read on other parts of the site, I’m lucky enough to have a building at the bottom of the garden that hopefully one day will become my office (And as this post has taken so long to write, it is now my office) as I work from home. This raises the question as to how you make the front door bell audible in the office (or the garage for that matter of fact) as I’m going to be spending most of the working day down there and don’t want to miss that delivery, whilst still having it audible in the main house. Wireless doorbells just don’t have the range required for the job in hand so I turn to my old faithful the RaspberryPi for the answer once again!

I have an old Pi A kicking around in the house and most of the parts needed to get the prototype built and give it a whirl so I set about, after some initial testing the Pi A was upgraded to a Pi B, but more on that later.

I can’t find a person to credit for this but here is the original page that started me along my way

So armed with an idea lets look at the list of  required parts to get us started

  • Raspberry Pi (plus power supply and 4GB, Class 4 SD card) with internet access
  • PC speaker (I used something similar to this)
  • Pushover account
  • Android or Apple smartphone, not compulsory
  • LED Pilot Light Momentary Action Pushbutton Switch (eBay has them for as low as ¬£1)
  • 10k-Ohm resistor
  • Multicore cable (at least 4-strand, such as telephone wire)

Your also going to need a couple of tools

  • Soldering iron and solder
  • Wire strippers

All that covered lets make a start,

Nip over to the Pi foundation and download the latest version of Jessie LITE. I use Win32 Disk Imager I burnt the image to the SD Card (note to write about how to do this at some point, but in the mean time there are already a load of guides out there on how to do it)

Pop it in the Pi and boot it up, now as your Pi is going to need an internet connection for this (it wouldn’t be IoT without one) the 1st thing to do after resizing the partition to fit the card is to configure the network connection, I had already popped a WiFi card in the Pi prior to boot but need to configure it to connect to my AP (EDIT) I no longer use WiFi for the doorbell but have left this in here for reference on how to configure WiFi on Jessie

So login to your Pi and set the WiFi connection up by typing in the following command

sudo nano /etc/wpa_supplicant/wpa_supplicant.conf

You going to see roughly the below on the screen

wpa_supplicant_config_org

 

and what we want to do is make it look something like this

wpa_supplicant_config_new

 

Changing “MY_SSID” and “MY_PASSWORD” to the required for your network and then save the file by pressing “CTRL+X” followed by “Y” to save the changes

Whilst I’m about it I’m going to change the hostname of the Pi so that its easily identifiable on the network and if I left all my Pi’s called “raspberrypi” I would never be able to find anything at a later date to make changes to them. To do this we need to edit a couple of files on the Pi

So run the following command

sudo nano /etc/hosts

Your going to see something similar to the bellow

etc_hosts

 

Change “raspberrypi” to something meaning full and save the file¬†by pressing “CTRL+X” followed by “Y” to save the changes

and then type the following to edit the hostname file

sudo nano /etc/hostname

Again your going to see something similar to the below

etc_hostname

 

Change “raspberrypi” to match the entry you made in the previous step and then¬†save the file¬†by pressing “CTRL+X” followed by “Y” to save the changes

Reboot your Pi by typing the following command

sudo reboot

It’s good practice and kind of goes without saying but make sure everything is up to date before you start by running the following commands

sudo apt-get update
sudo apt-get upgrade

That could take a bit of time so whilst its going lets go and sort out the Pushover account,

Push Notifications

We are going to use¬†Pushover¬†to¬†send doorbell alerts to the smartphone, it’s available for both Apple’s iOS and Google’s Android but not Windows Phone there is also a browser client enabling desktop notifications in newer versions of Chrome (including Chrome OS), Firefox, and Safari when you have a tab open to their desktop app website.

The¬†Android version¬†has a seven-day free trial before asking for $4.99 as an in-app¬†purchase,¬†while the¬†iOS version¬†is ¬£2.99. Pushover can push any kind of alert to your smartphone, so can be used for more than just your doorbell. I’ve tested both the Android and the iOS versions and they work well.

Install the app, sign up for an account and name the device which you want to receive the notifications. When asked, give permission to Pushover to send you Push Notifications. A welcome message should appear on the Pushover Home screen, telling you that the device can receive messages.

Nip over to¬†https://pushover.net¬†and log in. Click Register an Application under the Your Applications section. You’ll be asked to give this new application a name, type, description,¬†URL¬†and icon ‚Äď see the screenshot for our suggestions. Click Create Application and note the API Token/Key that Pushover generates (it’s a long string of numbers and letters that’s case-sensitive) before pressing Save Changes. Pressing this button will take you to a usage screen with two graphs, but just head home by clicking the Pushover logo at the top-left of the screen. From this main screen, note your UserKey (the¬†top-most¬†of the two long strings of letters and numbers) and log out.

Software 

OK all that done we are now about ready to start turning this thing into your new door bell ūüôā

Lets make sure that the GPIO connector is running in software access mode rather than serial mode

Run the following command

sudo nano /boot/cmdline.txt

You should see something similar to this in the nano window that opens up, if not change it to match

[I can’t find the screen grab I had for this]

dwc_otg.lpm_enable=0 console=tty1 root=/dev/mmcblk0p2 rootfstype=ext4 elevator=deadline rootwait

If you have made changes don’t forget to save the file by pressing “CTRL+X” followed by “Y”

Next your going to need a few prerequisites for things to work, so type the following commands into your PI

Lets grab the Python Dev kit, GPIO addon for python and mpg321 audio player (we want a ding dong)

sudo apt-get install python-dev python-rpi.gpio mpg321

You can get a royalty free doorbell chime by typing the following command in to your Pi

wget http://uncle-muddy.me.uk/wp-content/uploads/2016/09/doorbell.mp3

The next thing to do is create a new python script by typing the following command

sudo nano doorbell.py

Paste the following lines into your editor

#!/usr/bin/env python
 
from time import sleep
import os
import httplib, urllib
import RPi.GPIO as GPIO
 
GPIO.setmode(GPIO.BCM)
GPIO.setup(4, GPIO.IN)
 
def PushOver(title,message,url):
   app_key = "AppKeyHere"
   user_key = "UserKeyHere"
   #Connect with the Pushover API server
   conn = httplib.HTTPSConnection("api.pushover.net:443")
 
   #Send a POST request in urlencoded json
   conn.request("POST", "/1/messages.json",
   urllib.urlencode({
   "token": app_key,
   "user": user_key,
   "title": title,
   "message": message,
   "url": url,
   }), { "Content-type": "application/x-www-form-urlencoded" })
 
   #Any error messages or other responses?
   conn.getresponse()
 
#App-specific variables
 
PushOver('Doorbell','Started','')
print 'Doorbell Server Started\r'
 
while True:
 
   if (GPIO.input(4) == False):
      print 'Button Pushed!\r'
      os.system('mpg321 /home/pi/doorbell.mp3')
      PushOver('Doorbell','Someone is at the door!','')
   sleep(0.2);

Remember earlier when we where setting up Pushover? and I said write down your API Token/Key and UserKey your going to need to enter them on lines 12 and 13 of the above script.

Close and save the file by pressing “CTRL+X” followed by “Y”

Thats the software pretty much sorted now, you can of course load other doorbell chimes to the Pi and change the line below in doorbell.py to reflect the new file name

os.system('mpg321 /home/pi/doorbell.mp3')

Hardware

With the software all sorted its time to start making the hardware, this is not complicated and you shouldn’t damage anything, but to be safe double check everything before powering anything up.

I hate soldering things directly to any of my boards so I always find it a good idea to have some “Jumper Cables” laying around, they are nice and cheep from Amazon and let you easily make changes quickly without having to wave a hot iron in the direction of the Pi, but the choice is yours!

We are going to use 4 Pin from the GPIO port on the Pi:

  • Pin 1 – 3.3v
  • Pin 4 – GPIO 4
  • Pin 24 – Ground
  • Pin 26 – +5v

On the back of our switch there are 4 pins

  • + (5v power to the LED)
  • – (0v for the LED)
  • NO (Normally Open)
  • C (Common)

Start off by soldering a wire between the (-) and (C) Pins on the back of the switch leaving enough length from one side to connect to the GPIO Pins on the Pi, Then solder another wire to the (+) Pin on the switch again leaving enough length to connect to the Pi, now solder a wire and one side of the resistor to the (NC) Pin on the switch leaving enough length to reach the Pi once more, then add a wire from the other side of the resistor to reach the Pi. Your setup should look something like this..

Check that you have nothing shorting out or touching any metal casings. I have included the Pin number on the GPIO port that you should know connect things up to, MAKE SURE YOUR PI IS POWERED DOWN AT THIS POINT, hook everything up and power your Pi up.

Testing

The 1st thing you should notice is the LED in the switch comes on when you apply power to the Pi, this is a good sign ūüôā

Once the Pi has booted login and type the following command

sudo python doorbell.py

On the screen you should see a message saying “Doorbell Server Started” if everything is working well a second or two later you will get a Pushover alert on your phone saying “Doorbell Started” If everything to this point is working, press the button on the switch and you should hear the MP3 chime you downloaded followed quickly by a Pushover alert telling you ‘Someone is at the door!’

Thats about it you have it working ūüôā

Start on Boot

So the last thing to do is to get the script to run on start up so you can make your Pi headless and just leave it running. To do this we need to add it to the crontab. Type the following command to open the crontab file

<code>sudo¬†crontab ‚Äďe</code>

Add the following line to the end of the file

<code>@reboot python /home/pi/doorbell.py &</code>

Close and save the file by pressing “CTRL+X” followed by “Y”

Test that everything is working by rebooting the Pi

<code>sudo reboot</code>

The Pi will reboot and shortly after you should get a message saying “Doorbell Started” on your phone.

Pi B Upgrade and Power

Earlier on I said I had switched from a Pi A to a Pi B, and here is one of the reasons why, I didn’t have any power in the location I was going to place the Pi to run the doorbell but I did have an ethernet cable very close, I was also looking at adding a camera to the Pi to provide an image of who was ringing the bell which meant I was going to need another USB port, so I switched to a Pi B and made use of the ethernet cable and came off WiFi. At one end of the setup we inject power into the ethernet using a PoE Injector

at the Raspberry Pi end we split the power and network away from each other using a PoE splitter

What this gave me was a very tidy solution to the no power problem, and also provides enough power to keep the speaker on charge via a USB port on the Pi. I have toyed with the idea of putting a PoE switch in at some point as I have PoE phones in the house as well so if that every happens I can remove the injectors and the solution will still work.

I hope if nothing else the above provides you with some light tinkering on a Sunday afternoon, or maybe like myself a resolution to a problem

UM

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *