Dear All,
I have a simple rfid attendance project. Everything is working perfectly. The program is as simple as an rfid attached to an arduino esp with wifi scans an rfid tag, and posts the rfid tag number to a google sheet, and responds with an led light if the post was successful.
The problem i am facing is that whenever a post is made, the led takes a lot of time (15 seconds) to turn on, which means, the time consumed to get a successful post response is long.
are there any suggestions?
here is my code:
#include <Arduino.h>
#include <ESP8266WiFi.h>
#include <SPI.h>
#include <MFRC522.h>
#include <HTTPSRedirect.h>
#include<Wire.h>
#define LED_PIN D8 // PIN TO CONNECT TO RELAY, TO OPEN DOOR. FOR INSIDE FINGERPRINT CONNECT NORMALLY OPEN, FOR OUTSIDE CONNECT NORMALLY CLOSED
#define LED_BLUE D0 // PIN TO INDICATE THAT FINGERPRINT ON SHEETS SUCCESSFULLY ADDED
//---------------------------------------------------------------------------------------------------------
// Enter Google Script Deployment ID:
const char *GScriptId = "AKfycbxoSbmuDNozp-rE3kbn6cabtS6qJKkazFiqPSw--HmsMFP0YrOwPtxFpBIZyZos0E4I"; // THE GOOGLE SCRIPT ID NOT THE GOOGLE SHEETS LINK
//---------------------------------------------------------------------------------------------------------
// Enter network credentials:
const char* ssid = "Saba and Atalla 2.4"; // SSID
const char* password = "HaffiTuttu"; // PASSWORD
//---------------------------------------------------------------------------------------------------------
// Enter command (insert_row or append_row) and your Google Sheets sheet name (default is Sheet1):
String payload_base = "{\"command\": \"insert_row\", \"sheet_name\": \"Sheet1\", \"values\": ";
String payload = "";
//---------------------------------------------------------------------------------------------------------
// Google Sheets setup (do not edit)
const char* host = "script.google.com";
const int httpsPort = 443;
const char* fingerprint = "";
String url = String("/macros/s/") + GScriptId + "/exec";
HTTPSRedirect* client = nullptr;
//------------------------------------------------------------
// Declare variables that will be published to Google Sheets
String student_id; // EMPLOYEE FINGERPRINT ID
//------------------------------------------------------------
int blocks[] = {4,5,6};
#define total_blocks (sizeof(blocks) / sizeof(blocks[0]))
//------------------------------------------------------------
#define RST_PIN 0 //D3
#define SS_PIN 2 //D4
#define BUZZER 4 //D2
//------------------------------------------------------------
MFRC522 mfrc522(SS_PIN, RST_PIN);
MFRC522::MIFARE_Key key;
MFRC522::StatusCode status;
//------------------------------------------------------------
/* Be aware of Sector Trailer Blocks */
int blockNum = 2;
/* Create another array to read data from Block */
/* Legthn of buffer should be 2 Bytes more than the size of Block (16 Bytes) */
byte bufferLen = 18;
byte readBlockData[18];
//------------------------------------------------------------
/****************************************************************************************************
* setup Function
****************************************************************************************************/
void setup() {
//----------------------------------------------------------
Serial.begin(9600);
delay(10);
pinMode(LED_PIN, OUTPUT);
pinMode(LED_BLUE, OUTPUT);
Serial.println('\n');
//----------------------------------------------------------
SPI.begin();
//----------------------------------------------------------
//----------------------------------------------------------
// Connect to WiFi
WiFi.begin(ssid, password);
Serial.print("Connecting to ");
Serial.print(ssid); Serial.println(" ...");
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.print(".");
}
Serial.println('\n');
Serial.println("Connection established!");
Serial.print("IP address:\t");
Serial.println(WiFi.localIP());
//----------------------------------------------------------
// Use HTTPSRedirect class to create a new TLS connection
client = new HTTPSRedirect(httpsPort);
client->setInsecure();
client->setPrintResponseBody(true);
client->setContentTypeHeader("application/json");
//----------------------------------------------------------
delay(5000);
//----------------------------------------------------------
Serial.print("Connecting to ");
Serial.println(host);
//----------------------------------------------------------
// Try to connect for a maximum of 5 times
bool flag = false;
for(int i=0; i<5; i++){
int retval = client->connect(host, httpsPort);
//*************************************************
if (retval == 1){
flag = true;
String msg = "Connected. OK";
Serial.println(msg);
delay(2000);
break;
}
//*************************************************
else
Serial.println("Connection failed. Retrying...");
//*************************************************
}
//----------------------------------------------------------
if (!flag){
//____________________________________________
Serial.print("Could not connect to server: ");
Serial.println(host);
delay(5000);
return;
//____________________________________________
}
//----------------------------------------------------------
delete client; // delete HTTPSRedirect object
client = nullptr; // delete HTTPSRedirect object
//----------------------------------------------------------
}
/****************************************************************************************************
* loop Function
****************************************************************************************************/
void loop() {
/* Initialize MFRC522 Module */
mfrc522.PCD_Init();
/* Look for new cards */
/* Reset the loop if no new card is present on RC522 Reader */
if ( ! mfrc522.PICC_IsNewCardPresent()){return;}
/* Select one of the cards */
if ( ! mfrc522.PICC_ReadCardSerial()) {return;}
/* Read data from the same block */
Serial.println();
Serial.println(F("Reading last data from RFID..."));
String rfidUID = "";
for (byte i = 0; i < mfrc522.uid.size; i++) {
rfidUID += String(mfrc522.uid.uidByte[i] < 0x10 ? "0" : "");
rfidUID += String(mfrc522.uid.uidByte[i], HEX);
}
//----------------------------------------------------------------
static bool flag = false;
if (!flag){
client = new HTTPSRedirect(httpsPort);
client->setInsecure();
flag = true;
client->setPrintResponseBody(true);
client->setContentTypeHeader("application/json");
}
if (client != nullptr){
if (!client->connected())
{client->connect(host, httpsPort);}
}
else{Serial.println("Error creating client object!");}
//----------------------------------------------------------------
//creating payload - method 2 - More efficient
String IN= "IN";
//----------------------------------------------------------------
// Create json object string to send to Google Sheets
// values = "\"" + value0 + "," + value1 + "," + value2 + "\"}"
payload = payload_base + "\"" + rfidUID +"," + IN +"\"" "}";
//----------------------------------------------------------------
//----------------------------------------------------------------
// Publish data to Google Sheets
Serial.println("Publishing data...");
Serial.println(payload);
if(client->POST(url, host, payload)){
// do stuff here if publish was successful
digitalWrite(LED_PIN, HIGH);
digitalWrite(LED_BLUE, HIGH);
delay (1000);
digitalWrite(LED_PIN, LOW);
digitalWrite(LED_BLUE, LOW);
}
//----------------------------------------------------------------
else{
// do stuff here if publish was not successful
Serial.println("Error while connecting");
rfidUID ="";
return;
}
//----------------------------------------------------------------
// a delay of several seconds is required before publishing again
delay(3000);
}
Thank you