-
Notifications
You must be signed in to change notification settings - Fork 3
/
rfid-access-control.py
55 lines (48 loc) · 2.12 KB
/
rfid-access-control.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import sys, os, time
from OmegaExpansion import oledExp
from OmegaExpansion import relayExp
import subprocess
import json
#initializing the Relay and Oled Expansions, and block the lock if it's open
def initial_setup():
status_oled = oledExp.driverInit()
status_relay = relayExp.driverInit(7) # 7 is the address of the Relay Expansion; 7 is when all relay switches are switched OFF
check = relayExp.readChannel(7, 0) # (7, 0) - again 7 is the address and 0 is the relay channel
if check == 1:
close_lock()
with open('data.json') as json_file: # Your UIDs could be different, make changes to the data.json file according to your settings
data = json.load(json_file)
return data['accepted']
return None
#close the lock
def close_lock():
relayExp.setChannel(7,0,0) # Setting Relay with address 7, channel 0 and state is 0 (OFF)
return
#open the lock
def open_lock():
relayExp.setChannel(7,0,1) # # Setting Relay with address 7, channel 0 and state is 1 (ON)
return
#function to access the lock for 5 seconds, print the output on the OLED expansion and close the lock
def access_lock(uid):
oledExp.write("Your UID is: " + uid)
relayExp.setChannel(7,0,1)
time.sleep(5)
relayExp.setChannel(7,0,0)
oledExp.clear()
return
#constantly scan for the rfid tag presence
def __main__():
# perform initial setup and read list of accepted tag uids from json file
acceptedUids = initial_setup()
if acceptedUids is None:
print 'ERROR: could not read accepted UIDs from file'
sys.exit(1)
while 1:
#assign a system call to a variable cmd and use it in uid subprocess
cmd = "nfc-list | grep UID | sed -e 's/ //g' -e 's/^.*://'"
uid = subprocess.check_output(cmd, shell=True).rstrip('\n')
for acceptedUid in acceptedUids:
if(acceptedUid == uid):
access_lock(uid)
if __name__ == '__main__':
__main__()