Last active
April 27, 2018 02:27
-
-
Save HectorTorres/0c94e141ddd8c8e41060a45704a2997d to your computer and use it in GitHub Desktop.
rfid-rc522 Arduino uno
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//https://www.hetpro-store.com/TUTORIALES/rc522-rfid-escritura-lectura | |
#include <SPI.h> | |
#include <MFRC522.h> | |
#define RST_PIN 9 // Pin configurable, puedes cambiarlo a tus necesidades | |
#define SS_PIN 10 // Pin configurable, puedes cambiarlo a tus necesidades | |
MFRC522 mfrc522(SS_PIN, RST_PIN); // Crear instancia MFRC522 | |
void setup() { | |
Serial.begin(9600); | |
SPI.begin(); | |
mfrc522.PCD_Init(); | |
Serial.println(F("Escribir datos en MIFARE PICC ")); | |
} | |
void loop() { | |
// Preparar llave, todas las llaves son preconfiguradas de fabrica a FFFFFFFFFFFFh. | |
MFRC522::MIFARE_Key key; | |
for (byte i = 0; i < 6; i++) key.keyByte[i] = 0xFF; | |
// Buscar por tarjetas nuevas | |
if ( ! mfrc522.PICC_IsNewCardPresent()) { | |
return; | |
} | |
// Seleccionar una de las tarjetas | |
if ( ! mfrc522.PICC_ReadCardSerial()) { | |
return; | |
} | |
Serial.print(F("Card UID:")); //Vaciar UID | |
for (byte i = 0; i < mfrc522.uid.size; i++) { | |
Serial.print(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " "); | |
Serial.print(mfrc522.uid.uidByte[i], HEX); | |
} | |
Serial.print(F(" PICC type: ")); // Vaciar PICC | |
MFRC522::PICC_Type piccType = mfrc522.PICC_GetType(mfrc522.uid.sak); | |
Serial.println(mfrc522.PICC_GetTypeName(piccType)); | |
byte buffer[34]; | |
byte block; | |
MFRC522::StatusCode status; | |
byte len; | |
Serial.setTimeout(20000L) ; | |
// Preguntar nombre de la familia | |
Serial.println(F("Escribir nombre de familia, Finaliza con: #")); | |
len = Serial.readBytesUntil('#', (char *) buffer, 30) ; // Leer nombre de familia | |
for (byte i = len; i < 30; i++) buffer[i] = ' '; // Espacios en blanco | |
block = 1; | |
status = mfrc522.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, block, &key, &(mfrc522.uid)); | |
if (status != MFRC522::STATUS_OK) { | |
Serial.print(F("PCD_Authenticate() falla: ")); | |
Serial.println(mfrc522.GetStatusCodeName(status)); | |
return; | |
} | |
else Serial.println(F("PCD_Authenticate() exitoso: ")); | |
// Escribir un bloque | |
status = mfrc522.MIFARE_Write(block, buffer, 16); | |
if (status != MFRC522::STATUS_OK) { | |
Serial.print(F("MIFARE_Write() falla: ")); | |
Serial.println(mfrc522.GetStatusCodeName(status)); | |
return; | |
} | |
else Serial.println(F("MIFARE_Write() exitoso: ")); | |
block = 2; | |
status = mfrc522.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, block, &key, &(mfrc522.uid)); | |
if (status != MFRC522::STATUS_OK) { | |
Serial.print(F("PCD_Authenticate() falla: ")); | |
Serial.println(mfrc522.GetStatusCodeName(status)); | |
return; | |
} | |
// Escribir un bloque | |
status = mfrc522.MIFARE_Write(block, &buffer[16], 16); | |
if (status != MFRC522::STATUS_OK) { | |
Serial.print(F("MIFARE_Write() falla: ")); | |
Serial.println(mfrc522.GetStatusCodeName(status)); | |
return; | |
} | |
else Serial.println(F("MIFARE_Write() exitoso: ")); | |
// Preguntar por el primer nombre | |
Serial.println(F("Escriba primer nombre, terminar con #")); | |
len = Serial.readBytesUntil('#', (char *) buffer, 20) ; // read first name from serial | |
for (byte i = len; i < 20; i++) buffer[i] = ' '; // pad with spaces | |
block = 4; | |
//Serial.println(F("Authenticating using key A...")); | |
status = mfrc522.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, block, &key, &(mfrc522.uid)); | |
if (status != MFRC522::STATUS_OK) { | |
Serial.print(F("PCD_Authenticate() falla: ")); | |
Serial.println(mfrc522.GetStatusCodeName(status)); | |
return; | |
} | |
// Write block | |
status = mfrc522.MIFARE_Write(block, buffer, 16); | |
if (status != MFRC522::STATUS_OK) { | |
Serial.print(F("MIFARE_Write() falla: ")); | |
Serial.println(mfrc522.GetStatusCodeName(status)); | |
return; | |
} | |
else Serial.println(F("MIFARE_Write() exitoso: ")); | |
block = 5; | |
//Serial.println(F("Authenticating using key A...")); | |
status = mfrc522.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, block, &key, &(mfrc522.uid)); | |
if (status != MFRC522::STATUS_OK) { | |
Serial.print(F("PCD_Authenticate() falla: ")); | |
Serial.println(mfrc522.GetStatusCodeName(status)); | |
return; | |
} | |
// Write block | |
status = mfrc522.MIFARE_Write(block, &buffer[16], 16); | |
if (status != MFRC522::STATUS_OK) { | |
Serial.print(F("MIFARE_Write() falla: ")); | |
Serial.println(mfrc522.GetStatusCodeName(status)); | |
return; | |
} | |
else Serial.println(F("MIFARE_Write() exitoso: ")); | |
Serial.println(" "); | |
mfrc522.PICC_HaltA(); | |
mfrc522.PCD_StopCrypto1(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment