Receiving Multiple Messages, only need one

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
import commands
import smtplib
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.ehlo()
    server.starttls()
    server.ehlo()
    server.login(GMAIL_LOGIN,GMAIL_PASSWORD)
    server.sendmail(from_addr, to_addr, msg.as_string())
    server.close()


while 1: #loop forever
   result = commands.getoutput(ser)
   if result.strip() == "1":
    status=open('garage.txt', 'w')
    status.write("Garage door is open")
    send_email('OPEN', 'The garage door is open')
    status.close()
#    print("open")

   elif result.strip() == "0":
    status=open('garage.txt', 'w')
    status.write("Garage door is closed")
    send_email('CLOSED','The garage door is closed')
    status.close()
#    print("closed")
dynamicOneAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

TommySzalapskiCommented:
So the loop just goes forever. while 1: will keep executing over and over again and never stop. Checks the state of the door and sends the email every time.
Perhaps you want some kind of sleep in there?
Or better yet, take out the while 1: so the script only checks once and create a cron job to run the script every hour or day or whatever.
0
dynamicOneAuthor Commented:
That's a good idea, but it won't accomplish what I want. Any idea what the garage.txt file is for?
0
TommySzalapskiCommented:
Or are you looking for a way to have it notify you every time the door opens, but only once?

You could do something like
while 1: #loop forever
   result = commands.getoutput(ser)
   if result.strip() == "1" and result.strip() != last_result:
    status=open('garage.txt', 'w')
    status.write("Garage door is open")
    send_email('OPEN', 'The garage door is open')
    status.close()
#    print("open")

   elif result.strip() == "0" and result.strip() != last_result:
    status=open('garage.txt', 'w')
    status.write("Garage door is closed")
    send_email('CLOSED','The garage door is closed')
    status.close()
   last_result = result.strip();
#    print("closed") 

Open in new window

That way it will only email you one time each time the state changes.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

TommySzalapskiCommented:
I just added the
last_result = result.strip();
at the end of the loop and the 'and' conditions on the ifs.

No I don't know what garage.txt would be for.
0
dynamicOneAuthor Commented:
This is exactly what I want. Thank you. When I added the code I get the following error:

Traceback (most recent call last):
  File "garagestatus.py", line 36, in <module>
    elif result.strip() == "0" and result.strip() != last_result:
NameError: name 'last_result' is not defined

Here is my code if you want to check if I added it correctly:

#! /usr/bin/env python
import commands
import smtplib
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.ehlo()
    server.starttls()
    server.ehlo()
    server.login(GMAIL_LOGIN,GMAIL_PASSWORD)
    server.sendmail(from_addr, to_addr, msg.as_string())
    server.close()


while 1: #loop forever
   result = commands.getoutput(ser)
   if result.strip() == "1" and result.strip() != last_result:
    status=open('garage.txt', 'w')
    status.write("Garage door is open")
    send_email('OPEN', 'The garage door is open')
    status.close()
#    print("open")

   elif result.strip() == "0" and result.strip() != last_result:
    status=open('garage.txt', 'w')
    status.write("Garage door is closed")
    send_email('CLOSED','The garage door is closed')
    status.close()
    last_result = result.strip();
#    print("closed")
0
TommySzalapskiCommented:
Right, you need to initialize it to something.
Put
last_result = ""
right before the loop.
Or if you don't want it to send you an email when you start running the script, use
last_result = commands.getoutput(ser)

So that the first time it checks, they will match.
0
dynamicOneAuthor Commented:
Tommy,

This is where I put it. Is that correct?

last_result = ""
while 1: #loop forever
0
dynamicOneAuthor Commented:
Tommy, it works! Thank you. You made my day!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Programming

From novice to tech pro — start learning today.