forked from interaktivarum/nfc2server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnfc2server.py
executable file
·56 lines (42 loc) · 1.46 KB
/
nfc2server.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
56
#!/usr/bin/env python
import Adafruit_PN532
import binascii, sys, time, json
from functions import sendRequest
read_sleep = 0.1;
# Instantiate and configure the PN532
pn532 = Adafruit_PN532.PN532(cs=18, sclk=25, mosi=23, miso=24)
pn532.begin()
pn532.SAM_configuration()
#Read json
print("Data folder: " + sys.path[0])
with open(sys.path[0]+'/settings.json') as data_file:
settings = json.load(data_file)
#Static user data
ip = settings["server"]["ip"]
host = settings["server"]["host"]
endpoint = settings["server"]["endpoint"]
static_user_data = settings["staticUserData"];
#Tag uid variables
uid = None;
uid_last = uid;
# Main loop to detect cards and send server requests
print('Application running')
while True:
# Read card, if available
uid = pn532.read_passive_target()
#If tag uid has changed from previous iteration
if uid_last != uid:
# If old tag has been removed
if uid_last != None:
print('Removed card with UID: 0x{0}'.format(binascii.hexlify(uid_last)))
# Send HTTP request to server
sendRequest(ip,host,endpoint,static_user_data,uid_last,"remove")
# If new tag is detected
if uid != None:
print('Found card with UID: 0x{0}'.format(binascii.hexlify(uid)))
# Send HTTP request to server
sendRequest(ip,host,endpoint,static_user_data,uid,"touch")
#Update uid_last
uid_last = uid
time.sleep(read_sleep)
sys.exit(0)