Garage door opener – tiny computer project

Recently we had two new garage doors installed, to replace very old clunky things. The new doors are sectional, insulated and have brand new automatic openers. They are draft free and the temperature in the workshop is a few degrees higher, and the noise level is a lot lower.

With the doors came 4 key ring wireless remotes, and two wireless wall mounted remotes, one of which is mounted between the two doors, and the other is mounted by the connecting door between the garage and the main part of the house. When we bought the doors we considered getting either an external keypad entry, or a wifi base system that used an app on a mobile phone, but the extra cost put us off a bit. Under the covers of the door openers is a connector block, and a connection labelled OSC or O/S/C can be used to connect a momentary switch to control the doors manually, but this is not used in the installation, all controls being wirelessly attached.

Since then I have started to get irritated always having to carry one of the wireless remotes, they are small, but still a nuisance on a key ring, and I am always misplacing or forgetting to carry them. In  particular when I am working in the garden, when I want to use the garage without walking through the house in dirty boots. So I am revisiting the idea of a wifi based door opener, using my phone as the remote. Thing is, I am a user of Arduino, and Raspberry PIs, and have a couple of each lying around, and was pretty certain I could do something myself, and learn something about IoT (internet of things) at the same time.

The obvious place to start is on the internet, and searches found quite a few projects out there, each with their own good ideas and quirkiness. The most interesting of which seemed to be based on relatively new technology called the Onion Omega 2. This is an IoT specialist product. Barely larger than a large postage stamp, this device is reasonably capable Linux based computer (preloaded with linux), with built in wifi, GPIO pins and a bunch of other cool stuff, including very low power consumption. The main board requires an expansion board to be really useful (i.e. to give it a USB port, power supply switches, leds etc),  so the $7-$9 price goes up by another $15, and to control the doors I figured out I would require a relay board, which is available for another $15, and comes with two relays (one for each door).  This relay would be attached to the door openers, at the OSC and zero volts connectors.

So on the promise that I can program in PHP, not just Python or similar, I ordered the necessary bits, and in two or three weeks the courier will knock and I will take possession!

Until then, I thought I would prototype it with a Raspberry PI. Not quite as small, a bit more power hungry, but still a Linux based computer, with USB, power supplies, GPIO pins etc. No wifi built in on my version, but wifi dongles are fairly cheap. I have a spare Raspberry Pi 2 Model B in my box of bits, with a 4GB SD card loaded with Rasbian. Time to get started.

It booted up fine, but previous incarnations of this computer were still on it, namely a display of business related photos that started automatically at start up. I could not for the life of me remember how I did this, as it was at least 4 years ago, so I decided it was time to upgrade my version of Rasbian. A quick visit to the Rasbian download page and I was downloading the latest version, and using the suggested image burner to update my SD card. Except – my 4GB SD guard was no longer big enough! Linux bloat is even affecting tiny computers. Back to the download page and there sitting next to it was a “Lite” version, which does not have the desktop software included. As this computer is to run headless (no monitor, keyboard or mouse) I decided to bite the bullet and go for this version – it will save me the cost of a new Sd card! Downloaded, installed and plugged into the Pi, it was time to start the setup.

To get through the initial steps, I plugged in a mouse and keyboard, and an HDMI monitor. A number of set up changes and software installs are required, including.

  • Change the password to log on to the machine. Use sudo raspi-config.
  • Setting up the date and time zone, and allowing the whole of the SD card to be used as system memory. Use sudo raspi-config.
  • Allowing another computer to SSH into the computer. Use raspi-config again, as described here
  • Installing and enabling an FTP server, Proftpd was the one I used, description here.
  • Installing Apache and PHP, this page is useful
  • Installing the Motion software for the webcam, and allowing the webcam to be used on the local network. Useful webpage here and use sudo nano /etc/motion/motion.conf to change the camera setup as required.
  • Install the software for getting access to the GPIO pins. As I am now using a Raspberry Pi Relay Board, which uses I²C instead of GPIO pins, this has become a bit redundant, but details are here.
  • Some extra work around the SMBUS and I2C tools is required for the Relay hat, this page here will help.
  • Use  sudo visudo to modify  /etc/sudoers.  An extra line is appended to the end of the file, namely www-data ALL=(ALL) NOPASSWD:ALL. Beware - danger!
  • Installing and setting up the wifi dongle (an Edimax Nano type). Instructions here.
  • Setting up a fixed IP address for wlan0 while you are installing the wifi dongle, especially if your router won’t let you reserve IP addresses.
  • Set up a cron job to remove the days image files, useful page here. 
  • Or, instead of a cron job, update the /etc/motion/motion.conf values to switch off recording the images.

