ndef.h 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. #include <SPI.h>
  2. #include <MFRC522.h>
  3. #include "NfcAdapter.h"
  4. // 定义RC522模块的引脚
  5. #define SS_PIN 7 // SDA
  6. #define RST_PIN 3 // RST
  7. String errorValue = "r:rfid error";
  8. String notRfid = "r:not rfid";
  9. String writeValue = "r:write ok";
  10. // 创建MFRC522对象
  11. MFRC522 mfrc522(SS_PIN, RST_PIN);
  12. NfcAdapter nfc = NfcAdapter(&mfrc522);
  13. void ndefSetup(void) {
  14. SPI.begin(); // Init SPI bus
  15. mfrc522.PCD_Init(); // Init MFRC522
  16. nfc.begin();
  17. }
  18. void readRFID(){
  19. String str = errorValue;
  20. if(nfc.tagPresent()){
  21. Serial.println("Reading NFC tag");
  22. NfcTag tag = nfc.read();
  23. Serial.println(tag.getTagType());
  24. Serial.print("UID: ");Serial.println(tag.getUidString());
  25. NdefMessage message = tag.getNdefMessage();
  26. Serial.print("\nThis NFC Tag contains an NDEF Message with ");
  27. Serial.print(message.getRecordCount());
  28. Serial.print(" NDEF Record");
  29. int recordCount = message.getRecordCount();
  30. for (int i = 0; i < recordCount; i++){
  31. Serial.print("\nNDEF Record ");Serial.println(i+1);
  32. NdefRecord record = message.getRecord(i);
  33. Serial.print(" TNF: ");Serial.println(record.getTnf());
  34. String type((char*)record.getType());
  35. Serial.println(" Type: " + type);
  36. int payloadLength = record.getPayloadLength();
  37. const byte *payload = record.getPayload();
  38. String payloadHEX((char*)payload);
  39. Serial.println(" Payload (HEX): " + payloadHEX);
  40. String payloadAsString = "";
  41. for (int c = 0; c < payloadLength; c++) {
  42. payloadAsString += (char)payload[c];
  43. }
  44. Serial.print(" Payload (as String): ");
  45. Serial.println(payloadAsString);
  46. if (record.getIdLength() > 0) {
  47. String ID((char*)record.getId());
  48. Serial.println(" ID: " + ID);
  49. }
  50. str = payloadAsString;
  51. }
  52. }
  53. pCharacteristic->setValue(str.c_str());
  54. }
  55. void writeRFID(String data){
  56. String str = errorValue;
  57. if(nfc.tagPresent()){
  58. Serial.println("Writing multiple records to NFC tag");
  59. NdefMessage message = NdefMessage();
  60. message.addTextRecord(data.c_str());
  61. // message.addUriRecord("https://arduino.cc");
  62. boolean success = nfc.write(message);
  63. if (success)str = writeValue;
  64. }
  65. pCharacteristic->setValue(str.c_str());
  66. }
  67. //0:开头字符串读取 1:开头字符串写入
  68. void ndefCommand(){
  69. String command = pCharacteristic->getValue().c_str();
  70. if (command.startsWith("0:")) {
  71. readRFID();
  72. } else if (command.startsWith("1:")) {
  73. String data = command.substring(2);
  74. writeRFID(data);
  75. }
  76. }
  77. int loopNdefSize=1000; //loop间隔
  78. int ndefMillisTime;
  79. void ndefLoop(void) {
  80. //1秒读取一次指令
  81. int ndefTimeCurrent = millis();
  82. if(ndefTimeCurrent<=0)ndefMillisTime = ndefTimeCurrent;
  83. if((ndefTimeCurrent - ndefMillisTime)>loopNdefSize){
  84. ndefMillisTime = ndefTimeCurrent;
  85. ndefCommand();
  86. }
  87. }