Smart doorbell configuration

Documentation for video:

Install Raspian Buster on a sd-card in standard configuration with graphical user interface. Then do:

sudo apt-get update //update package list
sudo apt-get upgrade //upgrade packages
sudo apt-get install nginx //install the nginx webserver
sudo apt-get install php-fpm //install php
sudo apt-get install omxplayer //install omxplayer

Now we need to modify the config for the default site on Nginx:

sudo nano /etc/nginx/sites-enabled/default

find the line

index index.html index.htm;

and add index.php after index to look like this:

index index.php index.html index.htm;

Scroll down until you find the block which starts like:

# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000

and uncomment these lines! ONLY these:

#location ~ \.php$ {
#                include snippets/fastcgi-php.conf;
#fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
#}

The block should look like this now:

  # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        location ~ \.php$ {
                include snippets/fastcgi-php.conf;

        # With php-fpm (or other unix sockets):
        fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
        # With php-cgi (or other tcp sockets):
    #    fastcgi_pass 127.0.0.1:9000;
        }

Restart the webserver to apply changes:

sudo /etc/init.d/nginx reload

Lets create a test file to see if php is working properbly:

sudo nano /var/www/html/phptest.php

Add this line to the file and save it:

<?php echo phpinfo(); ?>

Now open your browser and enter the url http://<yourraspberryip>/phptest.php and you should see this screen:

Congrats! The webserver and php is running. Now lets create the scripts and allow www-data to execute:

cd /var/www/html/
sudo mkdir scripts
sudo nano scripts/klingel.sh

klingel.sh:

#!/bin/bash

#export x-window vars so that the pi now where to show things
export DISPLAY=":0"
export XAUTHORITY=/home/pi/.Xauthority

#start omxplayer and open the stream from your camera.
#check your camera config for the url of a rtsp or mjpeg stream!
#the --orientation flag is optional. My screen is tilted so I added that!
omxplayer rtsp://<ipofyourcamera>/Streaming/Channels/2 --orientation 270 &

#pause for 16 seconds to show the stream
sleep 16

#all done, so kill the player and end the script!
kill $(pgrep omxplayer)

Save and close and create the start.sh in the same directory:

sudo nano scripts/start.sh

start.sh:

#!/bin/bash

#start the klingel.sh script as user pi!
sudo -u pi /var/www/html/scripts/klingel.sh

The scripts need to execute so do the following command:

sudo chmod +x /var/www/html/scripts/klingel.sh
sudo chmod +x /var/www/html/scripts/start.sh

To make it work, we need to allow the user www-data to run scripts as “pi” user. This could be a security risk so be careful and don’t expose the host to the internet!

sudo visudo

Find the following line:

root    ALL=(ALL:ALL) ALL

Add add below:

www-data ALL=(pi) NOPASSWD: /var/www/html/scripts/start.sh

The last step: create a php file that executes the start script:

sudo nano /var/html/www/show.php

show.php


<?php

exec("sudo -u pi /var/www/html/scripts/start.sh");

?>

That should be it! Reboot your Pi and on another computer open the url http://<yourpiipaddress>/show.php and the omxplayer should start showing the feed of your camera for 16 seconds!

Share

You may also like...

Leave a Reply

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