SHARE
TWEET

RFID (Door Check)

Tempist May 23rd, 2019 (edited) 85 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # Remember you do need a 125kHz USB RFID Reader, Adafruit 12V DC 1000mA (1A) regulated switching power adapter, Parallax # 12V Relay Board, Electric Door Strike, Adafruit 5V 2.5A Switching Power Supply with 20AWG MicroUSB Cable and a        
  2. # Raspberry Pi 3 Model B   
  3.  
  4. # This will just check if door opens or not
  5. # Instagram: @ImWizardly
  6.  
  7.  
  8. from time import sleep
  9. import RPi.GPIO as GPIO
  10. import csv
  11. import datetime
  12. import sys
  13. import os
  14.  
  15. # Pin definition
  16. doorswitch = 4
  17. GPIO.setmode(GPIO.BCM)
  18. GPIO.setup(doorswitch, GPIO.IN)
  19.  
  20. # Define starting states
  21. door_is_open = False
  22. prior_state = False
  23.  
  24. # Open log file in append mode
  25. log_file = open('/home/pi/RFID/log-door.csv', 'a')
  26.  
  27. try:    ### Loop continuously
  28.  
  29.     while True:
  30.  
  31.         ### Check if the door is open
  32.         if GPIO.input(doorswitch)# If door is open, define door_is_open as True
  33.             door_is_open = True
  34.         else:               # if closed, define door_is_open as False
  35.             door_is_open = False
  36.  
  37.         ### Check if door status has changed
  38.         if door_is_open == prior_state: # If status hasn't changed, do nothing
  39.             pass
  40.  
  41.         else:               # If changed:
  42.                 time = datetime.datetime.now()              # deefine time
  43.             log_file = open('/home/pi/RFID/log-door.csv', 'a')  # Open log file in append mode
  44.  
  45.             if door_is_open == True:                # If door just opened:
  46.                 print('Door Opened')
  47.                 time_opened = time              # Define current time as last time opened
  48.                 duration_closed = time_opened - time_closed # Define duration since last opened
  49.                 duration_closed_string = str(duration_closed)[0:10]
  50.         #       log_file.write(time.strftime('%Y-%m-%d %H:%M:%S') + ', Door Opened \n')
  51.             else:                           # If door just closed:
  52.                 print('Door Closed')
  53.                 time_closed = time
  54.                 duration_open = time_closed - time_opened
  55.                 duration_open_string = str(duration_closed)[0:10]
  56.  
  57.                 log_file.write(time.strftime('%a %Y-%m-%d %H:%M:%S') + duration_closed_string + ', Door opened for:' + duration_open_string + '\n'
  58.         #       log_file.write(time.strftime('%Y-%m-%d %H:%M:%S') + ', Door Closed \n')
  59.             os.system('rclone copy /home/pi/RFID/log-door.csv door-log3:door-access')
  60.                 print('Backup complete')
  61.  
  62.             prior_state = door_is_open
  63.             log_file.close()
  64.  
  65.         sleep(0.1)
  66.  
  67. # When you press ctrl+c, this will be called
  68. finally:
  69.     GPIO.cleanup()
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top