Advertisement
EmptySet5150

Raspberry Pi - Python - SQLite - Twilio - Door Alarm

Jun 5th, 2017
152
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.88 KB | None | 0 0
  1. # Import some needed libaries
  2. import sqlite3
  3. import RPi.GPIO as GPIO
  4. from twilio.rest import Client # For sms
  5. from time import time
  6. from time import sleep
  7. from datetime import datetime
  8.  
  9.  
  10. GPIO.setmode(GPIO.BOARD) # Set to board numbering of the GPIO pins
  11. doorSwitch = 11
  12. buzzer = 15
  13. dbConn = sqlite3.connect('door.db') # Database file
  14. dbC = dbConn.cursor() # Database cursor
  15. ACCOUNT_SID = "[Get your own at twilio.com]" # Account info from twilio
  16. AUTH_TOKEN = "[Get your own at twilio.com]" # Account info from twilio
  17.  
  18. # Setup the GPIO pins
  19. GPIO.setup(doorSwitch, GPIO.IN, pull_up_down=GPIO.PUD_UP) # Use the pull up resistor on the board
  20. GPIO.setup(buzzer, GPIO.OUT)
  21. GPIO.output(buzzer, 0) # Make sure the buzzer is off
  22.  
  23. doorStatus = 0
  24. openTime = 0
  25. dbTimeStamp = datetime.now().strftime('%y/%m/%d %H:%M:%S')
  26. lastSend = 3600 # Used as a place holder
  27. smsMsg = Client(ACCOUNT_SID, AUTH_TOKEN) # Used for twilio
  28.  
  29.  
  30. # Connect to the database and insert time and status values. Then commit the changes
  31. def saveDB(time, status):
  32.     dbC.execute('INSERT INTO RDStatus (DateTime, Status) VALUES (?, ?)', (time, status))
  33.     dbConn.commit()
  34.  
  35.  
  36. # If the door is open for more than two minutes and I have not been notified
  37. # about it in the last hour send a txt message
  38. def sendSMS():
  39.     global lastSend
  40.     sendOK = time() - lastSend
  41.     if sendOK >= 3600:
  42.         print("Sending SMS Message")
  43.         lastSend = time()
  44.         smsMsg.messages.create(to="+15555555555", from_="+15555555555", body="Door is open!")
  45.  
  46. # If the table does not exist in the database create it
  47. dbC.execute('CREATE TABLE IF NOT EXISTS RDStatus(DateTime DATETIME, Status TEXT)')
  48.  
  49.  
  50. try:
  51.     while True:
  52. #       Check to see if the GPIO pin is high and the door was closed
  53.         if GPIO.input(doorSwitch) == 1 and doorStatus == 0:
  54.             dbTimeStamp = datetime.now().strftime('%y/%m/%d %H:%M:%S')
  55.             openTime = time()
  56.             doorStatus = 1
  57.             saveDB(dbTimeStamp, "Open") # Save to database
  58. #       Check to see if the GPIO pin is high and the door was open
  59.         if GPIO.input(doorSwitch) == 1 and doorStatus == 1:
  60. #           If the door has been open two minutes send a text message and sound the buzzer
  61.             if time() - openTime >= 120:
  62.                 sendSMS()
  63.                 GPIO.output(buzzer, 1)
  64.                 sleep(5)
  65.                 GPIO.output(buzzer, 0)
  66.                 sleep(1)
  67. #           If the door has been open thirty seconds sound the buzzer          
  68.             if time() - openTime >= 30 and time() - openTime < 120:
  69.                 GPIO.output(buzzer, 1)
  70.                 sleep(1)
  71.                 GPIO.output(buzzer, 0)
  72.                 sleep(3)
  73. #       Check to see if the GPIO pin is low and the door was open      
  74.         if GPIO.input(doorSwitch) == 0 and doorStatus == 1:
  75.             dbTimeStamp = datetime.now().strftime('%y/%m/%d %H:%M:%S')
  76.             doorStatus = 0
  77.             GPIO.output(buzzer, 0)
  78.             saveDB(dbTimeStamp, "Closed") # Save to database
  79.  
  80.  
  81. # On keyboard interrupt cleanup the GPIO pins and close the connection to the database
  82. except KeyboardInterrupt:
  83.     GPIO.cleanup()
  84.     dbC.close()
  85.     dbConn.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement