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
- 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.
- 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).
- The ‘pin’ line specifies which GPIO pin the magnetic sensor is connected to.
- If you have multiple garage doors, replace “Example Garage Door” with a description of which garage door the sensor is connected to.
- Each alert has three parameters
- state – what state triggers the alert, open or closed. This almost always should be open.
- time – how many seconds the garage door must be in that state before the alert is triggered.
- recipients – who to notify
- 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:email@example.com' ]
'recipients': [ 'twitter_dm:twitter_user' ]
- Save the updated configuration file.
- Each type of alert – email, twitter, SMS – requires configuration. Follow the below sections for the alert types you’re using:
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/.
- Run the following commands in an LXTerminal window to install the prerequisites:
sudo apt-get install postfix mailutils libsasl2-2 ca-certificates libsasl2-modules
- When prompted, configure postfix as an Internet site, and give raspberrypi as the mail name.
- 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
- Open /etc/postfix/sasl_passwd in an editor and fill it with the following. Replace firstname.lastname@example.org with your GMail address, and gmail_password with your GMail password.
- Fix the permissions and update postfix config to use sasl_passwd
sudo chmod 400 /etc/postfix/sasl_passwd
sudo postmap /etc/postfix/sasl_passwd
- Reload postfix config
sudo /etc/init.d/postfix reload
- Send yourself a test email. Replace email@example.com with your email address.
echo "test mail" | mail -s "test subject" firstname.lastname@example.org
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.
- Create a twitter account for pi_garage_alert.py
- 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:
- Consumer key and secret
- Access key and secret
- Edit /usr/local/etc/pi_garage_alert_config.py and enter those keys in the twitter section.
- pi_garage_alert.py can only send direct messages to its followers, so make sure you’re following the Twitter account you created.
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:
- Sign up for a Twilio account and put some funds in your account.
- 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.
- Edit /usr/local/etc/pi_garage_alert_config.py and copy the account SID into TWILIO_ACCOUNT, and the auth token into TWILIO_TOKEN.
- Copy your Twilio phone number into TWILIO_PHONE_NUMBER.
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.
- If pi_garage_alert.py is still running, press ctrl-c to stop it and return to the command prompt.
- Run pi_garage_alert.py again with the new configuration:
- Move the magnet away from the sensor so pi_garage_alert reports the garage door is open.
- 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).
- 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.
- 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
- Try rebooting your RPi, then in an LXTerminal window, run the following command:
tail -f /var/log/pi_garage_alert.log
- 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.
- 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.
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.