MicroPython_PN532/examples/pn532_simpletest.py

51 lines
1.7 KiB
Python
Raw Normal View History

2018-08-25 20:39:16 +00:00
"""
This example shows connecting to the PN532 with I2C (requires clock
stretching support), SPI, or UART. SPI is best, it uses the most pins but
is the most reliable and universally supported.
After initialization, try waving various 13.56MHz RFID cards over it!
"""
2018-08-25 05:45:47 +00:00
import board
import busio
2018-08-25 20:39:16 +00:00
from digitalio import DigitalInOut
2018-08-28 00:25:03 +00:00
import adafruit_pn532
2018-08-25 05:45:47 +00:00
2018-08-25 20:39:16 +00:00
# I2C connection:
i2c = busio.I2C(board.SCL, board.SDA)
2018-08-25 05:45:47 +00:00
2018-08-25 20:39:16 +00:00
# Non-hardware
#pn532 = adafruit_pn532.PN532_I2C(i2c, debug=False)
2018-08-25 05:45:47 +00:00
2018-08-25 20:39:16 +00:00
# With I2C, we recommend connecting RSTPD_N (reset) to a digital pin for manual
# harware reset
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
req_pin = DigitalInOut(board.D12)
pn532 = adafruit_pn532.PN532_I2C(i2c, debug=False, reset=reset_pin, req=req_pin)
2018-08-25 05:45:47 +00:00
# SPI connection:
#spi = busio.SPI(board.SCK, board.MOSI, board.MISO)
2018-08-25 20:39:16 +00:00
#cs_pin = DigitalInOut(board.D5)
#pn532 = adafruit_pn532.PN532_SPI(spi, cs_pin, debug=False)
2018-08-25 05:45:47 +00:00
# UART connection
#uart = busio.UART(board.TX, board.RX, baudrate=115200, timeout=100)
2018-08-25 20:39:16 +00:00
#pn532 = adafruit_pn532.PN532_UART(uart, debug=False)
2018-08-25 05:45:47 +00:00
ic, ver, rev, support = pn532.get_firmware_version()
print('Found PN532 with firmware version: {0}.{1}'.format(ver, rev))
2018-08-25 20:39:16 +00:00
# Configure PN532 to communicate with MiFare cards
2018-08-25 05:45:47 +00:00
pn532.SAM_configuration()
2018-08-25 20:39:16 +00:00
print('Waiting for RFID/NFC card...')
2018-08-25 05:45:47 +00:00
while True:
# Check if a card is available to read
2018-08-25 20:39:16 +00:00
uid = pn532.read_passive_target(timeout=0.5)
2018-08-25 05:45:47 +00:00
print('.', end="", flush=True)
# Try again if no card is available.
if uid is None:
continue
print('Found card with UID:', [hex(i) for i in uid])