PN532 Driver library This library provides an abstract API to drive the pn532 nfc chip, with I2C/HSU/SPI interface. Its based on the Seeed Studio's Arduino version.
Dependents: PN532_ReadUid Nfctest2
PN532/examples/emulate_tag_ndef/emulate_tag_ndef.ino@0:db8030e71f55, 2016-09-13 (annotated)
- Committer:
- dotnfc
- Date:
- Tue Sep 13 06:01:19 2016 +0000
- Revision:
- 0:db8030e71f55
first commit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
dotnfc | 0:db8030e71f55 | 1 | |
dotnfc | 0:db8030e71f55 | 2 | #include "SPI.h" |
dotnfc | 0:db8030e71f55 | 3 | #include "PN532_SPI.h" |
dotnfc | 0:db8030e71f55 | 4 | #include "emulatetag.h" |
dotnfc | 0:db8030e71f55 | 5 | #include "NdefMessage.h" |
dotnfc | 0:db8030e71f55 | 6 | |
dotnfc | 0:db8030e71f55 | 7 | PN532_SPI pn532spi(SPI, 10); |
dotnfc | 0:db8030e71f55 | 8 | EmulateTag nfc(pn532spi); |
dotnfc | 0:db8030e71f55 | 9 | |
dotnfc | 0:db8030e71f55 | 10 | uint8_t ndefBuf[120]; |
dotnfc | 0:db8030e71f55 | 11 | NdefMessage message; |
dotnfc | 0:db8030e71f55 | 12 | int messageSize; |
dotnfc | 0:db8030e71f55 | 13 | |
dotnfc | 0:db8030e71f55 | 14 | uint8_t uid[3] = { 0x12, 0x34, 0x56 }; |
dotnfc | 0:db8030e71f55 | 15 | |
dotnfc | 0:db8030e71f55 | 16 | void setup() |
dotnfc | 0:db8030e71f55 | 17 | { |
dotnfc | 0:db8030e71f55 | 18 | Serial.begin(115200); |
dotnfc | 0:db8030e71f55 | 19 | Serial.println("------- Emulate Tag --------"); |
dotnfc | 0:db8030e71f55 | 20 | |
dotnfc | 0:db8030e71f55 | 21 | message = NdefMessage(); |
dotnfc | 0:db8030e71f55 | 22 | message.addUriRecord("http://www.seeedstudio.com"); |
dotnfc | 0:db8030e71f55 | 23 | messageSize = message.getEncodedSize(); |
dotnfc | 0:db8030e71f55 | 24 | if (messageSize > sizeof(ndefBuf)) { |
dotnfc | 0:db8030e71f55 | 25 | Serial.println("ndefBuf is too small"); |
dotnfc | 0:db8030e71f55 | 26 | while (1) { } |
dotnfc | 0:db8030e71f55 | 27 | } |
dotnfc | 0:db8030e71f55 | 28 | |
dotnfc | 0:db8030e71f55 | 29 | Serial.print("Ndef encoded message size: "); |
dotnfc | 0:db8030e71f55 | 30 | Serial.println(messageSize); |
dotnfc | 0:db8030e71f55 | 31 | |
dotnfc | 0:db8030e71f55 | 32 | message.encode(ndefBuf); |
dotnfc | 0:db8030e71f55 | 33 | |
dotnfc | 0:db8030e71f55 | 34 | // comment out this command for no ndef message |
dotnfc | 0:db8030e71f55 | 35 | nfc.setNdefFile(ndefBuf, messageSize); |
dotnfc | 0:db8030e71f55 | 36 | |
dotnfc | 0:db8030e71f55 | 37 | // uid must be 3 bytes! |
dotnfc | 0:db8030e71f55 | 38 | nfc.setUid(uid); |
dotnfc | 0:db8030e71f55 | 39 | |
dotnfc | 0:db8030e71f55 | 40 | nfc.init(); |
dotnfc | 0:db8030e71f55 | 41 | } |
dotnfc | 0:db8030e71f55 | 42 | |
dotnfc | 0:db8030e71f55 | 43 | void loop(){ |
dotnfc | 0:db8030e71f55 | 44 | // uncomment for overriding ndef in case a write to this tag occured |
dotnfc | 0:db8030e71f55 | 45 | //nfc.setNdefFile(ndefBuf, messageSize); |
dotnfc | 0:db8030e71f55 | 46 | |
dotnfc | 0:db8030e71f55 | 47 | // start emulation (blocks) |
dotnfc | 0:db8030e71f55 | 48 | nfc.emulate(); |
dotnfc | 0:db8030e71f55 | 49 | |
dotnfc | 0:db8030e71f55 | 50 | // or start emulation with timeout |
dotnfc | 0:db8030e71f55 | 51 | /*if(!nfc.emulate(1000)){ // timeout 1 second |
dotnfc | 0:db8030e71f55 | 52 | Serial.println("timed out"); |
dotnfc | 0:db8030e71f55 | 53 | }*/ |
dotnfc | 0:db8030e71f55 | 54 | |
dotnfc | 0:db8030e71f55 | 55 | // deny writing to the tag |
dotnfc | 0:db8030e71f55 | 56 | // nfc.setTagWriteable(false); |
dotnfc | 0:db8030e71f55 | 57 | |
dotnfc | 0:db8030e71f55 | 58 | if(nfc.writeOccured()){ |
dotnfc | 0:db8030e71f55 | 59 | Serial.println("\nWrite occured !"); |
dotnfc | 0:db8030e71f55 | 60 | uint8_t* tag_buf; |
dotnfc | 0:db8030e71f55 | 61 | uint16_t length; |
dotnfc | 0:db8030e71f55 | 62 | |
dotnfc | 0:db8030e71f55 | 63 | nfc.getContent(&tag_buf, &length); |
dotnfc | 0:db8030e71f55 | 64 | NdefMessage msg = NdefMessage(tag_buf, length); |
dotnfc | 0:db8030e71f55 | 65 | msg.print(); |
dotnfc | 0:db8030e71f55 | 66 | } |
dotnfc | 0:db8030e71f55 | 67 | |
dotnfc | 0:db8030e71f55 | 68 | delay(1000); |
dotnfc | 0:db8030e71f55 | 69 | } |