-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreader.ino
More file actions
286 lines (205 loc) · 6.39 KB
/
reader.ino
File metadata and controls
286 lines (205 loc) · 6.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
/*
Con questo sketch è possibile fare attivare un relè
al passaggio di una tessera magnetica sul lettore RFID usando un server online per la validazione del badge.
Lo script, tramite lo shield ethernet, effettuerà una richiesta al server http://[your-server]/track?card=[ID-card] per ogni badge letto.
Il server dovrà rispondere "RESULT:3" dove il numero indica il numero di accessi rimanenti. Se sarà magggiore di zero verrà abilitato il relè
Autore Pasqui Andrea
PINOUT:
RC522 MODULO Uno/Nano
SDA D10
SCK D13
MOSI D11
MISO D12
IRQ N/A
GND GND
RST D9
3.3V 3.3V
Relè al Pin 5
*/
#include <SPI.h>
#include <Ethernet.h>
#include <RFID.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
/* Vengono definiti PIN del RFID reader*/
#define SDA_DIO 7 // Pin 53 per Arduino Mega
#define RESET_DIO 9
#define delayRead 1000 // Tempo
#define delayLed 2000
#define ledVerde 3
#define ledRosso 3
#define rele 5
#define Eth_cs 10
#define SD_cs 4
/* Viene creata una istanza della RFID libreria */
RFID RC522(SDA_DIO, RESET_DIO);
// inserire tutti i codici esadecimali delle schede magnetiche riconosciute
LiquidCrystal_I2C lcd(0x27,20,4); // set the LCD address to 0x27 for a 16 chars and 2 line display
int loopStatus = 0;
// Enter a MAC address for your controller below.
// Newer Ethernet shields have a MAC address printed on a sticker on the shield
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
// if you don't want to use DNS (and reduce your sketch size)
// use the numeric IP instead of the name for the server:
//IPAddress server(91,134,164,10); // numeric IP for Google (no DNS)
IPAddress server(10,0,0,2); // numeric IP for Google (no DNS)
//char server[] = "coffee.holocron.it"; // name address for Google (using DNS)
// Set the static IP address to use if the DHCP fails to assign
IPAddress ip(10, 0, 0, 101);
IPAddress myDns(10, 0, 0, 1);
// Initialize the Ethernet client library
// with the IP address and port of the server
// that you want to connect to (port 80 is default for HTTP):
EthernetClient client;
// Variables to measure the speed
unsigned long beginMicros, endMicros;
unsigned long byteCount = 0;
bool printWebData = true; // set to false for better speed measurement
void setup()
{
lcd.init();
lcd.backlight();
lcd.setCursor(0,0);
lcd.print("Starting...");
pinMode(Eth_cs,OUTPUT);
pinMode(SD_cs,OUTPUT);
pinMode(rele,OUTPUT);
//Serial.begin(9600);
digitalWrite( SDA_DIO, HIGH );
digitalWrite( Eth_cs, HIGH );
digitalWrite( SD_cs, HIGH );
digitalWrite( rele , HIGH );
SPI.begin();
//Serial.println("Setup RFID");
enableRFID();
RC522.init();
//Serial.println("Setup Ethenet");
enableETH();
Ethernet.init(10);
lcd.setCursor(0,1);
if (Ethernet.begin(mac) == 0) {
//Serial.println("Failed to configure Ethernet using DHCP");
lcd.print("Error DHCP");
// Check for Ethernet hardware present
if (Ethernet.hardwareStatus() == EthernetNoHardware) {
lcd.print("No Ethernet");
while (true) {
delay(1); // do nothing, no point running without Ethernet hardware
}
}
if (Ethernet.linkStatus() == LinkOFF) {
lcd.print("No Ethernet Cable");
}
// try to congifure using IP address instead of DHCP:
Ethernet.begin(mac, ip, myDns);
} else {
lcd.print("DHCP:");
lcd.print(Ethernet.localIP());
}
// give the Ethernet shield a second to initialize:
delay(5000);
lcd.clear();
enableRFID();
loopStatus = 1;
lcd.setCursor(0,0);
lcd.print("Want a coffee?");
}
void loop()
{
/* Temporary loop counter */
byte i;
// Se viene letta una tessera
if (RC522.isCard() && loopStatus == 1)
{
// Viene letto il suo codice
RC522.readCardSerial();
String codiceLetto ="";
lcd.clear();
lcd.print("Badge: ");
// Viene caricato il codice della tessera, all'interno di una Stringa
for(i = 0; i <= 4; i++)
{
codiceLetto+= String (RC522.serNum[i],HEX);
codiceLetto.toUpperCase();
}
lcd.print(codiceLetto);
enableETH();
if (client.connect(server, 80)) {
//Serial.print("connected to ");
//Serial.println(client.remoteIP());
// Make a HTTP request:
client.print("GET /track?card=");
client.print(codiceLetto);
client.println(" HTTP/1.1");
client.println("Host: your.server.com");
client.println("Connection: close");
client.println();
loopStatus = 2;
} else {
// if you didn't get a connection to the server:
lcd.print("Err Server");
}
delay(delayRead);
}
if( loopStatus == 2 ){
int len = client.available();
if (len > 0) {
byte responseBuffer[80];
if (len > 80) len = 80;
client.read(responseBuffer, len);
String bufferString;
for (int k=0; k <= len; k++){ bufferString += (char)responseBuffer[k]; }
int posInStr = bufferString.indexOf("RESULT:");
if( posInStr != -1 ){
String avalableCoffee = bufferString.substring( (posInStr+7) , (posInStr+9) );
processResult( avalableCoffee.toInt() );
}
}
if (!client.connected()) {
loopStatus = 1;
//Serial.println("disconnected");
}
}
}
// Questa funzione verifica se il codice Letto è autorizzato
boolean verificaCodice(String codiceLetto, String codiceAutorizzato){
if(codiceLetto.equals(codiceAutorizzato)){
return true;
}else{
return false;
}
}
// Questa funzione permette di accendere un LED per un determinato periodo
void accendiLed(int ledPin){
digitalWrite(ledPin,HIGH);
delay(delayLed);
digitalWrite(ledPin,LOW);
}
void enableETH(){
digitalWrite( SDA_DIO, HIGH );
digitalWrite( Eth_cs, LOW );
digitalWrite( SD_cs, HIGH );
}
void enableRFID(){
digitalWrite( SDA_DIO, LOW );
digitalWrite( Eth_cs, HIGH );
digitalWrite( SD_cs, HIGH );
}
void processResult( int coffee ){
lcd.setCursor(0,1);
if( coffee > 0 ){
lcd.print("Ready to go!");
lcd.setCursor(0,2);
lcd.print(coffee);
lcd.print(" access left!");
digitalWrite( rele , LOW );
delay(5000);
digitalWrite( rele , HIGH );
}else{
lcd.print("No more access for toady :(");
}
delay(2000);
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Access?");
}