piGarage – Redux

2 09 2014

Garage_door_sliding_upHey! Another post in so little time! This time about visually checking the status (opened/closed) and controlling the garage door directly with any smartphone connected to a Raspberry Pi through the personal wifi network.

I have seen a number of posts and articles recently about building one’s own pi-controlled garage door (as an example see issue #22 of the excellent MagPi or the corresponding tutorial on Adafruit), but none of them had all the features I was looking for. The basic idea is to be able to check the status of the garage from any location (imagine you’re at work and suddenly you realize that you may have missed to close the door) and if required to be able to remotely open or close it. And to ease the process, we want the controlling device to operate wifi and to show a little bit of information when plugged in. So we want something that would be autonomous, nicely packed, wifi-enabled, with a camera and a web interface, and a few controls and pieces of information directly on the device. And of course, we want to use this as an opportunity to discover GPIO’s, I2C and more…!

Here’s what I came up with as a first prototype. Here again this project leverages code snippets and tutorials found on the web so I have tried to name the authors as much as possible and to provide all references. If someone recognizes his code, let me know and I will certainly update this post. This is how the current version looks like:


Parts list

  • 1 x Raspberry Pi model B or B+ from your preferred reseller. Both will work, using the B+ version will give you additional pins but you will see later in this article that with a little bit of soldering, the regular B version is running great.
  • 1 x Rapsberry Pi camera module, either normal or infrared
  • 1 x 5V Relay interface board which you can find at amazon.com
  • 1 x wifi USB adapter, I would recommend the TP-LINK TL-WN725N, works great with the Pi
  • 1 x Adafruit RGB Negative 16×2 LCD+Keypad Kit for Raspberry Pi, neat for displaying the information we want to have at boot time (note that you will need soldering skills here in order to assemble the parts of the kit)
  • … and of course a bunch of jumper wires (female to female and female to male) and a set of M3 spacers ans screws, which you can find at any electronic reseller.
  • in addition, you may want to have an HDMI cable and a keyboard for fresh install / debugging parts as well as a plastic or acrylic box to sotore and hold all parts together. I found mine at the local mall so you should have no issue on this. make sure that the box is thick enough to sustain screws yet thin enough to be easily cut.

Initial setup of the Pi

Install the latest Debian for the Pi available at http://www.raspberrypi.org/downloads/. Once the install has completed, configure Internationalization options and enable the camera and SSH for further installation. As an option, change the password to something faster for the installaiton as you’ll have a few reboots to go with. Next, make sure you have the latest packages with

$ sudo apt-get update; sudo apt-get upgrade

Here we go, first reboot to start with a clean environment

$ sudo reboot

Now we are going to install the required components for the Wifi dongle. Mendrugox has an excellent, detailed tutorial on getting the TP-LINK TL-WN725N v2 to work on Raspberry Pi (Raspbian):     http://www.mendrugox.net/2013/08/tp-link-tl-wn725n-v2-working-on-raspberry-raspbian/. Here we are just going to show the main steps (make sure you have the dongle inserted in one of the USB ports before proceeding):

$ uname -a

You should get something like

$ Linux raspberrypi 3.12.22+ #691 PREEMPT Wed Jun 18 18:29:58 BST 2014 armv6l GNU/Linux.

So we’re looking for the Drivers for TL-WN725N V2 – 3.6.11+ -> 3.12.26+ which are conveniently available at http://www.raspberrypi.org/forums/viewtopic.php?p=462982#p462982

$ wget https://dl.dropboxusercontent.com/u/80256631/8188eu-20140616.tar.gz

Extract the file from this gunzipped archive and install the module:

$ sudo install -p -m 644 8188eu.ko /lib/modules/$(uname -r)/kernel/drivers/net/wireless  
$ sudo insmod /lib/modules/$(uname -r)/kernel/drivers/net/wireless/8188eu.ko  
$ sudo depmod -a

Next, configure the network interfaces. A detailed page at http://www.fars-robotics.net/ provides all the information required to properly configure the Wifi according to your own needs and network setup.

$ sudo vi /etc/network/interfaces

Edit the file and reboot to get the changes applied (ok, you could also run a sudo ifdown and sudo ifup but I got lazy recently and a small reboot does’nt hurt, does it?)

$ sudo reboot

Just to make sure: after the reboot simply run ifconfig and confirm that everything is fine, i.e. that you can get your IP address on wlan0 interface; if not, check alls steps above.


Log back in, start installing the required components for the web part:

$ sudo apt-get install lighttpd php5-cgi php5-gd 
$ sudo lighty-enable-mod fastcgi 
$ sudo lighty-enable-mod fastcgi-php 
$ echo '<?php phpinfo(); ?>' | sudo tee /var/www/phpinfo.php

Check this is fine by browsing to http://your.raspberry.ip/phpinfo.php.

Setting up the camera to take still pictures each 5 seconds

First you have to make sure to enable the camera by using the raspi-config utility.


If you are unsure on how to do this, check out http://www.raspberrypi.org/documentation/configuration/raspi-config.md.

After this is done, we are going to setup the camera for taking a picture every 5 seconds and save it on a RAM disk, and this is going to be installed as a daemon. This part is directly reused from the excellent article from ED at http://dronkert.net/rpi/webcam.html. I cannot recommend more that you would read his post very carefully to understand all the steps as I will simply quote a sub-part of it.

$ cd ~ 
$ nohup raspistill -n -e jpg -w 800 -h 600 -q 70 -t 2147483647 -tl 5000 -o /dev/shm/webcam.jpg & 
$ wget http://dronkert.net/rpi/webcam-service-1.0.1.txt 
$ sudo cp webcam-service-1.0.1.txt /etc/init.d/webcam 
$ sudo chmod 755 /etc/init.d/webcam
$ sudo update-rc.d webcam defaults 99 01

To disable the camera light, for stealth operation, edit config.txt:

$ sudo vi /etc/config.txt

and add on a new line disable_camera_led = 1.

A few more steps and you will be able to see the picture on the web server.

Installation of wiringPi

Wiring Pi is a library which makes it a lot easier to use the Raspberry Pi GPIOs. Check that you have the latest version of git:

$ cd ~ 
$ sudo apt-get install git-core

and install wiringPi

$ git clone git://git.drogon.net/wiringPi 
$ cd wiringPi/
$ git pull origin
$ ./build

Only part left is to install the .php files.

Code part 1 – the web site to be hosted on the Pi

This is the code to control the image & the buttons. Again, (very) largely inspired by Ewoud Dronkert’s (http://dronkert.net) code as well as the one from TheFreeElectron at http://www.instructables.com/id/Simple-and-intuitive-web-interface-for-your-Raspbe/?ALLSTEPS. Just made it simpler for the intended purpose, somewhat merged both codes and fixed a couple of issues when doing so. Authors of both articles are to be praised for the quality of their tutorials.

The result is composed of the following files:

  • /var/www/index.php: the main file for the site, includes scripts for both starting to shoot the pictures and the GPIO code,
  • /var/www/webcam.php: the page that will check if a file is available, then timestamp it and return to the main file for update,
  • /var/www/script.js: the javascript used to change the state of the buttons and thus of the GPIO pins of the Raspberry,
  • /var/www/gpio.php: used by the javaScript, updates the pins’ statuses.,
  • of course the “on” and “off” image file – also to be placed in /var/www.

Get all the code on GitHub at https://github.com/Narno38/piGarage and make sure that the adequate rights and owners are set:

$ sudo chown -R www-data:www-data /var/www

With all this, we are ready to test the camera and the relay card, we just need some cabling.

Assembly and wiring part 1 – GPIO and camera

First thing we want to wire is the 5V Relay board. As I wanted to save all the Pi GPIO pins and thanks to ozzmaker.com I learned that as of revision 2 boards there is space to add some header pins to get access to eight more GPIO pins. These are found on the Header 5 with the following functions: (image courtesy of ozzmaker.com)


  • P5-Pin1 – 5v0
  • P5-Pin2 – 3.3v
  • P5-Pin3 – GPIO28
  • P5-Pin4 – GPIO30
  • P5-Pin5 – GPIO29
  • P5-Pin6 – GPIO31
  • P5-Pin7 – GND
  • P5-Pin8 – GND

In this setup, I have used pins 1, 3, 5 and 7 and wired them directly to the GND, VCC (5v0), IN1 (GPIO28) and IN2 (GPIO29) pins of the relay board after a little bit of soldering onto the Pi. If you don’t feel like doing so, you can use any of the regular GPIO pins of the Pi as the display will be using I2C. The additional pins above are useful only in case you would want to use a different display or wire more parts. You will simply have to modify thre references to the GPIO pins in files index.php, gpio.php and script.js.

Notice that GPIO28, 29, 30 and 31 correspond to wiringPi pins 17, 18, 19 and 20 (see http://wiringpi.com/pins/special-pin-functions/).

At this stage you should be able to boot your Rapsberry Pi, to browse to http://your.raspberry.ip/index.php and to see the interface with the pictures taken every 5 seconds and the 2 buttons for switching on and off the relays.

Assembly and wiring part 2 – RGB LCD Display

Now that we have the main features up and running, we want a minimal interface on the front. Why is that? As an example, we want to be able to display the IP address that the Pi was given when the network is set to dhcp. Of course if you have networking skills, you can always ensure that the MAC addresses for the LAN and wLAN interfaces are static in your dhcp domain, but where’s the fun, eh?

The Adafruit RGB Negative 16×2 LCD+Keypad Kit for Raspberry Pi comes in handy. The good news about this sweet little piece of hardware is that Adafruit had the excellent idea to add a plate that makes the display i2c compatible, so that 2 pins (besides 5V and GND) are required. Let’s wire up!

  • LCD ground (GND) goes to pin 6 on the Raspberry Pi
  • 5V goes to pin 2
  • SDA goes to pin 3
  • SCL goes to pin 5

Below is an approximate schematics of what we want to have. the LCD is shown with a Pi Cobbler as the exact product does not exist (yet?) in Fritzing.


Now we are going to install the necessary packages in order to use the LCD. An excellent tutorial is available at https://learn.adafruit.com/adafruit-16×2-character-lcd-plus-keypad-for-raspberry-pi, we will simply list below the main steps.

As explained on Adafruit’s tutorial, you may feel better by knowing that the backlights of the display will not turn on until you have the code running!

Start by setting up your Pi for I2C:

$ sudo vi /etc/modules

and add 2 new lines with i2c-bcm2708 and i2c-dev to the end of the file. Then save and reboot to enable the hardware I2C driver.

Before you can get started with I2C on the Pi, you’ll need to run through a couple quick steps from the console.  Just enter the following commands to add SMBus support (which includes I2C) to Python:

$ sudo apt-get install python-smbus
$ sudo apt-get install i2c-tools

i2c-tools isn’t strictly required, but it’s a useful package since you can use it to scan for any I2C or SMBus devices connected to your board.  If you know something is connected, but you don’t know it’s 7-bit I2C address, this library has a great little tool to help you find it:

$ sudo i2cdetect -y 1

This will search /dev/i2c-0 or /dev/i2c-1 for all address, and if an Adafruit LCD Plate is connected, it should show up at 0x20. Once both of these packages have been installed, you have everything you need to get started accessing I2C and SMBus devices in Python.

The LCD Pi Plate Python code for Pi is available on Github at: https://github.com/adafruit/Adafruit_Python_CharLCD and we will use most of it and adapt to our needs.

$ sudo apt-get install build-essential python-dev python-smbus python-pip git
$ sudo pip install RPi.GPIO
$ git clone https://github.com/adafruit/Adafruit_Python_CharLCD.git
$ cd Adafruit_Python_CharLCD/
$ sudo python setup.py install

Retrieve the part of the code to display the IP’s from https://github.com/Narno38/piGarage.

As you will read from the code, you can also use the right button to simply switch off the screen. Probably this should be improved by putting a delay after which the screen would switch off…:)

Last thing to do, we need to make sure that the Python code runs at start.

$ cd /etc/init.d
$ sudo vi char_lcd_plate_ipaddress_v2

Paste the following:

# /etc/init.d/test
# Provides:          IP display on LCD
# Required-Start:    $remote_fs $syslog
# Required-Stop:     $remote_fs $syslog
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Simple script to auto start lan0 and wlan0 IP's on LCD
# Description:       Simple script to auto start lan0 and wlan0 IP's on LCD
 case "$1" in
    sleep 10
    sudo python /home/pi/char_lcd_plate_ipaddress_v2.py &
    killall python
    exit 1
exit 0

Make it executable

$ chmod +x char_lcd_plate_ipaddress_v2

and make sure it starts at boot

$ sudo insserv char_lcd_plate_ipaddress_v2

Try it out without restarting

$ sudo /etc/init.d/char_lcd_plate_ipaddress_v2 start

After you have observed it starting, we should be safe to reboot

sudo reboot

and everything should be fine then! If not, don’t hesitate to drop me a note.

One last word of caution: from http://wiringpi.com/pins/special-pin-functions/, “The Raspberry Pi is a 3.3 volt device! Attempting to directly connect to any 5V logic system will very likely result in tears…”

On my setup, I have seen from time to time the Pi refusing to boot and I had to disconnect / reconnect the relay. This is really the only issue I had. I guess the best thing would be to have a 3.3V solid state relay but I haven’t found one yet. Or maybe to add another piece of electronic to power up the GPIO voltage prior to going to the relay. If anyone has a simple way to do it, let me know. the idea here was really to test a number of things together.




3 responses

11 09 2014

Il you’re at work and you forgot to close your door, then what you need is not a Pi but a brain 😉

Thanks for sharing your thoughts Arnaud !

11 09 2014

BTW I think the call to sude in your init script is useless :
sudo python /home/pi/char_lcd_plate_ipaddress_v2.py &
python /home/pi/char_lcd_plate_ipaddress_v2.py &

At that point you’re root anyway.

11 09 2014

Thank you Bruno. A young Padawan in Linux still I am 🙂

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: