Advertisement
safwan092

Untitled

Oct 30th, 2023
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.60 KB | None | 0 0
  1. #include <SPI.h>
  2. #include "WiFi.h"
  3. #include <MFRC522.h>
  4. #include <HTTPClient.h>
  5. #include <LiquidCrystal_I2C.h>
  6.  
  7. const char* ssid = "network";
  8. const char* password = "123456789";
  9. String GOOGLE_SCRIPT_ID = "AKfycbxsh9O28pzVBFKb0sHJi5MoHBLnoZ5qJ6ZHRmFBynuENTBroAKVADacxwsgpWmo_u0NeA";
  10. const int number_of_cards = 2;
  11. String NAME[number_of_cards] = {"Name_1",
  12. "Name_2"};
  13. String CARD[number_of_cards] = {"D9 B8 21 B9",
  14. "D0 3A ED 32"};
  15. String content = "";
  16. // Blue Tag = D9 B8 21 B9
  17. // White Card = D0 3A ED 32
  18.  
  19. #define SS_PIN 27 /*Slave Select Pin*/
  20. #define RST_PIN 26 /*Reset Pin for RC522*/
  21.  
  22. MFRC522 mfrc522(SS_PIN, RST_PIN);
  23. LiquidCrystal_I2C lcd(0x27, 16, 2);
  24.  
  25. void setup() {
  26. Serial.begin(9600); /*Serial Communication begin*/
  27. connect_To_WiFi();
  28. Setup_LCD_and_RFID_Reader();
  29. }
  30.  
  31. void loop() {
  32. Read_RFID_Card_And_Send_Data_To_Google_Sheets();
  33. }//end of Loop
  34.  
  35.  
  36. void Setup_LCD_and_RFID_Reader() {
  37. lcd.init();
  38. lcd.backlight();
  39. lcd.setCursor(0, 0);
  40. lcd.print("Hello, World!");
  41. SPI.begin(); /*SPI communication initialized*/
  42. mfrc522.PCD_Init(); /*RFID sensor initialized*/
  43. Serial.println("Put your card to the reader...");
  44. Serial.println();
  45. delay(1000);
  46. lcd.clear();
  47. }
  48.  
  49. void Read_RFID_Card_And_Send_Data_To_Google_Sheets() {
  50. lcd.setCursor(0, 0);
  51. lcd.print("Scanning...");
  52. /*Look for the RFID Card*/
  53. if ( ! mfrc522.PICC_IsNewCardPresent())
  54. {
  55. return;
  56. }
  57. /*Select Card*/
  58. if ( ! mfrc522.PICC_ReadCardSerial())
  59. {
  60. return;
  61. }
  62. /*Show UID for Card/Tag on serial monitor*/
  63. Serial.print("UID tag :");
  64. content = "";
  65. byte letter;
  66. for (byte i = 0; i < mfrc522.uid.size; i++)
  67. {
  68. Serial.print(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " ");
  69. Serial.print(mfrc522.uid.uidByte[i], HEX);
  70. content.concat(String(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " "));
  71. content.concat(String(mfrc522.uid.uidByte[i], HEX));
  72. }
  73. Serial.println();
  74. Serial.print("Message : ");
  75. content.toUpperCase();
  76. lcd.clear();
  77. lcd.setCursor(0, 0);
  78. lcd.print(content.substring(1));
  79. for (int i = 0; i < number_of_cards; i++) {
  80. if (content.substring(1) == CARD[i])
  81. {
  82. Serial.println("Authorized access");
  83. Serial.println();
  84. lcd.setCursor(0, 1);
  85. lcd.print("Welcome ");
  86. lcd.print(NAME[i]);
  87. send_Data_To_Google_Sheets(NAME[i]);
  88. }
  89. }
  90. lcd.clear();
  91. }
  92.  
  93.  
  94.  
  95. void connect_To_WiFi() {
  96. Serial.println();
  97. Serial.print("Connecting to wifi: ");
  98. Serial.println(ssid);
  99. Serial.flush();
  100. WiFi.begin(ssid, password);
  101. while (WiFi.status() != WL_CONNECTED) {
  102. delay(500);
  103. Serial.print(".");
  104. }
  105. }
  106.  
  107. void send_Data_To_Google_Sheets(String DataToSend) {
  108. if (WiFi.status() == WL_CONNECTED) {
  109. static bool flag = false;
  110. String urlFinal = "https://script.google.com/macros/s/" + GOOGLE_SCRIPT_ID + "/exec?" + "name=" + String(DataToSend);
  111. Serial.print("POST data to spreadsheet:");
  112. Serial.println(urlFinal);
  113. HTTPClient http;
  114. http.begin(urlFinal.c_str());
  115. http.setFollowRedirects(HTTPC_STRICT_FOLLOW_REDIRECTS);
  116. int httpCode = http.GET();
  117. Serial.print("HTTP Status Code: ");
  118. Serial.println(httpCode);
  119. //---------------------------------------------------------------------
  120. //getting response from google sheet
  121. String payload;
  122. if (httpCode > 0) {
  123. payload = http.getString();
  124. Serial.println("Payload: " + payload);
  125. }
  126. //---------------------------------------------------------------------
  127. http.end();
  128. }
  129. delay(1000);
  130. }
  131.  
  132.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement