|
#!/usr/bin/python3
|
|
from email.mime.text import MIMEText
|
|
import logging
|
|
from smtplib import SMTP_SSL
|
|
import sys
|
|
import time
|
|
|
|
import RPi.GPIO as GPIO
|
|
|
|
# These should probably be protected...
|
|
RELAY_EMAIL = 'dsorber.phalanx@gmail.com'
|
|
RELAY_PASSWD = "You'reNotMySupervisor"
|
|
TO_EMAIL = 'baranovich@gmail.com'
|
|
|
|
LOG_FORMAT_STR = '%(asctime)s|%(levelname)s|%(filename)s:%(lineno)s|%(message)s'
|
|
LOG_DATE_FORMAT_STR = '%a %b %d %H:%M:%S %Y'
|
|
DEFAULT_LOG_LEVEL = logging.DEBUG
|
|
|
|
#
|
|
# Automated Watering System Daemon
|
|
#
|
|
|
|
def send_email(subject, msg_text):
|
|
""" Simple helper function to send an email
|
|
"""
|
|
msg = MIMEText(msg_text, 'plain')
|
|
msg['Subject'] = subject
|
|
msg['To'] = TO_EMAIL
|
|
try:
|
|
conn = SMTP_SSL('smtp.gmail.com')
|
|
conn.set_debuglevel(True)
|
|
conn.login(RELAY_EMAIL, RELAY_PASSWD)
|
|
try:
|
|
conn.sendmail(RELAY_EMAIL, TO_EMAIL, msg.as_string())
|
|
finally:
|
|
conn.close()
|
|
except Exception as exp:
|
|
error = '{:s}: {!r}'.format(type(exp).__name__, exp.args)
|
|
logging.error(error)
|
|
|
|
def main():
|
|
# Configure logging before attempting anything serious
|
|
logging.basicConfig(format=LOG_FORMAT_STR,
|
|
datefmt=LOG_DATE_FORMAT_STR,
|
|
level=DEFAULT_LOG_LEVEL)
|
|
|
|
logging.info('automated water system deamon started')
|
|
|
|
# Configure GPIO
|
|
GPIO.setmode(GPIO.BCM)
|
|
GPIO.setup(17, GPIO.OUT)
|
|
|
|
try:
|
|
# Turn on pump
|
|
GPIO.output(17, True)
|
|
logging.debug('pump on')
|
|
|
|
# Wait for a while -- 1.5 hours
|
|
time.sleep(5400)
|
|
|
|
finally:
|
|
# Turn off pump
|
|
GPIO.output(17, False)
|
|
logging.debug('pump off')
|
|
|
|
send_email('Automated Watering System Report', 'watering completed')
|
|
logging.info('notification sent')
|
|
|
|
logging.info('automated water system deamon stopping')
|
|
|
|
|
|
if __name__ == '__main__':
|
|
sys.exit(main())
|