Skip to main content

PN532MifareClassic

Latest Version: 1.0.0

This library exposes access to the MIFARE classic functionality of the NXP PN532/C106 NFC Module. It provides functions needed to read and write to MIFARE Classic tags, which support read/write storage. It also serves as an example of how to build on the PN532 library to interface with the many other protocols and features that the PN532 supports.

You can view the library’s source code on GitHub. Click here to see information on other versions of this library.

To add this library to your project, add #require "PN532MifareClassic.class.nut:1.0.0" and #require "PN532.class.nut:1.0.0" to the top of your device code

Example

For an example of using the PN532MifareClassic library to build an ID card reader, see the MIFARE reader example.

Class Usage

Constructor: PN532MifareClassic(pn532)

Associates this class with a previously constructed PN532 object.

The PN532 instance must have been constructed with a version of the PN532 library with a version number in the 1.x.x major revision of at least 1.0.0.

#require "PN532.class.nut:1.0.0"
#require "PN532MifareClassic.class.nut:1.0.0"

// Set up PN532 pins and callback
reader <- PN532(spi, nss, rstpd_l, irq, constructorCallback);
mifareReader <- PN532MifareClassic(reader);

Class Methods

pollNearbyTags(pollAttempts, pollPeriod, callback)

A wrapper around the PN532 class’ pollNearbyTags() method which configures it to search for MIFARE NFC tags.

mifareReader.pollNearbyTags(0x0A, 6, scanCallback);

authenticate(tagSerial, address, aOrB, key, callback)

Attempts to authenticate the reader to perform operations on a specified EEPROM address.

Parameter Description
tagSerial A blob representing the UID of the tag to be authenticated. This UID can be taken from the response to the pollNearbyTags() call.
address The address of the memory block to be authenticated. For the MIFARE Classic 1k, this is an integer in the range 0-63.
aOrB The key type to authenticate against. The options are the static class members PN532MifareClassic.AUTH_TYPE_A and PN532MifareClassic.AUTH_TYPE_B.
key The key to use when authenticating for the given block. If no key has been set, set this parameter to null to use the default 0xFFFFFFFFFFFF value.
callback A function that takes the following arguments:
  • error — A string that is null on success.
  • status — A boolean that is set to true if the authentication succeeded.

 
Example:

function authCallback(error, status) {
    if (error != null) {
        server.error(error);
        return;
    }

    if (authenticated) {
        // Operate on tag...
    }
}

mifareReader.authenticate(tagData.NFCID, 0x2, PN532MifareClassic.AUTH_TYPE_A, null,
    authCallback);

read(address, callback)

Reads 16 bytes from the address specified on the currently authenticated tag. callback is a function taking the following parameters:

  • error — A string that is null on success.
  • data — A 16-byte blob containing the data read from the tag.

That this call will fail with an error if the address being read has not previously been authenticated.

// Authenticate the address to prevent read error...

function readCallback(error, data) {
    if (error != null) {
        server.error(error);
        return;
    }
    server.log(data.tostring());
}

mifareReader.read(0x2, readCallback);

write(address, data, callback)

Writes 16 bytes of blob data to the address specified on the currently authenticated tag. callback is a function taking a string parameter, error, that is null on success.

Errors

  • If the address being read has not previously been authenticated, the callback will return a PN532MifareClassic.ERROR_BAD_AUTHENTICATION error.
  • If data is not exactly 16 bytes long, the callback will return a PN532MifareClassic.ERROR_INCORRECT_LENGTH error.
// Authenticate the address to prevent read error...

function writeCallback(error) {
    if (error != null) {
        server.error(error);
        return;
    }
}

local data = blob(16);
for (local i = 0 ; i < 16 ; i++) {
    data[i] = 0x88;
}

mifareReader.write(0x2, data, writeCallback);

Release History

The Electric Imp Dev Center documents the latest version of the library. For past versions, please see the Electric Imp public GitHub repos listed below.

Version Source Code Notes
1.0.0 GitHub Initial release

License

The PN532MifareClassic library is licensed under the MIT License.