Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python3
- # Code tested and is working as of 21/01/2022 for BP
- # SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
- # SPDX-License-Identifier: MIT
- """
- This example shows connecting to the PN532 with I2C (requires clock
- stretching support), SPI, or UART. SPI is best, it uses the most pins but
- is the most reliable and universally supported.
- After initialization, try waving various 13.56MHz RFID cards over it!
- """
- from pickle import FALSE
- import board
- import busio
- from digitalio import DigitalInOut
- import binascii
- #
- # NOTE: pick the import that matches the interface being used
- #
- from adafruit_pn532.i2c import PN532_I2C
- # from adafruit_pn532.spi import PN532_SPI
- # from adafruit_pn532.uart import PN532_UART
- # I2C connection:
- i2c = busio.I2C(board.SCL, board.SDA)
- # Non-hardware
- # pn532 = PN532_I2C(i2c, debug=False)
- # With I2C, we recommend connecting RSTPD_N (reset) to a digital pin for manual
- # harware reset
- reset_pin = DigitalInOut(board.D6)
- # On Raspberry Pi, you must also connect a pin to P32 "H_Request" for hardware
- # wakeup! this means we don't need to do the I2C clock-stretch thing
- req_pin = DigitalInOut(board.D12)
- pn532 = PN532_I2C(i2c, debug=False, reset=reset_pin, req=req_pin)
- ic, ver, rev, support = pn532.firmware_version
- print("Found PN532 with firmware version: {0}.{1}".format(ver, rev))
- # Configure PN532 to communicate with MiFare cards
- pn532.SAM_configuration()
- # Read and store the db in RAM
- byte_arr1 = []
- with open("rfidBinDatabase", "rb") as f:
- print('Opening the file!')
- byte = f.read(1)
- while byte:
- # Do stuff with byte.
- byte = f.read(1)
- byte_arr1.extend(bytes(byte))
- byte_arr = byte_arr1
- print('Byte arr of db is:',byte_arr)
- print("Waiting for RFID/NFC card...")
- prevUID = bytearray(b'3\x00\x00\x00')
- while True:
- # Check if a card is available to read
- uid = pn532.read_passive_target(timeout=3)
- print(".", end="")
- # Try again if no card is available.
- if uid is None:
- continue
- # print("Detected card with UID:", uid)
- # print("Same ID?",prevUID, uid)
- if prevUID == uid:
- # print("Same ID",prevUID, uid)
- continue
- # Log the card that is detected
- prevUID = uid
- file = open("rfidLog.txt", "ab")
- # Write bytes to file
- immutable_bytes = bytes(uid)
- file.write(immutable_bytes)
- file.close()
- # if (i+1) % 3:
- # print(byte_arr)
- # byte_arr.clear()
- print("Detected card with UID:", uid)
- print(set(immutable_bytes).issubset(byte_arr))
Add Comment
Please, Sign In to add comment