Outdoor Home Security Camera

This is part 1 of 4 of how to make security camera (or surveillance camera) built using a Raspberry Pi.

I got all the components to make four security cameras as my 2019 Christmas present.

Calin Crisan's MotionEye combined with a Raspberry Pi and a Raspberry Pi camera make an impressive DIY project that requires very little effort.

I don't think my home needs security cameras. We live in a low crime area. There are kids who open car doors and rummage for goodies, and we have an occasional porch pirate. I don't think the crime rate justifies the project. but this is a relatively easy project and I've always wanted to be like Brill in Enemy of the State and blow up my house when the feds break-in.

We do get cool animals going through the yard: skunks, deer, coyotes, foxes and opossums and possibly javelinas and badgers.

Parts:

  • 75 foot CAT6 Ethernet Cable, Amazon, $13.95

  • Power Over Ethernet (PoE) HAT for Raspberry Pi 3 B+ and 802.3af PoE Network, Amazon, $33.29

    • 2 x 20 Pin Stacking Header 2.54mm 11mm (the pins must be square and not flat), Amazon, $5.99 for four

    • 2 x 2 Pin Stacking Header 2.54mm 11mm (the pins must be square and not flat) (I sacrificed one of the 2x20 and cut out the 2x2 using a cutting tool that uses a high-strength razor-blade. Of course, this isn't recommended.)

    • 4x Nylon M2.5 15mm or longer standoffs with screws and nuts, Amazon, $12.99

  • Raspberry Pi 3 Model B+ Board (3B+), Amazon, $42.99

    • Initially, I tried a Raspberry Pi Zero. I wanted to use highest resolution and highest frame rate. This isn't really necessary. There are several options that would allow high resolution images using a Zero. Another consideration was that the 3B+ fit perfectly into the camera case.

  • 4x Red LEDs

  • Raspberry Pi Camera Module V2-8 Megapixel,1080p (RPI-CAM-V2 + 20"/500mm Flex Cable + Pi Zero Cable), Amazon, $29.99

  • Solar Powered Fake Security Camera with Realistic Red Flashing Lights (Quantity 4 black), Amazon, $30.99

  • SanDisk 64GB X2 (128GB) MicroSDXC Ultra Uhs-1 Memory Card, Amazon, $18.44

    • or SanDisk 64GB Ultra Fit Flash Drive, Amazon, $10.89

  • NETGEAR 24-Port Gigabit Ethernet Smart Managed Plus PoE Switch (JGS524PE) - with 12 x PoE @ 100W, Amazon, $199.99

  • 3 Inch Ultra Short Bootless Flat Ethernet Cable, Amazon, $8.99

  • RJ45 Waterproof Panel Mounting Cat5/5e/6 Female to Female, Amazon, $24.99 for 5

I first installed MotionEye OS and it worked great. However, I ran into issues when trying to use the GPIO pins. So, I found directions for installing MotionEye on top of Raspberry Pi OS. MotionEye OS is not Raspberry Pi OS-like (i.e., it is not debian derivative). MotionEye is a front end for motion.

I was also going to use one camera to display all the other cameras, and then thought it might be better to have Master Display with no camera.

Step 1.1. Add Camera and Heat Sinks to Raspberry Pi

Attach a heat sink. The PoE components on the bottom should not touch the components or heat sinks on the Raspberry Pi. (The flat or very low profile heat sinks don't do anything. My CameraStudy has one and it runs at 80.1-80.6 degrees C with the fan running at full speed. The other RPis with normal heat sinks run below 70C)

Remove the heat sink's tape and press firmly on to the CPU.

I bought Raspberry Pi heat sinks in bulk.

Insert screws and 15mm or longer hex standoffs (one piece hex nut and screw) from the PoE hat into the Raspberry Pi

If there is a small red protector strip on the Raspberry Pi camera slot, then remove it. Gently loosen the Raspberry Pi Camera connectors, insert the camera's ribbon cable and gently push the connectors back in.

Step 1.2. Add PoE Hat

Before doing the steps in the link,

  • It is easier to put 2x20 & 2x2 headers into the PoE, and then gently push the PoE Assembly on the Raspberry Pi.

  • add the female to male 2x20 pin header to the PoE Hat

  • add the female to male 2x2 pin header to the PoE Hat

  • use 15mm or longer nylon M2.5 hex standoff in the parts list, rather than the ones that come with the PoE Hat.

Then do the steps in 1.2's link to the Add PoE Hat

Note: In the PoE Install Instructions, do not insert the microSD card, that comes below in Step 1.3

I am going to install four security cameras under the eaves (or soffits) of my home. It is much easier to run an ethernet cable and use Power over Ethernet (PoE) than to install electrical outlets where the cameras are going to be placed.

A PoE Hat requires an ethernet switch that has Power over Ethernet ports.

There isn't any configuration required, just plug it in and it works.

Step 1.3. Setup Raspberry Pi

I think it is easier to use a flash drive instead of a microSD card for this application, but either can be used.

In the directions in the link, don't setup wi-fi but do set up ssh.

Also, instead of plugging in a Power Supply, plug in an ethernet cable from a PoE switch to power the Raspberry Pi.

After this step, you will have a chance Raspberry Pi setup with the following:

your-ip-address

♣hostname♣

your-password

For this project, I skipped the ssh hardening

Step 1.5. Enable Camera

Enable the camera:

$ sudo raspi-config

Interface Options, enable camera

$ sudo reboot

Step 1.6. Install MotionEye

Get the script and run it. The script installs all of the needed packages and python modules.

$ cd ~/.

$ wget https://raw.githubusercontent.com/dumbo25/security-camera/main/motioneye.sh

$ sudo bash motioneye.sh

MotionEye and MotionEye OS are so wonderful that I encourage donations to keep this work going.

MotionEye uses systemd. So, it starts and runs on boot and restarts on failure.

If required, here are the steps that are supposed to update MotionEye to the latest version. I installed motionEye version 4.2.2. So, I have not successfully updated yet:

$ sudo pip install motioneye --upgrade

$ systemctl restart motioneye

Step 1.7. Setup Camera Drivers

Edit

$ sudo nano /etc/modules

and add line

bcm2835-v4l2

Execute the commands:

$ sudo modprobe bcm2835-v4l2

$ sudo reboot

Step 1.8. Configure uncomplicated firewall

ufw was setup Run the following commands:

$ sudo ufw status

$ sudo ufw allow from 192.168.1.0/24 to any port 8765

$ sudo ufw allow from 192.168.1.0/24 to any port 80

$ sudo ufw deny 443/tcp

$ sudo ufw deny 80/tcp

$ sudo ufw allow from 192.168.1.0/24 to any port 22

$ sudo ufw deny 22

$ sudo ufw allow from 192.168.1.0/24 to any port 7 # allows ping from my LAN

$ sudo ufw status

This configuration allows MotionEye to work within my LAN (192.168.1.0/24) but not from outside of it (i.e., no one on the internet should be able to access your camera)

I had to delete several firewall rules. Run this command to see your firewall rules (mine are shown):

$ sudo ufw status numbered

Status: active

To Action From

-- ------ ----

[ 1] 22/tcp LIMIT IN Anywhere

[ 2] 443/tcp DENY IN Anywhere

[ 3] 80/tcp DENY IN Anywhere

[ 4] Anywhere ALLOW IN 192.168.0.0/24

[ 5] 8765 ALLOW IN 192.168.1.0/24

[ 6] 80 ALLOW IN 192.168.1.0/24

[ 7] 22 ALLOW IN 192.168.1.0/24

[ 8] 22 DENY IN Anywhere

[ 9] 7 ALLOW IN 192.168.1.0/24

[10] 22/tcp (v6) LIMIT IN Anywhere (v6)

[11] 443/tcp (v6) DENY IN Anywhere (v6)

[12] 80/tcp (v6) DENY IN Anywhere (v6)

[13] 22 (v6) DENY IN Anywhere (v6)

And then to delete one of the firewall rules in brackets [N], run the command

$ sudo ufw delete N

Until it looks like the above

Note: MotionEye streams video on port 8081, which might need to be allowed on ufw from my LAN

Step 1.9. Try It

Open a browser and enter one of the following in the URL box:

http://your-ip-address:8765/

http://hostname:8765/

Login with admin and no password

Setup camera

      • Local MMAL Camera

      • VideoCore Camera

Note: if the camera above doesn't work, you can add (camera1 drop down) or delete (trash can icon) cameras and try again

Step 1.10. General Settings

MotionEye automatically expands the filesystem to use the whole microSD card

Click on the hamburger icon (three lines) and set your admin and user password

General Settings:

Password: your-admin-password

Password: your-user-password

Click Apply

Reboot

Step 1.11. Match resolution to camera

The Raspberry Pi camera is 1080p, which means the resolution is 1,920x1,080. However, if you use this setting in MotionEye OS, then you get the following error in /var/log/motion.log:

motioneye motion_init: Substream not available. Image sizes not modulo 16.

1920x1080 is not evenly divisible by 16 (1080/15=67.5). The highest working size is 1600x1200


Click on the hamburger icon

Expand Video Device:

Change Video Resolution to 1600x1200

Change Frame Rate 2 seems too jerky, 10 is okay, but this will drive up video file size

Click Apply

Step 1.12. Network, Services and Expert Settings

MotionEye OS has these settings, but with MotionEye on Raspberry Pi OS these are set with rasp-config.

Step 1.13. Script and cron to check disk space usage

Videos are only kept for 7 days and only shot when there is motion. However, it is still possible for all the disk space to be used relatively quickly*.

The script checks if a threshold of disk usage is being approached, and it will automatically delete the oldest files.

Get the script

$ wget https://raw.githubusercontent.com/dumbo25/security-camera/main/disk-usage.sh

And a add crontab

$ sudo crontab -e

and add a line at the end:

*/10 * * * * sudo /usr/bin/bash /home/pi/disk-usage.sh >/dev/null 2>&1

Go to the next part (Part 2) of the Security Camera project