Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for NXP PN532 NFC RFID reader #2915

Closed
ghost opened this issue Jun 5, 2018 · 84 comments
Closed

Add support for NXP PN532 NFC RFID reader #2915

ghost opened this issue Jun 5, 2018 · 84 comments
Labels
enhancement Type - Enhancement that will be worked on fixed Result - The work on the issue has ended

Comments

@ghost
Copy link

ghost commented Jun 5, 2018

Have you look for this feature in other issues and in the wiki?
Yes

Is your feature request related to a problem? Please describe.
Support for NFC RFID reader NXP PN532 as per:
https://www.adafruit.com/product/364
On I2C address 0x48

**Describe the solution you'd like **
Device is selectable / dectected

Describe alternatives you've considered
Can't see any thing else

Additional context
Add any other context or screenshots about the feature request here.

(Please, remember to close the issue when the problem has been addressed)

@Frogmore42
Copy link
Contributor

But, what do you expect to do with it?
What use case do you envision for this, or it is it just good enough that Tasmota can see there is an NFC reader there?

@ghost
Copy link
Author

ghost commented Jun 5, 2018

It will be good if it recognised the device and was able to read the serial number off the RFID tag but then sent into mqtt broker for custom automations.

Ok ?

Btw - I've looked at ESPEasy and ESPurna and I'm really liking Tasmota - seems so much better

@andrethomas
Copy link
Contributor

Should be possible in theory - you'd need to use one of the input pins for the IRQ from the PN532 and then read the card data over I2C - I'm uncertain on how this will impact on the way other I2C devices (mostly sensors) are polled in a cyclic fashion and perhaps not as frequently as you'd need for the PN532 data to be received in a timeous manner.

That said, it may be easier to just use a standalone esp8266 to send the data to your mqtt server but let's see if others have comments on how to implement?

I have a PN532 somewhere but it's not the same as the one from adafruit but I guess its just the protocol that needs to work.

@ghost
Copy link
Author

ghost commented Jun 5, 2018

Ok brill! Thanks.

This is the cheap eBay one I've just ordered...

https://rover.ebay.com/rover/0/0/0?mpre=https%3A%2F%2Fwww.ebay.co.uk%2Fulk%2Fitm%2F152819843755

@andrethomas
Copy link
Contributor

Looks like one of the ones I have somewhere... let me know when you get it and look at the implementation from https://github.com/adafruit/Adafruit-PN532/blob/master/examples/iso14443a_uid/iso14443a_uid.pde with specific reference to

// Or use this line for a breakout or shield with an I2C connection:
//Adafruit_PN532 nfc(PN532_IRQ, PN532_RESET);

Looks like you need to use the reset pin during initialisation also...

@ghost
Copy link
Author

ghost commented Jun 9, 2018

Ok thanks. My unit turned up, I need to solder the pins and get a basic connection going (as per your link which seems to use I2C) and I might even test it via ESPEasy but would sooner use Tasmota ;) cheers

Edit: I found this hookup example with example code using I2C which I thought I would add if it helps:

http://rdiot.tistory.com/173

@Snipercaine
Copy link

I'm also hoping to use Tasmota on a D1mini or NodeMCU with the RF522 reader to read the card ID and send an MQTT message.
I'll be struggling with a home-made Arduino sketch in the meantime.

@simonbove
Copy link

Would like the same functionality

@timtimsson
Copy link

PN532 - letscontrolit

@ascillato2 ascillato2 added the enhancement Type - Enhancement that will be worked on label Aug 29, 2018
@gabrielklein
Copy link

gabrielklein commented Sep 18, 2018

I like the idea.... it would be great to support a subset of NDEF messages (NFC).

In particular:

  • Raw data => send a message using MQTT
  • http: or https => directly call a url (get or post) => usefull for quick simple api calls

It means you can write data on a rfid card using an android application, and "program" cards to send messages like "toggle light 15", "pause VLC application", etc...

As an example if I have on a card "LIGHT1/TOGGLE ON", the mqtt message ON will be sent on channel "/sonoff/xxx/yyy/LIGHT1/TOGGLE"

@Eina61
Copy link

Eina61 commented Oct 8, 2018

I would love RFID functionality on a Tasmota device - except I am using Weigand protocol.
I have an existing Weigand system but I find the WiFi connection to be very unstable whereas all my Tasmota units connections are rock solid all the time.

@robrec
Copy link

robrec commented Nov 15, 2018

would also love this function. Especial with the sonoff SV, this would be so awesome....

@andrethomas
Copy link
Contributor

Still on my to-do list ;)

@dknubben
Copy link

Here is a good working solution:
https://github.com/esprfid/esp-rfid
(RC522, PN532, Wiegand, RDM6300 and MQTT support)

@ozett
Copy link

ozett commented Dec 22, 2018

if anybody get to work on the code, may consider also wiegand-input.
if have it running with espeasy, but would love to see it also on tasmota.
(there a more details in the espeasy-forum, if needed, but is an easy thing)

@Jason2866
Copy link
Collaborator

To get it working nice with Tasmota SPI interface has to be used.
Since SPI is not (yet) there. It needs some more time.

@andrethomas
Copy link
Contributor

SPI is available... also software SPI is used in xdsp_05_epaper_29.ino so should be pretty easy to integrate from a working PN532 sketch.

@andrethomas
Copy link
Contributor

@ozett
Will look at it after the festivities are over :)

@ozett
Copy link

ozett commented Dec 24, 2018

👍

@gemu2015
Copy link
Contributor

gemu2015 commented Jan 1, 2019

i have an initial i2c version working on my universal branch. you may give it a try.
https://github.com/gemu2015/Sonoff-Tasmota

@ozett
Copy link

ozett commented Jan 1, 2019

🆒 (cool!)
i will grab some wemos-d1 and some spare-reader and will see how it goes.
i come back with report ..

@andrethomas
Copy link
Contributor

@gemu2015 works perfectly just copying the sensor ino file and the library over.

Just needs to be cleaned up a little to remove the serial prints etc and then it can be PR'd to here I guess.

@andrethomas
Copy link
Contributor

On second thought I think I will continue with a self-contained driver...

@gemu2015
Copy link
Contributor

gemu2015 commented Jan 2, 2019 via email

@Jason2866
Copy link
Collaborator

May it work better via SPI interface?

@gemu2015
Copy link
Contributor

gemu2015 commented Jan 2, 2019 via email

@gemu2015
Copy link
Contributor

gemu2015 commented Jan 4, 2019 via email

@gemu2015
Copy link
Contributor

gemu2015 commented Jan 5, 2019 via email

@andrethomas
Copy link
Contributor

andrethomas commented Jan 5, 2019

we have to use a reset toggle.

Resetting the PN532 does not bring the I2C functionality back to life so it must be the ESP which is holding the I2C bus low (which does not make sense after a soft restart), or the reset pin on the PN532 is only for SPI functionality - idk for sure.

FUNC_SAVE_BEFORE_RESTART

Only happens for me on restart or restart after OTA.

I have not tested if it is called when saving VAR's but it should not be - Will test later during the day.

i changed the polling interval to 250 ms

Also done this already in the current codebase. I have not seen problems with the 10ms timeout but I do get problems with it < 6 which is why I left it on 10.

@gemu2015
Copy link
Contributor

gemu2015 commented Jan 5, 2019

i had frequent hangs which i did not have with my old version. the ic pulled the clock down (proven by desoldering scl => esp generates clocks)

after enabling clock stretching i did not have any more hangs
https://circuitpython.readthedocs.io/projects/pn532/en/latest/examples.html
and also the data sheet says that the pn532 needs clock stretching in i2c mode.

Wire.setClockStretchLimit(1000);

@andrethomas
Copy link
Contributor

I'll need to tackle it with a logic analyzer as I cannot reproduce the timeout anymore.

If it needs to be changed when transacting with the PN532 then it may need to be reverted after the I2C transaction is done as to not impair other connected I2C devices.

@gemu2015
Copy link
Contributor

gemu2015 commented Jan 5, 2019 via email

@andrethomas
Copy link
Contributor

In the Tasmota codebase it is only done in the CCS811 afaik... but yes, in theory, it cannot break anything else.

@gemu2015
Copy link
Contributor

gemu2015 commented Jan 5, 2019 via email

@andrethomas
Copy link
Contributor

Cool. Let me hook it up to a logic analyzer so I can back the change with confidence... just to satisfy my pedantic nature :)

@andrethomas
Copy link
Contributor

andrethomas commented Jan 5, 2019

PR #4832 made to enable Wire.setClockStretchLimit(1000); in line with datasheet Table 12.25 (Timing for the I2C interface)

@qingz2004
Copy link

I tried to use a rule with PN532.
I write it the same way as other sensors, but it does not work.

rule1 on PN532#UID=1234ABCD do power toggle endon

While this works.

rule1 on EVENT#PN532=1234ABCD do power toggle endon

I don't know the why.

@ozett
Copy link

ozett commented Jan 6, 2019

looks like second verson for rule is coded this way ...

sprintf(command,"event PN532=%s",uids);

https://github.com/gemu2015/Sonoff-Tasmota/blob/png532/sonoff/xsns_40_pn532_i2c.ino#L416

--- i looked up the wiki for rule-definition:
both syntax version should be somehow possible according to wiki :

image

dont know, if on version2 the UID is within syntax of wiki,
but looks like a good and understandble syntax

image

@gemu2015
Copy link
Contributor

gemu2015 commented Jan 6, 2019

@andrethomas

btw your hex formatter doesnt work properly. it suppresses the leading zeros of each hex nibble

@andrethomas
Copy link
Contributor

andrethomas commented Jan 6, 2019

@qingz2004 @ozett

rule1 on PN532#UID=1234ABCD do power toggle endon

Will not work because the driver does not publish telemetry like a sensor - It has no callback to collect the telemetry data because usually by the time telemetry data is collected it is too late to act on a card that was present 300 seconds ago.

So the telemetry is generated immediately and sent over MQTT but the local device cannot do anything with it - This is only intended for home automation purposes.

This is why the event was added - To enable action on the device and as was shown above the correct way to capture it currently is

rule1 on EVENT#PN532=1234ABCD do power toggle endon

The short answer is that events are handled differently to immediate telemetry messages and differently to telemetry data collected for teleperiod.

@gemu2015 Thx, unintentional feature :) Fixed with PR #4842

@qingz2004
Copy link

@andrethomas Got it. Thanks!

@qingz2004
Copy link

Possible to support write or copy?

@andrethomas
Copy link
Contributor

@qingz2004 It is possible but not necessary for Tasmota to have this functionality.

@qingz2004
Copy link

Understand. Read only is good enough for home automation.

@qingz2004
Copy link

Can we read more info from the tag besides the UID? I try to use my android phone to emulate a tag, the UID is different each time. So I can't use the UID for judgement.

@andrethomas
Copy link
Contributor

@qingz2004

It depends...

Mifare Classic compatible card (the one with 4 bytes uid, i.e. "AABBCCDD") requires authentication with a known key to read the data block. These are the most commonly used cards and although they could have factory default key of 0xFFFFFFFFFFFF I find mine to not respond to that.

The Mifare Ultralight card (the one with 7 bytes uid i.e. "AABBCCDDEEFFAA") does not seem to require authentication but are less commonly found... I do not have one so I cannot confirm this.

Maybe @arendst will explore this when he receives his module board but I'm inclined to think the UID is fine for automation purposes.

@ozett
Copy link

ozett commented Jan 6, 2019

Can we read more info from the tag besides the UID? I try to use my android phone to emulate a tag, the UID is different each time. So I can't use the UID for judgement.

think the random UID happens because of android implementation:
https://stackoverflow.com/questions/27239473/get-static-nfc-tag-id-with-hce-mode

i have seen that with the nfc/rfid-function of the german passport
i commented it on a RFID-youtube-video

Unique Identifier (UID, [ISO 14443] Typ A) ==> gets ==>
Pseudo-Unique PICC Identifier (PUPI, [ISO 14443] Typ B)

beim deutschen personalausweis wird immer einer ander ID erzeugt beim auslesen. eine sicherheitsfunktion: 
"Entsprechend dem korrekten Verhalten eines RFID-Tags benötigt auch der nPA-Chip einen Unique Identifier zur Adressierung im Feld des Lesegeräts. 
Um eine Identifizierung des Nutzers schon auf RFID-Ebene zu verhindern (Tracking) 
„wird der Unique Identifier (UID, [ISO 14443] Typ A) bzw. der Pseudo-Unique PICC Identifier (PUPI, [ISO 14443] Typ B) 
des Chips bei jeder Aktivierung des Chips zufällig erzeugt“ ([TR-03127], S. 8).  
aus. "https://sar.informatik.hu-berlin.de/research/publications/SAR-PR-2011-09/diplomarbeit-mj-print_.pdf", S.27 
 -- spannende Funktion. müsste man mal untersuchen

(german language original)

@gemu2015
Copy link
Contributor

gemu2015 commented Jan 7, 2019

just as an info: i can write to all of my (blank classic) cards with the default key
there are also UID writeable cards, cloning is a matter of seconds, so don't rely just on UID for very secure locks

@gemu2015
Copy link
Contributor

gemu2015 commented Jan 7, 2019

@andrethomas
may be you can not write to your cards because the write block routine is defective in the pn532 GitHub drivers. i discovered the bug and implemented block read and write commands, format NDEF and write NDEF records. don't know if we have a use for that in Tasmota,

@ozett
Copy link

ozett commented Jan 7, 2019

discovered the bug and implemented block read and write commands, format NDEF and write NDEF records. don't know if we have a use for that in Tasmota,

since 2016 i rely with my nfc/rfid-door opener only on the UID. everythings fine with this.
Because there have been no other attractive solutions for makers around.
but it would be somehow more than "nice" to brand all my own tags/keys only for only my own doors with a encrypted secret on the tags (but this could raise the ususal "what is really secure/secret" discussion, and maybe out of scope for tasmota itself?
i personally would love to see it... 👍

@andrethomas
Copy link
Contributor

@gemu2015

i discovered the bug and implemented block read and write commands, format NDEF and write NDEF records. don't know if we have a use for that in Tasmota,

Worth checking out, thanks :)

@qingz2004
Copy link

qingz2004 commented Apr 9, 2019

PN532 HSU mode does not work with core 2.3.0. I have to use core 2.4.2.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement Type - Enhancement that will be worked on fixed Result - The work on the issue has ended
Projects
None yet
Development

No branches or pull requests