MicroPython with adafruit-circuitpython-pn532 / CircuitPython on ESP32

All ESP32 boards running MicroPython.
Target audience: MicroPython users with an ESP32 board.
Post Reply
rena2019
Posts: 8
Joined: Sun Sep 29, 2019 3:00 pm

MicroPython with adafruit-circuitpython-pn532 / CircuitPython on ESP32

Post by rena2019 » Sun Oct 27, 2019 3:47 pm

Hi all,
I want to use micropython with PN532. I found the adafruit-circuitpython-pn532 lib @ adafruit and found out that the current version of circuitpython does not run on ESP32 anymore. Now I have the following questions:
1) Is it somehow possible to patch and run the current version of CircuitPython on ESP32?
2) Is it possible to run the libraries from adafruit with MicroPython?

User avatar
Roberthh
Posts: 3667
Joined: Sat May 09, 2015 4:13 pm
Location: Rhineland, Europe

Re: MicroPython with adafruit-circuitpython-pn532 / CircuitPython on ESP32

Post by Roberthh » Sun Oct 27, 2019 4:01 pm

Option 2) seems to be the easy way. Looking into the driver code, you just have to adapt the calls for GPIO and SPI, and maybe some delay calls (e.g. a busy_wait functions). I have ported adafruit libraries a few times, and it was not difficult.

cgglzpy
Posts: 47
Joined: Thu Jul 18, 2019 4:20 pm

Re: MicroPython with adafruit-circuitpython-pn532 / CircuitPython on ESP32

Post by cgglzpy » Sun Oct 27, 2019 5:36 pm

Hi, I've actually done this (option 2) but I forgot to post that to the drivers forum section.

Here is the repo: https://github.com/Carglglz/NFC_PN532_SPI

:)

rena2019
Posts: 8
Joined: Sun Sep 29, 2019 3:00 pm

Re: MicroPython with adafruit-circuitpython-pn532 / CircuitPython on ESP32

Post by rena2019 » Sun Oct 27, 2019 7:05 pm

cgglzpy wrote:
Sun Oct 27, 2019 5:36 pm
Hi, I've actually done this (option 2) but I forgot to post that to the drivers forum section.

Here is the repo: https://github.com/Carglglz/NFC_PN532_SPI

:)
I just tried your code without luck:
>> import NFC_PN532 as nfc
>>> from machine import Pin, SPI
>>> spi_dev = SPI(1, baudrate=1000000)
>>> cs = Pin(5, Pin.OUT)
>>> cs.on()
>>> pn532 = nfc.PN532(spi_dev,cs)
>>> ic, ver, rev, support = pn532.get_firmware_version()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "NFC_PN532.py", line 294, in get_firmware_version
RuntimeError: Failed to detect the PN532

Do you know if it also work with Elechouse board also known as "PN532 NFC-RFID V3 NFC-Modul" cheap board that is available @ ebay for $5)?
Is it possible to configure the SPI Pins?
#define PN532_SCK (18)
#define PN532_MOSI (23)
#define PN532_SS (5)
#define PN532_MISO (19)

rena2019
Posts: 8
Joined: Sun Sep 29, 2019 3:00 pm

Re: MicroPython with adafruit-circuitpython-pn532 / CircuitPython on ESP32

Post by rena2019 » Sun Oct 27, 2019 7:12 pm

RTFM helps ;-)

>>> import NFC_PN532 as nfc
>>> from machine import Pin, SPI
>>> spi_dev = SPI(2, baudrate=1000000, sck=Pin(18), mosi=Pin(23), miso=Pin(19))
>>> cs = Pin(5, Pin.OUT)
>>> cs.on()
>>> pn532 = nfc.PN532(spi_dev,cs)
>>> ic, ver, rev, support = pn532.get_firmware_version()
>>> print('Found PN532 with firmware version: {0}.{1}'.format(ver, rev))
Found PN532 with firmware version: 1.6

User avatar
Roberthh
Posts: 3667
Joined: Sat May 09, 2015 4:13 pm
Location: Rhineland, Europe

Re: MicroPython with adafruit-circuitpython-pn532 / CircuitPython on ESP32

Post by Roberthh » Sun Oct 27, 2019 7:13 pm

You can assign the Pin as you like. See http://docs.micropython.org/en/latest/l ... hlight=spi
To match with the defines you referenced, it would for instance be:

Code: Select all

spi_dev = SPI(2, baudrate=1000000, sck=Pin(18), mosi=Pin(23), miso=Pin(19))
cs = Pin(5, Pin.OUT) 

User avatar
Roberthh
Posts: 3667
Joined: Sat May 09, 2015 4:13 pm
Location: Rhineland, Europe

Re: MicroPython with adafruit-circuitpython-pn532 / CircuitPython on ESP32

Post by Roberthh » Sun Oct 27, 2019 7:17 pm

@rena2019 To be honest, the manual is confusing, as it mixes information for different boards, which not work on all boards.

rena2019
Posts: 8
Joined: Sun Sep 29, 2019 3:00 pm

Re: MicroPython with adafruit-circuitpython-pn532 / CircuitPython on ESP32

Post by rena2019 » Sun Oct 27, 2019 7:29 pm

@roberthh thanks for the reply. The first command pn532.get_firmware_version() works now for me. See also my previous post.

I think I now have to modify some "delay calls" because the communication seems to be instable:

>>> print(pn532.read_passive_target(timeout=500))
None
>>> print(pn532.read_passive_target(timeout=500))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "NFC_PN532.py", line 320, in read_passive_target
File "NFC_PN532.py", line 316, in read_passive_target
File "NFC_PN532.py", line 280, in call_function
File "NFC_PN532.py", line 248, in _read_frame
RuntimeError: ('Response checksum did not match expected value: ', 36)

cgglzpy
Posts: 47
Joined: Thu Jul 18, 2019 4:20 pm

Re: MicroPython with adafruit-circuitpython-pn532 / CircuitPython on ESP32

Post by cgglzpy » Sun Oct 27, 2019 8:51 pm

Hi, there is a 'debug' option

Code: Select all

class PN532:
    """Driver for the PN532 connected over SPI. Pass in a hardware or bitbang
    SPI device & chip select digitalInOut pin. Optional IRQ pin (not used),
    reset pin and debugging output."""

    def __init__(self, spi, cs_pin, irq=None, reset=None, debug=False):
It may help you to 'fine-tuning' the time delays in the read/write methods...

And no, I've just tested the code with the Adafruit breakout board :(

Laxistar
Posts: 1
Joined: Wed Feb 03, 2021 1:58 pm

Re: MicroPython with adafruit-circuitpython-pn532 / CircuitPython on ESP32

Post by Laxistar » Wed Feb 03, 2021 2:10 pm

I'm pretty new to programming in general but is working on a solution to emulate different kinds of Mifare-tags to be read by external Mifare readers.

I have a Pycom MCU and from what I'm reading the PN532 should make it possible to emulate a Mifare Classic UID tag. Is this correct? Also I'm wondering which function I should call in the library and if you have any more information on how it works?

The idea is to send a pre-defined UID number to the Pycom and have it forward it through the PN532 to be read by an external reader.

Post Reply