Raspberry Pi Security Camera With Privacy Mode - ClosedCam

by opensource4life in Circuits > Raspberry Pi

810 Views, 8 Favorites, 0 Comments

Raspberry Pi Security Camera With Privacy Mode - ClosedCam

closedCamWebcamera 1.png
closedCamWebcamera 2.png

In this project you will learn how to build a surveillance camera based on a Raspberry Pi. You will be able to access the video stream from the camera through a password protected website. When logged into the website you will also be able to rotate the camera. In addition to this, the camera will have a built in feature to alert people nearby when it records them. This way, you can have a security camera that you are 100% sure is not filming you unless you are aware of it.

The Concept

closedCamWebcamera v14.png

The pan movement of the camera is quite straightforward; we simply connect the camera to a servo and control it with the pi.

The feature that alert people nearby when the camera is able to record is also fairly simple. A physical barrier connected to another servo is placed in front of the camera to block its field of view. To alert people nearby when the camera is able to record we simply introduce a switch that is pressed only when the barrier is fully closed. The switch is connected to a piezo-buzzer that makes a loud sound when the circuit is closed. As soon as the barrier is slightly opened the switch is opened and the buzzer is triggered. To avoid this feature being hackable we make it fully analog without any computers involved.

The last feature of this project is the website. To build this we will use flask and some simple HTML, CSS and JavaScript.

Hardware Components Needed

logitechbrio4k.jpeg
raspberrypi4power.jpg
m3screws.jpg
jumperwires.jpeg
rasapberrypi4.jpeg
buzzer_module.jpg
microswitch.png
microservo.jpg
202010.jpg

To make this project you need the following parts:

  • Raspberry Pi 4
  • Logitech Brio
  • A varied assortment of M3 screws from 4-12 mm and some M3 nuts
  • Some jumper wires
  • Two micro servos with servo horns and its respective screws
  • A piezo-buzzer (preferably a module to make it easier to mount)
  • Two small screws to fasten the piezo buzzer
  • One microswitch
  • A 20*20*10 fan running on 5V
  • 3D printed parts, get the 3D models here: https://gitlab01.copyleft.no/martin.ansteensen/closedcam.git

Physical Assembly

partsNumberedV2.png
mountRasPi.png
servoAndFan.png
fastenLid.png
fastenServoHorn.png
fastenServoHornToServo.png
lidServo.png
switchAndBuzzer.png
fastenCamera.png
connectCameraHo,der.png

Look at the pictures for further explanation and to see the names of the 3D printed parts. The physical assembly goes as follows:

  1. Mount the Raspberry Pi in Part 1
  2. Mount the fan and one of the servos onto Part 2
  3. Place Part 2 on top of Part 1 and make sure that the holes line up. Use three M3 bolts to join the two pieces together
  4. Fasten the servo horn to Part 6 using two screws
  5. Connect the servo horn on Part 6 to the servo in Part 2, and secure them with a screw trough the small hole in the center of Part 6
  6. Put the other servo in its designated place in Part 4. Secure the servo with two screws.
  7. Mount Part 3 to a servo horn using one screw, and secure the servo horn to the servo in Part 4 by using another screw
  8. Mount the buzzer module and the switch onto Part 4
  9. Insert the camera into Part 4 and use two M3 bolts to secure Part 5 to keep the camera in place
  10. Connect the two assembled parts by inserting two M3 bolts and nuts through the wholes in Part 6 and 4

Electrical Assembly

closedcam_curcuit.jpg

Wire the components as shown. You may have to undo some of the physical assembly in order to connect some of the wires:

  • Fan
    • Ground to any ground pin
    • VCC to any 5V pin

Because we use more VCC (5V) pins than the raspberry pi has, you either have to drop using the fan or solder together the two VCC wires (red) of the servos.

  • Pan servo
    • Signal to GPIO 17
    • Ground to any ground pin
    • VCC to any 5V pin
  • Lid servo
    • Signal to GPIO 27
    • Ground to any ground pin
    • If the two servo VCC wires are connected together, this pin will be the same as the one for the pan servo. If you chose to leave out the fan you can connect VCC to any 5V pin
  • Buzzer module with switch
    • Connect one of the outer pins of the switch (any pin but the one in the middle) to a ground pin on the pi (By utilizing the two outer pins of the switch the circuit is completed when the switch is not pressed, resulting in the buzzer making sound whenever the lid/barrier in front of the camera is not fully closed)
    • Connect the other outer pin of the switch to the ground pin of the buzzer module
    • Connect the VCC (+) of the buzzer module to the 3.3V pin of the pi

