Initial Wiki for PN532

Andre Thomas 2019-01-10 18:42:03 +02:00
parent 27be812c70
commit 4c42402fc7
1 changed files with 125 additions and 0 deletions

125
PN532.md Normal file

@ -0,0 +1,125 @@
### PN532 I2C Driver for Sonoff-Tasmota
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: [https://www.nxp.com/docs/en/nxp/data-sheets/PN532_C1.pdf](https://www.nxp.com/docs/en/nxp/data-sheets/PN532_C1.pdf)
Please note that although the datasheet mentions that the PN532 can be used on SPI, I2C and HSUART that only the I2C interface is implemented in the Sonoff-Tasmota driver.
### PN532 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](https://raw.githubusercontent.com/andrethomas/images/master/pn532/PN532_nfc_module.PNG)
Please make sure the breakout board you order has the I2C pins (SDA and SCL) broken out as it will be very difficult to add them manually - Usually they have them but for good measure just make sure.
### PN532 Configuration in Sonoff-Tasmota
The driver for the PN532 is not included in any of the default release binaries so you will need to compile your own binary for the purpose of using the PN532.
To enable it prior to compilation you need to uncomment the following two lines in your `my_user_config.h` file.
`#define USE_PN532_I2C`
`#define USE_PN532_CAUSE_EVENTS`
The latter is 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 to uncomment `#define USE_PN532_CAUSE_EVENTS` as it has no use if you are not going to take an action on the device itself.
Once you have compiled and uploaded your new firmware you will need to configure the usual (such as your wifi configuration and MQTT server configuration etc) and when complete proceed to enable I2C pins for SDA and SCL on your ESP8266 device.
For this example we will configure it using D1 and D2 as follows - you can follow the buttons pressed from left to right in the image below:
![PN532 - Configuring I2C pins on Sonoff-Tasmota](https://raw.githubusercontent.com/andrethomas/images/master/pn532/PN532_Configuration.PNG)
The module will reboot when you save this configuration.
### Configuring and Connecting the PN532 to your ESP8266 based module
First make sure that your device is powered off and that all power connections are removed.
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 I2C 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.
![PN532 Hardware Config and Connection](https://raw.githubusercontent.com/andrethomas/images/master/pn532/pn532_nfc_i2c_config.png)
After selecting the correct protocol mode and connecting the PN532 to the I2C pins on the ESP8266 board you can power it up and the PN532 should be detected automatically.
To rule out any issues you may perform a `I2cScan` command from the console (web or serial) as follows to ensure that the device is detected on the I2C bus.
```
17:14:29 CMD: i2cscan
17:14:29 MQT: stat/sonoff/RESULT = {"I2CScan":"Device(s) found at 0x24"}
```
If the device was not found please check your wiring and configuration and confirm that everything is as it should be.
### Using the PN532 on Sonoff-Tasmota
Sonoff-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/sonoff/SENSOR = {"Time":"2019-01-10T18:23:24","PN532":{"UID":"94D8FC5F", "DATA":""}}
18:23:24 MQT: stat/sonoff/RESULT = {"Event":"Done"}
18:23:25 MQT: stat/sonoff/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,I_LOVE_TASMOTA`
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 in Sonoff-Tasmota
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/sonoff/SENSOR = {"Time":"2019-01-10T18:31:39","PN532":{"UID":"94D8FC5F", "DATA":"I_LOVE_TASMOTA"}}
```
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 the `#define USE_PN532_CAUSE_EVENTS` directive prior to building/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`
Example output/result:
```
18:39:20 MQT: tele/sonoff/SENSOR = {"Time":"2019-01-10T18:39:20","PN532":{"UID":"94D8FC5F", "DATA":"I_LOVE_TASMOTA"}}
18:39:20 MQT: stat/sonoff/RESULT = {"Event":"Done"}
18:39:20 RUL: EVENT#PN532_UID=94D8FC5F performs "power on"
18:39:20 MQT: stat/sonoff/RESULT = {"POWER":"ON"}
18:39:20 MQT: stat/sonoff/POWER = ON
18:39:20 MQT: stat/sonoff/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
`on EVENT#PN532_DATA=I_LOVE_TASMOTA do power on endon`
```
18:41:12 MQT: tele/sonoff/SENSOR = {"Time":"2019-01-10T18:41:12","PN532":{"UID":"94D8FC5F", "DATA":"I_LOVE_TASMOTA"}}
18:41:13 MQT: stat/sonoff/RESULT = {"Event":"Done"}
18:41:13 MQT: stat/sonoff/RESULT = {"Event":"Done"}
18:41:13 RUL: EVENT#PN532_DATA=I_LOVE_TASMOTA performs "power on"
18:41:13 MQT: stat/sonoff/RESULT = {"POWER":"ON"}
18:41:13 MQT: stat/sonoff/POWER = ON
```