ESP32 + RFID + 512k EEPROM + 2.4'' TFT + Logging Door Lock
RFID door lock with ESP32 + Microchip 25AA512 SPI EEPROM + SPI 2.4 TFT + WiFi logging on server.
Components and supplies
1
Previous Versions RFID Reader ID-12LA (125 kHz)
1
2.4" SPI TFT LCD Display 2.4 Inch Touch Panel LCD ILI9341 240x320 5V/3.3V
1
25AA512-I/P Memory EEPROM SPI 64kx8bit 1.8÷5.5V 20MHz DIP8
1
Espressif ESP32 Development Board - Developer Edition
1
RobotGeek Relay
1
Glass Reed Relay Switch N/O 2mmX14mm Gold Plated Contact Point Heavy Duty
Project description
Code
ESP32_RFID_TFT_EEPROM_LOG.ino
arduino
1 2#include <SPI.h> // SPI library for TFT & Eeprom 3#include <TFT_eSPI.h> // Graphics and font library for ST7735 driver chip 4#include <WiFi.h> // WiFi library for network 5#include <TimeLib.h> // Time library for time 6#include <NtpClientLib.h> // NTP Client library fot network time 7 8// including 3 320x240 pixel logo 9#include "logo.h" // Konica Minolta logo 10#include "denied.h" // Denied logo 11#include "granted.h" // Granted logo 12#include "swipe.h" // Granted logo 13 14//define pin in/out 15#define EE_CS 2 // goes to eeporm CS 16#define _cs 4 // goes to TFT CS 17#define _dc 0 // goes to TFT & EEPROM DC 18#define _mosi 23 // goes to TFT MOSI & EERPOM MISO 19#define _sclk 18 // goes to TFT & EEPROM SCK/CLK 20#define _rst 5 // goes to TFT RESET 21#define _miso 19 // goes to EEPROM mosi 22// 3.3V // Goes to TFT & Eeprom Vcc & TFT LED 23// Gnd // Goes to TFT & Eeprom Vss 24#define eepromreset 12 // detect press key for delete card index from Eeprom 25#define doorPin 14 // connect to door opener relay 26 27#define BUFF_SIZE 64 28 29const char* ssid = "xxxx "; 30const char* password = "xxxx"; 31 32//const char* ssid = "xxxx"; // wifi SSD 33//const char* password = "xxxx"; // wifi password 34 35String NTPServer = "x.x.x.x"; // NTP server address 36const char* socketserver = "x.x.x.x";// logig server 37 38String cardstr = "0"; // define cardstr 39int8_t timeZone = 1; // timezone in central europe (hour) 40int8_t minutesTimeZone = 0; // timezone in central europe (miniute) 41 42byte olvas, cim = 0; 43char val; 44boolean programMode = false; 45boolean match = false; 46byte fnb = 0; // upper 8 bit 47byte anb = 1; // lower 8 bit 48unsigned int address; 49byte storedCard[6]; // stored byte array 50byte readCard[6]; // readed byte array 51byte checksum = 0; // Checksum 52 53HardwareSerial Serial2(2); // 2nd Serial port (IO16-> RXD, IO17-> TXD) 54TFT_eSPI tft = TFT_eSPI(); // TFT driver (don't forget change settings in User_Setup.h (pin out and driver chip) 55 56void setup() { 57 58 Serial.begin(115200); // initialize serial port 0 for debug 59 Serial2.begin(9600); // initialize serial port 2 for communicate to ID-12 card reader connet to IO16(RXD) 60 61 WiFi.begin(ssid, password); // initialize WiFi 62 63 tft.init(); // initialize TFT 64 tft.setRotation(3); // rotate 0 degree 65 tft.fillScreen(TFT_BLACK); // clear the screen with black pixel 66 tft.setTextColor(TFT_BLACK, TFT_BLACK); // text color 67 68 tft.fillScreen(TFT_WHITE); 69 70 drawIcon(logo, 0,0, logoWidth, logoHeight); // draw 128x128pix Konica Minolta logo; 71 72 73 tft.setCursor(0,4 74 ); 75 tft.print(" "); // show Wifi connection status 76 tft.drawString("Connecting to Wifi:", 2, 0, 2); 77 while (WiFi.status() != WL_CONNECTED) { 78 delay(500); 79 tft.print("."); 80 } 81 // stop socket connection 82 83 SPI.begin(); // initialize SPI 84 pinMode(doorPin, OUTPUT); // set relay pin to output 85 pinMode(eepromreset, INPUT); // set eeprom reset pin to input 86 87 digitalWrite(eepromreset, LOW); // set eeprom reset pin to high 88 digitalWrite(doorPin, HIGH); // set door pin to high 89 90 pinMode(EE_CS,OUTPUT); // eeprom CS pin to output 91 digitalWrite(EE_CS, HIGH); // set CS pin to high 92 93 NTP.begin (NTPServer, timeZone, true, minutesTimeZone); // initialize NTP 94 NTP.setInterval (1200); // sync interval 95 96 tft.fillScreen(TFT_WHITE); 97 98 WiFiClient client; // initialize socket connection 99 client.connect(socketserver,10000); // open socket port 100 client.print(String(NTP.getTimeDateString ())); // send time and date to loging sever 101 client.print(String(";")); // send semicolon (excel recognize next row) 102 client.print(String("0000000000")); 103 client.print(String(";")); // send semicolon 104 client.println(String("CLIENT RESTARTED!!!!")); // send card status to loging server 105 client.stop(); 106 107 drawIcon(logo, 0,0, logoWidth, logoHeight); // redraw 128x128pix Konica Minolta logo 108} 109 110void loop () 111{ 112 113 if (digitalRead(eepromreset) == HIGH) {eeprom_erase(); } // erase eeprom when button is pressed 114 byte val = 0; // 115 116 normalModeOn(); // 117 118 119 if ( programMode) // program mode wait for new card data 120 { 121 programModeOn(); // go to program mode 122 123 if(Serial2.available() > 0) // wait for data from serial port 124 { 125 if((val = Serial2.read()) == 2) // first 2 byte is STX byte 126 { 127 getID(); // read data from card 128 if ( !isMaster(readCard) ) // check MASTER card 129 { 130 writeID(readCard); // if yes, the store the new card 131 programMode = false; // turn off program mode 132 checksum = 0; // checksum celar 133 } 134 } 135 } 136 137 138 } 139 140 // normal mode 141 // ------------------------------------------------------------------ 142 else 143 { 144 if(Serial2.available() > 0) // if serial port is active 145 { 146 if((val = Serial2.read()) == 2) // first 2 byte is STX 147 { 148 getID(); // read out the card ID 149 byte bytesread = 0; 150 151 for ( int i = 0; i < 5; i++ ) // read out 5 byte 152 { 153 if ( readCard[i] < 16 ) 154 delay(0); 155 156 } 157 158 if ( readCard[5] == checksum ) // check the chechsum between readed with calculated 159 { 160 checksum = 0; 161 if ( isMaster( readCard ) ) // if MASTER card 162 { 163 programMode = true; // then set program mode on 164 165 } 166 else 167 { 168 if ( findID(readCard) ) // search card in stored database 169 { 170 openDoor(5); // if find then open door and show granted logo 171 172 } 173 else 174 { 175 failed(); // if not find card data from database and show denied logo 176 } 177 } 178 } 179 else // if checksum is wrong 180 { 181 182 183 } 184 } 185 } 186 } 187 188} 189 190void eeprom_erase() 191 { 192 drawIcon(logo, 0,0, logoWidth, logoHeight); // redraw 128x128pix Konica Minolta logo 193 tft.setTextSize(4); // set font size 2 194 tft.setTextColor(TFT_RED); // set font color red 195 196 tft.drawCentreString("EEPROM", 160, 15, 2); // draw red "EEPROM ERASE STARTED" to TFT 197 tft.drawCentreString("ERASE", 160, 60, 2); 198 tft.drawCentreString("STARTED", 160, 105, 2); 199 delay(1000); // wait 500ms 200 201 drawIcon(logo, 0,0, logoWidth, logoHeight); // redraw 128x128pix Konica Minolta logo 202 203 pinMode(EE_CS, OUTPUT); // eeprom CS out 204 digitalWrite(EE_CS, HIGH); // eeprom CS high (initialization) 205 delay (1); // wait 1ms 206 digitalWrite(EE_CS, LOW); // set eeprom chip select pin to high (chip is active) 207 SPI.transfer(1); // initialize eeprom 208 SPI.transfer(0); // initialize eeprom 209 digitalWrite(EE_CS, HIGH); // set eeprom chip selec pint to low 210 211 for (unsigned int j = 0; j < 2; ) 212 { 213 digitalWrite(EE_CS, LOW); // set eeprom chip select pin to high (chip is active) 214 SPI.transfer(6); // write enabled 6 215 digitalWrite(EE_CS, HIGH); // set eeprom chip select pin to high (chip is inactive) 216 217 digitalWrite(EE_CS, LOW); // set eeprom chip select pin to high (chip is active) 218 SPI.transfer(2); // write 2,0 219 SPI.transfer(j >> 8); // eepro address upper 8 bit 220 SPI.transfer(j & 0x00FF); // eeprom address lower 8 bit 221 SPI.transfer(0); // data write to eeprom 222 digitalWrite(EE_CS, HIGH); // set eeprom chip select pin to high (chip is inactive) 223 delay(4); // delay 4ms to wite is gone 224 225 j++; 226 227 digitalWrite(EE_CS, LOW); // set eeprom chip select pin to high (chip is active) 228 SPI.transfer(4); // write disabled 4 229 digitalWrite(EE_CS, HIGH); // set eeprom chip select pin to high (chip is inactive) 230 231 } 232 233 drawIcon(logo, 0,0, logoWidth, logoHeight); // redraw 128x128pix Konica Minolta logo 234 235 // write date and time under logo 236 tft.setTextSize(4); // set font size 2 237 tft.setTextColor(TFT_GREEN); // set font color green 238 tft.drawCentreString("EEPROM", 160, 15, 2); // draw red "EEPROM ERASE SUCCES" to TFT 239 tft.drawCentreString("ERASING", 160, 60, 2); 240 tft.drawCentreString("SUCCES", 160, 105, 2); 241 delay(1000); // wait 500ms 242 243 WiFiClient client; // initialize socket connection 244 client.connect(socketserver,10000); // open socket port 245 client.print(String(NTP.getTimeDateString ())); // send time and date to loging sever 246 client.print(String(";")); // send semicolon (excel recognize next row) 247 client.print(String("0000000000")); 248 client.print(String(";")); // send semicolon 249 client.println(String("EEPROM ERASED!!!!!")); // send card status to loging server 250 client.stop(); // stop socket connection 251 252 253 drawIcon(logo, 0,0, logoWidth, logoHeight); // redraw 128x128pix Konica Minolta logo 254 } 255 256void normalModeOn() 257{ 258 259if ( programMode) // program mode wait for new card data 260 { 261 normalModeOn2(); 262 } 263 264 else { 265 drawIcon(swipe, 0, 0, swipeWidth, swipeHeight); // redraw 128x128pix Konica Minolta logo 266 267 268 tft.setCursor(50, 225); // set text position 269 tft.setTextColor(TFT_BLACK, TFT_WHITE); // set font color to black with white background 270 tft.setTextSize(2); // set font size 1 271 tft.print(NTP.getTimeDateString ()); // print to tft current time and date hh:mm:ss dd/mm/yyyy 272 tft.setTextSize(3); // set font size 2 273 tft.setTextColor(TFT_BLACK); // set font color black 274 275 } 276} 277void normalModeOn2() 278{ 279 delay(0); 280} 281 282void programModeOn() 283{ 284 285 drawIcon(logo, 0, 0, logoWidth, logoHeight); // redraw 128x128pix Konica Minolta logo 286 tft.setTextColor(TFT_BLACK,TFT_WHITE); // set text position 287 tft.setCursor(50, 225); // set font color to black with white background 288 tft.setTextSize(2); // set font size 1 289 tft.print(NTP.getTimeDateString ()); // print to tft current time and date hh:mm:ss dd/mm/yyyy 290 tft.setTextSize(4); // set font size 2 291 tft.setTextColor(TFT_RED); // set font color red 292 tft.drawCentreString("SWIPE", 160, 15, 2); // draw red "SWIPE NEW CARD" to TFT 293 tft.drawCentreString("NEW", 160, 60, 2); 294 tft.drawCentreString("CARD", 160, 105, 2); 295 delay(20); // wait 200ms 296 297 tft.setTextColor(TFT_GREEN); // set font color red 298 tft.drawCentreString("SWIPE", 160, 15, 2); // draw red "SWIPE NEW CARD" to TFT 299 tft.drawCentreString("NEW", 160, 60, 2); 300 tft.drawCentreString("CARD", 160, 105, 2); 301 delay(20); //wait 200ms 302 303 tft.setTextColor(TFT_YELLOW); // set font color red 304 tft.drawCentreString("SWIPE", 160, 15, 2); // draw red "SWIPE NEW CARD" to TFT 305 tft.drawCentreString("NEW", 160, 60, 2); 306 tft.drawCentreString("CARD", 160, 105, 2); 307 delay(20); 308 309 tft.setTextColor(TFT_BLUE); // set font color red 310 tft.drawCentreString("SWIPE", 160, 15, 2); // draw red "SWIPE NEW CARD" to TFT 311 tft.drawCentreString("NEW", 160, 60, 2); 312 tft.drawCentreString("CARD", 160, 105, 2); 313 delay(20); 314 315 tft.setTextColor(TFT_PINK); // set font color red 316 tft.drawCentreString("SWIPE", 160, 15, 2); // draw red "SWIPE NEW CARD" to TFT 317 tft.drawCentreString("NEW", 160, 60, 2); 318 tft.drawCentreString("CARD", 160, 105, 2); 319 delay(20); 320 321 tft.setTextColor(TFT_ORANGE); // set font color red 322 tft.drawCentreString("SWIPE", 160, 15, 2); // draw red "SWIPE NEW CARD" to TFT 323 tft.drawCentreString("NEW", 160, 60, 2); 324 tft.drawCentreString("CARD", 160, 105, 2); 325 delay(20); 326 327 tft.setTextColor(TFT_MAGENTA); // set font color red 328 tft.drawCentreString("SWIPE", 160, 15, 2); // draw red "SWIPE NEW CARD" to TFT 329 tft.drawCentreString("NEW", 160, 60, 2); 330 tft.drawCentreString("CARD", 160, 105, 2); 331 delay(20); 332 333 tft.setTextColor(TFT_BLACK); // set font color red 334 tft.drawCentreString("SWIPE", 160, 15, 2); // draw red "SWIPE NEW CARD" to TFT 335 tft.drawCentreString("NEW", 160, 60, 2); 336 tft.drawCentreString("CARD", 160, 105, 2); 337 delay(20); 338 } 339 340 void getID() 341{ 342 byte bytesread = 0; 343 byte i = 0; 344 byte val = 0; 345 byte tempbyte = 0; 346 347 // a 5 hexa byte az 10 ASCII byte 348 while ( bytesread < 12 ) // read out 12 byte, last 2 byte is checksum 349 { 350 if( Serial2.available() > 0) // wait serial port is active 351 { 352 val = Serial2.read(); // read data from serial port 353 354 if((val == 0x0D)||(val == 0x0A)||(val == 0x03)||(val == 0x02)) // when readed byte is 0d or 0a or 03 or 02 break 355 { 356 break; 357 } 358 359 if ( (val >= '0' ) && ( val <= '9' ) ) // convert ASCII to HEX 360 { 361 val = val - '0'; 362 } 363 else if ( ( val >= 'A' ) && ( val <= 'F' ) ) 364 { 365 val = 10 + val - 'A'; 366 } 367 368 if ( bytesread & 1 == 1 ) // every ASCII byte pair convert to one BYTE 369 { 370 371 readCard[bytesread >> 1] = (val | (tempbyte << 4)); 372 if ( bytesread >> 1 != 5 ) 373 { 374 checksum ^= readCard[bytesread >> 1]; // calculate Checksum 375 }; 376 } 377 else 378 { 379 tempbyte = val; 380 }; 381 bytesread++; 382 } 383 } 384 bytesread = 0; 385} 386 387boolean isMaster( byte test[] ) 388{ 389 byte bytesread = 0; 390 byte i = 0; // Example card, replace with one of yours you want to be the master 391 byte val[10] = {'0','F','0','0','5','3','F','0','3','0' }; // master card data byte 392 byte master[6]; 393 byte checksum = 0; 394 byte tempbyte = 0; 395 bytesread = 0; 396 397 for ( i = 0; i < 10; i++ ) // First we need to convert the array above into a 5 HEX BYTE array 398 { 399 if ( (val[i] >= '0' ) && ( val[i] <= '9' ) ) // Convert one char to HEX 400 { 401 val[i] = val[i] - '0'; 402 } 403 else if ( (val[i] >= 'A' ) && ( val[i] <= 'F' ) ) 404 { 405 val[i] = 10 + val[i] - 'A'; 406 } 407 408 if (bytesread & 1 == 1) // Every two hex-digits, add byte to code: 409 { 410 // make some space for this hex-digit by 411 // shifting the previous hex-digit with 4 bits to the left: 412 master[bytesread >> 1] = (val[i] | (tempbyte << 4)); 413 414 if (bytesread >> 1 != 5) // If we're at the checksum byte, 415 { 416 checksum ^= master[bytesread >> 1]; // Calculate the checksum... (XOR) 417 }; 418 } 419 else 420 { 421 tempbyte = val[i]; // Store the first hex digit first... 422 }; 423 bytesread++; 424 } 425 426 if ( checkTwo( test, master ) ) // Check to see if the master = the test ID 427 return true; 428 else 429 return false; 430} 431 432void writeID( byte a[] ) 433{ 434 if ( !findID( a ) ) // Before we write to the EEPROM, check to see if we have seen this card before! 435 { 436 digitalWrite(EE_CS, LOW); // set eeprom chip select pin to high (chip is active) 437 SPI.transfer(3); // read 3,0 438 SPI.transfer(0); // eeprom address upper 8 bit 439 SPI.transfer(0); // eeprom address lower 8 bit 440 fnb = SPI.transfer(0x00); // read addess upper 8 bit 441 digitalWrite(EE_CS, HIGH); // set eeprom chip select pin to high (chip is inactive) 442 443 digitalWrite(EE_CS, LOW); // set eeprom chip select pin to high (chip is active) 444 SPI.transfer(3); // read 3,0 445 SPI.transfer(0); // eeprom address upper 8 bit 446 SPI.transfer(1); // eeprom address lower 8 bit 447 anb = SPI.transfer(0x00); // read addess upper 8 bit 448 digitalWrite(EE_CS, HIGH); // set eeprom chip select pin to high (chip is inactive) 449 450 unsigned num = ((fnb * 256) + anb); // calculate 16 bit address from upper 8 bit and lower 8 bit 451 452 unsigned int start = ( num * 5 ) + 2; // Calculate where the next slot starts 453 454 num++; // Increment the counter 455 456 delay(1); // wait 1ms 457 digitalWrite(EE_CS, LOW); // set eeprom chip select pin to high (chip is active) 458 SPI.transfer(6); // write enabled 6 459 digitalWrite(EE_CS, HIGH); // set eeprom chip select pin to high (chip is inactive) 460 delay(1); // wait 1ms 461 digitalWrite(EE_CS, LOW); // set eeprom chip select pin to high (chip is active) 462 SPI.transfer(2); // write 2,0 463 SPI.transfer(0); // eeprom address upper 8 bit 464 SPI.transfer(0); // eeprom address lower 8 bit 465 fnb = num >>8; // calculate 16 address to two byte (upper and lower 8 bit) 466 SPI.transfer(fnb); // write upper 8 bit to eeprom index 467 digitalWrite(EE_CS, HIGH); // set eeprom chip select pin to high (chip is inactive) 468 delay(6); // wait 6ms to write is done 469 digitalWrite(EE_CS, LOW); // set eeprom chip select pin to high (chip is active) 470 SPI.transfer(4); // write disabled 4 471 digitalWrite(EE_CS, HIGH); // set eeprom chip select pin to high (chip is inactive) 472 delay(1); // wait 1ms 473 digitalWrite(EE_CS, LOW); // set eeprom chip select pin to high (chip is active) 474 SPI.transfer(6); // write enabled 6 475 digitalWrite(EE_CS, HIGH); // set eeprom chip select pin to high (chip is inactive) 476 delay(1); // wait 1ms 477 digitalWrite(EE_CS, LOW); // set eeprom chip select pin to high (chip is active) 478 SPI.transfer(2); // write 2,0 479 SPI.transfer(0); // eeprom address upper 8 bit 480 SPI.transfer(1); // eeprom address lower 8 bit 481 anb = num & 0x00FF; // calculate 16 address to two byte (upper and lower 8 bit) 482 SPI.transfer(anb); // write lower 8 bit to eeprom index 483 digitalWrite(EE_CS, HIGH); // set eeprom chip select pin to high (chip is inactive) 484 delay(6); // wait 6ms to write is done 485 digitalWrite(EE_CS, LOW); // set eeprom chip select pin to high (chip is active) 486 SPI.transfer(4); // write disabled 4 487 digitalWrite(EE_CS, HIGH); // set eeprom chip select pin to high (chip is inactive) 488 delay(1); // wait 1ms 489 for ( int j = 0; j < 5; j++ ) // Loop 5 times to record to eeprom new card data 490 { 491 492 digitalWrite(EE_CS, LOW); // set eeprom chip select pin to high (chip is active) 493 SPI.transfer(6); // write enabled 6 494 digitalWrite(EE_CS, HIGH); // set eeprom chip select pin to high (chip is inactive) 495 496 address = start + j; // incement address 497 digitalWrite(EE_CS, LOW); // set eeprom chip select pin to high (chip is active) 498 SPI.transfer(2); // write 2,0 499 SPI.transfer(address >>8); // eeprom address upper 8 bit 500 SPI.transfer(address & 0x00FF); // eeprom address lower 8 bit 501 SPI.transfer(a[j]); // store new card data in eeprom 502 digitalWrite(EE_CS, HIGH); // set eeprom chip select pin to high (chip is inactive) 503 delay(6); // wait 6 ms to write is done 504 505 digitalWrite(EE_CS, LOW); // set eeprom chip select pin to high (chip is active) 506 SPI.transfer(4); // write disabled 4 507 digitalWrite(EE_CS, HIGH); // set eeprom chip select pin to high (chip is inactive) 508 509 } 510 successWrite(); // print to tft card data is succes in eeprom 511 } 512 else 513 { 514 failedWrite(); // print to tft card is already stored 515 } 516} 517 518boolean checkTwo ( byte a[], byte b[] ) 519{ 520 if ( a[0] != NULL ) // Make sure there is something in the array first 521 match = true; // Assume they match at first 522 523 for ( int k = 0; k < 5; k++ ) // Loop 5 times 524 { 525 526 if ( a[k] != b[k] ) // IF a != b then set match = false, one fails, all fail 527 match = false; 528 } 529 if ( match ) // Check to see if if match is still true 530 { 531 return true; // Return true 532 } 533 else { 534 return false; // Return false 535 } 536} 537 538boolean findID( byte find[] ) 539{ 540 541 digitalWrite(EE_CS, LOW); // set eeprom chip select pin to high (chip is active) 542 SPI.transfer(3); // read 3,0 543 SPI.transfer(0); // eeprom address upper 8 bit 544 SPI.transfer(0); // eeprom address lower 8 bit 545 int fnb = SPI.transfer(0x00); // read out upper 8 bit 546 digitalWrite(EE_CS, HIGH); // set eeprom chip select pin to high (chip is inactive) 547 548 digitalWrite(EE_CS, LOW); // set eeprom chip select pin to high (chip is active) 549 SPI.transfer(3); // read 3,0 550 SPI.transfer(0); // eeprom address upper 8 bit 551 SPI.transfer(1); // eeprom address lower 8 bit 552 int anb = SPI.transfer(0x00); // read out lower 8 bit 553 digitalWrite(EE_CS, HIGH); // set eeprom chip select pin to high (chip is active) 554 555 unsigned count = ((fnb * 256) + anb); // calculate address 556 557 for ( int i = 1; i <= count; i++ ) // Loop once for each EEPROM entry 558 { 559 readID(i); // Read an ID from EEPROM, it is stored in storedCard[6] 560 if( checkTwo( find, storedCard ) ) // Check to see if the storedCard read from EEPROM 561 { // is the same as the find[] ID card passed 562 return true; 563 break; // Stop looking we found it 564 } 565 else // If not, return false 566 { 567 568 } 569 570 } 571 return false; 572} 573 574void successWrite() 575{ 576 tft.fillScreen(TFT_WHITE); 577 drawIcon(logo, 0,0, logoWidth, logoHeight); // redraw 128x128pix Konica Minolta logo 578 579 tft.setTextColor(TFT_GREEN); // set font color to black 580 tft.setTextSize(4); // set font size 2 581 tft.drawCentreString("CARD", 160, 15, 2); // draw "CARD STORED SUCCES" to TFT 582 tft.drawCentreString("STORED", 160, 60, 2); 583 tft.drawCentreString("SUCCES", 160, 105, 2); 584 delay(2000); // wait 2000ms 585 586 drawIcon(logo, 0,0, logoWidth, logoHeight); // redraw 128x128pix Konica Minolta logo 587 588 WiFiClient client; // initialize socket connection 589 client.connect(socketserver,10000); // open socket port 590 client.print(String(NTP.getTimeDateString ())); // send time and date to loging sever 591 client.print(String(";")); // send semicolon (excel recognize next row) 592 for ( int i = 0; i < 5; i++ ) // read 5 byte 593 { 594 byte cardbyte = readCard[i]; // read out byte from readCard array 595 if (cardbyte < 16) // if readed byte is lower 0x0F 596 { 597 cardstr = "0" + String(cardbyte,HEX); // then add 0 to byte (example converting 2 to 02) 598 } 599 else { 600 cardstr = String(cardbyte,HEX); // else store to cardstr 601 } 602 client.print(String(cardstr)); // send card byte to loging server 603 604 } 605 606 client.print(String(";")); // send semicolon 607 client.println(String("new")); // send card status to loging server 608 client.stop(); // stop socket connection 609 610} 611 612void failedWrite() 613{ 614 tft.fillScreen(TFT_WHITE); 615 drawIcon(logo, 0,0, logoWidth, logoHeight); // redraw 128x128pix Konica Minolta logo 616 tft.setTextColor(TFT_RED); // set font color to black 617 tft.setTextSize(4); // set font size 2 618 tft.drawCentreString("CARD", 160, 15, 2); // draw "CARD ALREADY STORED" to TFT 619 tft.drawCentreString("ALREADY", 160, 60, 2); 620 tft.drawCentreString("STORED", 160, 105, 2); 621 delay(2000); // wait 2000ms 622 drawIcon(logo, 0,0, logoWidth, logoHeight); // redraw 128x128pix Konica Minolta logo 623} 624void readID( unsigned int number ) // calculating number from eeprom first two byte 625{ 626 unsigned int start = (number * 5 ) - 3; // calculating addres from number * 5 627 628 for ( int i = 0; i < 5; i++ ) // read out 5 byte from calculated address 629 { 630 address = start + i; 631 digitalWrite(EE_CS, LOW); // set eeprom chip select pin to high (chip is active) 632 SPI.transfer(3); // read 3,0 633 SPI.transfer(address >>8); // eeprom address upper 8 bit 634 SPI.transfer(address & 0xff); // eeprom address lower 8 bit 635 storedCard[i] = SPI.transfer(0x00); // read data from eeprom stroreCard array 636 digitalWrite(EE_CS, HIGH); // set eeprom chip select pin to high (chip is inactive) 637 } 638} 639 640void openDoor( int setDelay ) 641{ 642 643 setDelay *= 1000; // Sets delay in seconds 644 drawIcon(granted, 0,0, grantedWidth, grantedHeight); // draw 128x128pix granted logo 645 digitalWrite(doorPin, LOW); // Unlock door! 646 // send log information to loging server 647 WiFiClient client; // initialize socket connection 648 client.connect(socketserver,10000); // open socket port 649 client.print(String(NTP.getTimeDateString ())); // send time and date to loging sever 650 client.print(String(";")); // send semicolon (excel recognize next row) 651 for ( int i = 0; i < 5; i++ ) // read 5 byte 652 { 653 byte cardbyte = readCard[i]; // read out byte from readCard array 654 if (cardbyte < 16) // if readed byte is lower 0x0F 655 { 656 cardstr = "0" + String(cardbyte,HEX); // then add 0 to byte (example converting 2 to 02) 657 } 658 else { 659 cardstr = String(cardbyte,HEX); // else store to cardstr 660 } 661 client.print(String(cardstr)); // send card byte to loging server 662 663 } 664 665 client.print(String(";")); // send semicolon 666 client.println(String("granted")); // send card status to loging server 667 client.stop(); // stop socket connection 668 669 delay(setDelay); // Hold door lock open for 5 seconds 670 671 digitalWrite(doorPin, HIGH); // Relock door 672 673 drawIcon(logo, 0,0, logoWidth, logoHeight); // redraw 128x128pix Konica Minolta logo 674 675} 676 677void failed() 678{ 679 drawIcon(denied, 0,0, deniedWidth, deniedHeight); // draw 128x128pix denied logo 680 // send log information to loging server 681 WiFiClient client; // initialize socket connection 682 client.connect(socketserver,10000); // open socket port 683 client.print(String(NTP.getTimeDateString ())); // send time and date to loging sever 684 client.print(String(";")); // send semicolon (excel recognize next row) 685 686 for ( int i = 0; i < 5; i++ ) // read 5 byte 687 { 688 byte cardbyte = readCard[i]; // read out byte from readCard array 689 if (cardbyte < 16) // if readed byte is lower 0x0F 690 691 { 692 cardstr = "0" + String(cardbyte,HEX); // then add 0 to byte (example converting 2 to 02) 693 } 694 695 else { 696 cardstr = String(cardbyte,HEX); // else store to cardstr 697 } 698 699 client.print(String(cardstr)); // send card byte to loging server 700 701 } 702 703 client.print(String(";")); // send semicolon 704 client.println(String("denied")); // send card status to loging server 705 client.stop(); // stop socket connection 706 707 digitalWrite(doorPin, HIGH); // lock door 708 delay(5000); // wait 5000ms 709 710 drawIcon(logo, 0,0, logoWidth, logoHeight); // redraw 128x128pix Konica Minolta logo 711 712} 713 714// logo wire routine 715void drawIcon(const unsigned short* icon, int16_t x, int16_t y, uint16_t width, uint16_t height) { 716 717 uint16_t pix_buffer[BUFF_SIZE]; // Pixel buffer (16 bits per pixel) 718 719 // Set up a window the right size to stream pixels into 720 tft.setAddrWindow(x, y, x + width - 1, y + height - 1); 721 722 // Work out the number whole buffers to send 723 uint16_t nb = ((uint16_t)height * width) / BUFF_SIZE; 724 725 // Fill and send "nb" buffers to TFT 726 for (int i = 0; i < nb; i++) { 727 for (int j = 0; j < BUFF_SIZE; j++) { 728 pix_buffer[j] = pgm_read_word(&icon[i * BUFF_SIZE + j]); 729 } 730 tft.pushColors(pix_buffer, BUFF_SIZE); 731 } 732 733 // Work out number of pixels not yet sent 734 uint16_t np = ((uint16_t)height * width) % BUFF_SIZE; 735 736 // Send any partial buffer left over 737 if (np) { 738 for (int i = 0; i < np; i++) pix_buffer[i] = pgm_read_word(&icon[nb * BUFF_SIZE + i]); 739 tft.pushColors(pix_buffer, np); 740 } 741} 742 743
Downloadable files
esp32rfid_x2jNOBrywH.fzz
esp32rfid_x2jNOBrywH.fzz
esp32rfid_x2jNOBrywH.fzz
esp32rfid_x2jNOBrywH.fzz
Comments
Only logged in users can leave comments