Come si utilizza un lettore RFID con Arduino 2009 o con la scheda ETA BETA EDI ?
Come si memorizzano nelle celle EEPROM i codici dei tag ??
Per il nostro esperimento abbiamo utilizzato un lettore RFID - RC522 (acquistabile qui) e la nostra scheda ETA BETA EDI o arduino 2009.
Lo scopo del TUTORIAL è mostrarvi come si memorizzano i codici dei TAG RFID nelle celle EEPROM senza bisogno di aggiungerli a “mano” nel codice sorgente.
La libreria che abbiamo utilizzato è disponibile qui.
SKETCH:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 | /* * Tutorial: Lettore RFID - RC522 Arduino - EDI * * etabetastudio.blogspot.it * * Christian Acqua - Massimo Pinto * */ #include <SPI.h> #include <MFRC522.h> #include <EEPROM.h> #define SS_PIN 10 #define RST_PIN 9 MFRC522 mfrc522(SS_PIN, RST_PIN); // Creare un instanza di MFRC522 int ser; int programma=0; byte puntatore = 0; // Puntatore card registrate String uid_s1; String uid_s; String uidRFID[50]; boolean controllo; //Variabile di verica card registrate void setup() { Serial.begin(9600); // inizializzazione seriale SPI.begin(); // Inizializzazione BUS SPI mfrc522.PCD_Init(); // IInizializzazione MFRC522 reader // lettura da EEPROM --- IN ACCENSIONE SARANNO STAMPATI A MONITOR SERIALE I CODICI RFID DEI TAG PROGRAMMATI E LA CELLA EEPROM DOVE SONO MEMORIZZATI puntatore = EEPROM.read(0); if (puntatore == 2) { // leggo valori puntatore=EEPROM.read(1); Serial.println(puntatore); int i=1; while(i<=puntatore) { uidRFID[i-1]=""; for (int p=i*8-8+2;p<i*8+2;p++) { uidRFID[i-1]=uidRFID[i-1]+char(EEPROM.read(p)); } Serial.println(uidRFID[i-1]); i++; } } else { // mai scritta EEPROM EEPROM.write (0,2); EEPROM.write (1,0); } } void loop() { //AVVICINARE IL TAG RFID AL LETTORE E POI DIGITARE LA LETTERA "a" SUL MONITOR SERIALE PER ENTRARE IN PROGRAMMAZIONE. //SE IL TAG NON è MEMORIZZATO QUINDI NON è PRESENTE NELLE CELLE EEPROM VERRà ACQUISITO //SE IL TAG è GIà STATO MEMORIZZATO IN PRECEDENZA CI APPARIRà LA SCRITTA TAG GIA PRESENTE // entra in programmazione if (Serial.available() > 0) { ser=Serial.read(); switch (ser){ //digito sul monitor seriale la lettera a per entrare nella memorizzazione dei codici case 'a': delay(1500); programma =1; progchiave(); break; } } } // VOID PER LA PROGRAMMAZIONE E LA MEMORIZZAZIONE DELLE CHIAVI void progchiave(){ // lettura codice card ---------------------------------------- String uid_s = ""; if ( ! mfrc522.PICC_IsNewCardPresent()) { return; } // Select one of the cards if ( ! mfrc522.PICC_ReadCardSerial()) { return; } if (!mfrc522.PICC_IsNewCardPresent() && !mfrc522.PICC_ReadCardSerial()) { for (byte i = 0; i < mfrc522.uid.size; i++) { String uid_a = String(mfrc522.uid.uidByte[i] < 0x10 ? "0" : ""); String uid_b = String(mfrc522.uid.uidByte[i], HEX); uid_s = uid_s+uid_a+uid_b; } Serial.print("RFID UID rivelato1 --> "); Serial.println(uid_s); delay(500); Serial.println(""); // in uid_s la stringa con il codice della card letta --------- boolean controllo = false; for (int i = 0; i < puntatore; i++){ if(uidRFID[i] == uid_s){ Serial.println("TAG GIA PRESENTE"); controllo = true; break; } } if(!controllo){ if (programma==1) { programma=2; uid_s1 = uid_s; // Serial.println( "Card da registrare -> INVIARE d SU SERIALE PER USCIRE" ); uidRFID[puntatore]=uid_s1; // scrivi in EEPROM EEPROM.write (1,puntatore+1); for (int p=0;p<8;p++) { EEPROM.write(puntatore*8+2+p,byte(uid_s1.charAt(p))); } puntatore++; delay(1000); programma=0; Serial.println("Codice aggiunto"); delay(1000); // break; delay(1000); } } Serial.println(); Serial.println("AVVICINARE NUOVO TAG"); delay(500); //} } } |
Carichiamo lo skecth sulla scheda ed il gioco è fatto!!!
Buon lavoro!
Christian Acqua
Massimo Pinto
Christian Acqua
Massimo Pinto
Nessun commento:
Posta un commento