Advertisement
Double_G

ESP32 RFID card reader+ 2.4'' TFT + 512k EEPROM + server log

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