The previous commit message was useless
This commit is contained in:
parent
b7a58675c6
commit
91c67d2b5c
2
.gitignore
vendored
Normal file
2
.gitignore
vendored
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
laundry_notifier/laundry_notifier.conf
|
||||||
|
|
||||||
@ -1,4 +1,4 @@
|
|||||||
import configparse
|
import configparser
|
||||||
import math
|
import math
|
||||||
import smtplib
|
import smtplib
|
||||||
import time
|
import time
|
||||||
@ -21,9 +21,10 @@ Ianonavy Bot
|
|||||||
"""
|
"""
|
||||||
G_RANGE = 2
|
G_RANGE = 2
|
||||||
INTERVAL = 0.005 # seconds
|
INTERVAL = 0.005 # seconds
|
||||||
WINDOW_SIZE = 40 # intervals
|
WINDOW_SIZE = 400 # intervals
|
||||||
THRESHOLD = 0.003 # G's
|
THRESHOLD = 0.025 # G's
|
||||||
MAX_NOTIFICATION_FREQUENCY = 60 # seconds
|
MAX_NOTIFICATION_FREQUENCY = 60 # seconds
|
||||||
|
CONFIG_FILE_PATH = '/home/pi/laundry-notifier/laundry_notifier/laundry_notifier.conf'
|
||||||
|
|
||||||
|
|
||||||
def average(s):
|
def average(s):
|
||||||
@ -39,7 +40,7 @@ def stdev(s):
|
|||||||
|
|
||||||
|
|
||||||
def notify_user(recipient_email_address):
|
def notify_user(recipient_email_address):
|
||||||
print("Alerting " + recipient_email_address)
|
log("Alerting " + recipient_email_address)
|
||||||
|
|
||||||
msg = MIMEText(ALERT_EMAIL_TEXT)
|
msg = MIMEText(ALERT_EMAIL_TEXT)
|
||||||
|
|
||||||
@ -68,7 +69,7 @@ def enqueue(sliding_window, item):
|
|||||||
def amplitude_stdev(sliding_window):
|
def amplitude_stdev(sliding_window):
|
||||||
standard_deviations = {}
|
standard_deviations = {}
|
||||||
for key in ('x', 'y', 'z'):
|
for key in ('x', 'y', 'z'):
|
||||||
values = [instance[key] for instance in sliding_window]
|
values = [measurement[key] for measurement in sliding_window]
|
||||||
standard_deviations[key] = stdev(values)
|
standard_deviations[key] = stdev(values)
|
||||||
return standard_deviations
|
return standard_deviations
|
||||||
|
|
||||||
@ -77,19 +78,22 @@ def send_notifications(last_notification_sent_at, iftttkey):
|
|||||||
seconds_since_last_notification = \
|
seconds_since_last_notification = \
|
||||||
(datetime.now() - last_notification_sent_at).seconds
|
(datetime.now() - last_notification_sent_at).seconds
|
||||||
# Log the time notifications were sent
|
# Log the time notifications were sent
|
||||||
print(seconds_since_last_notification)
|
log("Sending notification after %ds" % seconds_since_last_notification)
|
||||||
# limit frequency of notifications
|
# limit frequency of notifications
|
||||||
if seconds_since_last_notification > MAX_NOTIFICATION_FREQUENCY:
|
if seconds_since_last_notification > MAX_NOTIFICATION_FREQUENCY:
|
||||||
[notify_user(email) for email in RECIPIENT_EMAILS]
|
[notify_user(email) for email in RECIPIENT_EMAILS]
|
||||||
# Notify if this, then that
|
# Notify if this, then that
|
||||||
requests.get('https://maker.ifttt.com/trigger/{event}/with/key/%s'
|
requests.get(
|
||||||
|
'https://maker.ifttt.com/trigger/laundry_done/with/key/%s'
|
||||||
% iftttkey)
|
% iftttkey)
|
||||||
return datetime.now()
|
return datetime.now()
|
||||||
return last_notification_sent_at
|
return last_notification_sent_at
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
config = configparser.ConfigParser().read('laundry_notifier.conf')
|
log('started laundry notifier')
|
||||||
|
config = configparser.ConfigParser()
|
||||||
|
config.read(CONFIG_FILE_PATH)
|
||||||
iftttkey = config['notifications']['ifttt_key']
|
iftttkey = config['notifications']['ifttt_key']
|
||||||
with MMA8452Q() as accelerometer:
|
with MMA8452Q() as accelerometer:
|
||||||
# Configure accelerometer
|
# Configure accelerometer
|
||||||
@ -108,20 +112,27 @@ def main():
|
|||||||
g_values = accelerometer.get_xyz()
|
g_values = accelerometer.get_xyz()
|
||||||
enqueue(sliding_window, g_values)
|
enqueue(sliding_window, g_values)
|
||||||
sliding_stdev = amplitude_stdev(sliding_window)
|
sliding_stdev = amplitude_stdev(sliding_window)
|
||||||
print(sliding_stdev, dryer_state) # for debugging
|
|
||||||
# don't send emails right at the beginning
|
|
||||||
if g_values:
|
if g_values:
|
||||||
if sliding_stdev['x'] < THRESHOLD:
|
if sliding_stdev['x'] < THRESHOLD:
|
||||||
|
# Notify recipients on state transitions from 'on' to 'off'
|
||||||
if dryer_state == 'on':
|
if dryer_state == 'on':
|
||||||
|
log('Dryer turned off; sliding stdev is %f' % sliding_stdev['x'])
|
||||||
last_notification_sent_at = send_notifications(
|
last_notification_sent_at = send_notifications(
|
||||||
last_notification_sent_at,
|
last_notification_sent_at,
|
||||||
iftttkey)
|
iftttkey)
|
||||||
dryer_state = 'off'
|
dryer_state = 'off'
|
||||||
else:
|
else:
|
||||||
|
# Log state transitions from 'off' to 'on'
|
||||||
|
if dryer_state == 'off':
|
||||||
|
log('Dryer turned on; sliding stdev is %f' % sliding_stdev['x'])
|
||||||
dryer_state = 'on'
|
dryer_state = 'on'
|
||||||
|
|
||||||
time.sleep(INTERVAL)
|
time.sleep(INTERVAL)
|
||||||
|
|
||||||
|
|
||||||
|
def log(message):
|
||||||
|
print('%s\t%s' % (datetime.isoformat(datetime.now()), message))
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
main()
|
main()
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user