Yesterday an expert help me get my script to stop delivering multiple messages. He did a great job! I would have contacted him directly, but this is the only way I know how to.
My script should run 24/7. However, after a couple of hours (an educated guess at best). It quits working. The way I know it quits working is because I don't receive the text messages the script sends when the garage door opens and closes.
Any ideas?
#! /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()
last_result = ""
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()
last_result = result.strip();
# 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")
ASKER