Introduction: RPI Safety Camera With Motion Detection
This instructable will help you build a safety camera that takes pictures when a motion is detected. To turn the camera on and off you have to enter the code (using 4 simple buttons).
I also made a website with its associated database to view your pictures easily. You can also add more than 1 camera this way, and more than 1 user. If you don't want this you can skip step 3, and the website part of step 4.
Required to make this project:
- Know how to connect with Raspberry Pi & upload files
- Have python3 installed on your Raspberry Pi
- Know how to set up Flask on your Raspberry Pi
- Know how to set up MySQL on your Raspberry Pi
Step 1: Used Materials
Materials:
- 1 Raspberri Pi
- 1 Breadboard
- 1 RPI T-cobbler
- 1 Raspberry Pi Camera module
- 1 Infrared motion sensor
- 4 buttons
- 3 LED's
- 14 Male to female cables
- 7 Resistors (220 Ohm)
If you buy all the materials separately, the project will have a total cost of about €95.
I already had a raspberry pi starter kit that contained a lot of the materials.
In the attachment you can find a bill of materials.
Attachments
Step 2: Connect Everything
In this step we will connect everything to the raspberry pi.
Connect everything as shown in the fritzing picture.
For the buttons and LED's use the following GPIO pins:
- Button 1: GPIO12
- Button 2: GPIO16
- Button 3: GPIO20
- Button 4: GPIO21
- Green LED: GPIO18
- Orange LED: GPIO23
- Red LED: GPIO24
You can use different gpio pins if you want, but then you will need to adjust the code.
Connect the infrared motion sensor to GPIO pin 4.
Connecting and setting up the camera is really simple. Here's a video that explains how to do it.
Step 3: Create Your Database
In the file 'Snapguard.sql' you will find the sql code to create the database and the tables.
tblgebruiker
In this table we store all the users with their information.
tblcamera
In this table we put all the cameras and on what place we put them.
tblfoto
All pictures are stored in this table, also on what date & time they were made.
Attachments
Step 4: Upload the Code
Code explanation
Download the code here: https://github.com/MargodtM/snapguard
Code for the camera (snapguard.py):
If the green led is on, that means that the camera is taking pictures when someone walks in front of it. To turn it off, you have to press the first button, then the orange led will turn on. That means you can enter the code. You can always change the order of the code, but standard it's 2 times the first button, then the forth button and finally the third button. Only press the next button when the orange led is on again.
You can also change the path where the pictures are stored. Now they are stored in /home/pi/project/static/images
Code for the website (all the other files):
The website uses flask
If the file is running, you can view the website on http:// <enter rpi ip>:8080
Be sure to enter your database information in file "dbclass.py". Otherwise you will not be able to make connection.
Website pages:
- Pictures: Here you can view all the pictures that your camera made, click on one to view it full size or to delete it from the database
- Cameras: Here you can see all the cameras linked with your accound, edit them, or add them. The standard camera has ID: '1'. You can change the ID in the snapguard.py code
- Log in, log out & sign up
Step 5: Testing
Once the code is uploaded, you can test if everything works.
To run the python files, go to the terminal, en go to the folder where the files are located. Once you did that, type the following line:
sudo python3 snapguard.py & sudo python3 Project.py &
It's important to use python3, otherwise the mysql connector won't work.
Here's a video that shows what your project should do.
Step 6: Run Python Script on Startup
If you don't want to start the script manually everytime you plug in the raspberry pi, you can make this go automatically.
Go to your raspberry pi terminal and type:
sudo nano /etc/rc.local
In this file, go to the bottom and write the the following command:
sudo python3 [path to file]/snapguard.py & (and if you also use the website: sudo python3 [path to file]/Project.py &)
Don't forget the "&" at the end, this is needed because our script runs a continuous loop.
Make sure the "exit 0" is still at the very bottom of the file.
Now save the file and reboot.
If you're still having trouble doing this, this site might have a better explanation.
Step 7: Build a Case (optional)
You can build the case however you want, but here is how I did it.
I took 6 pieces of wood and nailed them all (except for one) together as a box. The one other piece, I attached with strong tape, this way I can use it as a little door to put the raspberry pi & cables in (as you can see on the first picture).
If you want the leds to come out the case from the top, you can use female to male cables, so you don't have to solder. (as you can see in the second picture).
The pins from the buttons were to short to put the cables on, so I had no other choise than to solder cables to it. (picture 3).
Now I drilled the holes for the leds, buttons, camera and motion sensor and I used a glue gun to put everything in place. If you want to paint the case, do this before you glue all your parts in!
As you can see, in my case I put the camera at the back of the case. If I had to remake it, I would put it in the front. That way you're sure that the case is not on the pictures the camera makes.