Compare commits

...

7 Commits

Author SHA1 Message Date
Ian Adam Naval
f4caad52ff Convert to standard logging library 2015-09-14 23:38:51 -04:00
Ian Adam Naval
40a9e0f5f7 Move email notification to config file 2015-09-14 23:26:13 -04:00
Ian Adam Naval
b26802003d Merge branch 'ifttt' 2015-09-14 22:52:05 -04:00
Ian Adam Naval
daef848cc0 Merge branch 'gitignore' 2015-09-14 22:51:06 -04:00
15de7719bf Add a gitignore 2015-09-14 22:40:18 -04:00
Ian Adam Naval
4ab145c62a Tweak thresholds and sliding window size
Increasing sliding window size makes it less sensitive to change, which
makes it take a little longer to register that the dryer is on. This
allows for short bursts of acceleration due to opening/closing the door
and loading clothes.
2015-09-12 01:28:08 -04:00
Ian Adam Naval
b83c342a81 Fix minor formatting issue 2015-09-12 01:27:47 -04:00

View File

@ -1,4 +1,5 @@
import configparser
import logging
import math
import smtplib
import time
@ -9,8 +10,6 @@ import requests
from microstacknode.hardware.accelerometer.mma8452q import MMA8452Q
EMAIL_USERNAME = 'noreply@ianonavy.com'
EMAIL_PASSWORD = 'S2PENjQbO6=cHgchw@CXs.bJ'
RECIPIENT_EMAILS = ['ianonavy@gmail.com']
ALERT_EMAIL_TEXT = """Hi,
@ -21,8 +20,9 @@ Ianonavy Bot
"""
G_RANGE = 2
INTERVAL = 0.005 # seconds
WINDOW_SIZE = 400 # intervals
WINDOW_SIZE = 1000 # intervals
THRESHOLD = 0.025 # G's
MAX_EMAIL_FREQUENCY = 60 # seconds
MAX_NOTIFICATION_FREQUENCY = 60 # seconds
CONFIG_FILE_PATH = '/home/pi/laundry-notifier/laundry_notifier/laundry_notifier.conf'
@ -39,8 +39,8 @@ def stdev(s):
return math.sqrt(average(list(variance)))
def notify_user(recipient_email_address):
log("Alerting " + recipient_email_address)
def notify_user(username, password, recipient_email_address):
logging.info("Alerting " + recipient_email_address)
msg = MIMEText(ALERT_EMAIL_TEXT)
@ -51,8 +51,6 @@ def notify_user(recipient_email_address):
msg['To'] = recipient_email_address
# Send the message via our own SMTP server.
username = EMAIL_USERNAME
password = EMAIL_PASSWORD
server = smtplib.SMTP('smtp.gmail.com:587')
server.starttls()
server.login(username, password)
@ -77,8 +75,7 @@ def amplitude_stdev(sliding_window):
def send_notifications(last_notification_sent_at, iftttkey):
seconds_since_last_notification = \
(datetime.now() - last_notification_sent_at).seconds
# Log the time notifications were sent
log("Sending notification after %ds" % seconds_since_last_notification)
logging.info("Sending notification after %ds" % seconds_since_last_notification)
# limit frequency of notifications
if seconds_since_last_notification > MAX_NOTIFICATION_FREQUENCY:
[notify_user(email) for email in RECIPIENT_EMAILS]
@ -91,10 +88,16 @@ def send_notifications(last_notification_sent_at, iftttkey):
def main():
log('started laundry notifier')
LOG_FORMAT = '%(asctime)-15s %(message)s'
logging.basicConfig(format=LOG_FORMAT)
logging.getLogger().setLevel(logging.INFO)
logging.info('Started laundry notifier')
config = configparser.ConfigParser()
config.read(CONFIG_FILE_PATH)
iftttkey = config['notifications']['ifttt_key']
notifications_section = config['notifications']
iftttkey = notifications_section['ifttt_key']
email_username = notifications_section['email_username']
email_password = notifications_section['email_password']
with MMA8452Q() as accelerometer:
# Configure accelerometer
accelerometer.standby()
@ -116,23 +119,21 @@ def main():
if sliding_stdev['x'] < THRESHOLD:
# Notify recipients on state transitions from 'on' to 'off'
if dryer_state == 'on':
log('Dryer turned off; sliding stdev is %f' % sliding_stdev['x'])
logging.info('Dryer turned off; sliding stdev is %f' % sliding_stdev['x'])
last_notification_sent_at = send_notifications(
last_notification_sent_at,
email_username,
email_password,
iftttkey)
dryer_state = 'off'
else:
# Log state transitions from 'off' to 'on'
if dryer_state == 'off':
log('Dryer turned on; sliding stdev is %f' % sliding_stdev['x'])
logging.info('Dryer turned on; sliding stdev is %f' % sliding_stdev['x'])
dryer_state = 'on'
time.sleep(INTERVAL)
def log(message):
print('%s\t%s' % (datetime.isoformat(datetime.now()), message))
if __name__ == '__main__':
main()