Skip to content

PN532 NFC reader

This feature is included only in tasmota-sensors.bin

Otherwise you must compile your build. Add the following to user_config_override.h:

#ifndef USE_PN532_HSU 
#define USE_PN532_HSU                            // Add support for PN532 using HSU (Serial) interface (+1k8 code, 140 bytes mem)
// Optional defines, uncomment (remove //) as needed
//  #define USE_PN532_CAUSE_EVENTS                 // Cause event execution for PN532_UID= and PN532_DATA=[if defined] (+ 30 bytes code)
//  #define USE_PN532_DATA_FUNCTION                // Add sensor40 command support for erase, setting data block content (+1k7 code, 388 bytes mem)
//  #define USE_PN532_DATA_RAW                     // Allow DATA block to be used by non-alpha-numberic data (+ 80 bytes code, 48 bytes ram)
#endif

The PN532 is a highly integrated transceiver module for contactless communication at 13.56 MHz based on the 80C51 microcontroller core.

The datasheet for the PN532 chip is available here.

Please note that although the datasheet mentions that the PN532 can be used on SPI, I2C and HSUART that only the HSU interface is implemented in the Tasmota driver.

Configuration~

Please note that the

#define USE_PN532_DATA_FUNCTION
This function is experimental. There are limitations because it seems not all cards are supported by this driver and/or the PN532 module. See issue 4941 for more information. We are still researching the

#define USE_PN532_CAUSE_EVENTS
Only necessary if you need to perform actions on the local device using rules. If you only plan to handle the resulting scanned information on your home automation software then you do not need this define as it has no use if you are not going to take an action on the device itself.

Wiring~

As mentioned earlier the PN532 breakout boards usually have pins broken out for all three protocols supported by the PN532 but we are only interested in the HSU interface as that is all the driver currently supports.

For this reason breakout boards have either micro dip switches as shown in the image below, or they have pads on the PC board which you need to bridge out with solder to select which mode the PN532 will operate in.

HSU Dipswitch Setting

After selecting the correct protocol mode and connecting the HSU TX/RX pins of the PN532 to the pins you configured on your ESP8266 board you can power it up and the PN532 should be detected automatically.

Wiring~

PN532 ESP8266
GND GND
VCC 3.3V
SCL GPIOx
SDA GPIOy

Tasmota Settings~

In the Configuration -> Configure Module page assign:

  • GPIOx to PN532 Rx (139)
  • GPIOy to PN532 Rx (138)

The module will reboot when you save this configuration. During start-up the following information should be visible in your console output:

00:00:00 NFC: PN532 NFC Reader detected (V1.6)

If the device was not found please check your wiring and configuration and confirm that everything is as it should be.

Example

Configured using Wemos D1 mini on pins: D1 (connected to PN532 SCL) and D2 (connected to PN532 SDA)

PN532 Software Configuration

Usage~

Tasmota will scan for a new card detect 4 times per second and if found will report it via immediate telemetry and in addition cause an event if you chose to uncomment #define USE_PN532_CAUSE_EVENTS prior to compiling your firmware.

The output on the console will look similar to the below when a new card is detected

18:23:24 MQT: tele/tasmota/SENSOR = {"Time":"2019-01-10T18:23:24","PN532":{"UID":"94D8FC5F", "DATA":""}}
18:23:24 MQT: stat/tasmota/RESULT = {"Event":"Done"}
18:23:25 MQT: stat/tasmota/RESULT = {"Event":"Done"}

The UID of the card/tag is reported and any text stored in BLOCK 1 of a Mifare Classic card (up to 15 characters in length) is reported in the DATA field of the JSON sent via telemetry. Please note that the DATA field cannot contain spaces.

The content of the DATA on BLOCK 1 of a Mifare Classic card can be set as follows

Sensor40 S,ILOVETASMOTA

Once executed the very next card/tag that is presented to the reader will be programmed accordingly and the data will be retained on the card/tag until either changed or erased.

To erase the content of the DATA field the following command may be used

Sensor40 E

Once executed the very next card/tag that is presented to the reader will have its BLOCK 1 erased.

Text logging of the above two actions are also presented during the process for information purposes.

Using the UID and DATA of a presented card~

When a card is presented to the PN532 under normal operating conditions up to 3 ways of using the data is possible.

The first is the immediate telemetry generated which looks as follows

18:31:39 MQT: tele/tasmota/SENSOR = {"Time":"2019-01-10T18:31:39","PN532":{"UID":"94D8FC5F", "DATA":"ILOVETASMOTA"}}

Since this is an immediate telemetry generation as opposed to the sensor data you would normally be expected to be presented when the telemetry period occurs, this telemetry data is not directly usable on the device itself. It is generated and immediately transmitted over MQTT and the purpose of this is so that immediate action may be taken by any home automation software you are using with the data obtained from the card/tag as opposed to waiting for the telemetry period to expire and be sent with normal telemetry data.

For the purpose of using card/tag data on the device itself you will need to use rules along with the events that are caused.

Remember

To use events you need to uncomment #define USE_PN532_CAUSE_EVENTS directive prior to compiling your firmware"

Example rule for responding to a specific UID on the device when a card/tag matching a specific UID is presented

rule on EVENT#PN532_UID=94D8FC5F do power on endon

Output/result:

18:39:20 MQT: tele/tasmota/SENSOR = {"Time":"2019-01-10T18:39:20","PN532":{"UID":"94D8FC5F", "DATA":"ILOVETASMOTA"}}
18:39:20 MQT: stat/tasmota/RESULT = {"Event":"Done"}
18:39:20 RUL: EVENT#PN532_UID=94D8FC5F performs "power on"
18:39:20 MQT: stat/tasmota/RESULT = {"POWER":"ON"}
18:39:20 MQT: stat/tasmota/POWER = ON
18:39:20 MQT: stat/tasmota/RESULT = {"Event":"Done"}

Example rule for responding to a specific DATA content that was previously programmed to one or more cards using the Sensor40 S,xxxx command

Rule 1on EVENT#PN532_DATA=ILOVETASMOTA do power on endon
18:41:12 MQT: tele/tasmota/SENSOR = {"Time":"2019-01-10T18:41:12","PN532":{"UID":"94D8FC5F", "DATA":"ILOVETASMOTA"}}
18:41:13 MQT: stat/tasmota/RESULT = {"Event":"Done"}
18:41:13 MQT: stat/tasmota/RESULT = {"Event":"Done"}
18:41:13 RUL: EVENT#PN532_DATA=ILOVETASMOTA performs "power on"
18:41:13 MQT: stat/tasmota/RESULT = {"POWER":"ON"}
18:41:13 MQT: stat/tasmota/POWER = ON

Breakout Boards~

Since the PN532 chip itself is surface mount and requires some external components to operate the best is probably to obtain a breakout board similar to the one below from your favourite online supplier.

PN532 Breakout Board Module

Please make sure the breakout board you order has the HSU pins (SCL=TX and SDA=RX) broken out as it will be very difficult to add them manually - Usually they have them but for good measure just make sure.