Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <SPI.h>
- #include "WiFi.h"
- #include <MFRC522.h>
- #include <HTTPClient.h>
- #include <LiquidCrystal_I2C.h>
- const char* ssid = "network";
- const char* password = "123456789";
- String GOOGLE_SCRIPT_ID = "AKfycbxsh9O28pzVBFKb0sHJi5MoHBLnoZ5qJ6ZHRmFBynuENTBroAKVADacxwsgpWmo_u0NeA";
- const int number_of_cards = 2;
- String NAME[number_of_cards] = {"Ebtehal",
- "Reem"};
- String CARD[number_of_cards] = {"D9 B8 21 B9",
- "D0 3A ED 32"};
- String content = "";
- // Blue Tag = D9 B8 21 B9
- // White Card = D0 3A ED 32
- #define SS_PIN 27 /*Slave Select Pin*/
- #define RST_PIN 26 /*Reset Pin for RC522*/
- MFRC522 mfrc522(SS_PIN, RST_PIN);
- LiquidCrystal_I2C lcd(0x27, 16, 2);
- void setup() {
- Serial.begin(9600); /*Serial Communication begin*/
- connect_To_WiFi();
- Setup_LCD_and_RFID_Reader();
- }
- void loop() {
- Read_RFID_Card_And_Send_Data_To_Google_Sheets();
- }//end of Loop
- void Setup_LCD_and_RFID_Reader() {
- lcd.init();
- lcd.backlight();
- lcd.setCursor(0, 0);
- lcd.print("Hello, World!");
- SPI.begin(); /*SPI communication initialized*/
- mfrc522.PCD_Init(); /*RFID sensor initialized*/
- Serial.println("Put your card to the reader...");
- Serial.println();
- delay(1000);
- lcd.clear();
- }
- void Read_RFID_Card_And_Send_Data_To_Google_Sheets() {
- lcd.setCursor(0, 0);
- lcd.print("Scanning...");
- /*Look for the RFID Card*/
- if ( ! mfrc522.PICC_IsNewCardPresent())
- {
- return;
- }
- /*Select Card*/
- if ( ! mfrc522.PICC_ReadCardSerial())
- {
- return;
- }
- /*Show UID for Card/Tag on serial monitor*/
- Serial.print("UID tag :");
- content = "";
- byte letter;
- 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" : " "));
- content.concat(String(mfrc522.uid.uidByte[i], HEX));
- }
- Serial.println();
- Serial.print("Message : ");
- content.toUpperCase();
- lcd.clear();
- lcd.setCursor(0, 0);
- lcd.print(content.substring(1));
- for (int i = 0; i < number_of_cards; i++) {
- if (content.substring(1) == CARD[i])
- {
- Serial.println("Authorized access");
- Serial.println();
- lcd.setCursor(0, 1);
- lcd.print("Welcome ");
- lcd.print(NAME[i]);
- send_Data_To_Google_Sheets(NAME[i]);
- }
- }
- lcd.clear();
- }
- void connect_To_WiFi() {
- Serial.println();
- Serial.print("Connecting to wifi: ");
- Serial.println(ssid);
- Serial.flush();
- WiFi.begin(ssid, password);
- while (WiFi.status() != WL_CONNECTED) {
- delay(500);
- Serial.print(".");
- }
- }
- void send_Data_To_Google_Sheets(String DataToSend) {
- if (WiFi.status() == WL_CONNECTED) {
- static bool flag = false;
- String urlFinal = "https://script.google.com/macros/s/" + GOOGLE_SCRIPT_ID + "/exec?" + "name=" + String(DataToSend);
- Serial.print("POST data to spreadsheet:");
- Serial.println(urlFinal);
- HTTPClient http;
- http.begin(urlFinal.c_str());
- http.setFollowRedirects(HTTPC_STRICT_FOLLOW_REDIRECTS);
- int httpCode = http.GET();
- Serial.print("HTTP Status Code: ");
- Serial.println(httpCode);
- //---------------------------------------------------------------------
- //getting response from google sheet
- String payload;
- if (httpCode > 0) {
- payload = http.getString();
- Serial.println("Payload: " + payload);
- }
- //---------------------------------------------------------------------
- http.end();
- }
- delay(1000);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement