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.
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.
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.
- 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:
- New Out Of Box Software – This is the operating system that will run on the RPi.
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!
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.
- 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:
- Format the SD card using the SD Card Association’s formatting tool.
- Unzip the New Out Of Box Software zip file
- Windows – right click on the file and choose “Extract All”
- Mac – double click on the file
- Linux – Run “unzip <filename>” from a terminal
- Copy the extracted files onto the SD card you just formatted
- Connect the USB keyboard and mouse to the USB hub.
- Insert the SD card into your RPi and connect the following to your RPi:
- 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.
- Select Raspbian from the list if it’s not already selected. Click “Install OS” in the top left corner.
- 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.
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.
- Internationalization Options
- The RPi will reboot into a graphical user environment. Double click on WiFi config on the desktop. The “wpa_gui” dialog will appear.
- Click the “Scan” button to search for Wifi networks. Double click on your Wifi network.
- A new dialog will appear with “SSID” in the first row. If your Wifi network has a password, enter it in the PSK field.
- Click Add to close the dialog box with “SSID” in the first row.
- Click Close in the “Scan results” dialog box.
- The “wpa_gui” dialog should now show “Completed (station)” as its status.
- Double click on Midori on the desktop and ensure you can browse to http://www.google.com/
- Close Midori and wpa_gui.
At this point, your Raspberry Pi has the Raspbian operating system installed and it can connect to the Internet.
The next step is to update the operating system and install the software required to run the Pi Garage Alert software.
- 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
- 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.
- You’ll also want to ensure git is installed to clone the pi_garage_alert code and pip is installed so Python dependencies can be installed. To install them, enter the following command in the LXTerminal window:
sudo apt-get install git python3-pip
Installing Pi Garage Alert
We’re finally ready to install the Pi Garage Alert software.
- Start by downloading the software using the “git” tool:
git clone https://github.com/rllynch/pi_garage_alert.git
- Next, install the Python packages required by pi_garage_alert:
sudo pip3 install -r requirements.txt
- Now, copy the files into the correct location:
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
- Test out the Pi Garage Alert software by running the following command:
- 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
- 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
- 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.