Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <SPI.h>
- #include <Wire.h>
- #include <MFRC522.h>
- #include <MFRC522Extended.h>
- #include <ESP8266WiFi.h>
- #include <ESP8266HTTPClient.h>
- #define SS_PIN D4
- #define RST_PIN D3
- MFRC522 mfrc522(SS_PIN,RST_PIN);
- const char* NAME; // variable to save the name of the person
- char* response = " ";
- String res = "";
- char* succ_code = "200 OK";
- //Necessary parameters for IFTTT request
- String Event_Name = "rfid";
- String key = "kjAPKzVS3HifS4sMFHxjEBdqod3JevEQz_4v0-7yy0d";
- String resource = "/trigger/" + Event_Name + "/with/key/" + key;
- const char* server = "maker.ifttt.com";
- //Provide your WiFi Credentials
- const char* ssid = "Giant Infosys_2.4";
- const char* password = "Giant#123#456";
- void setup() {
- Serial.begin(9600);
- SPI.begin();
- mfrc522.PCD_Init();
- // Connect to Wi-Fi
- WiFi.begin(ssid, password);
- while (WiFi.status() != WL_CONNECTED) {
- delay(1000);
- Serial.println("Connecting to WiFi..");
- }
- // Print ESP32 Local IP Address
- Serial.print("WiFi connected in ");
- Serial.print(millis());
- Serial.print(",");
- Serial.println(WiFi.localIP());
- }
- void loop() {
- Serial.println("Scan tag");
- Serial.println("Waiting for the tag1");
- if ( ! mfrc522.PICC_IsNewCardPresent())
- {
- return;
- }
- // Select one of the cards
- if ( ! mfrc522.PICC_ReadCardSerial())
- {
- return;
- }
- String content = "";
- byte letter;
- Serial.println(mfrc522.uid.size);
- for (byte i = 0; i < mfrc522.uid.size; i++)
- {
- //Serial.print(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " ");
- //Serial.print(mfrc522.uid.uidByte[i], HEX);
- content.concat(String(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " "));
- Serial.println(content);
- content.concat(String(mfrc522.uid.uidByte[i], HEX));
- }
- Serial.println(content);
- content.toUpperCase();
- Serial.println("Waiting for the tag2");
- if (content.substring(1) == "E9 E2 8B A3") //change here the UID of the card that you want to give access
- {
- Serial.print("!!--");
- Serial.println(content.substring(1));
- NAME = "Aayush";
- delay(1000);
- makeIFTTTRequest(); // Making Registrations on Google Sheet via IFTTT
- }
- else if (content.substring(1) == "C9 B2 AE 82") //change here the UID of the card that you want to give access
- {
- Serial.print("!!--");
- Serial.println(content.substring(1));
- NAME = "Harsh";
- makeIFTTTRequest(); // Making Registrations on Google Sheet via IFTTT
- }
- else
- {
- Serial.println("Not Registered");
- }
- content.substring(1) = "";
- }
- void makeIFTTTRequest(){
- HTTPClient http;
- Serial.print("Connecting to ");
- Serial.print(server);
- WiFiClient client;
- int retries = 5;
- while (!!!client.connect(server, 80) && (retries-- > 0)) {
- Serial.print(".");
- }
- Serial.println();
- if (!!!client.connected()) {
- Serial.println("Failed to connect...");
- }
- Serial.print("Request resource: ");
- Serial.println(resource);
- String jsonObject = String("{\"value1\":\"") + NAME + "\"}";
- client.println(String("POST ") + resource + " HTTP/1.1");
- client.println(String("Host: ") + server);
- client.println("Connection: close\r\nContent-Type: application/json");
- client.print("Content-Length: ");
- client.println(jsonObject.length());
- client.println();
- client.println(jsonObject);
- int timeout = 5 * 10; // 5 seconds
- while (!!!client.available() && (timeout-- > 0))
- {
- delay(100);
- }
- if (!!!client.available())
- {
- Serial.println("No response...");
- }
- while (client.available())
- {
- // Serial.write(client.read());
- char add = client.read();
- res = res + add;
- }
- response = &res[0];
- Serial.println("=======");
- Serial.println(response);
- if (strstr(response, succ_code)) // If connected to internet, make registration
- {
- Serial.println("Registered");
- Serial.println(("Registered"));
- delay(1000);
- }
- else // If not connected to internet, don't make registration
- {
- Serial.println("Not Registered");
- delay(1000);
- }
- response = "";
- res = "";
- Serial.println("\nclosing connection");
- client.stop();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement