Raspberry Pi Garage Door Opener With Streaming Video of Door Status.

This Instructable modifies two other guides. Just got a Magnetic Sensor hooked up with door status on page.

Step 1: Do This Instructables First

You need do this Instructable @ https://www.instructables.com/id/Raspberry-Pi-Gara...

In above external Instructable:

  • Instead of step 1
    • Download Rasbian Jessie Lite has no GUI
    • Unzip the IMG
    • Use Etcher or some SDCARD imaging software for the IMG
    • Open the boot directory on the SDCARD and make a "ssh" file so SSH will work
    • Place card back in Raspbarry Pi2 or 3
    • Turn on
  • Before step 3
    • ssh to you pi
    • sudo raspi-config
      • #1 change the password from default
      • #2 change hostname
      • #3 Boot options
        • B2 yes
      • #4 Localisation .....
        • I1 , I2, I3
      • #5 Interface options
        • P1 enable Pi camera
      • #7 Advanced
        • A1 expand file system
      • #8 Update tool to latest version
      • Finish
    • Reboot
    • Update Raspbian and its packages
      • sudo apt-get update
      • sudo apt-get upgrade
      • sudo reboot
    • Continue Step 3 above

Step 2: Get the Camera Working on Raspbian Jessie

Ok here is the proper way to get the camera working:

Install motion to get dependecies.

sudo apt-get install motion
sudo apt-get install libjpeg62

Create a directory in your home called mmal

cd ~/
mkdir mmal

cd mmal

Download @maya fork of dozencrow’s motion program that works with Rasbian Jessie

wget https://www.dropbox.com/s/6ruqgv1h65zufr6/motion-...

sudo apt-get install -y libjpeg-dev libavformat56 libavformat-dev libavcodec56 libavcodec-dev libavutil54 libavutil-dev libc6-dev zlib1g-dev libmysqlclient18 libmysqlclient-dev libpq5 libpq-dev

tar -zxvf motion-mmal-lowflyerUK-20151114.tar.gz

./motion -c motion-mmalcam-both.conf

It should work now when you goto RaspiIP:8081

CTRL-C to quit

Edit the config file and make changes

nano motion-mmalcam-both.conf

target_dir /home/pi/mmal/m-video

output_pictures off

framerate 100

Step 3: Edit /var/www/css/style.css Next

pi@raspberrypi$ nano /var/www/css/style.css

Replace all the text with this

html,body {
	background-size: 980px 735px;
	background-repeat: no-repeat;
a {
	border-radius: 15px;
	border: 2px solid #263238;
	-moz-box-shadow:    inset 0 0 10px #000000;
	-webkit-box-shadow: inset 0 0 10px #000000;
	box-shadow:         inset 0 0 10px #000000;
	-webkit-tap-highlight-color: rgba(255, 255, 255, 0); 
a:active {
	-moz-box-shadow:    inset 0 0 50px #000000;
	-webkit-box-shadow: inset 0 0 50px #000000;
	box-shadow:         inset 0 0 50px #000000;

Step 4: Modify Sudo Nano /var/www/index.php

Download index.txt and then rename to index.php and put in /var/www/

Step 5: Make Your Webpage More Secure

In order to create the file that will store the passwords needed to access our restricted content, we will use a utility called htpasswd. This is found in the apache2-utils package. This was installed in Step 1.

pi@GaragePi2:~ $ sudo htpasswd -c /etc/apache2/.htpasswd pi

(You will be asked to supply and confirm a password for the user)

New password:

Re-type new password:

Adding password for user pi

If we view the contents of the file, we can see the username and the encrypted password for each record:

pi@GaragePi2:~ $ cat /etc/apache2/.htpasswd


Configuring Access Control within the Virtual Host Definition
Begin by opening up the virtual host file that you wish to add a restriction to. For our example, we'll be using the 000-default.conf file that holds the default virtual host installed through Raspbian's apache package:

sudo nano /etc/apache2/sites-enabled/000-default.conf

Inside, with the comments stripped, the file should look similar to this:

ServerAdmin webmaster@localhost

DocumentRoot /var/www

ErrorLog ${APACHE_LOG_DIR}/error.log

CustomLog ${APACHE_LOG_DIR}/access.log combined

Authentication is done on a per-directory basis. To set up authentication, you will need to target the directory you wish to restrict with a block. In our example, we'll restrict the entire document root, but you can modify this listing to only target a specific directory within the web space. Within this directory block, specify that we wish to set up Basic authentication. For the AuthName, choose a realm name that will be displayed to the user when prompting for credentials. Use the AuthUserFile directive to point Apache to the password file we created. Finally, we will require a valid-user to access this resource, which means anyone who can verify their identity with a password will be allowed in:

ServerAdmin webmaster@localhost

DocumentRoot /var/www

ErrorLog ${APACHE_LOG_DIR}/error.log

CustomLog ${APACHE_LOG_DIR}/access.log combined

See line on screenshot....and place on this line with indentation...Instructables will not display

AuthType Basic

AuthName "Restricted Content"

AuthUserFile /etc/apache2/.htpasswd

Require valid-user

See line on screenshot....and place on this line with indentation...Instructables will not display

Save and close the file when you are finished. Restart Apache to implement your password policy:

sudo service apache2 restart

The directory you specified should now be password protected.

Step 6: Modify Sudo Nano /etc/init.d/garagerelay

#! /bin/bash
# /etc/init.d/garagerelay

# Carry out specific functions when asked to by the system
case "$1" in
echo "Starting Relay"
# Turn 0 on which keeps relay off
/usr/local/bin/gpio write 0 1
#Start Gpio 0 or 17 in BCM out mode
/usr/local/bin/gpio mode 0 out
#Start Gpio 2 or 27 in BCM in mode with pull up
/usr/local/bin/gpio mode 2 in
/usr/local/bin/gpio mode 2 up
echo "Stopping gpio"
echo "Usage: /etc/init.d/garagerelay {start|stop}"
exit 1

exit 0

Step 7: Make Motion Start at Boot

$ sudo nano /etc/init.d/cam_motion

Then paste the blob below.

#! /bin/sh
# /etc/init.d/cam_motion

# Carry out specific functions when asked to by the system
case "$1" in
echo "Starting Camera Motion"
nohup /home/pi/mmal/motion -n -c /home/pi/mmal/motion-mmalcam-both.conf 1>/dev/null 2>&1 ;;

echo "Stopping Camera Motion"
killall motion
echo "Usage: /etc/init.d/cam_motion {start|stop}"
exit 1

exit 0

crtl o to save
crtl x to exit

Make it executable

$ sudo chmod 777 /etc/init.d/cam_motion

Make it start at boot

$ sudo update-rc.d -f cam_motion start 4


Step 8: Bring Up on Web Browser Once You Setup Your Router Port Forwarding

Got one of those SmartPi cases for Pi and Camera that has Lego as a cover. Attached relay board to back of case with zap straps.

I put the magnetic switch at the top of the garage door and used some cat5 cable I had to bring it back to the Pi. Cut some jumpers in have and soldered to the cat5 one pair. Wrapped the extra pairs back.

Found really simple fix for Away and Home.

Setup the index.php that does the buttons and video etc to be for home like

Then make a copy of index.php to away.php still in the /var/www/ folder.

Edit away.php to be like

In your router:
Port Forward Wan port xxxx1 to Lan 80 of RaspiIP
Port Forward Wan port xxxx2 to Lan 8081 of RaspiIP

Now in your Browser at home or your cellphone:
At home use http://RaspiIP this will default to port 80 and index.php
When Away use http://YourDdnsAddress:xxxx1/away.php

Step 9: Final Result: Garage Control/monitor

I soldered a pair of wires across the back of the circuit board of a RF Garage opener. Put wires behind the bigger button as there already was a slot in the plastic. Putting the relay directly to the 2 wires between wall and ceiling unit just caused the wall unit to reset. Now the relay is the same as putting the visor remote.



  • Epilog X Contest

    Epilog X Contest
  • PCB Contest

    PCB Contest
  • Warm and Fuzzy Contest

    Warm and Fuzzy Contest

15 Discussions


1 year ago

I got it update the mag switch and motion PIR in real time without refreshing the page. Using a php send file with a section in the index.php that receives the change when it occurs.

Also got the pir working with ifttt and pushover to get a unique sound when someone comes to the front door of the house. Cannot hear doorbell in basement.

I am not to going to update these instructions as this site is terrible for code.

See GitHub.


1 year ago

Ok after researching for a while, I found that I was able to fix my problem. I needed to wrap the Auth comments with a directory tag. See below:

<Directory "/var/www/html">

AuthType Basic

AuthName "Restricted Content"

AuthUserFile /etc/apache2/.htpasswd

Require valid-user


1 reply

Reply 1 year ago

For some reason I was using <Directory "/var/www">

Instructables will not show the stuff with <xxxxxxxx>


1 year ago

My SDcard went read only again.

I made a bunch of changes and reboot, they were all gone.

Just trying to copy and paste needed scripts as a back just in case my instructions do not work


1 year ago

When I attempt this step of adding the password protection, Apache 2 service refuses to restart. It gives me a message that: AuthType not allowed here. Action 'configtest' failed.


1 year ago

I followed the instrucion to the letter, but the last part is failing for me.

pi@raspberrypi /mmal $ ./motion -c motion-mmalcam-both.conf

./motion: error while loading shared libraries: libavformat.so.56: cannot open shared object file: No such file or directory

Anyone who can help me?

Kyle D

2 years ago

I got everything showing up internal and external. However, after I completed step 1 it would control the garage door. Now that I have gone through this part of the instructable to add video the button to control the garage door no longer works. Any ideas on how to fix this?

Not sure if it is in the step 6 part. The parts that say /user/local/... are not showing up green like in the picture on the step. Only the word local is green.


3 years ago

I can only get it to work local. With port forwarding only the button shows up.

3 replies

Reply 3 years ago

The poster did not give completely correct instructions to make it work correctly from outside your network, even if you completed the port forward properly on the Pimylife page. It should work from inside your network fine though.

The reason is because you cannot use a private IP address in the index.php file where it says <img src="http://192.168.x.x:8081"> it should say <img src="http://a.b.c.d:48461"> where a.b.c.d is your WAN IP or DDNS hostname. To find your WAN IP and have it working immediately, go to www.whatismyip.com. If you have a dynamic WAN IP with your ISP, at times this will change, but there is a workaround for this. You can either buy a static IP address from your ISP, or go to freedns.afraid.org and set up a free Dynamic DNS account, and download a freedns.afraid.org updater client so when your ip changes, it will automatically update on the site. You still need to log into your freedns account every 3-6 months or so, so it does not get automatically inactivated (or pay for their service).


Reply 3 years ago

Thanks for the info. I did what you said and it now it works while on my cell phone on LTE.

I have port forwarding for port 80 the status, control, video page.

I have port forwarding for port 8081 the mmal-motion stream.


<img style="-webkit-user-select: none" src="http://acbsdsds.ddns.net:xxxxx/" width="980" height="735">

<div class='awrap'>

<a href='/?trigger=1'></a>



Now video doesn't work when in my local network. How could I recode to figure out I am on wifi at home?


Reply 3 years ago

Now this is a much more router specific question. Sometimes, when a PC is requesting a URL/resource that is external to your network, but actually points back to a device that is internal to your network, your router does not like to forward those requests properly. What you need is a 'reflective' (also sometimes called 'reflexive') rule in your firewall/router to allow those internal->external->internal requests to be processed.

Basically when you port forwarded those ports to an internal address, you also need a rule to allow internal addresses to do their NAT before and then again after it comes back from the WAN. It could be called a number of things on your router, Reflective NAT, Virtual Host, Virtual server, Static NAT, etc.

If you want to PM me your router/firewall make and model, I can research it and help you through it.

Here is another explanation I found online:

The most common problem is that your gateway rewrites the destination
address of the packet to the internal server, but not the source. So,
when the internal server responds it sees that the packet came from
something on the local network, sends back the packet directly - and the
client can't tell this is from the server, because the packet still has
the internal, not the public, address on it.

The standard fix is to force the traffic to come back through your
gateway. One way to achieve that is to put the server that the public
address redirects to in a "DMZ", so that traffic between the client and
the server has to pass through the router.

The other way is to also apply NAT to the source address of internal
connections to the external IP, so that they look like they come from
the gateway. The internal server will then respond to the gateway, that
will undo both NAT changes, and send the packet back to the internal client.


3 years ago on Introduction

Thanks for this instructable, finally got around to doing it. I had to change a few things because your code for "index.php" had a file called "output.php" underneath it, which would give an error when I tried to download it (not sure what it is) but then I just read off of your screenshot to make my page.

I set up an openvpn server on my ac66u router (running merlin) and can connect to that when I want to check my garage door remotely, just a quick tap on the vpn connect icon and then I can use the normal webpage shortcut on my phone, I get video feed and functionality with this method and it is more secure than opening ports.

Now what I want to do is somehow setup android notifications for when the door opens and closes as well, this way, if I inadvertently hit the button I will at least get notified of it.

I have a veralite home automation server setup (and hue/lux lights, wemo switches, nest, dropcam, etc) so I have managed to setup some handy shortcuts by using an app called authomationhd to create one tap garage door operation from my phone's home screen, this method does not require any port forwarding or vpn as it uses the vera backend. I made sure to put the button in a folder on my homescreen for now so that I do not accidentally push it (until I get notifications working at least).

2 replies

Reply 3 years ago

Sorry about the file that won't download.

I'll edit the instructable with current code for the index.php.


3 years ago how would get the video without the IP address and port 8081? Even with port forwarding I cannot get video to load outside my local network.