* ------ [RFID1356_03] RFID Bus Example --------
* Explanation: This sketch shows how Libelium's RFID/NFC 13.56 can be
* used for ticketing solutions, in this case we use the RFID/NFC to
* simulate a RFID access control inside a bus.
* First, we assignate a quantity of money to the card (with a secret
* password of course). After that, each trip will substract the fare.
* This can be also used for a gym or a parking, for example.
* Copyright (C) 2012 Libelium Comunicaciones Distribuidas S.L.
* http://www.libelium.com
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
* Implementation: Ahmad Saad, Javier Solobera
// stores the status of the executed command:
// stores the UID (unique identifier) of a card:
// stores the key or password:
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
// EDIT: initial credit to put in the card (in cents!)
// it is also possible to put "credits"
int initialCredit = 800; // this is 8 euros (8*100 = 800)
//int initialCredit = 32000; // this is 320 euros (8*100 = 800)
// EDIT: the price per trip (in cents !)
// it is also possible to put 1 (if they are credits)
int tripFare = 105; // this is 1.05 euros (no problems with floats now)
boolean firstDone = false; // signal if the first credit write was done
int credit = 0; // stores the money inside the card
char text[16]; // for changing from one format to the other
boolean completed = false; // signals if all the process was successfuly completed
USB.println("RFID/NFC @ 13.56 MHz module started");
// switchs ON the module, type B, and asigns the socket
USB.print("\r\n++++++++++++++++++++++++++++++++++++");
// **** init the RFID/NFC reader
state = RFID13.init(UID, aux);
if (aux[0] == aux[1]) // if so, there is no card on the EM field
USB.print("\r\nRequest error - no card found");
// **** authenticate the key A of sector 1
state = RFID13.authenticate(UID, 1, keyAccess);
if (firstDone == false) // this part is for adding credit
// **** if passed authentication in block 1, we are able to set the inital credit
sprintf(text, "%d", initialCredit);
RFID13.string2vector(text, aux);
// **** write aux in block number 1, and check afterwards
state = RFID13.writeAndCheck(aux, 1);
for (int i=0; i<sizeof(text); i++) // clear this variable
// **** check the 16 bytes in block 1
state = RFID13.read(1, aux);
if (state == 0) // if the read command was successful, we show the data (16 bytes)
USB.print("\r\n Initial credit set: ");
if (aux[i] == '\0') // to avoid showing voids
USB.print(aux[i], BYTE); // print the 16 bits of block number 1, now in ASCII code
} // end of "adding credit part"
// **** if passed authentication in block 1, we will be able to read the data in this block (the credit)
state = RFID13.read(1, aux);
if (state == 0) // if the read command was successful, we show the data (16 bytes)
USB.print("\r\n Credit before the trip: ");
if (aux[i] == '\0') // to avoid showing voids
USB.print(aux[i], BYTE); // print the 16 bytes of the block 1 (the first ones are the credit, the rest are NULLs)
credit = RFID13.vector2int(aux); // convert the card data to int
sprintf(text, "%d", credit - tripFare);
RFID13.string2vector(text, aux);
for (int i=0; i<sizeof(text); i++) // clear this variable
// **** write aux in block number 1, and check afterwards
state = RFID13.write(1, aux);
//miSerial.print(" --> correct write checked");
credit = credit - tripFare; // only substract if success happened
// **** check the 16 bytes in block 1
state = RFID13.read(1, aux);
if (state == 0) // if the read command was successful, we show the data (16 bytes)
USB.print("\r\n Credit after the trip: ");
if (aux[i] == '\0') // to avoid showing voids
USB.print(aux[i], BYTE); // print the 16 bits of block number 1, now in ASCII code
USB.print("\r\n ** Not enough credit");
if (completed == true) // a complete operation was done !!!
USB.print("\r\n ACCESS GRANTED !!");
for (int i=0; i<2; i++) // blink the LED fast to show not-OK
for (int i=0; i<10; i++) // blink the LED fast to show not-OK
delay(2000); // wait some time in each loop