Contoh kode Arduino Menulis String data ke Mifare RFID Tag

contoh string yg akan ditulis  “tgl 20/07/2016 NO ST:001/ST/SDI-SYM/VII/2016 Rp.50.000,-#” .
string diatas dikirim dari serial port dan di simpan kedalam array buffer[64].
krn tiap 1 blok tag rfid cuma ada 16 byte maka string diatas ditulis ke dalam 4 blok yaitu blok 1,2 4 dan 5
blok 3,7,11, 15, 19… dst tdk boleh untuk menyimpan data krn berisi data untuk authentifikasi

/*
*menulis string ke MIFARE RFID card menggunakan RFID-RC522 reader
—————————————————————————–
* Pin layout should be as follows:
* Signal Pin Pin Pin
* Arduino Uno Arduino Mega MFRC522 board
* ————————————————————
* Reset 9 5 RST
* SPI SS 10 53 SDA
* SPI MOSI 11 52 MOSI
* SPI MISO 12 51 MISO
* SPI SCK 13 50 SCK
*————————————————————–

*/

#include  SPI.h
#include  MFRC522.h
//atur kembali konfigurasi pin sesuai modul arduino anda
#define SS_PIN 10 //Arduino Uno
#define RST_PIN 9
MFRC522 mfrc522(SS_PIN, RST_PIN); // Create MFRC522 instance.

void setup() {
Serial.begin(9600);
SPI.begin(); // Init SPI bus
mfrc522.PCD_Init(); // Init MFRC522 card
Serial.println(“Write string data on a MIFARE PICC “);
}

void loop() {

// Prepare key – all keys are set to FFFFFFFFFFFFh at chip delivery from the factory.
MFRC522::MIFARE_Key key;
for (byte i = 0; i < 6; i++) key.keyByte[i] = 0xFF;

// Look for new cards
if ( ! mfrc522.PICC_IsNewCardPresent()) {
return;
}

// Select one of the cards
if ( ! mfrc522.PICC_ReadCardSerial()) return;

Serial.print(“Card UID:”); //kirim UID ke serial port
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(” PICC type: “); // kirim PICC type ke serial port
byte piccType = mfrc522.PICC_GetType(mfrc522.uid.sak);
Serial.println(mfrc522.PICC_GetTypeName(piccType));

byte buffer[64]; //untuk menampung string dari serial port
byte block;
byte status, len;

Serial.setTimeout(20000L) ; // time out serial port 20 detik
// input string
Serial.println(“masukan data string akhiri dgn #”);
len=Serial.readBytesUntil(‘#’, (char *) buffer, 64) ; // terima data string dari serial
for (byte i = len; i < 64; i++) buffer[i] = ‘\s’; // sisa bufer disi kode spasi

block = 1;
//Serial.println(“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(“PCD_Authenticate() failed: “);
Serial.println(mfrc522.GetStatusCodeName(status));
return;
}

// Write block
status = mfrc522.MIFARE_Write(block, buffer, 16);
if (status != MFRC522::STATUS_OK) {
Serial.print(“MIFARE_Write() failed: “);
Serial.println(mfrc522.GetStatusCodeName(status));
return;
}
else Serial.println(“MIFARE_Write() success: “);

block = 2;
//Serial.println(“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(“PCD_Authenticate() failed: “);
Serial.println(mfrc522.GetStatusCodeName(status));
return;
}

// Write block
status = mfrc522.MIFARE_Write(block, &buffer[16], 16);
if (status != MFRC522::STATUS_OK) {
Serial.print(“MIFARE_Write() failed: “);
Serial.println(mfrc522.GetStatusCodeName(status));
return;
}
else Serial.println(“MIFARE_Write() success: “);
block = 4;
//Serial.println(“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(“PCD_Authenticate() failed: “);
Serial.println(mfrc522.GetStatusCodeName(status));
return;
}

// Write block
status = mfrc522.MIFARE_Write(block, &buffer[32], 16);
if (status != MFRC522::STATUS_OK) {
Serial.print(“MIFARE_Write() failed: “);
Serial.println(mfrc522.GetStatusCodeName(status));
return;
}
else Serial.println(“MIFARE_Write() success: “);

block = 5;
//Serial.println(“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(“PCD_Authenticate() failed: “);
Serial.println(mfrc522.GetStatusCodeName(status));
return;
}

// Write block
status = mfrc522.MIFARE_Write(block, &buffer[48], 16);
if (status != MFRC522::STATUS_OK) {
Serial.print(“MIFARE_Write() failed: “);
Serial.println(mfrc522.GetStatusCodeName(status));
return;
}
else Serial.println(“MIFARE_Write() success: “);

Serial.println(” “);
mfrc522.PICC_HaltA(); // Halt PICC
mfrc522.PCD_StopCrypto1(); // Stop encryption on PCD

}

 

Advertisements

About pccontrol

Berisi Tutorial Menggunakan PC untuk mengontrol Peralatan dengan cara mudah & praktis.

Posted on 04/08/2016, in Arduino, Daftar isi. Bookmark the permalink. 3 Comments.

  1. Olhe рara fߋra da caixa e comece hoje mᥱsmo a trilhar
    camіnho ɗe ԛuem busca deveras аs boas opçõеs
    Ԁe investimento qսe a crise possui propiciado. http://Canyouhack.it/Forum/index.php?action=profile&u=267627

  2. bang bagimana mengisi data pada rfid yang berupa nama kendaraan??

Komentar ,Saran atau Pertanyaan

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: