# Smart Doorbell with motion sensitivity and camera function on Raspberry Pi 2 *Project by: Nicole DiNatali (nd222gg)* This project is intended to create a simplified version of a Ring smart doorbell using Raspberry Pi and some inexpensive parts. It has two triggers (motion detection and manual button push) that will activate the camera which will in turn send notifications to our phones via Telegram. As the project is not completely assembled and mounted it is more of a proof of concept than finished prototype. Project time is: 5 - 10 hours depending on level of expertise. ## Objective I live in a farmhouse on the countryside and we currently have no doorbell once so ever. We have a loud household (with two toddlers) and can often not tell if someone is at our front door. The guests sometimes have to wander around the outside of our house knocking on windows or just let themselves in. Therefore, the objective of this project is to resolve this issue. The smart doorbell will allow for us to be notified not only if someone presses the doorbell, but also if anyone is lurking around for any reason. Additionally, I left the PIR sensor range at maximum as our long driveway pulls up outside our front door. This way we will even be notified when someone is pulling up to the house. This is helpful for when one of us is home from the store with a car full of groceries and could use some help getting out. Also, it is helpful if my children are sleeping and I would like to catch someone at the door before they ring the doorbell (which at this time is missing a sound function). ## Material Below is a table outlining the key components used to build the smart doorbell device. Additional and option tools and items will be listed further down. Please note, many items were spare parts that were already in the home due to my spouse's interest in smart home technology (these will be marked with a **^**). Therefore, many items were free for me. With that being said, I originally purchased the [Lopy4 with sensors bundle](https://www.electrokit.com/en/product/lnu-1dt305-tillampad-iot-lopy4-and-sensors-bundle/) from Electrokit with the intention of using it for this project. However, after a logistics discussion with one of the TA's it was recommended that it might be easier to use a Raspberry Pi device instead of Lopy4 to better handle the camera component. As we had an unused Rasperry Pi 2 device laying around, I decided to change tact and use that item instead. | Item Name | Price (sek) | Source (Link) | Item Image | Usage | Comment | | ------------------------------------- | ----------- | --------------------------------------------------------------------------------------------------- | ------------------------------------ | ------------------------------------------------------------------------------------------------------------------------------------------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | | Raspberry Pi 2 (Model B) **^** | 429 kr | [Electrokit](https://www.electrokit.com/produkt/raspberry-pi-2-1gb-model-b/) | ![](https://i.imgur.com/KFCnEil.jpg) | Essentially, a microcomputer that acts as the central station for all the sensors to be integrated and to connect to the WiFi and communicate data responses. | It was chosen in place of the Lopy 4, as it was better equipped to handle the camera function. | | HC-SR501 PIR Sensor | 49 kr | [Electrokit](https://www.electrokit.com/produkt/pir-rorelsedetektor-hc-sr501/) | ![](https://i.imgur.com/fK0kS9f.jpg) | Used to sense movement at a range of 3 to 7 meters depending on sensitivity adjustment | This sensor was chosen as it had a longer range, which would reach to approximately our driveway. | | Simple Push Button **^** | 15 kr | [Electrokit](https://www.electrokit.com/en/product/push-button-o12-2mm-1-p-off-on-white/)* | ![](https://i.imgur.com/Ufbekr1.jpg) | Used as a physical doorbell for guests to press to announce their presence. | **This part is not the exact model used. Mine has a different back for attaching jumper wires.* <br><br> I think in the next iteration I would rather have a button that has some LED lighting for greater nighttime visibility. | | Female to Female Jumper Wires (150mm) | 29 kr | [Electrokit](https://www.electrokit.com/en/product/jumper-wires-1-pin-female-female-150mm-10-pack/) | ![](https://i.imgur.com/z11pxNb.jpg) | Used to attach the PIR sensor and Push Button to the Raspberry Pi 2 header | Shorter cables would have been preferred, but unfortunately due to time constraints this was the best option. | | Raspberry Pi Camera **^** | 349 kr | [ Eletrokit](https://www.electrokit.com/en/product/camera-module-for-raspberry-pi-v-2/)* | ![](https://i.imgur.com/bvMJBcD.jpg) | Used to capture images of any visitors | This is not the exact model I used. My model is older. My model has a still picture resolution of: 2592 x 1944* | | Housing Unit **^** | | | | Used to hold all the components in a waterproof environment for mounting outside next to the front door. | In a perfect world I would prefer something custom printed with a 3D printer, but unfortunately I do not have one of those. This was the next best option. | ### Additional/Optional Material * **Wire cutters/strippers** * **Raspberry Pi case** * **SD Card reader** * **MicroSD to SD adapter** * **Ethernet Cable** * **TV/Monitor** ## Computer setup ### IDE or no IDE Originally, the plan was to use VS Code with python to write the code necessary for the device to work. As this was not part of the final iteration I will not go into great depth on the setup process. Ultimately, I was able to use Node-Red (a low code option), to manage the logic needed for the device to work. I will elaborate more below about the setup for Node-Red. ### Setting up Raspberry Pi Before attempting to connect any sensors to the Raspberry Pi 2, I needed to setup and update the device (the tutorial I followed from Raspberry Pi's website can be found [here](https://projects.raspberrypi.org/en/projects/raspberry-pi-setting-up/2). This was a very complicated process as I was away on vacation and did not have my usual tools and resources available. 1. Insert microSD card into SD adapter, then connect SD reader to computer. 2. Download Raspberry Pi Imager to computer. * Install 4. Select preferred OS to download onto flash card. 5. Choose data storage option to format (anything on the microSD card will be overwritten). 6. Select Write. After this you will remove the microSD card from the adapter/reader and insert it into the Raspberry Pi Device. ![](https://i.imgur.com/0Majas8.png) After this you can insert the USB cable to the Rasperry Pi, **then** insert the other end into the power supply. Once the device has power, it's time to connect it to a screen (in my case a tv via HDMI) and test to see if it boots up. In my case, at this point I also connected the ethernet cable from my Raspberry Pi device to my mobile internet modem. It also worth noting, that there is a 32-bit version of the OS (I used lite), that has a GUI option. In other words, it looks more like a desktop screen on the monitor. Unfortunately, I did not have a keyboard or mouse to plug in to the Raspberry Pi, so I had to use the lite version which was easier to set up with limited resources. Next came finding the device's serial number and setting up initial preferences. As Raspberry Pi's have randomly assigned serial numbers in hexidecimal format and I had just the device, the WiFi modem, and a tv monitor to find it, I needed to use [PortX](https://snapcraft.io/portx) (an SSH client) to use command codes in the terminal to retrieve the serial number. I used the following code: ```bash= cat /proc/cpuinfo |grep Serial|cut -d' ' -f2 ``` ### Setting up Node-Red As previously mentioned, I did away with VS Code and opted for Node-Red instead. Node-red is a visual flow program that I installed directly onto the Raspberry Pi. This was done using the following steps, which are better elaborated on [in this tutorial](https://nodered.org/docs/getting-started/raspberrypi). 1. I had to open a terminal to communicate with the Pi via SSH (PortX). 2. Connect via SSH to pi@<serial number> 3. Run the installation command. ```bash= bash <(curl -sL https://raw.githubusercontent.com/node-red/linux-installers/master/deb/update-nodejs-and-nodered) ``` 4. Next, set up for Node-Red to start automatically on bootup or restart. ```bash= sudo systemctl enable nodered.service` ``` 5. Now it's time to create! To do that you open a web browser and instead of typing www.xyz.com, you type http://localhost:1880. (http://192.168.9.125:1880 in my case) ## Assembly Assembly is fairly simple for this project. I used a total of 5 jumper cables that are plugged into the headers of the Raspberry Pi. As the device already had a port for camera, all I needed to do there was insert the cable from the camera into the corresponding port. ### PIR Sensor The first assembly I wanted to tackle was the PIR sensor. I used three jumper cables * black = GND = Pin 6 * red = 5V Power * yellow = Out = Pin 12 (GPIO 18) ![](https://i.imgur.com/f8Hzj1n.jpg =500x500) ### Doorbell Push Button Next I focused on the push button for the actual doorbell function. I used a simple push button with 2 prongs in the back. I attached two jumper cables to each prong and then attached the other end of the cables to certain pins on the Raspberry Pi header. * green = GND = Pin 14 * blue = Out = Pin 18 (GPIO 24) ### Rasperry Pi Camera Finally, was the camera. As the Raspberry Pi device has a specified plug in port for a Raspberry Pi camera, that is all I needed to do to connect. See image below. ![](https://i.imgur.com/evTN706.png) ### Power The last thing worth mentioning is the power supply. While a wireless power supply would be more aesthetically pleasing and more tamper-resistent, the device is power thirsty courtesy of both the camera and the PIR sensor (which could be triggered quite frequently - up to once a minute), so mainline power was selected as the chosen route. For the sake of this project a USB cable going from the device to the wall is selected method. However, in a future iteration it would be more benefial to have a hardline going from the device to a junction box position outside above the front door. This alternative would require an ac/dc adapter - stepdown transformer (which could not be solved during this project timeframe), as the junction box outside is 220 v. ## Platform The platforms used for this project are Node-Red and Telegram (will be discussed further under Connectivity). ### Node-Red Node-Red afforded me the opportunity to minimize coding the logic of the two triggers (manual button push and motion sensor) and how they interacted with the camera. Node-Red has many palettes that can be downloaded to allow you to interact with gmail, telegram, mosquitto, and ubidots to name a few. In my case, I installed several useful palettes. #### How to install a palette 1. Open Palette Manager. ![](https://i.imgur.com/enTgY0W.jpg =300x150) <br> <br> From drop down, select Manage Palette ![](https://i.imgur.com/cLDEDk7.jpg =200x400) 2. When Palette Manager opens, select install to search for desired palettes ![](https://i.imgur.com/IHFHMk9.jpg) #### List of palettes installed 1. **node-red-node-pi-gpio** (adds the Raspberry Pi nodes) 2. **node-red-contrib-camerapi** (this allows for the Raspberry Pi camera function) 3. **node-red-contrib-telegrambot** (this adds the Telegram node options) 4. **node-red-dashboard** (this activates the Node-Red UI Dashboard) 5. **node-red-contrib-dashboard-bar-chart-data** (allows me to configure hourly frequency for my bar chart) ## The code With Node-Red there is minimal coding. The nodes and wires establish the order logic in place of python code in an IDE. Instead of the demand of code, there is the need for configuring the preferences of each node instead. Let's begin by looking at the entire flow and then look further at how they are configured. ### Node-Red Flow ![](https://i.imgur.com/Bno50eA.png) I set this up to have two separate triggers, with their own switches and delays. However, the Button delay occurs after the PIR sensor delay. This is set up this way because it can be assumed that the PIR sensor will trigger first, as it will show activity long before someone reaches the door to push the button. Therefore, to reduce spamming, if the PIR sensor is triggered I do not also need an immediate notification by a button push. This is also where the two separate triggers come together into one action - take photo. Once the photo is captured, then the added timestamp function will be passed on to Telegram to notify me, Ubidots for data visualization and finally msg.payload (debug) to ensure everything is working as it should. #### Trigger For both the PIR and push button trigger I used the "rpi-gpio in" node. ![](https://i.imgur.com/QtvTSua.jpg =200x400) To see how to configure the nodes, see below: ![](https://i.imgur.com/MePcyPw.png =400x500) * First I chose the pin that coordinates to the **OUT** placement from above *(Assembly section)*. <br> ![](https://i.imgur.com/GC4vUj4.png =500x300) * Then I clarified if there were any "forced resistors" needed. And finally gave each node a name. * It is worth mentioning that the Push Button Node required a pullup resistor choice in the node set up menu. This was to stabilize the node, as otherwise it was constantly floating between 1 and 0 and I would be spammed non-stop. #### Switch & Delay For Switch I just added a 1 for the value for the PIR Sensor, and 0 for the doorbell button push (due to the need of a pullup resistor). For the "limit rate" I used the delay node. I configured this like so: ![](https://i.imgur.com/BEPeh6c.png =500x600) * Action = Rate Limit (I wanted to limit how many messages I got per a certain amount of time) * Rate: - 1 msg per 1 minute (PIR Sensor) - 1 msg per 30 second (Button Push) #### Taking Photo This is where I configured the image capture settings. I used the "camerapi-takephoto" node, found under Raspbery Pi from the left sidebar. ![](https://i.imgur.com/6IXazrt.png =500x600) * File Mode = Custom File Name * File Name = Here I set the image to always rewrite the previous image using the same name. * File default path = No * File Path = Used the path from my Raspberry Pi where I am working on this project File Format = JPEG Image Resolution = 2592x1944, this can vary depending on what camera you use. The rest of the settings are all default and I did not alter. #### Timestamp Function I used this as an opportunity to add code for the purpose of adding a timestamp to any messages forwarded to my Telegram. ```python= oldPayload = msg.payload date = new Date().toLocaleString(); newPayload = { type:'photo', content:oldPayload, caption: date, chatId: 1743387950 } msg.payload = newPayload; return msg; ``` This is done because, as previously described, any new image will overwrite the old image. Adding the timestamp function negates me having to save each file individually with file information (such as the timestamp), thereby saving space on my microSD flashcard. ## Connectivity Data is sent strictly when triggered. Using restrictions in Node-Red to configure the delay, I have configured the PIR trigger to have a 1 minute delay before it can be activated again. This is done so as to not inundate me with spam messages everytime a cat or leaf blows across my sensor. I decided on 1 minute as I figured that was a more than appropriate time for a person to initalize on edge of sensors range (approximately 7 meters), and walk to the front door and press the doorbell. Additionally, the doorbell also has a delay of 20 seconds from initalization to reactivation, for the same aforementioned reason. The device is intended to work using our home WiFi network through a usb WiFi dongle, but as of the time of project submission, I am still struggling to get it configured properly. I suspect it is too old and perhaps damaged as it is the same dongle my husband originally used 5 years ago. Moving forward, I would likely need to purchase a new usb dongle and try again. For the purpose of trigger notification, I chose Telegram. While researching options used in other diy projects online, I found many opted for e-mail notifications, but I found this rather cumbersome. The setup looked daunting, also how many people check their e-mail to see if someone is at the door? Instead, Telegram afforded me the option of having a message (with picture) sent to an app I have on my phone, which notifies me immediately of any possible triggers of the PIR sensor and/or the button push. ### Setup/Configure Telegram In order to use [Telegram](https://telegram.org/), I had to first create an account, a bot, and find my chat ID. All of which was data Node-Red needed to communicate. More information on how to set up Telegram can be found [here](https://core.telegram.org/bots). To integrate Telegram into your Node-Red project, as I have done, please follow these steps: 1. Click on node to open up preferences 2. Where it says Bot - Add new bot, click on the pencil beside it. 3. Fill in the form as needed: ![](https://i.imgur.com/pLN5Iv9.png =500x500) - Bot-Name (the name you set up with BotFather) - Token (Individual token which you get from BotFather) - ChatID (get this from @get_id_bot on Telegram= - Update mode: I kept polling ## Presenting the Data To present the data I used Node-Red dashboard. I am interested in seeing instance frequency regarding the trigger rate. I was able to configure it to show the instance frequency per hour, for a 24 hour period. To view the Node-Red dashbord, you just add /ui to the end of the url of normalling viewing your node-red workflow area. So "http://localhost:1880/ui" ![](https://i.imgur.com/hWcqFx9.png) To acheive a dashboard showing cumlative trigger frequency, I had to create an offshoot flow dedicated for data visualization *(can be seen below)*. ![](https://i.imgur.com/PQYS0Rg.jpg) The flow for the PIR sensor starts directly after the sensor button. I have added a rate limiter to make it comparable to the rate messages are sent on Telegram (1 per minute). The flow for the button push is set up differently. The offshoot begins directly after the switch node since it required a pullup resistor modification as previously explained. Then I added a change node to alter the value from 0 (as set up in the switch) to 0 for easier data calculation for the visualization. Then another message rate limiter, set for 1 per 30 seconds. To manage the database calculation information I used the "bar-chart-data" node *(see image below)*. With this node, am I able to set the time range on the x axis to hourly over 24 hour period. ![](https://i.imgur.com/XCvAJD4.png =400x500) Finally, I used the chart button *(configuration image below)* to create a bar chart for my visualization method. ![](https://i.imgur.com/gp7bAXB.png =400x500) * Size = 16 x 3 to make it fit longer across the screen * Type = Bar chart (but there are several other alternatives to play around with) * Y-Axis = this is the frequency per hour (as set by x axis). I chose 10 as the max ## Final Product I have not finalized the assembly within the housing unit as I wish to wait until I have made the desired changes before I have a finished working product suitable for mounting outside my house. However, the finish product to this point can be seen below. ![](https://i.imgur.com/6PwcegE.jpg) At this juncture, both triggers do work, and do engage the camera. When triggered I do receive a notification on my Telegram app with a picture and timestamp. So it does completely function to those standards. However, before finalizing the prototype I would like to take it further than the restraints of this summer course will allow. In the following sub-section I elaborate further. ### Lessons Learned and moving forward In this project I opted to break it down into goal stages. First stage would be a simple button push that activates the camera. Second was a PIR sensor that activates the camera. This is as far as I got in this project. However, I had other potential stages that would have been nice to achieve. Third stage was adding audio communication (speaker and microphone), so I could interact with someone at my door. Fourth stage, would be adding live video feeds with object/facial recognition. Additionally, for the sake of simplicity I configured the image library when a picture is capture to just re-rewrite the same image each time an image is captured. In the future, I would rather each image have a unique name and file so if I happened to capture a burglar or something on the screen I have an original hardcopy to be shared with officials. As previously stated, moving forward I would rather have a more secure power supply. Speaking of security, I would also like to research greater encryption options to protect any tech-smart creeps from getting into our network through our Raspberry Pi smart doorbell. But for the sake of this class/project I chose to keep things very basic. Moving forward, I think it would be better to have a push button that utlized an LED, and I think using a 3D printer to custom design and print the housing would save space and be more aesthtically pleasing. Overall, the biggest lesson learned was this project could be as simple or difficult as you decide to make it. I think I had lofty aspirations, and as time went on needed to adjust my output with reality. Given more time and money to dabble further, I think this could be a total knock-off of Ring, but won't look nearly as streamlined.