At some point, most everyone forgets to close their garage door when leaving for work, or wonders if they closed it before leaving for vacation.  This series of posts will cover how to easily build a system that will notify you by email, tweet, or SMS if your garage door has been left open.

Garage door SMSing

Garage Door Tweeting

What you’ll need:

At a minimum, you’ll need the following to get started:

  • Raspberry Pi model B ($35).  This is a credit card sized computer that will power the system.  Try not to spend much more than the official price – $35.  Shop at Element14 if you’re seeing higher prices.  There is also the Raspberry Pi model A, which is $10 cheaper.  It will also work, but is slightly harder to configure and does not support Ethernet.
  • 4GB or larger SD card ($6) to store the RPi’s files.
  • HDMI to HDMI -or- HDMI to DVI cable.  Initially the RPi will be connected to a keyboard, mouse, and monitor to be configured.
  • Computer monitor with HDMI input or DVI input.  This will only be needed for the initial configuration steps.
  • USB keyboard and mouse.  Again, only needed for the initial configuration steps.
  • USB hub.  Many monitors have one built in.  If not, borrow/get a powered USB hub (it should draw power from an AC outlet).  There is a list of hubs known to work with RPi on the web.  It’ll only be needed for the initial configuration.
  • Ethernet cable (if using a wired connection) -or- USB wifi adapter ($11).  The RPi will need Ethernet or Wifi connectivity to send emails/tweets/SMS.  Unless your garage is wired for Ethernet, you probably want Wifi.  I used the tiny Edimax EW-7811UN for my build and it worked well.  Newer models are now available.
  • USB power supply ($6).  The RPi draws a lot of current, so stay away from cheap ones.  If your RPi reboots or crashes a lot, your USB power supply may not be sourcing enough current.  Adafruit sells a power supply that works well.
  • USB – micro USB cable ($3).  This will connect the power supply to the RPi.  You likely already have one of these from a cell phone or other USB device.  If not, they only cost a few dollars on Amazon.com.
  • Magnetic garage door sensor ($10).  You’ll need one per garage door.  This should come with both a magnet to mount on the garage door and a sensor to mount on the door frame. Unlike window sensors, garage door sensors typically can sense the magnet from 2″ or more away. The Seco-Larm SM-226L works well.

    Magnetic sensor mounted to wooden header at top of garage door

    Magnetic sensor mounted to wooden header at top of garage door

Recommended, but not required:

  • Raspberry Pi enclosure ($10-$15).  There are many enclosures on the market that are designed specifically for RPi.  Make sure the enclosure you buy has an opening for the GPIO pins otherwise you’ll have to modify the enclosure.  Alternately, you can purchase an enclosure like the Velleman 135 x 75 x 50mm ABS Project Box and drill only the openings you need.  This is a little more work but will provide more protection for your RPi since it won’t have any unnecessary ports exposed.
    RPi in Plastic Case http://www.amazon.com/gp/product/B008TDBIDI/ref=wms_ohs_product?ie=UTF8&psc=1

    RPi in Plastic Case.  Note the GPIO opening on the right side.

    RPi in ABS Project Box

    RPi in ABS Project Box

  • Jumper wires with one female end ($5-$10).  Only two connections are required per garage door, so jumper wires work well here.
  • 2 conductor wire.  If you’re not able to position your RPi next to the garage door, you’ll need to run a wire from the magnetic sensor to your RPi.  It’s low voltage (3.3V), so thinner wire like 22 or 24 AWG is fine.
  • Multimeter.  A meter that can measure resistance will help diagnose any problems with the magnetic sensor.

You’ll also need to download the following from the Raspberry Pi web site:

Wiring up the Magnetic Sensor to the Raspberry Pi

The Raspberry Pi has numerous GPIO pins – multipurpose connections that can be inputs, outputs, or have special functionality.  The magnetic sensor should have two wires coming out of it.  When the magnet is brought close to the sensor, the two wires will behave as if they are connected together.  If your magnetic sensor has three wires, using the multimeter in resistance mode, determine which two wires go to 0 resistance when the magnet is brought close to the sensor and use only those wires.

Before you attach the magnetic sensor to the garage door, let’s test it out first.  Connect one wire of the magnetic sensor to pin 9 (ground), and the other to pin 15 (GPIO 22).  When the magnet is close to the sensor, GPIO 22 will be grounded.  When the magnet is away from the sensor, internal pull up resistors in the RPi will bring GPIO 22 up to 3.3V.

If you’re monitoring multiple garage doors, pick another ground and GPIO pin (e.g. pin 14 and 16) to connect the additional sensors to.

Double check your connections – getting it wrong could permanently damage your Raspberry Pi!

RPi GPIO Diagram 550px Wide

Setting up the Raspberry Pi

Once you’ve purchased all the components, downloaded the software, and connected the sensor(s), here’s how to get it configured.

  1. Follow the Raspberry Pi quick start guide (there’s a link to it on the download page) to prepare the SD card and copy NOOBS onto the SD card.  In summary:
    1. Format the SD card using the SD Card Association’s formatting tool.
    2. Unzip the New Out Of Box Software zip file
      1. Windows – right click on the file and choose “Extract All”
      2. Mac – double click on the file
      3. Linux – Run “unzip <filename>” from a terminal
    3. Copy the extracted files onto the SD card you just formatted
  2. Connect the USB keyboard and mouse to the USB hub.
  3. Insert the SD card into your RPi and connect the following to your RPi:
    1. USB hub
    2. USB wifi adapter
    3. HDMI-HDMI or HDMI-DVI cable.  Connect the other end to your monitor.
    4. Finally, connect the micro-USB cable to the RPi and the power supply.  It should look similar to the picture below:
      RPi_Initial_Setup
  4. Your RPi should now boot into NOOBS and display a list of operating systems.  If your display remains blank, try pressing “2” on the keyboard to change to HDMI safe mode.
    NOOBS OS selection cropped
  5. Select Raspbian from the list if it’s not already selected.  Click “Install OS” in the top left corner.
  6. Installing the OS will take a several minutes.  Once it is completed, a dialog box will appear indicating the image was successfully applied.  click Ok to acknowledge the dialog box and the RPi will reboot into raspi-config.
    raspi-config screen
    Choose the following options in raspi-config:

    • Internationalization Options
      • Change locale – US users should uncheck en_GB.UTF-8 and select en_US.UTF-8 instead.  Make en_US.UTF-8 the default locale for the system.
      • Change Timezone – select your timezone from the list.
      • Change keyboard layout – US users should typically choose “Generic 104-key PC” keyboard with the “English (US)” layout.
    • Enable Boot to Desktop – pick “yes” at the “Should we boot straight to desktop?” dialog.
    • Press the Tab key to select Finish in the bottom-right, then press Enter.  Select “Yes” at the “Would you like to reboot now?” dialog and press Enter.
  7. The RPi will reboot into a graphical user environment.  Double click on WiFi config on the desktop.  The “wpa_gui” dialog will appear.
    1. Click the “Scan” button to search for Wifi networks.  Double click on your Wifi network.
    2. A new dialog will appear with “SSID” in the first row.  If your Wifi network has a password, enter it in the PSK field.wpa_gui dialog to add a network
    3. Click Add to close the dialog box with “SSID” in the first row.
    4. Click Close in the “Scan results” dialog box.
    5. The “wpa_gui” dialog should now show “Completed (station)” as its status.wpa_gui joined network
  8. Double click on Midori on the desktop and ensure you can browse to http://www.google.com/
  9. Close Midori and wpa_gui.

At this point, your Raspberry Pi has the Raspbian operating system installed and it can connect to the Internet.

Installing prerequisites

The next step is to update the operating system and install the software required to run the Pi Garage Alert software.

  1. Double click on “LXTerminal” on the Raspberry Pi desktop.  A terminal window will appear with the “pi@raspberrypi” prompt.  To update the system software, run the following commands.  “apt-get” manages the software on the Raspbian operating system (and many other Linux distributions), and “sudo” runs apt-get with the privileges it needs.
    sudo apt-get update
    sudo apt-get upgrade
  2. After running “sudo apt-get upgrade”, you’ll be presented with a list of packages that need to be upgraded.  Type “y” then press Enter to proceed with the upgrade.  The upgrade may take a couple minutes.
  3. Some additional software is also required – tweepy for Tweeting, twilio for sending SMS, and sleekxmpp for Jabber.  It is necessary to install this software even if you aren’t using Tweet/SMS alerts.  To install them, enter the following commands in the LXTerminal window:
    sudo apt-get install python-setuptools python-dev libffi-dev libssl-dev
    sudo pip install tweepy twilio==5.6.0 sleekxmpp dnspython pyasn1
    sudo pip install pyasn1_modules requests[security] slackclient
    sudo pip install -U requests

Installing Pi Garage Alert

We’re finally ready to install the Pi Garage Alert software.

  1. Start by downloading the software using the “git” tool:
    git clone https://github.com/rllynch/pi_garage_alert.git
  2. Now, copy the files into the correct location:
    cd pi_garage_alert
    sudo cp bin/pi_garage_alert.py /usr/local/sbin/
    sudo cp etc/pi_garage_alert_config.py /usr/local/etc/
    sudo cp init.d/pi_garage_alert /etc/init.d/
    sudo chown pi /usr/local/etc/pi_garage_alert_config.py
  3. Test out the Pi Garage Alert software by running the following command:
    sudo /usr/local/sbin/pi_garage_alert.py
  4. You should see output similar to the following in the terminal:
    pi@raspberrypi ~ $ sudo /usr/local/sbin/pi_garage_alert.py
    2013-07-27 19:37:52: ==========================================================
    2013-07-27 19:37:52: Pi Garage Alert starting
    2013-07-27 19:37:52: Configuring global settings
    2013-07-27 19:37:52: Configuring pin 15 for "Example Garage Door"
    2013-07-27 19:37:52: Initial state of "Example Garage Door" is open
    2013-07-27 19:37:56: CPU temp: 46.5, GPU temp: 46.0, Uptime: 0:50:17, Example Garage Door: open/0/4
  5. Now, try moving the magnet close to the magnetic sensor and you should see the following:
    2013-07-27 19:39:29: State of "Example Garage Door" changed to closed after 97 sec
  6. If you see a message similar to this one, everything is working correctly together.  If you do not see this, double check the wiring and make sure your garage door sensor is plugged into the pin 15 and pin 9.  You may also want to using your multimeter to test the resistance of the sensor and make sure it goes to (nearly) zero when the magnet is close to the sensor.

At this point, the RPi can sense the state of the garage door, but it is not sending emails/tweets/SMSes yet.  Part 2 discusses how to configure these alerts.