Advertisement
dhruvag2000

RFID_RT_HMI.py

Mar 26th, 2022
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.12 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. from multiprocessing import Process    # for parallel processing
  19.  
  20. #
  21. # NOTE: pick the import that matches the interface being used
  22. #
  23. from adafruit_pn532.i2c import PN532_I2C
  24.  
  25. # from adafruit_pn532.spi import PN532_SPI
  26. # from adafruit_pn532.uart import PN532_UART
  27.  
  28. # I2C connection:
  29. i2c = busio.I2C(board.SCL, board.SDA)
  30.  
  31. # Non-hardware
  32. # pn532 = PN532_I2C(i2c, debug=False)
  33.  
  34. # With I2C, we recommend connecting RSTPD_N (reset) to a digital pin for manual
  35. # harware reset
  36. reset_pin = DigitalInOut(board.D6)
  37. # On Raspberry Pi, you must also connect a pin to P32 "H_Request" for hardware
  38. # wakeup! this means we don't need to do the I2C clock-stretch thing
  39. req_pin = DigitalInOut(board.D12)
  40. pn532 = PN532_I2C(i2c, debug=False, reset=reset_pin, req=req_pin)
  41.  
  42.  
  43. ic, ver, rev, support = pn532.firmware_version
  44. print("Found PN532 with firmware version: {0}.{1}".format(ver, rev))
  45.  
  46. # Configure PN532 to communicate with MiFare cards
  47. pn532.SAM_configuration()
  48.  
  49. # Read and store the db in RAM
  50. byte_arr1 = []
  51. with open("rfidBinDatabase", "rb") as f:
  52.     print('Opening the file!')
  53.     byte = f.read(1)
  54.     while byte:
  55.         # Do stuff with byte.
  56.         byte = f.read(1)
  57.         byte_arr1.extend(bytes(byte))
  58.  
  59. byte_arr = byte_arr1
  60. print('Byte arr of db is:',byte_arr)
  61.  
  62. def ScanCard():
  63.     print("Waiting for RFID/NFC card...")
  64.     prevUID = bytearray(b'3\x00\x00\x00')
  65.     while True:
  66.         # Check if a card is available to read
  67.         uid = pn532.read_passive_target(timeout=3)
  68.         print(".", end="")
  69.         # Try again if no card is available.
  70.         if uid is None:
  71.             continue
  72.         # print("Detected card with UID:", uid)
  73.         # print("Same ID?",prevUID, uid)
  74.         if prevUID == uid:
  75.             # print("Same ID",prevUID, uid)
  76.             continue
  77.         # Log the card that is detected
  78.         prevUID = uid
  79.         file = open("rfidLog.txt", "ab")
  80.         # Write bytes to file
  81.         immutable_bytes = bytes(uid)
  82.         file.write(immutable_bytes)
  83.         file.close()
  84.                 # if (i+1) % 3:
  85.                     # print(byte_arr)
  86.                     # byte_arr.clear()
  87.         print("Detected card with UID:", uid)
  88.         print(set(immutable_bytes).issubset(byte_arr))
  89.  
  90. def HMIDisplay():
  91.     print("HMI Display STARTED\n")
  92.     while True:
  93.         print("Some info\n")
  94.         for i in range(10000000):
  95.             for i in range(10000000):
  96.                 pass
  97.  
  98.  
  99. if __name__ == '__main__':
  100.   p1 = Process(target=ScanCard)
  101.   p1.start()
  102.   p2 = Process(target=HMIDisplay)
  103.   p2.start()
  104.   p1.join()
  105.   p2.join()
  106.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement