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 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462
|
// ***************************************************************
// *RFID + Clavier
// Version D du 20191228
// ***************************************************************
//****************************************************************
// -------------------- Bibliotheque -----------------------------
//****************************************************************
#include <EEPROM.h>
#include <Wire.h> // i2C Conection Library
#include <LiquidCrystal_PCF8574.h>
#include <Password.h>
#include <Keypad.h>
#include <SPI.h>
#include <deprecated.h> // inclut dasn la Bibliotheque MFRC522
#include <MFRC522.h>
#include <MFRC522Extended.h> // inclut dasn la Bibliotheque MFRC522
#include <require_cpp11.h> // inclut dasn la Bibliotheque MFRC522
//****************************************************************
//------------------- Raccordements ------------------------------
//****************************************************************
// Sorties ------------------- ----------------------------------
// Buzzer : Pin 7 // buzzer passif en Pin D7 et GND
// Relais : Pin 8 // Pilote selenoid de verrouillage de porte / entrée relais en broches D8
//***************************************************************
// Connection Module RFID RC522 ----------------------------------
// MOSI: Pin 11
// MISO: Pin 12
// SCK : Pin 13
// SS/SDA: Pin 10
// RST: Pin 9
// RQ: NC
// ----------- Definition ---------------------------------------
#define SS_PIN 10
#define RST_PIN 9
MFRC522 rfid(SS_PIN, RST_PIN);
String tagUID ="" ;
String tagUID1 = "BA 69 97 15";
String tagUID2 = "67 EA 6E 60";
//String tagUID3 = "87 78 FE 24";
//*****************************************************************
// Afficheur LCD I2C ----------------------------------------------
// SDA : Pin A4
// SCL : Pin A5
LiquidCrystal_PCF8574 lcd(0x27);
//*****************************************************************
// Clavier matrice 4x4 --------------------------------------------
const byte ROWS = 4; // Lignes
const byte COLS = 4; // Colonnes
byte rowPins[ROWS] = { 6, 5, 4, 3 }; // Connexion clavier Lignes 1, 2, 3, 4 à ces broches Arduino.
byte colPins[COLS] = { A3, A2, A1, A0 }; // Connexion clavier Colonnes 1, 2, 3, 4 à ces broches Arduino.
char keys[ROWS][COLS] = { // Definition Clavier
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'}
};
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS ); // Creation du clavier
Password password1 = Password("1234"); //<-- PASSWORD1
Password password2 = Password("4329"); //<-- PASSWORD2
//****************************************************************
//****************************************************************
//------------ Constantes & Variables ----------------------------
//****************************************************************
bool access = false;
const int buzzer = 7; // buzzer passif en Pin D7 et GND
const int doorLock = 8; // Pilote selenoid de verrouillage de porte / entrée relais en broches D8
int alarm = 0;
uint8_t alarmStat = 0;
uint8_t maxError = 3;
unsigned long previousMillis = 0;
const long interval = 1000;
uint8_t pwMode = 0;
uint8_t pwPos = 0;
uint8_t adminMode = 0;
String tag = "";
/*
const int tagNbr = 5; // Nombre de TAG
int tagTableauIndice; // Indice dans tagTableau
String tagTableau[tagNbr]; // Tableau des TAG
int eepromAdresseTagTableau; // Adresse de depart en EEPROM
*/
int tagTableauIndice; // Indice dans tagTableau
const int tagNbr = 5; // Nombre de TAG
const int tagSize = 12; // Taille d'un TAG
char tagTableau[tagNbr][tagSize]; // Tableau des TAG 12
int eepromAdresseTagTableau; // Adresse de depart de l'enregistrement en EEPROM
int tagIndex;
// ******************************************************************
// ------------------------ SETUP -----------------------------------
// ******************************************************************
void setup() {
Serial.begin(9600);
lcd.begin(16, 2);
lcd.setBacklight(255);
SPI.begin();
rfid.PCD_Init();
rfid.PCD_DumpVersionToSerial(); // Affichage des données de la bibliothèque
Serial.println(F("Scan PICC to see UID, type, and data blocks..."));
keypad.addEventListener(keypadEvent); // ajouter événement pour le clavier
pinMode(doorLock, OUTPUT);
digitalWrite(doorLock, LOW);
tone (buzzer,1200,100);
lcd.setCursor (0, 0);
lcd.print(F(" - DOOR LOCK - "));
lcd.setCursor (0, 1);
lcd.print(F(" - INIT - "));
delay (2000);
lcd.clear();
noTone (buzzer);
eepromAdresseTagTableau = 0; // Position en EEPROM du tableau des TAG tagTableau
// Mettre ces lignes en remarque quand tableau en EEPROM
tagEnregistrer("FF FF FF FF", 0);
tagEnregistrer("FF FF FF FF", 1);
tagEnregistrer("FF FF FF FF", 2);
tagEnregistrer("FF FF FF FF", 3);
tagEnregistrer("FF FF FF FF", 4);
tagRestaurer();
tagListe();
}
//********************************************************************
// ------------------------ LOOP ------------------------------------
//********************************************************************
void loop() {
keypad.getKey(); // Lecture du clavier
// si nb de tentative max
if (alarm >= maxError) {
ALARME() ;
}
// si tentative en cours
if (alarmStat == 0 && pwMode == 0 && adminMode == 0) {
//lcd.setCursor (0, 0);
//Serial.print(" -Systeme OK- ");
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= interval) {
//previousMillis = currentMillis;
lcd.setCursor (0, 0);
lcd.print(F("Scannez "));
lcd.setCursor (0, 1);
lcd.print(F("une Carte "));
}
if (currentMillis - previousMillis >= (3 * interval)) {
previousMillis = currentMillis;
lcd.setCursor (0, 0);
lcd.print(F("Tapez "));
lcd.setCursor (0, 1);
lcd.print(F("un Code '****' "));
}
}
// Attente d'une carte
if ( ! rfid.PICC_IsNewCardPresent()) {
return;
}
if ( ! rfid.PICC_ReadCardSerial()) {
return;
}
//Reading from the card
Serial.println("Carte detectée");
//rfid.PICC_DumpToSerial(&(rfid.uid));
//delay(2000);
Serial.print("UID de tag :");
String tag = "";
byte caractere;
for (byte i = 0; i < rfid.uid.size; i++)
{
Serial.print(rfid.uid.uidByte[i] < 0x10 ? " 0" : " ");
Serial.print(rfid.uid.uidByte[i], HEX);
tag.concat(String(rfid.uid.uidByte[i] < 0x10 ? " 0" : " "));
tag.concat(String(rfid.uid.uidByte[i], HEX));
}
/*{
Serial.print(rfid.uid.uidByte[i] < 0x10 ? " 0" : " ");
Serial.print(rfid.uid.uidByte[i], DEC);
tag.concat(String(rfid.uid.uidByte[i] < 0x10 ? " 0" : " "));
tag.concat(String(rfid.uid.uidByte[i], DEC));
}*/
tag.toUpperCase();
if (adminMode == 1) {
Serial.println();
Serial.print("N° Carte à enregistrer : ");
Serial.print(tag);
// Tester si TAG existe
Serial.println();
if (tagSiExiste(tag) > -1) {
{Serial.println("TAG " + tag + " connu");}
} else {
{Serial.println("TAG " + tag + " INCONNU");}
// Enregistrement dans EEPROM
tagEnregistrer(String(tag),tagIndex) ;
tagListe();
}
/*EEPROM.put(eepromAdresseTagTableau, tagTableau);
for (int i = 0; i < tagNbr; i++)
{
Serial.println("TAG #" + String(i) + " = " + tagTableau[i]);
}
Serial.println("Carte :"+tag+" enregistrée"); */
adminMode = 0;
pwMode= 0;
}
else
{
// vérifier l'autorisation
tagUID = tagTableau[0][12] ;
if (tag.substring(1) ==(tagUID))
{
Serial.println("Bienvenue!");
lcd.setCursor (3, 0);
lcd.print(F("- ACCES OK - "));
lcd.setCursor (0, 1);
lcd.print("Carte Reconnue");
ACCEPT ();
}
else if (tag.substring(1) == tagUID2)
{
Serial.println("Bienvenue!");
lcd.setCursor (3, 0);
lcd.print(F("- ACCES OK - "));
lcd.setCursor (0, 1);
lcd.print("Carte Reconnue");
ACCEPT ();
}
else {
alarm = alarm + 1;
Serial.println("Non Autorise!");
lcd.setCursor (0, 0);
lcd.print(F(" -ACCES REFUSE-"));
lcd.setCursor (0, 1);
lcd.print ("Carte Invalide");
RIJECT ();
}
}
} // Fin Loop
//*********************************************************************
void keypadEvent(KeypadEvent eKey) {
switch (keypad.getState()) {
case PRESSED:
Serial.print("Pressed: ");
Serial.println(eKey);
tone (buzzer,200,100);
pwMode = 1;
pwPos = pwPos + 1;
if (pwPos == 1) {
lcd.clear();
}
lcd.setCursor (0, 0);
lcd.print(F("Validez avec '*'"));
if (pwPos < 5) {
lcd.setCursor (5 + pwPos, 1);
lcd.print(F("*"));
}
switch (eKey) {
case '*': checkPassword(); break;
//case '#':
// Serial.print(" ");
// password1.reset();
// break;
default:
password1.append(eKey);
password2.append(eKey);
}
}
}
//*************************************************************************
void checkPassword()
{ // test si Mode Normal
if (password1.evaluate()){
Serial.println("MDP Correct");
lcd.setCursor (0, 1);
lcd.print(F(" -Accepte- "));
ACCEPT ();
password1.reset();
pwPos = 0;
} // test si mode ADMIN
else if (password2.evaluate()) {
Serial.println("MDP ADMIN");
lcd.setCursor (0, 1);
lcd.print(F(" -Accepte- "));
//ADMIN ();
lcd.clear();
lcd.setCursor (0, 0);
lcd.print(F(" -Mode ADMIN- "));
Serial.println("Scanner une Nouvelle Carte");
password2.reset();
adminMode = 1;
pwPos = 0;
}
else {
Serial.println("MDP Incorrect");
lcd.setCursor (0, 1);
lcd.print(F(" -Rejete- "));
alarm = alarm + 1;
RIJECT ();
password1.reset();
password2.reset();
pwPos = 0;
}
}
//***********************************************************************
void ACCEPT () {
lcd.clear();
digitalWrite(doorLock, HIGH);
tone (buzzer, 900);
delay(100);
tone (buzzer, 1200);
delay(100);
tone (buzzer, 1800);
delay(200);
noTone(buzzer);
delay(600);
lcd.setCursor (0, 1);
lcd.print(F("Verrouillage:"));
for (int i = 9; i > 0; i--) {
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= interval) {
lcd.setCursor (0, 0);
lcd.print(F("-PORTE OUVERTE-"));
}
if (currentMillis - previousMillis >= (2 * interval)) {
previousMillis = currentMillis;
lcd.setCursor (0, 0);
lcd.print(F(" "));
}
lcd.setCursor (14, 1); lcd.print(i); lcd.print("s");
delay (1000);
}
digitalWrite(doorLock, LOW);
pwMode = 0;
lcd.clear();
}
//***********************************************************************
void RIJECT () {
tone (buzzer, 900);
delay(200);
noTone(buzzer);
delay(200);
tone (buzzer, 900);
delay(200);
noTone (buzzer);
delay(500);
pwMode = 0;
}
//*************************************************************************
void ALARME() {
lcd.clear();
lcd.setCursor (0, 1);
lcd.print(F(" Attendre => "));
for (int i = 30; i > 0; i--) {
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= interval) {
lcd.setCursor (0, 0);
lcd.print(F("- VERROUILLE -"));
}
if (currentMillis - previousMillis >= (2 * interval)) {
previousMillis = currentMillis;
lcd.setCursor (0, 0);
lcd.print(F(" "));
}
tone (buzzer, 200, 500);
lcd.setCursor (13, 1); lcd.print(i);
if (i < 10) {
lcd.setCursor (14, 1);
lcd.print(F("s "));
} else {
lcd.setCursor (15, 1);
lcd.print(F("s"));
}
delay (1000);
}
noTone (buzzer);
alarmStat = 0;
alarm = 0;
lcd.clear();
}
//***************************************************************************
void tagRestaurer() {
EEPROM.get(eepromAdresseTagTableau, tagTableau); // On "regenere" le tableau tagTableau depuis l'EEPROM
}
//***************************************************************************
void tagListe() {
Serial.println("\nListe des TAG (" + String(tagNbr) + ")");
for (int i = 0; i < tagNbr; i++)
{
Serial.println("TAG #" + String(i) + " = " + tagTableau[i]);
}
Serial.println("");
}
//***************************************************************************
void tagEnregistrer(String tagNouveau, int tagIndex)
{
tagNouveau.toCharArray(tagTableau[tagIndex],tagSize);
EEPROM.put(eepromAdresseTagTableau, tagTableau);
}
//***************************************************************************
int tagSiExiste(String tagTest) // Retourne > -1 si tag recherche est dans le tableau
{
int tagIndex = -1; // -1 = n'existe pas
for (int i = 0; i < tagNbr; i++)
{
if ((String)tagTableau[i] == tagTest)
{
tagIndex = i; // Memoriser l'index et quitter la boucle
break;
}
}
return tagIndex; // On retourne l'index du tag teste |