doorman2/esp32/boot.py

54 lines
1.5 KiB
Python

from pn532 import PN532Uart
import utime
import machine
import hashlib
DEBUG = True
try:
door = machine.Pin(2, machine.Pin.OUT)
door.value(0)
uart = machine.UART(1, tx=16, rx=17, baudrate=9600)
uart.write("F")
rf = PN532Uart(2, tx=22, rx=19)
rf.SAM_configuration()
ic, ver, rev, support = rf.get_firmware_version()
print('Found PN532 with firmware version: {0}.{1}'.format(ver, rev))
except Exception as e:
rf = None
print('No NFC reader (PN532) detected')
while rf is not None:
try:
uid = rf.read_passive_target()
print("Card UUID: " + ''.join('{:02x}'.format(x) for x in uid))
card=''.join('{:02x}'.format(x) for x in uid)
uart.write("QG")
uart.flush()
c=0
buf=''
success = False
while True:
if uart.any():
c=c+1
buf+=str(chr(uart.read()[0]))
if c == 4:
break
hash=hashlib.sha256(str("{0:#0{1}x}".format(int(buf),10)+":"+card[6:8]+card[4:6]+card[2:4]+card[0:2])[2:].encode()).digest().hex()
f = open("hashes")
for user in f:
if(user.strip() == hash):
success = True
if(success):
uart.write("FH")
door.value(1)
utime.sleep(2)
door.value(0)
else:
uart.write("FS")
utime.sleep(2)
buf=''
except Exception as e:
print('timeout!')