Code examples and extended information
In the Waspmote Development section you can find complete examples:
http://www.libelium.com/development/waspmote/examples
/*
* ------ [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/>.
*
* Version: 0.1
* Design: David Gascon
* Implementation: Ahmad Saad, Javier Solobera
*/
#include <WaspRFID13.h>
// stores the status of the executed command:
uint8_t state;
// auxiliar buffer:
uint8_t aux[16];
// stores the UID (unique identifier) of a card:
uint8_t UID[4];
// stores the key or password:
uint8_t keyAccess[] = {
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
void setup()
{
USB.ON();
USB.println("RFID/NFC @ 13.56 MHz module started");
// switchs ON the module, type B, and asigns the socket
RFID13.ON(SOCKET0);
delay(1000);
}
void loop()
{
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");
}
else // a card was 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);
if (state == 0)
{
for (int i=0; i<sizeof(text); i++) // clear this variable
{
text[i] = '\0';
}
firstDone = true;
// **** 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: ");
for (int i=0; i<16; i++)
{
if (aux[i] == '\0') // to avoid showing voids
break;
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: ");
for (int i=0; i<16; i++)
{
if (aux[i] == '\0') // to avoid showing voids
break;
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
if (credit >= tripFare)
{
sprintf(text, "%d", credit - tripFare);
RFID13.string2vector(text, aux);
for (int i=0; i<sizeof(text); i++) // clear this variable
{
text[i] = '\0';
}
// **** write aux in block number 1, and check afterwards
state = RFID13.write(1, aux);
if (state == 0)
{
//miSerial.print(" --> correct write checked");
completed = true;
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: ");
for (int i=0; i<16; i++)
{
if (aux[i] == '\0') // to avoid showing voids
break;
USB.print(aux[i], BYTE); // print the 16 bits of block number 1, now in ASCII code
}
}
}
}
else
{
USB.print("\r\n ** Not enough credit");
}
}
}
if (completed == true) // a complete operation was done !!!
{
USB.print("\r\n ACCESS GRANTED !!");
completed = false;
for (int i=0; i<2; i++) // blink the LED fast to show not-OK
{
Utils.blinkLEDs(50);
}
}
else
{
for (int i=0; i<10; i++) // blink the LED fast to show not-OK
{
Utils.blinkLEDs(50);
}
}
delay(2000); // wait some time in each loop
}
Last updated