Raspberry pi, a Buzzer and Node.js – An IoT Doorbell

This post is part 3 of 9 in the series Raspberry Pi
Tweet about this on TwitterShare on LinkedIn0Share on Google+4Share on Reddit0Buffer this pageFlattr the authorEmail this to someonePrint this page

In this post, we will build an embedded system – A Doorbell, with Raspberry pi B+, A Buzzer and Node.js. This embedded system will act as both a normal Doorbell with a button switch and the same doorbell can be controlled over internet.  Simple right?

The final output of this embedded system would look like

 

The completed code can be found here.

So, let us get started.

Prerequisites

If you are new to Raspberry pi and have not yet installed Node.js on it, I would recommend going through Getting Started with Raspberry pi and Node.js.

If you are new to electronics devices and circuits, I would recommend going through the video lectures from All About Circuits.

Components needed

  1. 1 – Raspberry pi B+
  2. 1 – Breadboard
  3. 1 – 68 Ohms resistor
  4. 1 – 10K Ohm resistor
  5. 1 – LED (optional)
  6. 1 – Toggle Button
  7. 4 – Female to Male connectors
  8. 5 – Male to Male connectors
  9. 1 – Piezo Buzzer

If you are new to Raspberry Pi GPIO, please refer this.

Building the IoT Doorbell

There are 3 pieces involved in building the complete system.

  1. Hardware – Circuit
  2. Software – Pi program
  3. Software – A Client that can remotely control the Pi

We will start off with building the circuit. The circuit will consist of a Buzzer and a button switch hooked up with the pi. Once that is done, we will write the Node.js program that will listen to the button for changes and then trigger the Buzzer.

Now, to control the buzzer remotely, we will write a simple HTTP server on the pi, that will toggle the buzzer when it receives a HTTP request.

Hardware – Circuit

Before we get started off we will take a look at what are Pull-up/Pull-down resistors.

When you are dealing with digital components, they either have an ON/HIGH state or a OFF/LOW state. But in a switch, they have another state called as a Floating state. This state sometimes triggers a false ON state or a false OFF state, depending on the circuit. To avoid this erroneous reading, we introduce a Pull-up/Pull-down resistors.

You can know more about Pull-up/Pull-down resistors in this video

In our circuit we will include a pull-down resistor of 10K ohms value. Below is the circuit diagram, that we are going to set up on the breadboard.

Screen Shot 2014-10-12 at 1.54.29 pmThe red wire on the top left will be connected to Pin 1 on the Raspberry Pi (Info on GPIO pins here). And Pin 5 will be connected to the black wire that would be our ground. This setup makes the first 1 row of the breadboard a power bus and the second row a ground bus. From now on, we will connect our components to these rows as applicable.

Next, we will work on the Buzzer circuit. The buzzer circuit is pretty simple. We will start off with the out from GPIO pin 17 to the breadboard. This is connected to a 68 ohms resistor. This resistor is then connected to the buzzer’s positive end. And the negative end is grounded.

Finally our button switch setup. Before we do that, lets take a quick look at how the switch works

pbsource

When the button is open, the two leads on each side are connected. And when the button is closed, all 4 leads are connected. You can find more info on the button switch here.

Here is where the pull down resistor comes into picture. When the switch is open, there is some floating current between the 2 leads. When this is connected to the pi to complete the circuit, the GPIO pin reads random high and low values.

To complete the button circuit, we will take one end of the button and connect it with the 5v supply. That is row 1 in the breadboard. And the other leg on the same side will be connected to a 10K Ohm pull down resistor. And that would be connected to the Ground, row 2 of breadboard.

The other side of the push button, will be connect to GPIO pin 18 on the pi. So when the button is pressed, A high signal will be sent to to pin 18 and this will be used to trigger the buzzer.

If you want to check the current flow, you can add a LED between the buzzer and the ground like

Screen Shot 2014-10-13 at 3.37.06 pm

