Advertisement
rabirajkhadka

RFID based Attendance System

Mar 8th, 2022
1,330
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <SPI.h>
  2. #include <Wire.h>
  3. #include <MFRC522.h>
  4. #include <MFRC522Extended.h>
  5. #include <ESP8266WiFi.h>
  6. #include <ESP8266HTTPClient.h>
  7.  
  8.  
  9. #define SS_PIN D4
  10. #define RST_PIN D3
  11.  
  12. MFRC522 mfrc522(SS_PIN,RST_PIN);
  13.  
  14. const char* NAME; // variable to save the name of the person
  15.  
  16.  
  17. char* response = " ";
  18. String res = "";
  19. char* succ_code = "200 OK";
  20.  
  21. //Necessary parameters for IFTTT request
  22. String Event_Name = "rfid";
  23. String key = "kjAPKzVS3HifS4sMFHxjEBdqod3JevEQz_4v0-7yy0d";
  24. String resource = "/trigger/" + Event_Name + "/with/key/" + key;
  25. const char* server = "maker.ifttt.com";
  26.  
  27. //Provide your WiFi Credentials
  28. const char* ssid = "Giant Infosys_2.4";
  29. const char* password = "Giant#123#456";
  30.  
  31.  
  32.  
  33. void setup() {
  34.   Serial.begin(9600);
  35.   SPI.begin();
  36.   mfrc522.PCD_Init();
  37.  
  38.  
  39.     // Connect to Wi-Fi
  40.   WiFi.begin(ssid, password);
  41.   while (WiFi.status() != WL_CONNECTED) {
  42.     delay(1000);
  43.     Serial.println("Connecting to WiFi..");
  44.   }
  45.  
  46.   // Print ESP32 Local IP Address
  47.   Serial.print("WiFi connected in ");
  48.   Serial.print(millis());
  49.   Serial.print(",");
  50.   Serial.println(WiFi.localIP());
  51.  
  52.  
  53. }
  54.  
  55. void loop() {
  56.   Serial.println("Scan tag");
  57.   Serial.println("Waiting for the tag1");
  58.  
  59.       if ( ! mfrc522.PICC_IsNewCardPresent())
  60.       {
  61.         return;
  62.       }
  63.       // Select one of the cards
  64.       if ( ! mfrc522.PICC_ReadCardSerial())
  65.       {
  66.         return;
  67.       }
  68.  
  69.  
  70.       String content = "";
  71.       byte letter;
  72.       Serial.println(mfrc522.uid.size);
  73.       for (byte i = 0; i < mfrc522.uid.size; i++)
  74.       {
  75.         //Serial.print(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " ");
  76.         //Serial.print(mfrc522.uid.uidByte[i], HEX);
  77.         content.concat(String(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " "));
  78.          Serial.println(content);
  79.         content.concat(String(mfrc522.uid.uidByte[i], HEX));
  80.       }
  81.       Serial.println(content);
  82.       content.toUpperCase();
  83.       Serial.println("Waiting for the tag2");
  84.  
  85.       if (content.substring(1) == "E9 E2 8B A3") //change here the UID of the card that you want to give access
  86.       {
  87.         Serial.print("!!--");
  88.         Serial.println(content.substring(1));
  89.         NAME = "Aayush";
  90.         delay(1000);
  91.  
  92.         makeIFTTTRequest(); // Making Registrations on Google Sheet via IFTTT
  93.       }
  94.  
  95.       else if (content.substring(1) == "C9 B2 AE 82") //change here the UID of the card that you want to give access
  96.       {
  97.         Serial.print("!!--");
  98.         Serial.println(content.substring(1));
  99.         NAME = "Harsh";
  100.         makeIFTTTRequest(); // Making Registrations on Google Sheet via IFTTT
  101.  
  102.       }
  103.  
  104.       else
  105.       {
  106.         Serial.println("Not Registered");
  107.  
  108.       }
  109.  
  110.      content.substring(1) = "";
  111.  
  112. }
  113.  
  114.  
  115.  
  116.  
  117. void makeIFTTTRequest(){
  118.  
  119.     HTTPClient http;
  120.     Serial.print("Connecting to ");
  121.     Serial.print(server);
  122.  
  123.     WiFiClient client;
  124.     int retries = 5;
  125.     while (!!!client.connect(server, 80) && (retries-- > 0)) {
  126.       Serial.print(".");
  127.     }
  128.     Serial.println();
  129.     if (!!!client.connected()) {
  130.       Serial.println("Failed to connect...");
  131.     }
  132.  
  133.     Serial.print("Request resource: ");
  134.     Serial.println(resource);
  135.  
  136.     String jsonObject = String("{\"value1\":\"") + NAME + "\"}";
  137.  
  138.     client.println(String("POST ") + resource + " HTTP/1.1");
  139.     client.println(String("Host: ") + server);
  140.     client.println("Connection: close\r\nContent-Type: application/json");
  141.     client.print("Content-Length: ");
  142.     client.println(jsonObject.length());
  143.     client.println();
  144.     client.println(jsonObject);
  145.  
  146.     int timeout = 5 * 10; // 5 seconds
  147.     while (!!!client.available() && (timeout-- > 0))
  148.     {
  149.       delay(100);
  150.     }
  151.     if (!!!client.available())
  152.     {
  153.       Serial.println("No response...");
  154.     }
  155.  
  156.     while (client.available())
  157.     {
  158.       // Serial.write(client.read());
  159.       char add = client.read();
  160.       res = res + add;
  161.     }
  162.     response = &res[0];
  163.     Serial.println("=======");
  164.     Serial.println(response);
  165.  
  166.     if (strstr(response, succ_code)) // If connected to internet, make registration
  167.     {
  168.       Serial.println("Registered");
  169.       Serial.println(("Registered"));
  170.       delay(1000);
  171.     }
  172.  
  173.     else // If not connected to internet, don't make registration
  174.     {
  175.       Serial.println("Not Registered");
  176.  
  177.       delay(1000);
  178.     }
  179.     response = "";
  180.     res = "";
  181.     Serial.println("\nclosing connection");
  182.     client.stop();
  183.  
  184.  
  185.   }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement