/* mifare ultralight example (25-02-2018) * * RFID-RC522 (SPI connexion) * * CARD RC522 Arduino (UNO) * SDA1 ----------- 10 (Configurable, see SS_PIN constant) * SDA2 ----------- 0 (Configurable, see SS_PIN constant) * SCK ----------- 13 * MOSI ----------- 11 * MISO ----------- 12 * IRQ ----------- * GND ----------- GND * RST ----------- 9 (onfigurable, see RST_PIN constant) * 3.3V ----------- 3.3V * */ #include #include #define SS_1_PIN 10 #define SS_2_PIN 8 #define RST_PIN 9 constexpr uint8_t NR_OF_READERS = 1; byte ssPins[] = {SS_1_PIN, SS_2_PIN}; MFRC522 mfrc522[NR_OF_READERS]; MFRC522::StatusCode status; //variable to get card status byte buffer[18]; //data transfer buffer (16+2 bytes data+CRC) byte size = sizeof(buffer); uint8_t pageAddr = 0x06; //In this example we will write/read 16 bytes (page 6,7,8 and 9). //Ultraligth mem = 16 pages. 4 bytes per page. //Pages 0 to 4 are for special functions. void setup() { Serial.begin(9600); // Initialize serial communications with the PC SPI.begin(); // Init SPI bus for (uint8_t reader = 0; reader < NR_OF_READERS; reader++) { mfrc522[reader].PCD_Init(ssPins[reader], RST_PIN); // Init each MFRC522 card Serial.print(F("Reader ")); Serial.print(reader); Serial.print(F(": ")); mfrc522[reader].PCD_DumpVersionToSerial(); } Serial.println(F("Sketch has been started!")); // memcpy(buffer,"Bqq",16); //String mystring ="bcc"; // mystring.toCharArray(buffer, 16); } String mystring; bool set = true; void loop() { if(set){ Serial.println("What should be written"); while (Serial.available()==0) { //Wait for user input } mystring = Serial.readString(); mystring.toCharArray(buffer, 16); set = false; Serial.println("write :"+mystring); Serial.println("RFID Auflegen"); } if ( ! mfrc522[0].PICC_IsNewCardPresent()) return; // Select one of the cards if ( ! mfrc522[0].PICC_ReadCardSerial()) return; // Write data *********************************************** for (int i=0; i < 4; i++) { //data is writen in blocks of 4 bytes (4 bytes per page) status = (MFRC522::StatusCode) mfrc522[0].MIFARE_Ultralight_Write(pageAddr+i, &buffer[i*4], 4); if (status != MFRC522::STATUS_OK) { Serial.print(F("MIFARE_Read() failed: ")); Serial.println(mfrc522[0].GetStatusCodeName(status)); return; } } Serial.println(F("MIFARE_Ultralight_Write() OK ")); Serial.println(); // Read data *************************************************** Serial.println(F("Reading data ... ")); //data in 4 block is readed at once. status = (MFRC522::StatusCode) mfrc522[0].MIFARE_Read(pageAddr, buffer, &size); if (status != MFRC522::STATUS_OK) { Serial.print(F("MIFARE_Read() failed: ")); Serial.println(mfrc522[0].GetStatusCodeName(status)); return; } Serial.print(F("Readed data: ")); //Dump a byte array to Serial for (byte i = 0; i < 3; i++) { Serial.write(buffer[i]); } Serial.println(); mfrc522[0].PICC_HaltA(); set = true; }