Link to home
Start Free TrialLog in
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")
Avatar of TommySzalapski
TommySzalapski
Flag of United States of America image

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.
Avatar of dynamicOne
dynamicOne

ASKER

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
Avatar of TommySzalapski
TommySzalapski
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.
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")
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.
Tommy,

This is where I put it. Is that correct?

last_result = ""
while 1: #loop forever
Tommy, it works! Thank you. You made my day!