IDE Code
#include <SPI.h>
#include <MFRC522.h>
#include <ESP8266WiFi.h>
#include <WiFiClientSecure.h>
#include <HTTPSRedirect.h>
#define RST_PIN D3
#define SS_PIN D4
#define GREEN D0
#define RED D1
#define BUZZER D2
MFRC522 mfrc522(SS_PIN, RST_PIN);
MFRC522::MIFARE_Key key;
const char* SSID = "SSID";
const char* PASSWORD = "PASSWORD";
const char* HOST = "script.google.com";
const int httpsPort = 443;
const char* GScriptID = "SheetID";
String payload_base = "{\"command\":\"insert_row\",\"sheet_name\":\"Sheet1\",\"values\":";
String payload = "";
String url = String("/macros/s/") + GScriptID + "/exec";
const int numOfCards = 4;
byte cards[numOfCards][4] = {
{0x83, 0x40, 0x23, 0x24},
{0x43, 0x9B, 0x1F, 0x24},
{0x66, 0xF1, 0x62, 0xAF},
{0x53, 0x1E, 0x3A, 0x24}
};
String names[numOfCards] = {"Rakshith R", "Sharath M", "Shreyas P S Rao", "Uday C H"};
String usn[numOfCards] = {"1KS20EC077", "1KS20EC093", "1KS20EC098", "1KS20EC108"};
int state = 0; // 0: EM-18 reader inactive, 1: EM-18 reader active
HTTPSRedirect* client = nullptr;
void setup() {
Serial.begin(9600);
SPI.begin();
mfrc522.PCD_Init();
pinMode(RED, OUTPUT);
pinMode(GREEN, OUTPUT);
pinMode(BUZZER, OUTPUT);
connectWiFi();
// Use HTTPSRedirect class to create a new TLS connection
client = new HTTPSRedirect(httpsPort);
client->setInsecure();
client->setPrintResponseBody(true);
client->setContentTypeHeader("application/json");
Serial.print("Connecting to ");
Serial.println(HOST);
bool flag = false;
for (int i = 0; i < 5; i++) {
int retval = client->connect(HOST, httpsPort);
if (retval == 1) {
flag = true;
Serial.println("Connected");
break;
}
else
Serial.println("Connection failed. Retrying...");
}
if (!flag) {
Serial.print("Could not connect to server: ");
Serial.println(HOST);
return;
}
}
void loop() {
if (WiFi.status() == WL_CONNECTED) {
readRFID();
}
}
void connectWiFi() {
WiFi.begin(SSID, PASSWORD);
Serial.print("Connecting to WiFi");
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("WiFi connected");
digitalWrite(GREEN, HIGH);
digitalWrite(BUZZER, HIGH);
delay(1000);
digitalWrite(GREEN, LOW);
digitalWrite(BUZZER, LOW);
}
void readRFID() {
if (Serial.available()) {
char input[12];
int count = 0;
while (Serial.available() && count < 12) {
input[count] = Serial.read();
count++;
delay(5);
}
if (count == 12) {
if ((strncmp(input, "4D008CDDDAC6", 12) == 0) && (state == 0)) {
digitalWrite(GREEN, HIGH);
digitalWrite(BUZZER, HIGH);
delay(1000);
digitalWrite(GREEN, LOW);
digitalWrite(BUZZER, LOW);
Serial.println("Valid Card detected");
state = 1;
readRFID();
} else if ((strncmp(input, "4D008CDDDAC6", 12) == 0) && (state == 1)) {
digitalWrite(GREEN, HIGH);
digitalWrite(BUZZER, HIGH);
delay(1000);
digitalWrite(GREEN, LOW);
digitalWrite(BUZZER, LOW);
state = 0;
} else {
Serial.println("Invalid card detected");
digitalWrite(RED, HIGH);
digitalWrite(BUZZER, HIGH);
delay(1000);
digitalWrite(RED, LOW);
digitalWrite(BUZZER, LOW);
}
}
}
if (state == 1) {
// Check if a new card is present
if (mfrc522.PICC_IsNewCardPresent()) {
// Read the card
if (mfrc522.PICC_ReadCardSerial()) {
byte card_ID[4];
for (byte i = 0; i < mfrc522.uid.size; i++) {
card_ID[i] = mfrc522.uid.uidByte[i];
}
// Check if the detected card matches any of the stored cards
for (int i = 0; i < numOfCards; i++) {
if (compareCardIDs(card_ID, cards[i])) {
// If valid card detected, update Google Sheets with the student's information
updateGoogleSheets(names[i], usn[i], 1); // Assuming the present value is always 1 for valid card detection
return; // Exit the loop after updating attendance for the detected card
}
}
}
}
}
}
bool compareCardIDs(byte card1[], byte card2[]) {
for (int i = 0; i < 4; i++) {
if (card1[i] != card2[i]) {
return false;
}
}
return true;
}
bool updateGoogleSheets(String name, String usn, int present) {
payload = "{\"command\": \"insert_row\", \"sheet_name\": \"Sheet1\", \"values\": \"" + name + "," + usn + "," + String(present) + "\"}";
// Publish data to Google Sheets
Serial.println("Publishing data...");
Serial.println(payload);
if (client->POST(url, HOST, payload)) {
// Do something if publish was successful
return true;
}
else {
// Do something if publish was not successful
Serial.println("Error while connecting");
return false;
}
// A delay of several seconds is required before publishing again
delay(5000);
}