ESP32 RFID-RC522 Google Sheets Simple Attendance System (Arduino Code)
ESP32 RFID-RC522 Google Sheets Simple Attendance System (Arduino Code)
///////////////////////////////////////////////////////////
// - For this project, use a power supply of at least 5V 2A. If you use a power
supply with good quality components, 1A current is enough. //
// - If possible use good quality cables for the power supply.
//
// - If when the ESP32 connects to the SSID or when the ESP32 makes an http request
to Google Sheets, //
// you see the LCD backlight flashing or dimming, it is because there is not
enough current. //
///////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////
//
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
>>>>>> 01_Test_RFID-RC522_Serial
// This code only takes the UID in string form.
// If you want to see more detailed information about an RFID card or keychain,
please run the program code "DumpInfo".
// The "DumpInfo" program code is located at :
// Library folder "MFRC522" -> "examples" folder -> "DumpInfo" folder ->
DumpInfo.ino
// or
// From Arduino IDE : File -> Examples -> MFRC522 -> DumpInfo
// or
// From Arduino IDE : File -> Examples -> INCOMPATIBLE-> MFRC522 -> DumpInfo
// Create MFRC522 object as "mfrc522" and set SS/SDA PIN and Reset PIN.
MFRC522 mfrc522(SS_PIN, RST_PIN);
//
________________________________________________________________________________get
UID()
// Subroutine to obtain UID/ID when RFID card or RFID keychain is tapped to RFID-
RC522 module.
int getUID() {
if(!mfrc522.PICC_IsNewCardPresent()) {
return 0;
}
if(!mfrc522.PICC_ReadCardSerial()) {
return 0;
}
mfrc522.PICC_HaltA();
mfrc522.PCD_StopCrypto1();
return 1;
}
//________________________________________________________________________________
//
________________________________________________________________________________byt
eArray_to_string()
void byteArray_to_string(byte array[], unsigned int len, char buffer[]) {
for (unsigned int i = 0; i < len; i++) {
byte nib1 = (array[i] >> 4) & 0x0F;
byte nib2 = (array[i] >> 0) & 0x0F;
buffer[i*2+0] = nib1 < 0xA ? '0' + nib1 : 'A' + nib1 - 0xA;
buffer[i*2+1] = nib2 < 0xA ? '0' + nib2 : 'A' + nib2 - 0xA;
}
buffer[len*2] = '\0';
}
//________________________________________________________________________________
//
________________________________________________________________________________VOI
D SETUP()
void setup(){
// put your setup code here, to run once:
Serial.begin(115200);
Serial.println();
delay(1000);
delay(1000);
Serial.println();
Serial.println("Please tap your card or key chain to the RFID-RC522 module.");
}
//________________________________________________________________________________
//
________________________________________________________________________________VOI
D LOOP()
void loop(){
// put your main code here, to run repeatedly:
readsuccess = getUID();
if(readsuccess){
Serial.println();
Serial.print("UID : ");
Serial.println(UID_Result);
delay(500);
}
delay(10);
}
//________________________________________________________________________________
//
<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
<<<<<<
//
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
>>>>>> 02_Test_RFID-RC522_LCD-20x4_Button
//----------------------------------------Including the libraries.
#include <LiquidCrystal_I2C.h>
#include <SPI.h>
#include <MFRC522.h>
//----------------------------------------
// Create LiquidCrystal_I2C object as "lcd" and set the LCD I2C address to 0x27 and
set the LCD configuration to 20x4.
// In general, the address of a 20x4 I2C LCD is "0x27".
// However, if the address "0x27" doesn't work, you can find out the address with
"i2c_scanner". Look here : https://playground.arduino.cc/Main/I2cScanner/
LiquidCrystal_I2C lcd(0x27, lcdColumns, lcdRows); // (lcd_address, lcd_Columns,
lcd_Rows)
// Create MFRC522 object as "mfrc522" and set SS/SDA PIN and Reset PIN.
MFRC522 mfrc522(SS_PIN, RST_PIN);
//
________________________________________________________________________________get
UID()
// Subroutine to obtain UID/ID when RFID card or RFID keychain is tapped to RFID-
RC522 module.
int getUID() {
if(!mfrc522.PICC_IsNewCardPresent()) {
return 0;
}
if(!mfrc522.PICC_ReadCardSerial()) {
return 0;
}
mfrc522.PICC_HaltA();
mfrc522.PCD_StopCrypto1();
return 1;
}
//________________________________________________________________________________
//
________________________________________________________________________________byt
eArray_to_string()
void byteArray_to_string(byte array[], unsigned int len, char buffer[]) {
for (unsigned int i = 0; i < len; i++) {
byte nib1 = (array[i] >> 4) & 0x0F;
byte nib2 = (array[i] >> 0) & 0x0F;
buffer[i*2+0] = nib1 < 0xA ? '0' + nib1 : 'A' + nib1 - 0xA;
buffer[i*2+1] = nib2 < 0xA ? '0' + nib2 : 'A' + nib2 - 0xA;
}
buffer[len*2] = '\0';
}
//________________________________________________________________________________
//
________________________________________________________________________________VOI
D SETUP()
void setup(){
// put your setup code here, to run once:
pinMode(BTN_PIN, INPUT_PULLUP);
// Initialize LCD.
lcd.init();
// turn on LCD backlight.
lcd.backlight();
delay(1000);
lcd.clear();
}
//________________________________________________________________________________
//
________________________________________________________________________________VOI
D LOOP()
void loop(){
// put your main code here, to run repeatedly:
lcd.setCursor(0,0);
lcd.print("Please tap your card");
lcd.setCursor(0,1);
lcd.print("or keychain.");
lcd.setCursor(0,2);
lcd.print("UID : ");
lcd.print(UID_Result);
lcd.setCursor(0,3);
lcd.print("BTN State : ");
lcd.print(digitalRead(BTN_PIN));
readsuccess = getUID();
if(readsuccess){
lcd.setCursor(6, 2);
lcd.print(" ");
delay(500);
}
delay(10);
}
//________________________________________________________________________________
//
<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
<<<<<<
//
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
>>>>>> 03_ESP32_RFID_Google_Spreadsheet_Attendance
//----------------------------------------Including the libraries.
#include <LiquidCrystal_I2C.h>
#include <SPI.h>
#include <MFRC522.h>
#include "WiFi.h"
#include <HTTPClient.h>
//----------------------------------------
// Create LiquidCrystal_I2C object as "lcd" and set the LCD I2C address to 0x27 and
set the LCD configuration to 20x4.
// In general, the address of a 20x4 I2C LCD is "0x27".
// However, if the address "0x27" doesn't work, you can find out the address with
"i2c_scanner". Look here : https://playground.arduino.cc/Main/I2cScanner/
LiquidCrystal_I2C lcd(0x27, lcdColumns, lcdRows); // (lcd_address, lcd_Columns,
lcd_Rows)
// Create MFRC522 object as "mfrc522" and set SS/SDA PIN and Reset PIN.
MFRC522 mfrc522(SS_PIN, RST_PIN); //--> Create MFRC522 instance.
//
________________________________________________________________________________htt
p_Req()
// Subroutine for sending HTTP requests to Google Sheets.
void http_Req(String str_modes, String str_uid) {
if (WiFi.status() == WL_CONNECTED) {
String http_req_url = "";
Serial.println("-------------");
http.end();
//----------------------------------------
///////////////////////////////////////////////////////////////////////////////
/////////////////////////////
// Example :
//
// Sending an http request to fill in "Time In" attendance.
//
// User data :
//
// - Name : Adam
//
// - UID : A01
//
// So the payload received if the http request is successful and the parameters
are correct is as below : //
// OK,Adam,29/10/2023,08:30:00 ---> Status,Name,Date,Time_In
//
//
//
// So, if you want to retrieve "Status", then getValue(payload, ',', 0);
//
// String sts_Res = getValue(payload, ',', 0);
//
// So the value of sts_Res is "OK".
//
///////////////////////////////////////////////////////////////////////////////
/////////////////////////////
if (atc_Info == "TI_Successful") {
atc_Name = getValue(payload, ',', 2);
atc_Date = getValue(payload, ',', 3);
atc_Time_In = getValue(payload, ',', 4);
lcd.clear();
delay(500);
lcd.setCursor(pos,0);
lcd.print(atc_Name);
lcd.setCursor(0,1);
lcd.print("Date : ");
lcd.print(atc_Date);
lcd.setCursor(0,2);
lcd.print("Time IN : ");
lcd.print(atc_Time_In);
lcd.setCursor(0,3);
lcd.print("Time Out: ");
delay(5000);
lcd.clear();
delay(500);
}
if (atc_Info == "TO_Successful") {
atc_Name = getValue(payload, ',', 2);
atc_Date = getValue(payload, ',', 3);
atc_Time_In = getValue(payload, ',', 4);
atc_Time_Out = getValue(payload, ',', 5);
lcd.clear();
delay(500);
lcd.setCursor(pos,0);
lcd.print(atc_Name);
lcd.setCursor(0,1);
lcd.print("Date : ");
lcd.print(atc_Date);
lcd.setCursor(0,2);
lcd.print("Time IN : ");
lcd.print(atc_Time_In);
lcd.setCursor(0,3);
lcd.print("Time Out: ");
lcd.print(atc_Time_Out);
delay(5000);
lcd.clear();
delay(500);
}
if (atc_Info == "atcInf01") {
lcd.clear();
delay(500);
lcd.setCursor(1,0);
lcd.print("You have completed");
lcd.setCursor(2,1);
lcd.print("your attendance");
lcd.setCursor(2,2);
lcd.print("record for today");
delay(5000);
lcd.clear();
delay(500);
}
if (atc_Info == "atcErr01") {
lcd.clear();
delay(500);
lcd.setCursor(6,0);
lcd.print("Error !");
lcd.setCursor(4,1);
lcd.print("Your card or");
lcd.setCursor(4,2);
lcd.print("key chain is");
lcd.setCursor(3,3);
lcd.print("not registered");
delay(5000);
lcd.clear();
delay(500);
}
atc_Info = "";
atc_Name = "";
atc_Date = "";
atc_Time_In = "";
atc_Time_Out = "";
}
//..................
//..................
if (str_modes == "reg") {
reg_Info = getValue(payload, ',', 1);
if (reg_Info == "R_Successful") {
lcd.clear();
delay(500);
lcd.setCursor(2,0);
lcd.print("The UID of your");
lcd.setCursor(0,1);
lcd.print("card or keychain has");
lcd.setCursor(1,2);
lcd.print("been successfully");
lcd.setCursor(6,3);
lcd.print("uploaded");
delay(5000);
lcd.clear();
delay(500);
}
if (reg_Info == "regErr01") {
lcd.clear();
delay(500);
lcd.setCursor(6,0);
lcd.print("Error !");
lcd.setCursor(0,1);
lcd.print("The UID of your card");
lcd.setCursor(0,2);
lcd.print("or keychain has been");
lcd.setCursor(5,3);
lcd.print("registered");
delay(5000);
lcd.clear();
delay(500);
}
reg_Info = "";
}
//..................
}
//----------------------------------------
} else {
lcd.clear();
delay(500);
lcd.setCursor(6,0);
lcd.print("Error !");
lcd.setCursor(1,1);
lcd.print("WiFi disconnected");
delay(3000);
lcd.clear();
delay(500);
}
}
//________________________________________________________________________________
//
________________________________________________________________________________get
Value()
// String function to process the data (Split String).
// I got this from : https://www.electroniclinic.com/reyax-lora-based-multiple-
sensors-monitoring-using-arduino/
String getValue(String data, char separator, int index) {
int found = 0;
int strIndex[] = { 0, -1 };
int maxIndex = data.length() - 1;
//
________________________________________________________________________________get
UID()
// Subroutine to obtain UID/ID when RFID card or RFID keychain is tapped to RFID-
RC522 module.
int getUID() {
if(!mfrc522.PICC_IsNewCardPresent()) {
return 0;
}
if(!mfrc522.PICC_ReadCardSerial()) {
return 0;
}
mfrc522.PICC_HaltA();
mfrc522.PCD_StopCrypto1();
return 1;
}
//________________________________________________________________________________
//
________________________________________________________________________________byt
eArray_to_string()
void byteArray_to_string(byte array[], unsigned int len, char buffer[]) {
for (unsigned int i = 0; i < len; i++) {
byte nib1 = (array[i] >> 4) & 0x0F;
byte nib2 = (array[i] >> 0) & 0x0F;
buffer[i*2+0] = nib1 < 0xA ? '0' + nib1 : 'A' + nib1 - 0xA;
buffer[i*2+1] = nib2 < 0xA ? '0' + nib2 : 'A' + nib2 - 0xA;
}
buffer[len*2] = '\0';
}
//________________________________________________________________________________
//
________________________________________________________________________________VOI
D SETUP()
void setup(){
// put your setup code here, to run once:
Serial.begin(115200);
Serial.println();
delay(1000);
pinMode(BTN_PIN, INPUT_PULLUP);
// Initialize LCD.
lcd.init();
// turn on LCD backlight.
lcd.backlight();
lcd.clear();
delay(500);
// Init SPI bus.
SPI.begin();
// Init MFRC522.
mfrc522.PCD_Init();
delay(500);
lcd.setCursor(5,0);
lcd.print("ESP32 RFID");
lcd.setCursor(3,1);
lcd.print("Google Sheets");
lcd.setCursor(1,2);
lcd.print("Attendance System");
lcd.setCursor(4,3);
lcd.print("by UTEH STR");
delay(3000);
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Connecting to SSID");
delay(250);
lcd.clear();
delay(250);
Serial.println();
Serial.println("WiFi connected");
Serial.println("------------");
lcd.clear();
lcd.setCursor(0,0);
lcd.print("WiFi connected");
delay(2000);
//::::::::::::::::::
//----------------------------------------
lcd.clear();
delay(500);
}
//________________________________________________________________________________
//
________________________________________________________________________________VOI
D LOOP()
void loop(){
// put your main code here, to run repeatedly:
if (BTN_State == LOW) {
lcd.clear();
if (modes == "atc") {
modes = "reg";
} else if (modes == "reg") {
modes = "atc";
}
delay(500);
}
//----------------------------------------
// Detect if reading the UID from the card or keychain was successful.
readsuccess = getUID();
if (readsuccess){
lcd.clear();
delay(500);
lcd.setCursor(4,0);
lcd.print("Getting UID");
lcd.setCursor(4,1);
lcd.print("Successfully");
lcd.setCursor(0,2);
lcd.print("");
lcd.setCursor(3,3);
lcd.print("Please wait...");
delay(1000);
http_Req(modes, UID_Result);
}
}
//----------------------------------------
if (readsuccess){
lcd.clear();
delay(500);
lcd.setCursor(0,0);
lcd.print("Getting UID");
lcd.setCursor(0,1);
lcd.print("Successfully");
lcd.setCursor(0,2);
lcd.print("UID : ");
lcd.print(UID_Result);
lcd.setCursor(0,3);
lcd.print("Please wait...");
delay(1000);
http_Req(modes, UID_Result);
}
}
//----------------------------------------
delay(10);
}
//________________________________________________________________________________
//
<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
<<<<<<