There were myriad other little things I had to do, which I didn’t write down or remember. Once I had an IP address for the PI on the local network 192.168.1.73, I was able to use PuTTY to login to the new machine, and set up an FTP client to look at /var/www – I needed to change the permissions on this directory to get full access to to it. This meant I could remove the HDMI cable, mouse and keyboard, and the ethernet cable.

All this took a few hours of trial and error and finding out what I needed and how to install it, set it up and use it on the web.

At some point I created a “Hello World” PHP web page, and used a browser to look at the new IPaddress 192.168.1.73, which gave me the standard Apache opening page – index.html. As I wanted to get to index.php I wondered if I had to set up the Apache to look at this page, but I just had to remove the index.html page  from /var/www/html and the .php page was picked up and processed properly.

I added a form with a couple of Submit buttons and played with formats and actions, finally connecting the GPIO pins 17 and 27 to LEDs, and was able to turn LEDs on and off using the webpage on my phone or desktop. As the relays are going to simulate a momentary press, the buttons flashed the LEDs for just a second. I don’t have any relays currently, so this is as far as I can go down this path. I have orderd a Raspberry PI hat with 4 relays on from Nicegear, a NZ supplier, and this should arrive in a day or two and I can go on to the next phase of testing.

However, I don’t want to open and close the doors without making sure there is nothing in the way of the doors. If I am standing outside them with my phone, this is not an issue. If I am sitting upstairs wondering if the doors are shut or not it would be nice to know if they are open, and if the car or bike is parked halfway in! There are many sensors you can add  with safety beams, magnetic sensors etc, but I decided to add a webcam inside the garage allowing me to see the state of both doors.

When I downloaded and installed the Motion software to manage the web cam, it set up the camera on port 8081, and I made a couple of other changes to optimise for the rather slow R-PI and the rather rubbish web cam I found lying on my bits box. It turns out putting the camera onto the web page is as simple as

<img src= "http://192.168.1.73:8081/?action=stream" width="320" height="240"/>

The R-Pi is operating as a web server, serving up a simple web page direct to the  local IP address. It cannot be accessed outside the local network. The PHP program receives the input from a few buttons on the web page, and changes the state of a couple of GPIO pins, which flicks relays on and off, replicating a button press on the garage door opener.

By default the motion software records images when motion is detected, and records a video  until motion stops. This can quickly fill up an SD card. Use the following settings in the /etc/motion/motion.conf to get it all working properly. Framerates and image size may depend on your camera.

output_pictures off (default on)
ffmpeg_output_movies off (default on)
stream_localhost off
daemon on
stream_maxrate 10
framerate 10
weight 320
height 240

As the garage is dark most of the time, I may add a light which will be controlled by one of the spare relays on the PI Hat in order to see what is happening with the video camera.

OK, the relay hat for the PI has arrive, and I spent the first few hours staring at it wondering what I had bought! This is what I bought, a Seeed Studios Raspberry Pi Relay Board V1.0. Looks great, but it took me by surprise. I figured it would work by just setting GPIO pins on and off, on the basis it is plugged into the GPIO pins on the R-Pi. Instead, it uses I²C to connect from chip to chip, and the only pins it uses are the power and ground, and the two oins used for I²C. What this means is that there us a serious lack of information on how to use it. There is a WIKI entry on the page, which is a variation on this original page by John M. Wargo. The information here is not for the novice, and all the code is in Python, which I have little knowledge or experience of. Hours of fruitless searching resulted in no further ideas in how to use it in my PHP pages. I did however look at using Python Scripts in PHP scripts, and it didn’t look too hard.

So, I worked my way through Mr. Wargo’s page, installing and testing the card using a terminal to run the test scripts to test it all worked. I had  couple of things to install/change around the use of SMBus, this page here helped a lot. Eventually satisfying clicks were heard from the relays when the scripts were run from the terminal session.

The next job was to get scripts working in my PHP page. The concept is quite simple, use “exec (python /path/to/scriptname.py)”. In reality it is a little trickier. While the Python scripts run without root permissions in the terminal, to run them from php we need to use “exec (sudo python /path/to/scriptname.py)” . This throws up the issue of Sudo requiring a password to run. There is much about this, and the most common solution comes with dire warnings about using it.

