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.

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.<br><br>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.<br><br>I am not to going to update these instructions as this site is terrible for code.<br><br>See GitHub.<br>https://github.com/paulpoco/Raspi2GarageOpenerVideo<br>
<p>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:</p><p>&lt;Directory &quot;/var/www/html&quot;&gt;</p><p>AuthType Basic</p><p>AuthName &quot;Restricted Content&quot;</p><p>AuthUserFile /etc/apache2/.htpasswd</p><p>Require valid-user</p><p>&lt;/Directory&gt;</p>
<p>For some reason I was using &lt;Directory &quot;/var/www&quot;&gt;</p><p>Instructables will not show the stuff with &lt;xxxxxxxx&gt;</p>
<p>My SDcard went read only again.</p><p>I made a bunch of changes and reboot, they were all gone.</p><p>Just trying to copy and paste needed scripts as a back just in case my instructions do not work</p>
<p>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.</p>
<p>I followed the instrucion to the letter, but the last part is failing for me.</p><p>pi@raspberrypi /mmal $ ./motion -c motion-mmalcam-both.conf</p><p>./motion: error while loading shared libraries: libavformat.so.56: cannot open shared object file: No such file or directory</p><p>Anyone who can help me?</p>
<p>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?</p><p>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.</p>
I can only get it to work local. With port forwarding only the button shows up.
<p>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.</p><p>The reason is because you cannot use a private IP address in the index.php file where it says &lt;img src=&quot;http://192.168.x.x:8081&quot;&gt; it should say &lt;img src=&quot;http://a.b.c.d:48461&quot;&gt; 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). </p>
<p>Thanks for the info. I did what you said and it now it works while on my cell phone on LTE. </p><p>I have port forwarding for port 80 the status, control, video page. </p><p>I have port forwarding for port 8081 the mmal-motion stream.</p><p>&lt;body&gt;</p><p> &lt;img style=&quot;-webkit-user-select: none&quot; src=&quot;http://acbsdsds.ddns.net:xxxxx/&quot; width=&quot;980&quot; height=&quot;735&quot;&gt;</p><p>&lt;div class='awrap'&gt;</p><p> &lt;a href='/?trigger=1'&gt;&lt;/a&gt;</p><p>&lt;/div&gt;</p><p> &lt;/body&gt;</p><p>Now video doesn't work when in my local network. How could I recode to figure out I am on wifi at home?</p>
<p>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-&gt;external-&gt;internal requests to be processed. </p><p>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. </p><p>If you want to PM me your router/firewall make and model, I can research it and help you through it.</p><p>Here is another explanation I found online: </p><p>The most common problem is that your gateway rewrites the destination<br> address of the packet to the internal server, but not the source. So, <br>when the internal server responds it sees that the packet came from <br>something on the local network, sends back the packet directly - and the<br> client can't tell this is from the server, because the packet still has<br> the internal, not the public, address on it.</p><p>The standard fix is to force the traffic to come back through your <br>gateway. One way to achieve that is to put the server that the public <br>address redirects to in a &quot;DMZ&quot;, so that traffic between the client and <br>the server has to pass through the router.</p><p>The other way is to also apply NAT to the source address of internal <br>connections to the external IP, so that they look like they come from <br>the gateway. The internal server will then respond to the gateway, that<br> will undo <em>both</em> NAT changes, and send the packet back to the internal client.</p>
<p>Thanks for this instructable, finally got around to doing it. I had to change a few things because your code for &quot;index.php&quot; had a file called &quot;output.php&quot; 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. <br><br>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. <br><br>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. <br><br>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).</p>
<p><a href="http://www.bc-robotics.com/bcrforums/topic/magnetic-door-switch-raspicam-to-pushbullet/">http://www.bc-robotics.com/bcrforums/topic/magneti...</a></p><p>For Pushbullet for Python. I did close too but too many false edge detects.</p>
<p>Sorry about the file that won't download.</p><p>I'll edit the instructable with current code for the index.php.</p>
<p>Here are some pics, night here now so there is nothing to see on the video (but it is working!) I also used the magnetic door sensor.</p>
<p>I would also like to make the page automatically refresh every so often or find a more elegant way to update the status at the top of the video feed (door open, door closed). Some way to activate the trigger, wait X seconds, then update just that line of the page. I have no clue what I am doing with that sort of thing though.</p> 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.

About This Instructable




More by pber:Raspberry Pi Garage Door Opener With Streaming Video of Door Status. 
Add instructable to: