I downloaded a Python script so I could receive an email when my garage door is open or closed. However, when I run the Python script I receive numerous emails instead of just one. I would only like to receive one email per condition. Eventually I would like to run this script all of the time on my Pi, but I can't until I fix this error. I opened up the garage.txt file and there is nothing in it. Could this be related to the non-stop emails?
Thanks for the help.
Here is the code:
#! /usr/bin/env python
from email.MIMEText import MIMEText
ser = 'gpio read 1' #Change /dev/ttyACM0 to your com port
GMAIL_LOGIN = '***********@gmail.com'
GMAIL_PASSWORD = '*********'
SEND_TO = '**********@txt.att.net'
def send_email(subject, message, from_addr=GMAIL_LOGIN, to_addr=SEND_TO):
msg = MIMEText(message)
msg['Subject'] = subject
msg['From'] = from_addr
msg['To'] = to_addr
server = smtplib.SMTP('smtp.gmail.com',587) #port 465 or 587
server.sendmail(from_addr, to_addr, msg.as_string())
while 1: #loop forever
result = commands.getoutput(ser)
if result.strip() == "1":
status.write("Garage door is open")
send_email('OPEN', 'The garage door is open')
elif result.strip() == "0":
status.write("Garage door is closed")
send_email('CLOSED','The garage door is closed')