-
Notifications
You must be signed in to change notification settings - Fork 0
/
tag.py
39 lines (35 loc) · 910 Bytes
/
tag.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
#!/opt/kasse/venv/bin/python3
from pn532pi import Pn532I2c, Pn532, pn532
from hashlib import md5
class NFCtag:
def __init__(self, port = 1, uid_hash = True):
self.i2c = Pn532I2c(port)
self.nfc = Pn532(self.i2c)
self.nfc.begin()
self.nfc.setPassiveActivationRetries(0xFF)
self.nfc.SAMConfig()
self.uid_hash = uid_hash
"""Check if there is a tag available and return hash if present
"""
def get(self):
success, uid = self.nfc.readPassiveTargetID(pn532.PN532_MIFARE_ISO14443A_106KBPS)
if (success):
if self.uid_hash:
return md5(uid).hexdigest()
else:
tag_uid = 0
for i in uid:
tag_uid <<= 8
tag_uid += i
return tag_uid
else:
return None
if __name__ == '__main__':
oldTag = None
nfctag = NFCtag(uid_hash = False)
while True:
tag = nfctag.get()
if tag != oldTag:
oldTag = tag
if tag is not None:
print("Found new Tag, Hash is %s" % tag)