SHARE
TWEET

RFID Reader (Door Access)

Tempist May 23rd, 2019 (edited) 76 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. # Will give door access and will whitelist specific cards and ids
  5. # Instagram: @ImWizardly
  6.  
  7. import sys
  8. import csv
  9. import datetime
  10. import os
  11. import subprocess
  12. from gpiozero import LED
  13. from time import sleep
  14. import pigpio
  15. from RPLCD.pigpio import CharLCD
  16.  
  17. led = LED(17)
  18.  
  19. pi = pigpio.pi()
  20. lcd = CharLCD(pi,pin_rs=15, pin_rw=18, pin_e=16, pins_data=[21, 22, 23, 24],cols=20, rows=4, dotsize=8,charmap='A02',auto_linebreaks=True)
  21.  
  22.  
  23. ### Import whitelist
  24.  
  25. with open('/home/pi/RFID/keyed-members') as whitelist:
  26.     csv_reader = csv.reader(whitelist, delimiter=',')
  27.     line_count = 0
  28.     ID_list = []
  29.     names_list = []
  30.     nicknames_list = []
  31.     member_type = []
  32.     for row in csv_reader:
  33.         ID = row[0]
  34.         first_name = row[2]
  35.         last_name = row[1]
  36.         nickname = row[3]
  37.         member_type = row[4]
  38.         full_name = first_name+' '+last_name
  39.         ID_list.append(ID)
  40.         names_list.append(full_name)
  41.         nicknames_list.append(nickname)
  42.         member_type.append(member_type)
  43.     print(ID_list)
  44.  
  45.  
  46. whitelist = ''
  47.  
  48. ### Parse RFID input
  49.  
  50. hid = { 4: 'a', 5: 'b', 6: 'c', 7: 'd', 8: 'e', 9: 'f', 10: 'g', 11: 'h', 12: 'i', 13: 'j', 14: 'k', 15: 'l', 16: 'm', 17: 'n', 18: 'o', 19: 'p', 20: 'q', 21: 'r', 22: 's', 23: 't', 24: 'u', 25: 'v', 26: 'w', 27: 'x', 28: 'y', 29: 'z', 30: '1', 31: '2', 32: '3', 33: '4', 34: '5', 35: '6', 36: '7', 37: '8', 38: '9', 39: '0', 44: ' ', 45: '-', 46: '=', 47: '[', 48: ']', 49: '\\', 51: ';' , 52: '\'', 53: '~', 54: ',', 55: '.', 56: '/'  }
  51. hid2 = { 4: 'A', 5: 'B', 6: 'C', 7: 'D', 8: 'E', 9: 'F', 10: 'G', 11: 'H', 12: 'I', 13: 'J', 14: 'K', 15: 'L', 16: 'M', 17: 'N', 18: 'O', 19: 'P', 20: 'Q', 21: 'R', 22: 'S', 23: 'T', 24: 'U', 25: 'V', 26: 'W', 27: 'X', 28: 'Y', 29: 'Z', 30: '!', 31: '@', 32: '#', 33: '$', 34: '%', 35: '^', 36: '&', 37: '*', 38: '(', 39: ')', 44: ' ', 45: '_', 46: '+', 47: '{', 48: '}', 49: '|', 51: ':' , 52: '"', 53: '~', 54: '<', 55: '>', 56: '?'  }
  52. fp = open('/dev/hidraw0', 'rb')
  53. ss = ""
  54. shift = False
  55. done = False
  56. while True:
  57.     while not done:         ## Get the character from the HID
  58.         buffer = fp.read(8)
  59.         for c in buffer:
  60.             if ord(c) > 0##  40 is carriage return which signifies we are done looking for characters
  61.                 if int(ord(c)) == 40:
  62.                     done = True
  63.                     break;  ##  If we are shifted then we have to  use the hid2 characters.
  64.                 if shift:   ## If it is a '2' then it is the shift key
  65.                     if int(ord(c)) == 2 :
  66.                         shift = True
  67.                     else:   # if not a 2, lookup mapping
  68.                         ss += hid2[ int(ord(c)) ]
  69.                         shift = False
  70.                 else:       # if not shifted, use the hid characters
  71.                     if int(ord(c)) == 2 :   # if 2, then it is the shift key
  72.                         shift = True
  73.                     else:   # If not a 2, lookup mapping
  74.                         ss += hid[ int(ord(c)) ]
  75.  
  76.     ### When an RFID is detected:
  77.     print ss                # Print it
  78.     time = datetime.datetime.now()      # Define the current time
  79.  
  80.     ### Check whitelist
  81.     if ID_list.count(ss) > 0:       # If the ID appears on the white list:
  82.  
  83.         ### Check the membership type
  84.         pos = ID_list.index(ss)
  85.         if member_type[pos] == 'keyed_membership'# If keyed, open
  86.  
  87.             print('Welcome ' + nicknames_list[pos])
  88.     #       lcd.write_string('Access Granted')
  89.             led.on()
  90.             sleep(2)
  91.             led.off()
  92.  
  93.             log_file = open('/home/pi/RFID/log-door.csv', 'a')
  94.             log_file.write(time.strftime('%Y-%m-%d %H:%M%S') + ',' + ss ++ ',' + names_list[pos] + ',Approved \n')
  95.             log_file.close()
  96.  
  97.         if member_type[pos] == 'standard_membership':   # If standard, check the time
  98.  
  99.             if time_open < time < time_close:   # If within open hours, open
  100.  
  101.                 print('Welcome ' + nicknames_list[pos])
  102.     #           lcd.write_string('Access Granted')
  103.                 led.on()
  104.                 sleep(2)
  105.                 led.off()
  106.  
  107.             else:                   # If outside open hours, output to LCD
  108.                 print('Please return during open hours')
  109.     #           lcd.write_string('Return after 4')
  110.                 log_file = open('/home/pi/RFID/log-door.csv','a')
  111.                 log_file.write(time.strftime('%Y-%m-%d %H:%M:%S') + ',' + ss + ',' + names_list[pos] + ',Wrong time \n')
  112.                 log_file.close()
  113.  
  114.     ### If the user isn't found:
  115.     if ID_list.count(ss) == 0:
  116.         print('User not found.  Access Denied')
  117.         #lcd.write_string('Access DENIED')
  118.         log_file = open('/home/pi/RFID/log-door.csv', 'a')
  119.         log_file.write(time.strftime('%Y-%m-%d %H:%M:%S') + ',' + ss + ', Unknown (ACCESS DENIED) \n')
  120.         log_file.close()
  121.  
  122.     ### After completing checks, backup the new log file
  123.     os.system('rclone copy /home/pi/RFID/log-door.csv door-log3:door-access')
  124.     print('Backup complete')
  125.  
  126.     ss = ''
  127.     done = False
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