This section describes how to configure alerts in pi_garage_alert.  If you haven’t already read part 1 of this series, please read it first.

Configuring Pi Garage Alert

  1. The configuration is stored in /usr/local/etc/pi_garage_alert_config.py. Go into the menu in the bottom left corner of the screen, and select “Leafpad” from the “Accessories” menu. An editor will appear. Pick Open from the File menu, select “File System” from Places on the left, then navigate to and open /usr/local/etc/pi_garage_alert_config.py.
  2. You’ll notice two garage doors are defined, but many lines describing the garage doors start with “#”. This indicates a comment line in Python and these lines are ignored by the compiler. So, the default configuration actually only defines one door on pin 15 called “Example Garage Door” with no alerts (all commented out).garage_config2
  3. The ‘pin’ line specifies which GPIO pin the magnetic sensor is connected to.
  4. If you have multiple garage doors, replace “Example Garage Door” with a description of which garage door the sensor is connected to.
  5. Each alert has three parameters
    1. state – what state triggers the alert, open or closed.  This almost always should be open.
    2. time – how many seconds the garage door must be in that state before the alert is triggered.
    3. recipients – who to notify
  6. Uncomment (remove the #) one of the alerts, and make yourself the recipient of the alert.  Make sure there isn’t a comma after the last recipient.  The recipients line should look similar to one of the following:
    'recipients': [ 'sms:+11112223333' ]
    'recipients': [ 'email:someone@example.com' ]
    'recipients': [ 'twitter_dm:twitter_user' ]
  7. Save the updated configuration file.
  8. Each type of alert – email, twitter, SMS – requires configuration.  Follow the below sections for the alert types you’re using:

Email

The RPi needs to be configured with an SMTP server.  These instructions will cover forwarding mail through GMail and assume mail will be sent from GMail.  The email can be sent to any address, so if you don’t have a GMail account, create a new account for your RPi.  If you aren’t using email alerts, you can skip this section.

These instructions are largely summarized from http://rtcamp.com/wordpress-nginx/tutorials/linux/ubuntu-postfix-gmail-smtp/.

  1. Run the following commands in an LXTerminal window to install the prerequisites:
    sudo apt-get install postfix mailutils libsasl2-2 ca-certificates libsasl2-modules
  2. When prompted, configure postfix as an Internet site, and give raspberrypi as the mail name.
  3. Open /etc/postfix/main.cf in an editor, remove any line beginning with “relayhost =”, then append the following lines to it:
    relayhost = [smtp.gmail.com]:587
    smtp_sasl_auth_enable = yes
    smtp_sasl_password_maps = hash:/etc/postfix/sasl_passwd
    smtp_sasl_security_options = noanonymous
    smtp_tls_CAfile = /etc/ssl/certs/ca-certificates.crt
    smtp_use_tls = yes
  4. Open /etc/postfix/sasl_passwd in an editor and fill it with the following. Replace username@gmail.com with your GMail address, and gmail_password with your GMail password.
    [smtp.gmail.com]:587 username@gmail.com:gmail_password
  5. Fix the permissions and update postfix config to use sasl_passwd
    sudo chmod 400 /etc/postfix/sasl_passwd
    sudo postmap /etc/postfix/sasl_passwd
  6. Reload postfix config
    sudo /etc/init.d/postfix reload
  7. Send yourself a test email.  Replace username@example.com with your email address.
    echo "test mail" | mail -s "test subject" username@example.com

Twitter

This sections describes how to set up pi_garage_alert.py to send Tweets.  If you’re not using Twitter alerts, you can skip this section.

  1. Create a twitter account for pi_garage_alert.py
  2. Follow the instructions on http://talkfast.org/2010/05/31/twitter-from-the-command-line-in-python-using-oauth/ to create a new Twitter application on the new account.  Once you’re done, you should have the following:
    1. Consumer key and secret
    2. Access key and secret
  3. Edit /usr/local/etc/pi_garage_alert_config.py and enter those keys in the twitter section.
  4. pi_garage_alert.py can only send direct messages to its followers, so make sure you’re following the Twitter account you created.

SMS

SMSs are sent through a paid service called Twilio.  At the time of writing, they charge $1 per month for a phone number, and $0.01 per SMS message.  If you’d prefer free options, you can skip this section and use email or Twitter instead.

Here’s how to setup SMS through Twilio:

  1. Sign up for a Twilio account and put some funds in your account.
  2. At the top of your Twilio dashboard, an account SID and auth token are listed.  You may need to click the lock to show the auth token.
  3. Edit /usr/local/etc/pi_garage_alert_config.py and copy the account SID into TWILIO_ACCOUNT, and the auth token into TWILIO_TOKEN.
  4. Copy your Twilio phone number into TWILIO_PHONE_NUMBER.

Finishing Up

At this point you’ve updated the configuration file and performed the steps in either the Email, Twitter, or SMS section above.  Let’s test it out and configure pi_garage_alert to run when the RPi is powered on.

  1. If pi_garage_alert.py is still running, press ctrl-c to stop it and return to the command prompt.
  2. Run pi_garage_alert.py again with the new configuration:
    sudo /usr/local/sbin/pi_garage_alert.py
  3. Move the magnet away from the sensor so pi_garage_alert reports the garage door is open.
  4. Wait for the number of seconds specified in the configuration file (default was 120 or 600 seconds – you may want to shorten this for debugging).
  5. You should receive an email/tweet/SMS indicating the garage door is open, then if you close it, you should receive another email/tweet/SMS.  If you did not receive the email/tweet/SMS, examine the output of pi_garage_alert.py for any error messages and review the instructions above.
  6. Press ctrl-c to stop pi_garage_alert.py again.  In LXTerminal, enter the following command so it runs whenever the RPi powers on:
    sudo update-rc.d pi_garage_alert defaults
  7. Try rebooting your RPi, then in an LXTerminal window, run the following command:
    tail -f /var/log/pi_garage_alert.log
  8. You should see the log messages from pi_garage_alert.py.  Press ctrl-c when you’re done. pi_garage_alert.py will continue running in the background.
  9. If you have an additional garage door to configure or want to configure additional alerts, you should do that now.

All that remains is physically mounting the magnetic sensor and the RPi.  Many magnetic sensors are designed to be mounted to the bottom of the garage door, but also work equally well on the top of the garage door.  This avoids having to drill into the garage floor.

2013-08-04 GarageSensorMounted 001

Magnetic sensor mounted to wooden header at top of garage door

2013-08-04 GarageRPiMounted 001

RPi in ABS Project Box. The magnetic sensors connect to a terminal block on the bottom of the case.

The keyboard, mouse, monitor, and USB hub are no longer needed.  Power, USB Wifi (or Ethernet), and the magnetic sensors (to GPIO and ground) are the only connections required at this point.

If you have any problems after mounting the RPi, you can check on its status remotely over ssh.  Follow the instructions to ssh in, then examine the /var/log/pi_garage_alert.log log file.