Advertisement
Guest User

Thei

a guest
Jun 16th, 2019
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.50 KB | None | 0 0
  1. /**
  2. --------------------------------------------------------------------------------------------------------------------
  3. Example sketch/program showing how to read data from more than one PICC to serial.
  4. --------------------------------------------------------------------------------------------------------------------
  5. This is a MFRC522 library example; for further details and other examples see: https://github.com/miguelbalboa/rfid
  6.  
  7. Example sketch/program showing how to read data from more than one PICC (that is: a RFID Tag or Card) using a
  8. MFRC522 based RFID Reader on the Arduino SPI interface.
  9.  
  10. Warning: This may not work! Multiple devices at one SPI are difficult and cause many trouble!! Engineering skill
  11. and knowledge are required!
  12.  
  13. @license Released into the public domain.
  14.  
  15. Typical pin layout used:
  16. -----------------------------------------------------------------------------------------
  17. MFRC522 Arduino Arduino Arduino Arduino Arduino
  18. Reader/PCD Uno/101 Mega Nano v3 Leonardo/Micro Pro Micro
  19. Signal Pin Pin Pin Pin Pin Pin
  20. -----------------------------------------------------------------------------------------
  21. RST/Reset RST 9 5 D9 RESET/ICSP-5 RST
  22. SPI SS 1 SDA(SS) ** custom, take a unused pin, only HIGH/LOW required *
  23. SPI SS 2 SDA(SS) ** custom, take a unused pin, only HIGH/LOW required *
  24. SPI MOSI(g) MOSI 11 / ICSP-4 51 D11 ICSP-4 16
  25. SPI MISO(b) MISO 12 / ICSP-1 50 D12 ICSP-1 14
  26. SPI SCK SCK 13 / ICSP-3 52 D13 ICSP-3 15
  27.  
  28. */
  29.  
  30.  
  31. #include <SPI.h>
  32. #include <MFRC522.h>
  33.  
  34. #include <stdio.h>
  35. #include <string.h>
  36.  
  37. #define RST_PIN 9 //black // Configurable, see typical pin layout above
  38. #define SS_1_PIN 10 //orange // gelb Configurable, take a unused pin, only HIGH/LOW required, must be diffrent to SS 2
  39. #define SS_2_PIN 14 //brown //8 // blau
  40. // Configurable, take a unused pin, only HIGH/LOW required, must be diffrent to SS 1
  41.  
  42. #define NR_OF_READERS 2
  43.  
  44. byte ssPins[] = {SS_1_PIN, SS_2_PIN};
  45.  
  46. byte patent1[18] = {4, 70, 78, 106, 106, 95};
  47. byte patent2[18] = {4, 30, 78, 106, 106, 95};
  48. byte waben1[18] = {4, 226, 79, 106, 106, 95};
  49. byte waben2[18] = {4, 112, 79, 106, 106, 95};
  50. byte fisch1[18] = {4, 104, 78, 106, 106, 95};
  51. byte fisch2[18] = {4, 103, 78, 106, 106, 95};
  52.  
  53. bool patenta = true;
  54. bool patentb = true;
  55. bool fischa = true;
  56. bool fischb = true;
  57. bool wabena = true;
  58. bool wabenb = true;
  59.  
  60. MFRC522 mfrc522[NR_OF_READERS]; // Create MFRC522 instance.
  61.  
  62. /**
  63. Initialize.
  64. */
  65. void setup() {
  66.  
  67. Serial.begin(9600); // Initialize serial communications with the PC
  68. while (!Serial); // Do nothing if no serial port is opened (added for Arduinos based on ATMEGA32U4)
  69.  
  70. SPI.begin(); // Init SPI bus
  71.  
  72. for (uint8_t reader = 0; reader < NR_OF_READERS; reader++) {
  73. mfrc522[reader].PCD_Init(ssPins[reader], RST_PIN); // Init each MFRC522 card
  74. Serial.print(F("Reader "));
  75. Serial.print(reader);
  76. Serial.print(F(": "));
  77. mfrc522[reader].PCD_DumpVersionToSerial();
  78. }
  79. }
  80.  
  81. void dump_byte_array(byte *buffer, byte bufferSize) {
  82. for (byte i = 0; i < bufferSize; i++) {
  83. Serial.print(buffer[i] < 0x10 ? " 0" : " ");
  84. Serial.print(buffer[i], HEX);
  85. }
  86. }
  87.  
  88. /**
  89. Main loop.
  90. */
  91. void loop() {
  92.  
  93. bool patenta = true;
  94. bool patentb = true;
  95. bool fischa = true;
  96. bool fischb = true;
  97. bool wabena = true;
  98. bool wabenb = true;
  99.  
  100. // Look for new cards
  101.  
  102. if (mfrc522[0].PICC_IsNewCardPresent() && mfrc522[0].PICC_ReadCardSerial() && mfrc522[1].PICC_IsNewCardPresent() && mfrc522[1].PICC_ReadCardSerial()) {
  103.  
  104. //READER 1
  105. Serial.print(F("Reader 0"));
  106. // Show some details of the PICC (that is: the tag/card)
  107.  
  108. Serial.print(F(": Card UID:"));
  109. dump_byte_array(mfrc522[0].uid.uidByte, mfrc522[0].uid.size);
  110. Serial.println();
  111.  
  112. // Try the known default keys
  113. MFRC522::MIFARE_Key key;
  114. // Copy the known key into the MIFARE_Key structure
  115. for (byte i = 0; i < MFRC522::MF_KEY_SIZE; i++) {
  116. // Serial.println(mfrc522[reader].uid.uidByte[i]); //einkommentieren zum auslesen der tags
  117.  
  118. //abfrage patent
  119. if (mfrc522[0].uid.uidByte[i] != patent1[i] && mfrc522[1].uid.uidByte[i] != patent1[i]) {
  120. patenta = false;
  121. }
  122. if (mfrc522[0].uid.uidByte[i] != patent2[i] && mfrc522[1].uid.uidByte[i] != patent2[i]) {
  123. patentb = false;
  124. }
  125.  
  126. //abfrage fisch
  127. if (mfrc522[0].uid.uidByte[i] != fisch1[i] && mfrc522[1].uid.uidByte[i] != fisch1[i]) {
  128. fischa = false;
  129. }
  130. if (mfrc522[0].uid.uidByte[i] != fisch2[i] && mfrc522[1].uid.uidByte[i] != fisch2[i]) {
  131. fischb = false;
  132. }
  133.  
  134. //abfrage waben
  135. if (mfrc522[0].uid.uidByte[i] != waben1[i] && mfrc522[1].uid.uidByte[i] != waben1[i]) {
  136. wabena = false;
  137. }
  138. if (mfrc522[0].uid.uidByte[i] != waben2[i] && mfrc522[1].uid.uidByte[i] != waben2[i]) {
  139. wabenb = false;
  140. }
  141.  
  142. }
  143.  
  144. // Halt PICC
  145. mfrc522[0].PICC_HaltA();
  146. // Stop encryption on PCD
  147. mfrc522[0].PCD_StopCrypto1();
  148.  
  149. //READER2
  150. // Show some details of the PICC (that is: the tag/card)
  151. Serial.print(F("Reader 1"));
  152. Serial.print(F(": Card UID:"));
  153. dump_byte_array(mfrc522[1].uid.uidByte, mfrc522[1].uid.size);
  154. Serial.println();
  155.  
  156. for (byte i = 0; i < MFRC522::MF_KEY_SIZE; i++) {
  157. // Serial.println(mfrc522[reader].uid.uidByte[i]); //einkommentieren zum auslesen der tags
  158.  
  159. // übergabe an serial port
  160. if (patenta == true && patentb == true) {
  161. Serial.println("Ausgewähltes Modell: Patent");
  162. break;
  163. }
  164. if (fischa == true && fischb == true) {
  165. Serial.println("Ausgewähltes Modell: Fisch");
  166. break;
  167. }
  168. if (wabena == true && wabenb == true) {
  169. Serial.println("Ausgewähltes Modell: Waben");
  170. break;
  171. }
  172. else {
  173. Serial.println("Fehler");
  174. return;
  175. }
  176. }
  177. }
  178. else {
  179. //Serial.println("Es konnten keine zwei Gabelzinken erkannt werden");
  180. return;
  181. }
  182. // Halt PICC
  183. mfrc522[1].PICC_HaltA();
  184. // Stop encryption on PCD
  185. mfrc522[1].PCD_StopCrypto1();
  186.  
  187. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement