-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathRFID-Doorlock.cpp
413 lines (388 loc) · 9.43 KB
/
RFID-Doorlock.cpp
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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
#include "mbed.h"
#include "stdio.h"
#include "EthernetNetIf.h"
#include "HTTPClient.h"
#include "NTPClient.h"
#include <string>
#define DBNAME "/local/db.txt"
#define LOG "/local/logs.txt"
#define CARTESMAX 20
#define TAGMAX 15
#define MASTER "XXXXXXXXXX"
#define LOGLINEMAX 50
LocalFileSystem local("local");
//RFID data & enable
Serial rfidIn(p9,p10);
DigitalOut enable(p11);
//H-Bridge controllers
DigitalOut a(p26);
DigitalOut b(p29);
//MBED Leds
DigitalOut readyLed(LED1);
DigitalOut greenLed(p23);
DigitalOut redLed(p22);
DigitalOut statusLed(LED2);
DigitalOut errorLed(LED4);
/*
1
2 6
7
4 8
9 3
*/
DigitalOut seg1(p5);
DigitalOut seg2(p6);
DigitalOut seg4(p7);
DigitalOut seg6(p8);
DigitalOut seg7(p12);
DigitalOut seg8(p13);
DigitalOut seg9(p14);
DigitalIn status(p16);
DigitalIn setTweet(p17);
DigitalIn up(p19);
DigitalIn down(p18);
DigitalIn sendTweet(p20);
EthernetNetIf eth;
int readDB(char tags[CARTESMAX][TAGMAX]);
int checkTag(char tags[CARTESMAX][TAGMAX], char tag[TAGMAX], int nbTags);
int giveResult(int statut);
int openDoor(int statut);
int logAttempt(char tag[TAGMAX], int valid);
int addTag(char tag[TAGMAX]);
int printTags(char tags[CARTESMAX][TAGMAX], int nbTags);
void dispDigit(int i);
int tweet(char tweetMsg[20]);
int setupNetwork();
int main() {
//INIT
redLed = 1;
a=1;
b=1;
dispDigit(11);
enable = 1;
printf("\n\r");
//variables
char tags[CARTESMAX][TAGMAX];
char buffer[TAGMAX];
char tweetMsg[20];
int valid = 0;
int master = 0;
int nbTags, hours, statusMove;
//Baud settings for Serial
rfidIn.baud(2400);
//setup ethernet
//redLed = !setupNetwork(eth);
//Read tags & nbTags
nbTags = readDB(tags);
if(nbTags>0){
printf("READ DB OK (%d tags)\n\r",nbTags);
readyLed = 1;
redLed = 0;
enable = 0;
} else {
printf("READ DB NOK\n\r");
while(1){
errorLed = !errorLed;
wait(0.5);
}
}
if (status){
hours = 1;
} else {
hours = 42;
}
statusMove = status;
while(1) {
dispDigit(hours);
if (rfidIn.readable() && enable == 0 && master == 0 && !setTweet){
enable = 1;
rfidIn.scanf("%s", buffer);
if(strcmp (buffer,MASTER) == 0){
master = 1;
}else{
valid = checkTag(tags, buffer, nbTags);
logAttempt(buffer, valid);
printf("%s", buffer);
printf(" >>> %d\n\r", valid);
giveResult(valid);
enable = openDoor(valid);
}
}
if (master == 1 && !setTweet){
printTags(tags, nbTags);
wait(3);
printf("Scan new card now!\n\r");
enable = 0;
while(1){
if (rfidIn.readable()){
enable = 1;
rfidIn.scanf("%s", buffer);
addTag(buffer);
break;
}
}
nbTags = readDB(tags);
if(nbTags>0){
printf("READ DB OK (%d tags)\n\r",nbTags);
} else {
printf("READ DB NOK\n\r");
while(1){
errorLed = !errorLed;
wait(0.5);
}
}
printTags(tags, nbTags);
wait(3);
master = 0;
enable = 0;
}
if (status && !statusMove){
hours = 1;
statusMove = status;
}
if (!status && statusMove){
hours = 42;
statusMove = status;
}
if (status && up && hours < 9){
wait(.3);
hours++;
}
if (status && down && hours > 1){
wait(.3);
hours--;
}
if (sendTweet){
wait(.3);
if(status){
sprintf(tweetMsg, "do=custom&hours=%d",hours);
} else {
strcpy(tweetMsg, "do=close");
}
setupNetwork();
giveResult(tweet(tweetMsg));
}
}
}
//FUNCTIONS
int readDB(char tags[CARTESMAX][TAGMAX]){
FILE *fichier;
int i;
int result = 0;
char ligne[TAGMAX];
fichier = fopen(DBNAME,"r");
if ( fichier != NULL) {
i=0;
fgets (ligne, TAGMAX, fichier);
while (!feof(fichier)) {
sscanf (ligne, "%s", tags[i]);
i++;
fgets (ligne, TAGMAX, fichier);
}
result = i;
fclose (fichier);
}
return result;
}
int checkTag(char tags[CARTESMAX][TAGMAX], char tag[TAGMAX], int nbTags){
int valid = 0;
int i;
for (i=0;i<nbTags;i++){
if (strcmp (tag,tags[i]) == 0){
valid = 1;
}
}
return valid;
}
int giveResult(int statut){
switch ( statut ){
case 0:
redLed = 1;
wait(0.5);
redLed = 0;
break;
case 1:
greenLed = 1;
wait(0.5);
greenLed = 0;
break;
}
return NULL;
}
int openDoor(int statut){
if (statut == 1){
a=0;
wait(0.5);
a=1;
wait(5);
b=0;
wait(0.5);
b=1;
}
return 0;
}
int logAttempt(char tag[TAGMAX], int valid){
FILE *fichier;
char ligne[LOGLINEMAX];
fichier = fopen(LOG, "a");
if ( fichier == NULL) {
printf("cant find %s\n\r",LOG);
} else {
sprintf(ligne, "%s >>> %i\n", tag, valid);
fputs (ligne, fichier);
fclose (fichier);
}
return NULL;
}
int addTag(char tag[TAGMAX]){
FILE *fichier;
char ligne[TAGMAX];
int result = 0;
fichier = fopen(DBNAME,"a");
if ( fichier == NULL) {
printf("cant find %s\n\r",DBNAME);
}else {
sprintf(ligne, "%s\n", tag);
fputs (ligne, fichier);
fclose (fichier);
result = 1;
printf("ADDED %s ON DB.txt\n\r",ligne);
}
return result;
}
int printTags(char tags[CARTESMAX][TAGMAX], int nbTags){
printf("List of cards allowed:\n\r");
for(int i=0;i<nbTags;i++){
printf("%s\n\r",tags[i]);
}
return NULL;
}
void dispDigit(int i){
switch(i){
case 1: seg1 = 1;
seg2 = 1;
seg4 = 1;
seg6 = 0;
seg7 = 1;
seg8 = 0;
seg9 = 1;
break;
case 2: seg1 = 0;
seg2 = 1;
seg4 = 0;
seg6 = 0;
seg7 = 0;
seg8 = 1;
seg9 = 0;
break;
case 3: seg1 = 0;
seg2 = 1;
seg4 = 1;
seg6 = 0;
seg7 = 0;
seg8 = 0;
seg9 = 0;
break;
case 4: seg1 = 1;
seg2 = 0;
seg4 = 1;
seg6 = 0;
seg7 = 0;
seg8 = 0;
seg9 = 1;
break;
case 5: seg1 = 0;
seg2 = 0;
seg4 = 1;
seg6 = 1;
seg7 = 0;
seg8 = 0;
seg9 = 0;
break;
case 6: seg1 = 0;
seg2 = 1;
seg4 = 0;
seg6 = 0;
seg7 = 0;
seg8 = 0;
seg9 = 0;
break;
case 7: seg1 = 0;
seg2 = 1;
seg4 = 1;
seg6 = 0;
seg7 = 1;
seg8 = 0;
seg9 = 1;
break;
case 8: seg1 = 0;
seg2 = 0;
seg4 = 0;
seg6 = 0;
seg7 = 0;
seg8 = 0;
seg9 = 0;
break;
case 9: seg1 = 0;
seg2 = 0;
seg4 = 1;
seg6 = 0;
seg7 = 0;
seg8 = 0;
seg9 = 0;
break;
case 0: seg1 = 0;
seg2 = 0;
seg4 = 0;
seg6 = 0;
seg7 = 1;
seg8 = 0;
seg9 = 0;
break;
case 42:seg1 = 0;
seg2 = 0;
seg4 = 0;
seg6 = 1;
seg7 = 1;
seg8 = 1;
seg9 = 0;
break;
default: seg1 = 1;
seg2 = 1;
seg4 = 1;
seg6 = 1;
seg7 = 1;
seg8 = 1;
seg9 = 1;
break;
}
}
int tweet(char tweetMsg[20]){
int success = 0;
HTTPClient twitter;
char url[200];
sprintf(url, "http://fixme.ch/cgi-bin/twitter.pl?%s",tweetMsg);
HTTPResult r = twitter.get(url, NULL);
if( r == HTTP_OK ){
printf("Tweet sent with success!\n\r");
success = 1;
}else{
printf("Problem during tweeting, return code %d\n", r);
success = 0;
}
return success;
}
int setupNetwork(){
int result = 1;
printf("Init\n\r");
IpAddr ip = eth.getIp();
if(ip.isNull()){
printf("Setting up...\n\r");
EthernetErr ethErr = eth.setup(30000);
if(ethErr){
printf("Error %d in setup.\n\r", ethErr);
result = 0;
}
}
return result;
}