Sudo Su


How to turn a Raspberry into a security camera

Date: 5 October, 2021

A few months ago I started a project that tried to turn my Raspberry Pi into a security system with surveillance camera. This system has motion detection that when activated sends an alarm to our Telegram and also has 24 hour streaming to the security camera.

That is, I can see my security camera from anywhere and also sends me to my Telegram a video of what it sees when it detects movement.

To set all this up we are going to install a software called motion, it has a lot of options and has worked well for me for months so I recommend it.

To start in this project you need to have previously:

-A Telegram bot (click here to learn how to create one in less than 5 minutes).

-A Dynamic DNS (click here to learn how to configure it on your Raspberry easily).

-A camera (I bought this camera in Aliexpress, it is cheap and fulfills its function, although I do not rule out buying the official one someday to get a higher quality).

Once you have these 3 things let’s get started!

 

Configure camera in Raspberry.

After connecting the camera to our Raspberry, the first thing to do is to enable it.

To do this we will run the following command in our terminal:

sudo raspi-config

 

Use the arrow keys on your keyboard to go down to the Interfacing Options option and press the Enter key.

Interfacing options

 

Now click on the first option called Camera:

camara raspberry configuración

 

It will ask us if we want to enable our camera, we click Yes.

aceptamos que la cámara sea habilitada

 

A message will appear informing us that the camera has been enabled, we give to Accept and we click on Finish.
After all this, the following window will appear requesting a restart, click Yes.

 

After restarting our Raspberry we must check that our camera is working, to do this we will take an image by running the following command in our terminal:

raspistill -v -o test.jpg

 

Now if we go to our main directory, we can see how the picture was taken:

Foto de camara en raspberry

 

Download scripts to be used by Motion.

The next step is to download the directory that will contain the scripts that will notify us via Telegram and store the images taken by Motion.

For them we must have a directory for our scripts in our main directory. If we do not have it we create it using the following command:

mkdir scripts

We move to the scripts directory and download the directory containing everything with the following commands:

cd scripts
git clone https://github.com/CarlosPCL98/camara_seguridad.git

We enter the directory we just downloaded:

cd camara_seguridad
Once inside you will find two scripts and a directory called fotos_y_videos.
IMPORTANT! We need to change the permissions of the directory fotos_y_videos, we do this so that Motion has the necessary permissions that will allow it to save the photos and videos that it will take when it detects motion. We apply the permissions with this command:

sudo chmod 777 fotos_y_videos
Now we are going to modify the script that will alert us every time Motion detects movement, this scrips is called alerta_movimiento.sh:
nano alerta_movimiento.sh
We simply add our TOKEN and Telegram ID and save using Ctrl+O.
Now we must modify the script that will send us the video that Motion has taken when detecting movement. This script will also contain some lines that will clean the directory photos_and_videos, since Motion creates many photos and with the videos that it takes will be enough.

This file is called enviar_video.sh and we open it with:

nano enviar_video.sh

We simply add our TOKEN and Telegram ID and save using Ctrl+O.

These scripts should look something like this:

script que envía vídeo a telegram

With this last step we will have all the scripts ready and we will only need to install Motion.

 

Installation and configuration of Motion on Raspberry Pi.

At this point in the manual, we come to the really important part. Now we are going to install Motion and a friendly text editor that allows us to locate the parameters we need to modify in a quick and easy way.

The first thing we are going to do is to update the repositories of our operating system:

sudo apt-get update

After that we install Motion using the following command:

sudo apt-get install motion -y

Once the download is finished, we will install the Gedit text editor (it is not really necessary, but it is interesting since this editor allows us to easily search for what we want in a text document using the Ctrl+F key combination):

sudo apt-get install gedit -y
After having installed everything, the first thing we are going to do is to modify the file that allows the Motion daemon, for it we will open it by means of the following command:
sudo nano /etc/default/motion

 

The only thing we must change in this file is where it says no for a yes, as shown in the following image:start motion daemon yes

Once we have modified this, we save with Ctrl+O and restart our Raspberry by means of Ctrl+O:

sudo reboot

 

The Motion settings are made in the file /etc/motion/motion.conf, for security reasons we are going to back up this file using:
sudo cp /etc/motion/motion.conf /etc/motion/motion_new.conf

 

Before I start with the really interesting part, I want to tell you that Motion has many more options to configure than the ones I am going to explain here, all of them are available at oficial documentation
has very interesting security configurations.

 

Now we are finally going to make the necessary configurations for our system to work. We open the motion.conf file with the following command:
sudo gedit /etc/motion/motion.conf

 

I recommend that you look for the parameters that I will modify by pressing Ctrl+F..

 

To activate the Daemon we modify where it says daemon off by an on, as shown in the following screenshot:
daemon on motion

 

The next field is not really necessary to modify, this simply rotates the image of your camera. I for example want it at 270º because I have it in a special position, so I leave it like this:

.

rotate camera motion

 

These fields are really important to take into account (width,height and framerate), the following values refer to the resolution we want to use in our camera and how many fps it will use. I for example prefer to use 640×480 at 90 fps.
width height framerate camara motion

 

Now we are going to tell Motion the amount of pixels that must change to consider that a movement has occurred in front of the camera. One way to calculate this would be to think of the percentage of the screen that must have changed to be considered motion (For example 10% of the pixels).

.

So if we calculate 10% of 307200px(640×480) we would get 24576 and this would be the amount that we should indicate.
I’m going to put 8000 because it is the setting that has worked best for me in the months I’ve been using it, but I recommend that you start from a number and go testing the sensitivity and adjust it little by little, as this may vary to the environment you are focusing.
This will be configured in the field called threshold..
we will modify the pixels that must change to be considered a movement.

 

Now we will activate the option that allows Motion to save the pictures and videos it takes when it detects motion.
Now we will activate the option that allows Motion to save the pictures and videos it takes when it detects motion.
We will look for the option called .output_pictures and write on.
output pictures

 

The next step is to tell Motion the directory where it should save the images it takes, we go to the option called target_dir and indicate the directory we downloaded previously called photos_and_videos.

.

target_dir motion

 

Now let’s enable the option that allows accessing the stream from anywhere (if for security reasons we decide not to enable it, Motion will continue to send alarms to Telegram), this we will do by modifying the option called stream_local with an off. 
By leaving this option off, Motion understands that we want to access it from any device.
stream_localhost off

 

If we have activated the previous option, it would be highly recommended to add a login. Since if we don’t do it anyone could access the image of our camera.

 

In stream_auth_method I have chosen option 2, since it will treat our username and password using the MD5 cryptographic hash.

 

In stream_authentication add the username and password you want to use separated with a colon, I recommend you use a long password and containing a special character. Because if not, they could access our camera through brute force attacks.

 

Don’t forget to remove the  ; in front of stream_authentication:
authentication en motion login

 

The last configuration that we must perform is the one that allows Motion to run the scripts that we have downloaded before. We will do this by using the on_movie_start and on_movie_end, these two options allow Motion to execute a script when it starts and ends a motion.
Remove the ; before these options and write the script execution as shown in the image:
on_movie_start and on_movie_end

 

After this we would have all the configurations ready, we would only have to save the file. Even so I leave a comparative as a summary of the lines that I have modified in this file:
resumen de cambios en motion.conf

 

Finally, we will restart the Motion service using the following command, and all the settings will be applied:
sudo service motion restart

 

Checks.

.
The first thing we need to do is to trigger a movement in front of the camera to see if, it sends us our notifications to Telegram, these should look like this:

Motion alarma en Telegram

 

Now to access the streaming of our camera, simply open the browser and enter the IP of our Raspberry and the port used by Motion, which is 8081.

An example of how we would access is: http://192.168.100.15:8081

Once we enter the page we will see a login similar to this:

login camara motion

 

Enter the username and password that we have created in the motion.conf file, and we will have access to the image of our camera:motion funcionando pc

 

To access from anywhere in the world, as I said at the beginning of the article we will need a DDNS (you can learn how to create one by clicking here). Once you have it, you simply need to enable the traffic through port 8081 of your router firewall to the IP of your Raspberry and you will be able to access through your non-ip address, for example: http://ejemplo.ddns.net:8081

motion en tu telefono

 

Enter your username and password and you will have access to your security camera:

imagen de motion en el movil

 

At this point it would be advisable to create an access to this page in our home screen, click on the browser options and click where it says Add in the Start window.

Añadir motion a la ventana de inicio

 

This way we will already have access to our security camera with a simple icon on the screen of our phone.

Motion en la pantalla principal de android

 

Security recommendation

.
It would be advisable that you change the port 8081 by another, since being the one that brings by default would be easier to find and vulnerate, this would be done in the motion.conf file modifying the value to the option called stream_port..

 

Finally I want to thank you for having come this far and I hope you have configured your security camera without any problems, do not hesitate to let me know any questions or suggestions. Best regards!

 


Author: Carlos Categories: Raspberry / Security



Leave the first comment!-

Leave a Reply

Your email address will not be published. Required fields are marked *