140 lines
4.3 KiB
Python
140 lines
4.3 KiB
Python
import configparser
|
|
import logging
|
|
import math
|
|
import smtplib
|
|
import time
|
|
from datetime import datetime
|
|
from email.mime.text import MIMEText
|
|
|
|
import requests
|
|
from microstacknode.hardware.accelerometer.mma8452q import MMA8452Q
|
|
|
|
|
|
RECIPIENT_EMAILS = ['ianonavy@gmail.com']
|
|
ALERT_EMAIL_TEXT = """Hi,
|
|
|
|
Your laundry is done. Or maybe your roomate's. I don't know.
|
|
|
|
Regretfully,
|
|
Ianonavy Bot
|
|
"""
|
|
G_RANGE = 2
|
|
INTERVAL = 0.005 # seconds
|
|
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'
|
|
|
|
|
|
def average(s):
|
|
return sum(s) * 1.0 / len(s)
|
|
|
|
|
|
def stdev(s):
|
|
if not s:
|
|
return 0
|
|
avg = average(list(s))
|
|
variance = map(lambda x: (x - avg) ** 2, s)
|
|
return math.sqrt(average(list(variance)))
|
|
|
|
|
|
def notify_user(username, password, recipient_email_address):
|
|
logging.info("Alerting " + recipient_email_address)
|
|
|
|
msg = MIMEText(ALERT_EMAIL_TEXT)
|
|
|
|
# me == the sender's email address
|
|
# you == the recipient's email address
|
|
msg['Subject'] = 'Laundry Finished'
|
|
msg['From'] = 'pi@localhost'
|
|
msg['To'] = recipient_email_address
|
|
|
|
# Send the message via our own SMTP server.
|
|
server = smtplib.SMTP('smtp.gmail.com:587')
|
|
server.starttls()
|
|
server.login(username, password)
|
|
server.send_message(msg)
|
|
server.quit()
|
|
|
|
|
|
def enqueue(sliding_window, item):
|
|
if len(sliding_window) == WINDOW_SIZE:
|
|
sliding_window.pop(0)
|
|
sliding_window.append(item)
|
|
|
|
|
|
def amplitude_stdev(sliding_window):
|
|
standard_deviations = {}
|
|
for key in ('x', 'y', 'z'):
|
|
values = [measurement[key] for measurement in sliding_window]
|
|
standard_deviations[key] = stdev(values)
|
|
return standard_deviations
|
|
|
|
|
|
def send_notifications(last_notification_sent_at, iftttkey):
|
|
seconds_since_last_notification = \
|
|
(datetime.now() - last_notification_sent_at).seconds
|
|
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]
|
|
# Notify if this, then that
|
|
requests.get(
|
|
'https://maker.ifttt.com/trigger/laundry_done/with/key/%s'
|
|
% iftttkey)
|
|
return datetime.now()
|
|
return last_notification_sent_at
|
|
|
|
|
|
def main():
|
|
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)
|
|
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()
|
|
accelerometer.set_g_range(G_RANGE)
|
|
accelerometer.activate()
|
|
|
|
# Settle
|
|
time.sleep(INTERVAL)
|
|
|
|
sliding_window = []
|
|
dryer_state = 'off'
|
|
last_notification_sent_at = datetime(1970, 1, 1, 0, 0, 0)
|
|
|
|
while True:
|
|
g_values = accelerometer.get_xyz()
|
|
enqueue(sliding_window, g_values)
|
|
sliding_stdev = amplitude_stdev(sliding_window)
|
|
if g_values:
|
|
if sliding_stdev['x'] < THRESHOLD:
|
|
# Notify recipients on state transitions from 'on' to 'off'
|
|
if dryer_state == 'on':
|
|
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':
|
|
logging.info('Dryer turned on; sliding stdev is %f' % sliding_stdev['x'])
|
|
dryer_state = 'on'
|
|
|
|
time.sleep(INTERVAL)
|
|
|
|
|
|
if __name__ == '__main__':
|
|
main()
|