It involves The reason this is so dangerous it that it means the apache user www-data has root permissions without requiring a password, which open up the Pi to all sorts of hacking. If this is important to you,don’t do it. If you do do, use visudo to edit the /etc/sudoers file, as it validates what you are changing. Getting back from a corrupted sudoers file is a pain.

Eventually satisfying clicking sounds were heard from the relays while running my test PHP script.  Two new python scripts were written, each hardcoded to switch a specific relay on then off again 0.2 seconds later, simulating a momentary switch. The python scripts look like this and are remarkable simple, making use of the library of functions provided on Mr. Wargo’s page (relay_lib_seeed.py).

#!/usr/bin/python
'''*****************************************************************************************************************
 Switch relay 1 on, wait 0.2 seconds, and switch it off again
 By  Dave Glover
www.signsofsuccess.co.nz
********************************************************************************************************************'''
from __future__ import print_function

import sys
import time

from relay_lib_seeed import *

relay_on(1)
time.sleep(.2)
relay_off(1)

 

The full source for the single piece of PHP code is given below. The red lines are the two critcal ones for calling the python from within PHP. Security is based on this only being available on my home networks, so if you don’t have access to my networks, you can’t see the page.

<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Garage Door Control</title>
<style type="text/css">
body {background-color:lightblue;}
h1 {text-align: center; color:blue;}
img {width:100%; height:auto; display: block; margin: 0 auto;}
.inputform {width:100%; margin:auto; }
.buttonline {width:300px; margin:auto;}
.button {height:25px; width:100px; margin:10px 23px 10px 23px; 
 background-color:blue; color:white;}
.notification {margin:auto; text-align:center; color:blue; margin:20px;}
.footer {
width: 100%;
bottom: 10px;
text-align: center;
font-size: 8pt;
}
</style>
</head>

<body>
<div class="inputform">
<h1>Door Controls</h1>
<form method="get" action="index.php">

<div class="buttonline">
 <input class="button" type="submit" value="North" name="n">
 <input class="button" type="submit" value="South" name="s">

</div>
<div class="buttonline">
 <input class="button" type="submit" value="Video On" name="v">
 <input class="button" type="submit" value="Video Off" name="vo">

</div>
</form>

</div>
<?php



if(isset($_GET['n'])){
 exec('sudo /usr/bin/python /var/www/pirelay/relay_flick_1.py');
 echo '<div class="notification">North door activated</div>';
 }
else if(isset($_GET['s'])){
 exec('sudo /usr/bin/python /var/www/pirelay/relay_flick_2.py');
 echo '<div class="notification">South door activated</div>';
 }

if(isset($_GET['vo'])){
 echo '<div class="notification">Waiting for input</div>';
}
else {
 echo '<div class="notification">Live video stream</div>';
 echo '<img src= "http://192.168.1.73:8081/?action=stream"/>';
}
?>
<div id="myfooter" class="footer">
 Copyright Dave Glover (2017<?php if(date('Y') != 2017){echo ' - '.date('Y');}?>)
</div>
</body>
</html>

This is what it looks like

Screen Shot

and it is now installed and waiting for the upgrade to the Onion Omega 2 version!

I made changes to the physical installation, hanging the webcam from a ceiling bracket, so inverting the picture. Back into the camera config using sudo nano /etc/motion/motion.conf and change the Rotate value to 180, and it all looks good again.

Postscript 19/10/2017 – This webcam and garage door opener ran fine for a few days, but started to act badly after a week or so, not responding, SSH stopped working etc. Eventually while investigating what was wrong I got an error message about there being no space on the disk. I an using a 4GB SD card for my system, and it seems that the camera software “Motion” takes still photos and creates videos of everything it sees! This quickly mounts up and eventually the 39,000 images stored on the SD card swamped it! The files live in /var/lib/motion, and using “sudo rm *” is theoretically the way to get rid of them all (after CD /var/lib/motion of course). 39,000 is apparently too many parameters for the command, so I had to break it down into chunks by usin 0*, 1* up to 9* to delete 1/10 of the files at a time. To get round the problem I have added a cron job to remove the files once a day at midnight using “rm /var/lib/motion/*”.

Posted in Computer Stuff, Raspberry Pi

Leave a Reply

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

*