dhruvag2000

readAndVerify.py

Mar 23rd, 2022 (edited)
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.59 KB | None | 0 0
  1. #!/usr/bin/env python3
  2. # Code tested and is working as of 21/01/2022 for BP
  3. # SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
  4. # SPDX-License-Identifier: MIT
  5.  
  6. """
  7. This example shows connecting to the PN532 with I2C (requires clock
  8. stretching support), SPI, or UART. SPI is best, it uses the most pins but
  9. is the most reliable and universally supported.
  10. After initialization, try waving various 13.56MHz RFID cards over it!
  11. """
  12.  
  13. from pickle import FALSE
  14. import board
  15. import busio
  16. from digitalio import DigitalInOut
  17. import binascii
  18.  
  19. #
  20. # NOTE: pick the import that matches the interface being used
  21. #
  22. from adafruit_pn532.i2c import PN532_I2C
  23.  
  24. # from adafruit_pn532.spi import PN532_SPI
  25. # from adafruit_pn532.uart import PN532_UART
  26.  
  27. # I2C connection:
  28. i2c = busio.I2C(board.SCL, board.SDA)
  29.  
  30. # Non-hardware
  31. # pn532 = PN532_I2C(i2c, debug=False)
  32.  
  33. # With I2C, we recommend connecting RSTPD_N (reset) to a digital pin for manual
  34. # harware reset
  35. reset_pin = DigitalInOut(board.D6)
  36. # On Raspberry Pi, you must also connect a pin to P32 "H_Request" for hardware
  37. # wakeup! this means we don't need to do the I2C clock-stretch thing
  38. req_pin = DigitalInOut(board.D12)
  39. pn532 = PN532_I2C(i2c, debug=False, reset=reset_pin, req=req_pin)
  40.  
  41.  
  42. ic, ver, rev, support = pn532.firmware_version
  43. print("Found PN532 with firmware version: {0}.{1}".format(ver, rev))
  44.  
  45. # Configure PN532 to communicate with MiFare cards
  46. pn532.SAM_configuration()
  47.  
  48. # Read and store the db in RAM
  49. byte_arr1 = []
  50. with open("rfidBinDatabase", "rb") as f:
  51.     print('Opening the file!')
  52.     byte = f.read(1)
  53.     while byte:
  54.         # Do stuff with byte.
  55.         byte = f.read(1)
  56.         byte_arr1.extend(bytes(byte))
  57.  
  58. byte_arr = byte_arr1
  59. print('Byte arr of db is:',byte_arr)
  60.  
  61. print("Waiting for RFID/NFC card...")
  62. prevUID = bytearray(b'3\x00\x00\x00')
  63. while True:
  64.     # Check if a card is available to read
  65.     uid = pn532.read_passive_target(timeout=3)
  66.     print(".", end="")
  67.     # Try again if no card is available.
  68.     if uid is None:
  69.         continue
  70.     # print("Detected card with UID:", uid)
  71.     # print("Same ID?",prevUID, uid)
  72.     if prevUID == uid:
  73.         # print("Same ID",prevUID, uid)
  74.         continue
  75.     # Log the card that is detected
  76.     prevUID = uid
  77.     file = open("rfidLog.txt", "ab")
  78.     # Write bytes to file
  79.     immutable_bytes = bytes(uid)
  80.     file.write(immutable_bytes)
  81.     file.close()
  82.             # if (i+1) % 3:
  83.                 # print(byte_arr)
  84.                 # byte_arr.clear()
  85.     print("Detected card with UID:", uid)
  86.     print(set(immutable_bytes).issubset(byte_arr))
  87.  
Add Comment
Please, Sign In to add comment