/ examples / pn532_readwrite_mifare.py
pn532_readwrite_mifare.py
  1  # Example of detecting and reading a block from a MiFare classic NFC card.
  2  # Author: Tony DiCola & Roberto Laricchia
  3  # MiFare Classic modification: Francesco Crisafulli
  4  #
  5  # Copyright (c) 2015 Adafruit Industries
  6  #
  7  # Permission is hereby granted, free of charge, to any person obtaining a copy
  8  # of this software and associated documentation files (the "Software"), to deal
  9  # in the Software without restriction, including without limitation the rights
 10  # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 11  # copies of the Software, and to permit persons to whom the Software is
 12  # furnished to do so, subject to the following conditions:
 13  #
 14  # The above copyright notice and this permission notice shall be included in all
 15  # copies or substantial portions of the Software.
 16  #
 17  # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 18  # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 19  # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 20  # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 21  # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 22  # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 23  # SOFTWARE.
 24  
 25  """
 26  This example shows connecting to the PN532 and writing & reading a mifare classic
 27  type RFID tag
 28  """
 29  
 30  import board
 31  import busio
 32  
 33  # Additional import needed for I2C/SPI
 34  # from digitalio import DigitalInOut
 35  #
 36  # NOTE: pick the import that matches the interface being used
 37  #
 38  from adafruit_pn532.adafruit_pn532 import MIFARE_CMD_AUTH_B
 39  from adafruit_pn532.i2c import PN532_I2C
 40  
 41  # from adafruit_pn532.spi import PN532_SPI
 42  # from adafruit_pn532.uart import PN532_UART
 43  
 44  # I2C connection:
 45  i2c = busio.I2C(board.SCL, board.SDA)
 46  
 47  # Non-hardware reset/request with I2C
 48  pn532 = PN532_I2C(i2c, debug=False)
 49  
 50  # With I2C, we recommend connecting RSTPD_N (reset) to a digital pin for manual
 51  # harware reset
 52  # reset_pin = DigitalInOut(board.D6)
 53  # On Raspberry Pi, you must also connect a pin to P32 "H_Request" for hardware
 54  # wakeup! this means we don't need to do the I2C clock-stretch thing
 55  # req_pin = DigitalInOut(board.D12)
 56  # pn532 = PN532_I2C(i2c, debug=False, reset=reset_pin, req=req_pin)
 57  
 58  # SPI connection:
 59  # spi = busio.SPI(board.SCK, board.MOSI, board.MISO)
 60  # cs_pin = DigitalInOut(board.D5)
 61  # pn532 = PN532_SPI(spi, cs_pin, debug=False)
 62  
 63  # UART connection
 64  # uart = busio.UART(board.TX, board.RX, baudrate=115200, timeout=100)
 65  # pn532 = PN532_UART(uart, debug=False)
 66  
 67  ic, ver, rev, support = pn532.firmware_version
 68  print("Found PN532 with firmware version: {0}.{1}".format(ver, rev))
 69  
 70  # Configure PN532 to communicate with MiFare cards
 71  pn532.SAM_configuration()
 72  
 73  print("Waiting for RFID/NFC card to write to!")
 74  
 75  key = b"\xFF\xFF\xFF\xFF\xFF\xFF"
 76  
 77  while True:
 78      # Check if a card is available to read
 79      uid = pn532.read_passive_target(timeout=0.5)
 80      print(".", end="")
 81      # Try again if no card is available.
 82      if uid is not None:
 83          break
 84  
 85  print("")
 86  
 87  print("Found card with UID:", [hex(i) for i in uid])
 88  print("Authenticating block 4 ...")
 89  
 90  authenticated = pn532.mifare_classic_authenticate_block(uid, 4, MIFARE_CMD_AUTH_B, key)
 91  if not authenticated:
 92      print("Authentication failed!")
 93  
 94  # Set 16 bytes of block to 0xFEEDBEEF
 95  data = bytearray(16)
 96  data[0:16] = b"\xFE\xED\xBE\xEF\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
 97  
 98  # Write 16 byte block.
 99  pn532.mifare_classic_write_block(4, data)
100  # Read block #6
101  print(
102      "Wrote to block 4, now trying to read that data:",
103      [hex(x) for x in pn532.mifare_classic_read_block(4)],
104  )