Software Setup

Before we begin installing the dedicated software you will need to have a fresh version of Raspbian installed on your pi. To update your pi, run the following command:

sudo apt-get update

Next, we will install the necessary libraries.

pip3 install RPi.GPIO 
pip3 install opencv-python
pip3 install Flask
pip3 install bcrypt

You are now ready to download the repo

First, open main.py. In the beginning of the script you will find these lines of code:

with open("/home/pi/closedCam/config.json", 'r') as json_file:    
	config = json.load(json_file)

Change the path of the config file to match your system.

Next, we will have to set up the configuration file. We start by finding the limits of our servos. First you have to open the file called servo.py. Find the line that looks like this:

servo = Servo(17, 2, 12, 90)

17 shows which GPIO pin the servo is connected to, 2 is the minimum value of servo signal (on a range from 2-12), 12 is the maximum value of the signal (on a range from 2-12) and 90 is the home/rest position of the servo (on a range from 0-180). Begin with using values that are in the midrange (e.g. 5 for min and 8 for max), run the program, and increase/decrease them until you are satisfied with the range of motion the servo has. Navigate to the config.json file and fill in the values. Repeat the process for the second servo.

Next we can configure the webcam. Find out what resolutions your camera supports for filming and choose the resolution you think is appropriate. Enter the values in the config file. You may have to play with the "videosource" value in order for the Raspberry Pi to find your camera.

We are now ready to configure the login credentials for the website. You will enter these into the config file in the dictionary named "login". Choose any username you want. In order for the website to be secure, we store a hashed version of the password in the config file. To create the hashed version you first have to choose a password. Then run the following code, replacing "your-super-secure-password" with the password you chose, and copy the output string into the config file.

import bcrypt

def create_bcrypt_hash(password):
    # convert the string to bytes
    password_bytes = password.encode()      
    # generate a salt
    salt = bcrypt.gensalt(14)               
    # calculate a hash as bytes
    password_hash_bytes = bcrypt.hashpw(password_bytes, salt)   
    # decode bytes to a string
    password_hash_str = password_hash_bytes.decode()            
    # return the password hash string
    return password_hash_str

print(create_bcrypt_hash("your-super-secure-password"))

Returns: $2b$14$bUJ3/nAH41.FVu/4mUxm0.NAU504bOPzUrT.KWSBGqW496ygmmxu2

The last parameter to change to have a working login functionality is located in the script.js in the static folder. Find the code that looks like this:

if(idleTime > 5) {
logOut(); }

This piece of code logs the user out of the page after x minutes, in this case 5 minutes. If you want a shorter/longer timespan you can change this code.

Finally, we have to create a secret key for our app which is used to sign session cookies for protection against cookie data tampering. You can create a key for yourself by running the following command in the terminal:

python3 -c 'import os; print(os.urandom(16))'

The camera is now fully configured and ready to use. If you want to learn more about the software, check out the repo.

Accessing the Livestream

Find the IP address of your pi by running the following command in the terminal:

hostname -I

In your browser, type in the following, but replace the IP address with the one of your pi. You can leave the port (4444) as it is, unless you have changed it.

192.168.90.110:4444

You should now be looking at the login page. Enter your username and password to access the videostream. To save the current frame from the stream, click the save icon in the bottom left corner. The image will be saved to the folder called savedImages. To control the servos, you have to drag the sliders. The slider to the left controls the pan movement, and the other slider controls the barrier in front of the camera.

The camera is now finished and ready to use!

Running the Server on Startup

There are many ways to make the server/website to start when the Raspberry Pi is finished booting. I'm going to show you one of the simplest ones. Log into your pi and run:

crontab -e

You should now see a file full of text. Navigate to the bottom after all of the comments and add this line. Be sure to change the path of main.py so that it matches your system.

@reboot python3 /home/pi/closedCam/main.py

Exit the file. To check that everything worked as intended, run the following command.

crontab -l

The crontab file you just edited should now be printed to the terminal, and the line you added should appear in the bottom. You can restart your pi and be sure that the website will be available whenever the Raspberry Pi has power.