jayteasuk
Posts: 7
Joined: Tue Jul 31, 2012 7:11 am

email alert on door open

Tue Jul 31, 2012 7:16 am

well i have my raspberry pi and would like to set up an email alert when a door contact switch is opened/closed.

not sure where to start on this, im a sql developer and i have written a few c# net web apps also using javascript so im hoping im not biting off more than i can chew but i would like some direction on where to begin and any sample code anyone has of sending emails and using the IO pins

calebzulawski
Posts: 46
Joined: Tue Jan 31, 2012 6:00 am

Re: email alert on door open

Tue Jul 31, 2012 3:21 pm

This is a good starting project; it's relatively simple as long as you have a basic understanding of programming, which you do, so you're all set.

First, if you plan on using the GPIO pins, you must buffer your detection circuit, as the GPIO pins have no protection built in, and I believe are 3V3, not the standard 5V. Various methods of isolation exist... opto-isolators, reed switches, relays. I'm not sure which are appropriate for this application.

In addition, you'll need to create the detection circuit. If you are using a simple switch (contact is lost when the door opens) you'll need pull-up or pull-down resistors so that your switch outputs a digital voltage difference at all times.

domesday
Posts: 258
Joined: Fri Oct 21, 2011 5:53 pm
Location: UK

Re: email alert on door open

Tue Jul 31, 2012 3:25 pm

The latest issue of the MagPi (Issue 4) has an article on building an alarm, might be a good starting point.

YodaTheCoder
Posts: 14
Joined: Fri Jul 27, 2012 5:02 pm

Re: email alert on door open

Tue Jul 31, 2012 3:26 pm

Issue 2 of The MagPi has a tutorial on reading button (switch in your case) state using Python, that should suit your needs.

Sending an email can be done with a single command line provided the proper software is installed.

jayteasuk
Posts: 7
Joined: Tue Jul 31, 2012 7:11 am

Re: email alert on door open

Tue Jul 31, 2012 6:55 pm

calebzulawski wrote:This is a good starting project; it's relatively simple as long as you have a basic understanding of programming, which you do, so you're all set.

First, if you plan on using the GPIO pins, you must buffer your detection circuit, as the GPIO pins have no protection built in, and I believe are 3V3, not the standard 5V. Various methods of isolation exist... opto-isolators, reed switches, relays. I'm not sure which are appropriate for this application.

In addition, you'll need to create the detection circuit. If you are using a simple switch (contact is lost when the door opens) you'll need pull-up or pull-down resistors so that your switch outputs a digital voltage difference at all times.

I was hoping to just bridge the pins with switch rather than set up an independent circut board and have a separate voltage source, can this be done. If not i would run a relay but not sure about the digital resistors its been a while since i looked at electronics.

calebzulawski
Posts: 46
Joined: Tue Jan 31, 2012 6:00 am

Re: email alert on door open

Tue Jul 31, 2012 7:14 pm

I'm not too sure if you can bridge the pins, but my guess is that you might be able to, as long as you include the pull up or pull down resistor.

cpswan
Posts: 47
Joined: Tue May 15, 2012 9:52 am

Re: email alert on door open

Thu Aug 02, 2012 6:59 pm

domesday wrote:The latest issue of the MagPi (Issue 4) has an article on building an alarm, might be a good starting point.
I set my son a summer holiday challenge per the topic of this thread, so we had a go at the MagPi project. It wasn't entirely straightforward so I've posted a howto on my blog that covers getting GPIO going with Python and including working source code.

domesday
Posts: 258
Joined: Fri Oct 21, 2011 5:53 pm
Location: UK

Re: email alert on door open

Thu Aug 02, 2012 7:51 pm

The trouble is the limited space for an article in the MagPi magazine, it is part 3 of a series so if you haven't been following the series where it it explains that programs are python 2 and how to install v 0.2.0 of the GPIO library then that would explain why you ran into difficulty. I think the producers of the MagPi magazine are encouraging people to type the listings in as part of the experience but I think your blog page will certainly help people who are struggling to get the code working, especially people who have missed the first two parts of the series.

Obviously the next step for you is to modify the program to call a routine to send an email, I expect there is a library for that, http://docs.python.org/library/smtplib. ... le-smtplib maybe?.

BTW, well done for sticking with it and getting it working.

cpswan
Posts: 47
Joined: Tue May 15, 2012 9:52 am

Re: email alert on door open

Thu Aug 02, 2012 8:48 pm

domesday wrote:BTW, well done for sticking with it and getting it working.
I've been browsing MagPi, and dipping into interesting looking projects, but obviously I failed to appreciate the dependency on earlier issues here.

I'd wanted my son to type in the listing (as I think I learned most of my programming skills from debugging listings for the ZX81, Beeb, Dragon32 etc.), but progress was a little too slow. I suspect that even if he'd typed it in perfectly we'd have still hit some indentation issues.

domesday
Posts: 258
Joined: Fri Oct 21, 2011 5:53 pm
Location: UK

Re: email alert on door open

Thu Aug 02, 2012 9:12 pm

I agree indentation can be a real pain with python. On the one hand it causes programs to fail for no apparent reason to a beginner, I suppose on the upside it does force you to make readable code. When I started out, I think enforced indentation would have frustrated me.

cpswan
Posts: 47
Joined: Tue May 15, 2012 9:52 am

Re: email alert on door open

Fri Aug 03, 2012 8:05 am

domesday wrote:Obviously the next step for you is to modify the program to call a routine to send an email, I expect there is a library for that, http://docs.python.org/library/smtplib. ... le-smtplib maybe?
The family all use Gmail/GApps so I found some decent example code that sends emails via the Google SMTP servers using smtplib. I stripped out the attachment code as it's not required for this use case (though could be interesting for a more sophisticated project using a webcam) and left a working 'Hello World!' example on ~/ of my son's Raspbian SD card. Let's see how long it takes him to pull the code together?

Code: Select all

import os
import smtplib
import mimetypes
from email.MIMEMultipart import MIMEMultipart
from email.MIMEBase import MIMEBase
from email.MIMEText import MIMEText
from email.MIMEAudio import MIMEAudio
from email.MIMEImage import MIMEImage
from email.Encoders import encode_base64
def sendMail(subject, text):
  gmailUser = 'username@gmail.com'
  gmailPassword = 'yourSecr3t'
  recipient = 'whoever@example.com'
  msg = MIMEMultipart()
  msg['From'] = gmailUser
  msg['To'] = recipient
  msg['Subject'] = subject
  msg.attach(MIMEText(text))
  mailServer = smtplib.SMTP('smtp.gmail.com', 587)
  mailServer.ehlo()
  mailServer.starttls()
  mailServer.ehlo()
  mailServer.login(gmailUser, gmailPassword)
  mailServer.sendmail(gmailUser, recipient, msg.as_string())
  mailServer.close()
  print'Sent email to %s' % recipient

sendMail('Test', 'Hello World!')

Return to “Automation, sensing and robotics”