Code examples and extended information
In the Waspmote Development section you can find complete examples:
1
/*
2
* ------ [RFID1356_03] RFID Bus Example --------
3
*
4
* Explanation: This sketch shows how Libelium's RFID/NFC 13.56 can be
5
* used for ticketing solutions, in this case we use the RFID/NFC to
6
* simulate a RFID access control inside a bus.
7
* First, we assignate a quantity of money to the card (with a secret
8
* password of course). After that, each trip will substract the fare.
9
* This can be also used for a gym or a parking, for example.
10
*
11
* Copyright (C) 2012 Libelium Comunicaciones Distribuidas S.L.
12
* http://www.libelium.com
13
*
14
* This program is free software: you can redistribute it and/or modify
15
* it under the terms of the GNU General Public License as published by
16
* the Free Software Foundation, either version 3 of the License, or
17
* (at your option) any later version.
18
*
19
* This program is distributed in the hope that it will be useful,
20
* but WITHOUT ANY WARRANTY; without even the implied warranty of
21
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22
* GNU General Public License for more details.
23
*
24
* You should have received a copy of the GNU General Public License
25
* along with this program. If not, see <http://www.gnu.org/licenses/>.
26
*
27
* Version: 0.1
28
* Design: David Gascon
29
* Implementation: Ahmad Saad, Javier Solobera
30
*/
31
32
33
#include <WaspRFID13.h>
34
35
// stores the status of the executed command:
36
uint8_t state;
37
// auxiliar buffer:
38
uint8_t aux[16];
39
// stores the UID (unique identifier) of a card:
40
uint8_t UID[4];
41
// stores the key or password:
42
uint8_t keyAccess[] = {
43
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
44
// EDIT: initial credit to put in the card (in cents!)
45
// it is also possible to put "credits"
46
int initialCredit = 800; // this is 8 euros (8*100 = 800)
47
//int initialCredit = 32000; // this is 320 euros (8*100 = 800)
48
49
// EDIT: the price per trip (in cents !)
50
// it is also possible to put 1 (if they are credits)
51
int tripFare = 105; // this is 1.05 euros (no problems with floats now)
52
53
boolean firstDone = false; // signal if the first credit write was done
54
int credit = 0; // stores the money inside the card
55
char text[16]; // for changing from one format to the other
56
boolean completed = false; // signals if all the process was successfuly completed
57
58
59
60
void setup()
61
{
62
USB.ON();
63
USB.println("RFID/NFC @ 13.56 MHz module started");
64
// switchs ON the module, type B, and asigns the socket
65
RFID13.ON(SOCKET0);
66
delay(1000);
67
}
68
69
70
71
void loop()
72
{
73
USB.print("\r\n++++++++++++++++++++++++++++++++++++");
74
// **** init the RFID/NFC reader
75
state = RFID13.init(UID, aux);
76
if (aux[0] == aux[1]) // if so, there is no card on the EM field
77
{
78
USB.print("\r\nRequest error - no card found");
79
}
80
else // a card was found
81
{
82
// **** authenticate the key A of sector 1
83
state = RFID13.authenticate(UID, 1, keyAccess);
84
if (firstDone == false) // this part is for adding credit
85
{
86
// **** if passed authentication in block 1, we are able to set the inital credit
87
sprintf(text, "%d", initialCredit);
88
RFID13.string2vector(text, aux);
89
// **** write aux in block number 1, and check afterwards
90
state = RFID13.writeAndCheck(aux, 1);
91
if (state == 0)
92
{
93
for (int i=0; i<sizeof(text); i++) // clear this variable
94
{
95
text[i] = '\0';
96
}
97
firstDone = true;
98
// **** check the 16 bytes in block 1
99
state = RFID13.read(1, aux);
100
if (state == 0) // if the read command was successful, we show the data (16 bytes)
101
{
102
USB.print("\r\n Initial credit set: ");
103
for (int i=0; i<16; i++)
104
{
105
if (aux[i] == '\0') // to avoid showing voids
106
break;
107
USB.print(aux[i], BYTE); // print the 16 bits of block number 1, now in ASCII code
108
}
109
}
110
}
111
} // end of "adding credit part"
112
// **** if passed authentication in block 1, we will be able to read the data in this block (the credit)
113
state = RFID13.read(1, aux);
114
if (state == 0) // if the read command was successful, we show the data (16 bytes)
115
{
116
USB.print("\r\n Credit before the trip: ");
117
for (int i=0; i<16; i++)
118
{
119
if (aux[i] == '\0') // to avoid showing voids
120
break;
121
USB.print(aux[i], BYTE); // print the 16 bytes of the block 1 (the first ones are the credit, the rest are NULLs)
122
}
123
credit = RFID13.vector2int(aux); // convert the card data to int
124
if (credit >= tripFare)
125
{
126
sprintf(text, "%d", credit - tripFare);
127
RFID13.string2vector(text, aux);
128
for (int i=0; i<sizeof(text); i++) // clear this variable
129
{
130
text[i] = '\0';
131
}
132
// **** write aux in block number 1, and check afterwards
133
state = RFID13.write(1, aux);
134
if (state == 0)
135
{
136
//miSerial.print(" --> correct write checked");
137
completed = true;
138
credit = credit - tripFare; // only substract if success happened
139
// **** check the 16 bytes in block 1
140
state = RFID13.read(1, aux);
141
if (state == 0) // if the read command was successful, we show the data (16 bytes)
142
{
143
USB.print("\r\n Credit after the trip: ");
144
for (int i=0; i<16; i++)
145
{
146
if (aux[i] == '\0') // to avoid showing voids
147
break;
148
USB.print(aux[i], BYTE); // print the 16 bits of block number 1, now in ASCII code
149
}
150
}
151
}
152
}
153
else
154
{
155
USB.print("\r\n ** Not enough credit");
156
}
157
}
158
}
159
if (completed == true) // a complete operation was done !!!
160
{
161
USB.print("\r\n ACCESS GRANTED !!");
162
completed = false;
163
for (int i=0; i<2; i++) // blink the LED fast to show not-OK
164
{
165
Utils.blinkLEDs(50);
166
}
167
}
168
else
169
{
170
for (int i=0; i<10; i++) // blink the LED fast to show not-OK
171
{
172
Utils.blinkLEDs(50);
173
}
174
}
175
delay(2000); // wait some time in each loop
176
}
Copied!
Copy link