MicroPython_PN532/examples/pn532_readwrite_mifare.py

88 lines
2.5 KiB
Python
Raw Permalink Normal View History

2021-01-19 00:02:36 +00:00
# SPDX-FileCopyrightText: <text> 2015 Tony DiCola, Roberto Laricchia,
# and Francesco Crisafulli, for Adafruit Industries </text>
2021-01-14 16:45:11 +00:00
# SPDX-License-Identifier: MIT
# Example of detecting and reading a block from a MiFare classic NFC card.
"""
This example shows connecting to the PN532 and writing & reading a mifare classic
type RFID tag
"""
import board
import busio
2020-03-16 20:00:49 +00:00
2019-08-28 02:52:28 +00:00
# Additional import needed for I2C/SPI
2020-03-16 20:00:49 +00:00
# from digitalio import DigitalInOut
#
# NOTE: pick the import that matches the interface being used
#
2019-05-07 17:43:54 +00:00
from adafruit_pn532.adafruit_pn532 import MIFARE_CMD_AUTH_B
from adafruit_pn532.i2c import PN532_I2C
2020-03-16 20:00:49 +00:00
# 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 reset/request with I2C
pn532 = PN532_I2C(i2c, debug=False)
# With I2C, we recommend connecting RSTPD_N (reset) to a digital pin for manual
# harware reset
2020-03-16 20:00:49 +00:00
# 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
2020-03-16 20:00:49 +00:00
# req_pin = DigitalInOut(board.D12)
# pn532 = PN532_I2C(i2c, debug=False, reset=reset_pin, req=req_pin)
# SPI connection:
2020-03-16 20:00:49 +00:00
# spi = busio.SPI(board.SCK, board.MOSI, board.MISO)
# cs_pin = DigitalInOut(board.D5)
# pn532 = PN532_SPI(spi, cs_pin, debug=False)
# UART connection
2023-02-23 20:35:27 +00:00
# uart = busio.UART(board.TX, board.RX, baudrate=115200, timeout=0.1)
2020-03-16 20:00:49 +00:00
# pn532 = PN532_UART(uart, debug=False)
ic, ver, rev, support = pn532.firmware_version
2020-03-17 19:40:30 +00:00
print("Found PN532 with firmware version: {0}.{1}".format(ver, rev))
# Configure PN532 to communicate with MiFare cards
pn532.SAM_configuration()
2020-03-16 20:00:49 +00:00
print("Waiting for RFID/NFC card to write to!")
2020-03-16 20:00:49 +00:00
key = b"\xFF\xFF\xFF\xFF\xFF\xFF"
while True:
# Check if a card is available to read
uid = pn532.read_passive_target(timeout=0.5)
2020-03-16 20:00:49 +00:00
print(".", end="")
# Try again if no card is available.
if uid is not None:
break
print("")
2020-03-16 20:00:49 +00:00
print("Found card with UID:", [hex(i) for i in uid])
print("Authenticating block 4 ...")
2019-05-07 17:43:54 +00:00
authenticated = pn532.mifare_classic_authenticate_block(uid, 4, MIFARE_CMD_AUTH_B, key)
if not authenticated:
print("Authentication failed!")
# Set 16 bytes of block to 0xFEEDBEEF
data = bytearray(16)
2020-03-16 20:00:49 +00:00
data[0:16] = b"\xFE\xED\xBE\xEF\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
# Write 16 byte block.
pn532.mifare_classic_write_block(4, data)
# Read block #6
2020-03-16 20:00:49 +00:00
print(
"Wrote to block 4, now trying to read that data:",
[hex(x) for x in pn532.mifare_classic_read_block(4)],
)