Simple right? This completes the circuit part.

Software – Pi program

Now, we will write the Node code to control the buzzer, based on the inputs from the button switch.

Login to your pi via ssh – terminal/putty. As soon as you ssh into pi, you will be landing inside the /home/pi folder. We will create a new folder here named node_programs. And inside this folder, we will be maintaining all our programs. Run

mkdir node_programs

To step inside that folder, run

cd node_programs

For this post, we will create a new folder named doorBell and will step inside this folder. Run

mkdir doorBell && cd doorBell

Note : You can run multiple commands separated by a &&.

First we will initialize a new node project here. Run

npm init

Fill it up as applicable.

Now, we will use a node module named onoff to interact with the GPIO pins from inside our Node.js code. This is the simplest way to interact with the pi from our node program. Run

npm install onoff --save

Next, we will create a new file named index.js. And we will open the same in the nano editor. Run

nano index.js

Paste the below code into the nano editor

Things to Notice

Line 2 : We mark GPIO pin 17 as an output – this is our Buzzer

Line 3 : We mark GPIO pin 18 as an input – this is our Button Switch

Line 5 : We watch for changes in the button. If the button is pressed, the value will be 1. The same will be set as the output of the buzzer. And when release, the value will be 0.

Line 10 : The logic for a clean exit.

Let us save the file and run the program. To save the program, press (ctrl+x). This will ask you to save the file. Press Y and press enter key to complete the operation.

To run the program, execute

sudo node index.js

Now, when you press the button, the buzzer will “buzz”. If you added an LED, it will blink too.

Simple and easy right!

Taking this to the next level, we control this buzzer over the internet.

Software – A Client that can remotely control the Pi

Imagine this, you have parked your car in the cellar and by the time you reach your home on the top floor, your main door is open for you! How about that?

We can achieve this sweet feature by connecting our Doorbell to the internet, say with the pi. And using an App on your phone, you will trigger the doorbell, when you are parking your car in the cellar.  And bam!! by the time you reach your home, the door is open. (Don’t ask me, what happens if no one is at home)

Let us add that piece of functionality to the code. For that we will create a simple HTTP Server with Node.js. And expose the URL to our app. And now, our App can control the Doorbell!.

So, let us get started. Inside the doorBell folder, create another file named server.js.

nano server.js

Paste the below code into it

This is a very light weight Node.js HTTP server. On Line 17, we check the URL and Method. If they match our criterion, we will trigger the buzzer, wait for 2 seconds and turn it off. And then we send back a success to the client.

And if the URL and Method do not match, we will reply back with a status false.

Finally, we will include this logic in index.js

Do notice Line 6.

That is it, restart the program by running

sudo node index.js

And you should see the console log. But there is a small issue here. Our server is on localhost and only the devices that are connected to the same network can access the pi. This is not really “IoT”.

So, let us expose our localhost as a publicly accessible URL. For that we will use ngrok. To set up ngrok on our pi, grab the download link from the homepage corresponding to  Linux/ARM. And then you can run a wget.  

For that let us CD back to the /home/pi folder and create a new folder named ngrok.

cd /home/pi && mkdir ngrok && cd ngrok

Then from inside the ngrok folder, run

wget https://api.equinox.io/1/Applications/ap_pJSFC5wQYkAyI0FIVwKYs9h1hW/Updates/Asset/ngrok.zip?os=linux&arch=arm&channel=stable -O ngrok.zip

This will take a few minutes, please be patient. Once the download is completed, we will unzip it. Run

unzip ngrok.zip

Note : If you have issues download with wget, you can use the Pi’s GUI with VNC and then download it from the website.

Now, that is done, we will restart the node server. Run

sudo node index.js

Open a new terminal/putty into the Pi and cd into the ngrok folder. And from there run

./ngrok 8088

8088 is the port number we have used in server.js. This will give you a public URL. You can use either the http or https version. Open a browser on your computer and navigate to  http://41b9687d.ngrok.com/trigger where  http://41b9687d.ngrok.com needs to be replaced with your ngrok URL.

If everything is okay, as soon as you fire the request, your buzzer will be “buzzing”. Awesome right!

To give a decent interface, we will create a file named index.html anywhere on our computer. This will be like our “app”, but inside a browser. Update index.html as below

If you have a WAMP, LAMP, MAMP or XAMPP server, host this file in the htdocs/www folder. Or if you have python installed on your machine, you can simply run python -m SimpleHTTPServer  or  python -m http.server.

What ever way you host this web page, you should have a URL that looks like  http://localhost:8000. That will dispatch the index.html page.

Screen Shot 2014-10-13 at 6.21.07 pm

Now from here if you click on Trigger button, the buzzer should go off.  Sweet and simple IoT Doorbell.

You can use the same circuit and logic with a simple LED in place of the buzzer. To toggle your porch light or the hallway. Here is a quick example.

If you checked out my other post Raspberry Pi, Node.js and a LED – Emit Morse Code, where we emit morse code with the help of a LED. We can extend that example to use a buzzer to emit a sound as well. The code is same as the LED, except we replace the LED with the buzzer. Take a look

That was the famous SOS message. Simple right!

Hope this post gave you a basic idea on reading inputs from components and performing actions based on it. And how easily you can convert any pi component to an “Internet Thingy”


Thanks for reading! Do comment.
@arvindr21

Series Navigation<< Raspberry Pi, Node.js and a LED – Emit Morse CodeRaspberry pi, PIR Sensor and Node.js – An IoT Intruder Alert system >>
Tweet about this on TwitterShare on LinkedIn0Share on Google+4Share on Reddit0Buffer this pageFlattr the authorEmail this to someonePrint this page
  • paul

    Thanks for the tutorial. It is very helpful. I see the last comment was a year ago, so I’m pretty late here. But… In your diagrams, you show GPIO 18 connected to pin 2 of the switch. To fit your description and photos, I believe it should be connected to pin 3 of the switch. According to the switch diagram, being connected to pin 2 also connects it directly to pin 1, and, therefore, directly to the 3.3V supply and is not controlled by the switch at all.

  • dain crawford

    Arvind,

    Thanks for this post. I used it to create a remote switch and works perfectly.
    I knew that Node was the best, light weight tool to use with the Raspberry PI, and your detailed instructions proved it.
    Looking forward to reviewing some of your other work.
    Thanks again.
    Dain

    • http://thejackalofjavascript.com/ Arvind Ravulavaru

      Great! Thanks Dain!

  • 楊昌謀

    Hi I have some problem here
    I want to host the web page index.html by python
    But it shows that the address (127.0.0.1:8088) already in use
    Because i already run the index.js
    I dont know how to run index.js & index.html at the same time

    • http://thejackalofjavascript.com/ Arvind Ravulavaru

      Hello, Update server.js line no 2 to the port you want. This way you can resolve the conflict. Or you can start your python server on another port (say 9000) python -m SimpleHTTPServer 9000. Thanks.

      • 楊昌謀

        Thanks , I got it.
        And I run another ngork to expose the “trigger page” to public.

        • http://thejackalofjavascript.com/ Arvind Ravulavaru

          Nice!

  • Amal

    Hi I am trying this and got into some confusions.
    1. Pin 1 is 3.3 v and pin 2 is 5v but you mentioned pin1 as 5v. So is it pin1 or pin 2?
    2. As same as GPIO 17 and 18. is that viceversa?
    3. In prerequisites you never mentioned about that black round buzzer i think.. what is that?

    I am in middle of your example.. can you clarify as soon as possible?

  • sidwarkd

    Thanks for sharing this.

    • http://thejackalofjavascript.com/ Arvind Ravulavaru

      Thanks! Glad it helped!