Compare commits
7 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
f4caad52ff | ||
|
|
40a9e0f5f7 | ||
|
|
b26802003d | ||
|
|
daef848cc0 | ||
| 15de7719bf | |||
|
|
4ab145c62a | ||
|
|
b83c342a81 |
@ -1,4 +1,5 @@
|
|||||||
import configparser
|
import configparser
|
||||||
|
import logging
|
||||||
import math
|
import math
|
||||||
import smtplib
|
import smtplib
|
||||||
import time
|
import time
|
||||||
@ -9,8 +10,6 @@ import requests
|
|||||||
from microstacknode.hardware.accelerometer.mma8452q import MMA8452Q
|
from microstacknode.hardware.accelerometer.mma8452q import MMA8452Q
|
||||||
|
|
||||||
|
|
||||||
EMAIL_USERNAME = 'noreply@ianonavy.com'
|
|
||||||
EMAIL_PASSWORD = 'S2PENjQbO6=cHgchw@CXs.bJ'
|
|
||||||
RECIPIENT_EMAILS = ['ianonavy@gmail.com']
|
RECIPIENT_EMAILS = ['ianonavy@gmail.com']
|
||||||
ALERT_EMAIL_TEXT = """Hi,
|
ALERT_EMAIL_TEXT = """Hi,
|
||||||
|
|
||||||
@ -21,8 +20,9 @@ Ianonavy Bot
|
|||||||
"""
|
"""
|
||||||
G_RANGE = 2
|
G_RANGE = 2
|
||||||
INTERVAL = 0.005 # seconds
|
INTERVAL = 0.005 # seconds
|
||||||
WINDOW_SIZE = 400 # intervals
|
WINDOW_SIZE = 1000 # intervals
|
||||||
THRESHOLD = 0.025 # G's
|
THRESHOLD = 0.025 # G's
|
||||||
|
MAX_EMAIL_FREQUENCY = 60 # seconds
|
||||||
MAX_NOTIFICATION_FREQUENCY = 60 # seconds
|
MAX_NOTIFICATION_FREQUENCY = 60 # seconds
|
||||||
CONFIG_FILE_PATH = '/home/pi/laundry-notifier/laundry_notifier/laundry_notifier.conf'
|
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)))
|
return math.sqrt(average(list(variance)))
|
||||||
|
|
||||||
|
|
||||||
def notify_user(recipient_email_address):
|
def notify_user(username, password, recipient_email_address):
|
||||||
log("Alerting " + recipient_email_address)
|
logging.info("Alerting " + recipient_email_address)
|
||||||
|
|
||||||
msg = MIMEText(ALERT_EMAIL_TEXT)
|
msg = MIMEText(ALERT_EMAIL_TEXT)
|
||||||
|
|
||||||
@ -51,8 +51,6 @@ def notify_user(recipient_email_address):
|
|||||||
msg['To'] = recipient_email_address
|
msg['To'] = recipient_email_address
|
||||||
|
|
||||||
# Send the message via our own SMTP server.
|
# Send the message via our own SMTP server.
|
||||||
username = EMAIL_USERNAME
|
|
||||||
password = EMAIL_PASSWORD
|
|
||||||
server = smtplib.SMTP('smtp.gmail.com:587')
|
server = smtplib.SMTP('smtp.gmail.com:587')
|
||||||
server.starttls()
|
server.starttls()
|
||||||
server.login(username, password)
|
server.login(username, password)
|
||||||
@ -77,8 +75,7 @@ def amplitude_stdev(sliding_window):
|
|||||||
def send_notifications(last_notification_sent_at, iftttkey):
|
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
|
logging.info("Sending notification after %ds" % 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]
|
||||||
@ -91,10 +88,16 @@ def send_notifications(last_notification_sent_at, iftttkey):
|
|||||||
|
|
||||||
|
|
||||||
def main():
|
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 = configparser.ConfigParser()
|
||||||
config.read(CONFIG_FILE_PATH)
|
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:
|
with MMA8452Q() as accelerometer:
|
||||||
# Configure accelerometer
|
# Configure accelerometer
|
||||||
accelerometer.standby()
|
accelerometer.standby()
|
||||||
@ -116,23 +119,21 @@ def main():
|
|||||||
if sliding_stdev['x'] < THRESHOLD:
|
if sliding_stdev['x'] < THRESHOLD:
|
||||||
# Notify recipients on state transitions from 'on' to 'off'
|
# 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'])
|
logging.info('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,
|
||||||
|
email_username,
|
||||||
|
email_password,
|
||||||
iftttkey)
|
iftttkey)
|
||||||
dryer_state = 'off'
|
dryer_state = 'off'
|
||||||
else:
|
else:
|
||||||
# Log state transitions from 'off' to 'on'
|
# Log state transitions from 'off' to 'on'
|
||||||
if dryer_state == 'off':
|
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'
|
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