Advertisement
EmptySet5150

Raspberry Pi - Python - SQLite - Door Alarm

Jun 4th, 2017
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.55 KB | None | 0 0
  1. import sqlite3
  2. import RPi.GPIO as GPIO
  3. from time import time
  4. from time import sleep
  5. from datetime import datetime
  6.  
  7. GPIO.setmode(GPIO.BOARD)
  8. doorSwitch = 11
  9. buzzer = 15
  10. dbConn = sqlite3.connect('door.db')
  11. dbC = dbConn.cursor()
  12.  
  13. GPIO.setup(doorSwitch, GPIO.IN, pull_up_down=GPIO.PUD_UP)
  14. GPIO.setup(buzzer, GPIO.OUT)
  15. GPIO.output(buzzer, 0)
  16.  
  17. doorStatus = 0
  18. openTime = 0
  19. dbTimeStamp = datetime.now().strftime('%y/%m/%d %H:%M:%S')
  20.  
  21.  
  22. def saveDB(time, status):
  23.     dbC.execute('INSERT INTO RDStatus (DateTime, Status) VALUES (?, ?)', (time, status))
  24.     dbConn.commit()
  25.    
  26.  
  27. dbC.execute('CREATE TABLE IF NOT EXISTS RDStatus(DateTime DATETIME, Status TEXT)')
  28.  
  29.  
  30. try:
  31.     while True:
  32.         if GPIO.input(doorSwitch) == 1 and doorStatus == 0:
  33.             dbTimeStamp = datetime.now().strftime('%y/%m/%d %H:%M:%S')
  34.             openTime = time()
  35.             doorStatus = 1
  36.             print("Door is Open")
  37.             saveDB(dbTimeStamp, "Open")
  38.         if GPIO.input(doorSwitch) == 1 and doorStatus == 1:
  39.             if time() - openTime >= 120:
  40.                 print("Open 2 minutes")
  41.                 GPIO.output(buzzer, 1)
  42.                 sleep(5)
  43.                 GPIO.output(buzzer, 0)
  44.                 sleep(1)
  45.             if time() - openTime >= 30 and time() - openTime < 120:
  46.                 print("Open 30 seconds")
  47.                 GPIO.output(buzzer, 1)
  48.                 sleep(1)
  49.                 GPIO.output(buzzer, 0)
  50.                 sleep(3)
  51.         if GPIO.input(doorSwitch) == 0 and doorStatus == 1:
  52.             dbTimeStamp = datetime.now().strftime('%y/%m/%d %H:%M:%S')
  53.             doorStatus = 0
  54.             GPIO.output(buzzer, 0)
  55.             print("Door is Closed")
  56.             saveDB(dbTimeStamp, "Closed")
  57.  
  58.  
  59. except KeyboardInterrupt:
  60.     GPIO.cleanup()
  61.     dbC.close()
  62.     dbConn.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement