For school related purposes, I have been working on an IoT doorbell that takes pictures/records video of people who come stand near your front door. It has been a fun little project albeit not completely finished, this instructable will teach you how to set up to the point I've reached given the time limit.
Teachers! Did you use this instructable in your classroom?
Add a Teacher Note to share how you incorporated it into your lesson.
Step 1: Parts
First and foremost, you will need a couple of components in order to recreate the circuit:
- Raspberry Pi 3 Model B + power supply: €39.90
(You are free to choose a different model, but you will need Bluetooth and Wi-Fi functionality in order for this to work)
- HC-SR501 PIR Infrared Motion Sensor: €5.95
- 1x Push button
- 1x Buzzer (a speaker will work as well, as long as you add an additional capacitor)
- 1x Any LED
- 1x 220 Ω resistor
- 1x 1000 Ω resistor
- Cable connectors
- Raspberry Camera Module V2: €31.95
- Bluetooth speaker with power supply (I bought one for under €10 in the local Action, but any speaker will do)
- SD card (at least 8GB, ideally more if you want to store a lot of camera footage)
- Network cable
Step 2: Building Circuit
Note: while preparing this build I was utilizing a T-Cobbler Plus and a speaker. To make your build more compact, I advise connecting the cables to the pins directly and using a buzzer instead of speaker in order to save space. Note 2: ideally you want to use a PCB while finalizing the doorbell. If you want, you can keep the breadboard connections, but you will have to solder the button and LED light as it's hard to connect them properly by simpler means.
For this build, we are a voltage of 5V. The camera will be connected directly to the Pi with the camera port. In order to connect the Pi camera properly, I recommend watching the video above if you want to safely connect the camera to your Raspberry Pi without causing damage to the cable/tin connectors. A short summary of the video:
- Open the box the camera got delivered in. It's covered in an anti-static bag: I recommend keeping it in case you want to safely store away the camera for later projects, so you don't harm the camera with static electricity.
- Open up the Camera port on the Raspberry Pi. Don't touch the tin connectors as the camera is very sensitive to static.
- Insert the camera with the tin connectors facing the HDMI and the blue label facing the Ethernet port.
Step 3: Installing Python / IDE of Choice (Pycharm)
In order to start installing everything, you first need to have the proper software installed. An IDE (Integrated Development Environment) will make your life a lot easier while editing code.
The Python installer can be downloaded directly from their website.
You are free to utilize any IDE you want. This instructable will cover how to install necessary packages and how to make database connections in Pycharm.
Step 4: Downloading/installing Raspbian Jessie/PuTTy/WinSCP/XMing/Win32DiskImager
You will need to install an operating system on the Raspberry Pi (and be able to connect to it with SSH). In this instructable, we will be working with Raspbian Jessie (note: to establish the SSH connection with Raspbian Jessie, either download a version released before November 2016 or read this article in order to enable SSH on startup). To make the SSH connection, we will use PuTTY.
WinSCP is not a necessity, but will come in handy if you want to view the camera footage directly from the Pi rather than the website.
Step 5: Setting Up the Raspberry Pi, Establishing Connection Between Pi and Pycharm
- Open Win32DiskImager and write the Raspbian Jessie image to the SD card. Once installed, open the file "cmdline.txt" and add "ip=169.254.x.x" in front of "rootwait" (replace the x with whatever works for you, for example 169.254.10.11).
- Safely unplug the SD card from your PC and insert it into the Raspberry Pi. Open PuTTY, select SSH (it's the default option) and connect to the IP address you have inserted earlier into the cmdline.txt file. It will ask you for a username and password: the default username and password are 'pi' and 'raspberry' respectively.
- Configure your network connection. You can do this in /etc/dhcpcd.conf and /etc/wpa_supplicant/wpa_supplicant.conf. To restart the network, type 'sudo service networking restart'.
- Type 'sudo raspi-config' in the terminal window. Enable the camera module and expand the filesystem.
- In order to test if the camera module is enabled and the camera is properly connected, type 'vcgencmd get_camera'. This can be handy while troubleshooting issues with the camera connection.
- Update the Pi if necessary by performing the following command: 'sudo apt-get update && sudo apt-get upgrade'. This can take a while depending on how it has been since the last time you have updated the operating system.
- You will need to install a couple of (Python) libraries in order to make everything work properly. (due to issues with Pycharm it's easier to install these from the terminal, other libraries like os, time, datetime etc aren't half as difficult to install)
sudo pip install python3-dev
sudo pip install pybluez
sudo pip install bluetooth
sudo apt-get install libbluetooth-dev
sudo pip install pygame
sudo apt-get install python-picamera
- The SFTP host is the IP address of your Raspberry Pi.
- The root path is the folder in which the project will be saved on your Pi.
- Don't forget to define the local and deployment path in Mappings!
Step 6: Configuring Bluetooth Speaker to Auto-connect With Pi on Boot (Part 1)
The title says it all, we need to make the Bluetooth speaker connect with the Raspberry Pi on startup. In order to do this, we must first acquire the MAC-address of your speaker. Be sure to note it down somewhere for future reference as you will need this to edit the appropriate sound card and device for audio in the programming code. I'm assuming you have already installed the bluetooth tools and libraries listed in the previous step.
- Go to the Terminal and type the following commands:
sudo apt-get install pulseaudio -y
- scan on
- You should be able to see your device and its corresponding MAC address, implying the Bluetooth speaker is on. Save it for future reference.
mkdir -p ~/scripts
(enter the code below into the nano file and save it with CTRL + X)
bluetoothctl << EOF
connect [enter your MAC add]
chmod +x ~/scripts/autopair
(go to the bottom and add this line of code)
Step 7: Github Code, Editing Code to Your Liking, How to Set Up PIRCamera
To download the code, go to Github and download as .zip. Paste the necessary code in your project (docam.py is redundant and only for manual testing purposes).
Open PIRCamera.py and make sure all the Python libraries are installed. To install these, navigate to Settings → Deployment and press the green + button. Type in the packages listed in the various imports if you are missing a few.
Navigate to __bluetoothScan() and edit the last line to match your MAC address.
call('pacmd set-default-sink bluez_sink.', shell=True)
Check if the mentioned paths in takePicture(), recordVideo() and knop_callback are correct and edit accordingly depending on your deployment path.
Open docam_examen.py and adapt the UPLOAD_FOLDER path accordingly.
Edit the pin numbers of the PIRCamera object if needed according to the following format: pir, led, button, speaker
Step 8: Setting Up MySQL Database on Raspberry Pi
- Open the Terminal window and type in the following commands.
- Optional: sudo apt-get update && sudo apt-get upgrade (only necessary if you haven't done this yet in the previous steps)
- sudo apt-get install mysql-server
- sudo apt-get install mysql-client
- mysql -uroot -p
- We have to make two users.
CREATE USER 'finley' @ 'localhost' IDENTIFIED BY 'some_pass';
GRANT ALL PRIVILEGES ON *.* TO 'finley' @ 'localhost' WITH GRANT OPTION;
CREATE USER 'finley' @ '%' IDENTIFIED BY 'some_pass';
GRANT ALL PRIVILEGES ON *.* TO 'finley' @ '%' WITH GRANT OPTION;
We can now make our database.
CREATE DATABASE docam;
To quit the MySQL connection, type:
sudo apt-get install python3-mysql.connector
Open a data source in Pycharm. Fill in your made credentials (use the pictures as a reference).
Add the tables in Pycharm's editor (use the ERD as a reference on how to make them).
Open DbClass.py and edit the credentials located in the init if necessary.
Your database should be connected to your project now.
Step 9: Case
The way you make the case is up to you, just make sure the case is big enough. The material can be anything as long as it's non-conductive. I personally opted for multiplex due to its simplicity since I was running out of time.
Things to consider while making the case:
- You'll need to drill a couple of holes in the material. A standard LED light is about 5mm wide, the camera lens is 7.5mm wide and the PIR sensor is about 22mm wide. The button depends on the button you own, the push buttons in the Raspberry Pi toolkit that I used are about 7.5mm wide.
- Depending on how thick your material is, you might need to make the hole for the camera bigger going outwards so it doesn't pick up the material while gathering footage.
- The camera connector is very small. Ideally you would extend the camera connector by using a different flex cable. The reason this is a problem is because it's genuinely not a good idea to leave a box with the Pi in it because of various reasons:
- It means people can get direct access to your Pi and its files without much effort.
- Rather than trying to collect data from your Pi at your front door, it's more likely that thieves will take advantage of your Pi not being in a separate case and simply steal it. Considering the pricing of the parts, it'd be a real shame if someone stole all the parts. So either separate the Pi from the pir/camera/button/led or make your case a real pain to open by regular means.
- The login system only requires a quick glance at the docam_examen.py file to figure out. A better way to secure the data would be to make an additional table with (a) user()s and encrypt the password(s) (with MD5 for example).
Step 10: Testing
Now you have set up everything, it's time to test how it works.
Type the IP address and the corresponding port number in your browser of choice (Chrome recommended for optimal results).
Once you have connected to the website, fill in the login form. You can find/edit the credentials in 'def do_admin_login():'
if request.form['password'] == '<b>password</b>' and request.form['username'] == '<b>admin</b>':<br>
When you have successfully logged in, you will be referred to the homepage.
- Timeline contains a list of all images (and videos, but I have not managed to finish that functionality as of writing) captured by the camera
- Sound contains a volume slider and a list of all available ringtones. You can select .mp3 and .wav files to play from your Bluetooth speaker and add your own!
- User settings contains options to change framerate, resolution, camera mode (image, video) and brightness. (everything but brightness is still in progress)
If everything is working fine, make a script so it executes the Python script on boot.