"""Senses vibration and sends text after timeout""" import time import datetime import logging import RPi.GPIO as GPIO from twilio.rest import Client # pip3 install twilio #global vars BUTTON = 5 SENSOR = 25 LED = 13 WASHERLED = 6 INTERVAL = 5 # frequency to check STARTTIME = None DELTA = None WASHING = False TIMESTOPPED = False SMSNUMBERS = ["+1111111111", "+222222222222"] #setup pins GPIO.setwarnings(False) GPIO.setmode(GPIO.BCM) GPIO.setup(SENSOR, GPIO.IN) # setup vibration sensor GPIO.setup(LED, GPIO.OUT) # setup LED GPIO.setup(WASHERLED, GPIO.OUT) # setup WASHERLED GPIO.setup(BUTTON, GPIO.IN, pull_up_down=GPIO.PUD_DOWN) # pulldown to prevent bouncing # sensor up or down GPIO.add_event_detect(SENSOR, GPIO.RISING, bouncetime=200) # Configure debugging journal file on RPi logging.basicConfig(filename='/home/pi/washer.log', level=logging.INFO, filemode='w', format='%(asctime)s %(levelname)s:%(message)s') GPIO.output(LED, True) # LED off GPIO.output(WASHERLED, False) # LED on time.sleep(.5) GPIO.output(LED, False) # LED on GPIO.output(WASHERLED, True) # LED off time.sleep(.5) GPIO.output(LED, True) # LED off GPIO.output(WASHERLED, False) # LED on time.sleep(.5) GPIO.output(LED, False) # LED on GPIO.output(WASHERLED, True) # LED off time.sleep(.5) logging.info("*******************") logging.info("* System Loaded *") logging.info("*******************") print("*******************") print("* System Loaded *") print("*******************") # function to send SMS alert def sendalert(): """sends alerts via twilio""" account_sid = "xxxxxxxxx" auth_token = "yyyyyyyyy" client = Client(account_sid, auth_token) for i in SMSNUMBERS: client.messages.create( to=i, from_="+1333333333333", body="LAUNDRY COMPLETED") logging.info(" Alert attempted") print(" Alert attempted") def buttonpress(channel): """callback for button presses""" global WASHING #blink a few times GPIO.output(LED, True) # LED off time.sleep(.050) GPIO.output(LED, False) # LED on time.sleep(.050) # switch vars and send logs if GPIO.input(channel): logging.info("Button Pressed") print(" Button Pressed") if WASHING: WASHING = False GPIO.output(WASHERLED, True) # LED off logging.info(" Washing is stopping") print(" Washing is stopping after " + str(DELTA)) else: WASHING = True logging.info(" Washing is starting") print(" Washing is starting at " + str(datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S"))) GPIO.output(WASHERLED, False) # LED on # Tie callback to button events GPIO.add_event_detect(BUTTON, GPIO.BOTH, bouncetime=300) # let us know if pin goes high or low GPIO.add_event_callback(BUTTON, buttonpress) #assign function on change # main loop while True: # main loop time.sleep(INTERVAL) #keep CPU down while doing nothing while WASHING: GPIO.output(LED, True) # LED off time.sleep(1) # short pause GPIO.output(LED, False) # LED on time.sleep(1) # short pause # check status of vibration sensor if not GPIO.event_detected(SENSOR): # no motion if TIMESTOPPED: #logging.info(" No motion detected x" + str(STILLCOUNT)) DELTA = datetime.datetime.now() - STARTTIME # gives you a timedelta print(" No motion detected for " + str(DELTA)) else: TIMESTOPPED = True STARTTIME = datetime.datetime.now() else: # still washing STARTTIME = None DELTA = None TIMESTOPPED = False print(" Motion detected") if ((DELTA) and (DELTA > datetime.timedelta(minutes=12))): # end of wash cycle logging.info(" Sending alert") print(" Sending alert") sendalert() GPIO.output(LED, False) # LED on GPIO.output(WASHERLED, True) # WASHERLED off logging.info(" Exiting washing mode after " +str(DELTA) ) print(" Exiting washing mode after " + str(DELTA)) WASHING = False TIMESTOPPED = False STARTTIME = None DELTA = None