Raspberry Pi As Security Camera With Motion Detection

Ido Green
3 min readOct 9, 2018

--

It was a fun weekend project I did with my kids. We started with a new Pi Zero and in a few hours (of many ‘paths’ to nowhere) we got into the point of having a useful security camera. The useful part is when the camera sends you alerts (email or Telegram messages) when it detects movements.

We open the package and connected the Pi Zero to a USB power, a keyboard, a mouse and monitor. We cut a bit a corner by buying an SD card with NOOBS on it but it wasn’t working (nothing was coming up on the screen when we boot the Pi). So we downloaded a new version from Raspian Jessie 4.4 from NOOBS and install it. Now when we boot the Pi we got a new screen. We open the terminal and typed:

sudo raspi-config

This will open the Raspberry Pi Configuration tool.

In the tool you should make sure to enable:

  • The Camera — after all we need the camera as our input source.
  • The SSH Server — So we could work like pros.
  • Boot to command line without manual login.

At the terminal prompt type enter:

passwd

👍🏽 Make sure to have a strong password so no one could connect to your Pi.

Update your new OS with:

sudo apt-get update
sudo apt-get dist-upgrade

Now it’s a good time to check that your camera is working. Type:

raspistill -o theFirstImageFromPi.jpg

We will go with Motion as it got a low footprint. After all the Pi got very limited resources in terms of CPU, Memory.

The first step is to install motion. You can type:

sudo apt-get install motion

If something is not working with the apt-get you can try to download the last releases and install it with gdebi tool:

sudo apt-get install gdebi-core wget https://github.com/Motion-Project/motion/releases/download/release-4.1.1/pi_jessie_motion_4.1.1-1_armhf.deb sudo gdebi pi_jessie_motion_4.1.1-1_armhf.deb

Configuration

You can open the configuration file in edit mode by entering:

mkdir ~/.motion cp /etc/motion/motion.conf ~/.motion/motion.confsudo vi ~/.motion/motion.conf

Next go to this motion.conf gist and just copy it.

Executing

motion -c ~/.motion/motion.conf

You can connect to the web page that steam the videos at:

http://YourLocalPi-IP-address:9081 <-- Stream pagehttp://YourLocalPi-IP-address:9080 <-- Admin page

Alerts

We will set two types of alerts: emails and telegram messages.

Emails

sudo apt-get install ssmtpsudo apt-get install mailutilssudo apt-get install mpacksudo vi /etc/ssmtp/ssmtp.conf
# Below is the content for ssmtp.conf file. Make sure it's NOT your main gmail as we have the password here without encryption!
root=postmaster
mailhub=smtp.gmail.com:587
hostname=ras-pi
AuthUser=yourGmailUser@gmail.com
AuthPass=YourGmailPass
FromLineOverride=YES
UseSTARTTLS=YES

Now test that you can send emails:

mpack -s "The first email from my new Pi" /home/pi/logs/motion/someFile.txt yourEmail@gmail.com

👍🏽 Don’t worry as the motion.conf file is already configure to use mpack and send you the emails with the images once a motion is being detected!

Telegram

Now, if you like Telegram and wish to get these alerts in it as well — just use this IFTT integration between your gmail and telegram.

Last but not least, add the starting of motion to your rc.local file:

motion -c ~/.motion/motion.conf

You should also put a little script to clean images from Pi once the du -h is giving your less then ~25% of free space.

Done.

Be safe and happy.

Originally published at greenido.wordpress.com on October 9, 2018.

--

--

Ido Green
Ido Green

Written by Ido Green

Tech executive and entrepreneur 🏔️

Responses (1)