Avatar of dynamicOne
dynamicOne asked on

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")
ProgrammingProgramming TheoryPython

Avatar of undefined
Last Comment
dynamicOne

8/22/2022 - Mon
TommySzalapski

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.
ASKER
dynamicOne

That's a good idea, but it won't accomplish what I want. Any idea what the garage.txt file is for?
ASKER CERTIFIED SOLUTION
TommySzalapski

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
See how we're fighting big data
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
TommySzalapski

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.
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
ASKER
dynamicOne

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")
TommySzalapski

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.
ASKER
dynamicOne

Tommy,

This is where I put it. Is that correct?

last_result = ""
while 1: #loop forever
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
ASKER
dynamicOne

Tommy, it works! Thank you. You made my day!