MicroPython_PN532/examples/pn532_readwrite_ntag2xx.py

78 lines
2.2 KiB
Python
Raw Normal View History

2021-01-17 14:54:22 +00:00
# SPDX-FileCopyrightText: 2015 Tony DiCola and Roberto Laricchia for Adafruit Industries
2021-01-14 16:45:11 +00:00
# SPDX-License-Identifier: MIT
2018-08-25 21:07:50 +00:00
# Example of detecting and reading a block from a MiFare NFC card.
"""
This example shows connecting to the PN532 and writing & reading an ntag2xx
type RFID tag
"""
import board
import busio
from digitalio import DigitalInOut
2020-03-16 20:00:49 +00:00
2018-09-12 21:33:00 +00:00
#
# NOTE: pick the import that matches the interface being used
#
2020-03-16 20:00:49 +00:00
# from adafruit_pn532.i2c import PN532_I2C
2018-09-12 21:33:00 +00:00
from adafruit_pn532.spi import PN532_SPI
2020-03-16 20:00:49 +00:00
# from adafruit_pn532.uart import PN532_UART
2018-08-25 21:07:50 +00:00
# I2C connection:
2020-03-16 20:00:49 +00:00
# i2c = busio.I2C(board.SCL, board.SDA)
2018-08-25 21:07:50 +00:00
# Non-hardware reset/request with I2C
2020-03-16 20:00:49 +00:00
# pn532 = PN532_I2C(i2c, debug=False)
2018-08-25 21:07:50 +00:00
# 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)
2018-08-25 21:07:50 +00:00
# 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)
2018-08-25 21:07:50 +00:00
# SPI connection:
spi = busio.SPI(board.SCK, board.MOSI, board.MISO)
cs_pin = DigitalInOut(board.D5)
2018-09-12 21:33:00 +00:00
pn532 = PN532_SPI(spi, cs_pin, debug=False)
2018-08-25 21:07:50 +00:00
# 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)
2018-08-25 21:07:50 +00:00
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))
2018-08-25 21:07:50 +00:00
# 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!")
2018-08-25 21:07:50 +00:00
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="")
2018-08-25 21:07:50 +00:00
# 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])
2018-08-25 21:07:50 +00:00
# Set 4 bytes of block to 0xFEEDBEEF
data = bytearray(4)
2020-03-16 20:00:49 +00:00
data[0:4] = b"\xFE\xED\xBE\xEF"
2018-08-25 21:07:50 +00:00
# Write 4 byte block.
pn532.ntag2xx_write_block(6, data)
# Read block #6
ntag2xx_block = pn532.ntag2xx_read_block(6)
if ntag2xx_block is not None:
print(
"Wrote to block 6, now trying to read that data:",
2022-02-14 22:05:44 +00:00
[hex(x) for x in ntag2xx_block],
)
else:
print("Read failed - did you remove the card?")