Hello, I am working on a project for a company. My project is for workers when they come they check in with the RFID card.
Problems occur when the reader is not used for a few minutes. LCD shows time like normal but when I swipe the card it just doesn’t sense it. What could be the problem? I am using ESP32 an RC522 RFID reader. All RFID related code is in method named “void rfidReader()” after “void loop()”.
Thanks for the help and nice regards,
Tilen
/*----------------------------------------RFID_START*/
#include <SPI.h>
#include <MFRC522.h>
#include <Arduino.h>
int SS_PIN_RFID = 17;
int RST_PIN_RFID = 5;
MFRC522 rfid(SS_PIN_RFID, RST_PIN_RFID); // Instance of the class
MFRC522::MIFARE_Key key;
/*----------------------------------------RFID_END*/
/*----------------------------------------LCD_START*/
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
// set the LCD address to 0x27 for a 16 chars and 2 line display
LiquidCrystal_I2C lcd(0x3f, 16, 2);
/*----------------------------------------LCD_END*/
/*----------------------------------------TIME_START*/
#include <WiFi.h>
#include "time.h"
const char *ssid = "Komel_Wi-Fi";
const char *password = "buntovi2010";
const char *ntpServer = "pool.ntp.org";
const long gmtOffset_sec = 0;
const int daylightOffset_sec = 7200;
String sec = 0;
/*----------------------------------------TIME_END*/
/*----------------------------------------SD_START*/
#include <mySD.h>
File root;
/*----------------------------------------SD_END*/
/*----------------------------------------TONE_START*/
#include <Tone32.h>
#define BUZZER_PIN 25
#define BUZZER_CHANNEL 0
/*----------------------------------------TONE_END*/
int led_green = 33;
int led_red = 16;
void setup()
{
Serial.begin(115200);
pinMode(led_green, OUTPUT);
pinMode(led_red, OUTPUT);
/*----------------------------------------LCD_START_setUp*/
lcd.init();
// Print a message to the LCD.
lcd.backlight();
delay(2000);
/*----------------------------------------LCD_END_setUp*/
/*----------------------------------------RFID_START_setUp*/
// Serial.begin(115200);
SPI.begin(); // Init SPI bus
rfid.PCD_Init(); // Init MFRC522
for (byte i = 0; i < 6; i++)
{
key.keyByte[i] = 0xFF;
}
/*----------------------------------------RFID_END_setUp*/
/*----------------------------------------TIME_START_setUp*/
// Connect to Wi-Fi
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED)
{
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Wi-Fi ERR");
lcd.setCursor(0, 1);
lcd.print("Zapisi cas");
myTone("ERR");
delay(1000);
}
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Wi-Fi OK");
// Init and get the time
configTime(gmtOffset_sec, daylightOffset_sec, ntpServer);
printLocalTime();
//disconnect WiFi as it's no longer needed
WiFi.disconnect(true);
WiFi.mode(WIFI_OFF);
/*----------------------------------------TIME_END_setUp*/
/*----------------------------------------SD_START_setUp*/
Serial.print("Initializing SD card...");
/* initialize SD library with Soft SPI pins, if using Hard SPI replace with this SD.begin()*/
if (!SD.begin(26, 14, 13, 27))
{
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Card ERR");
lcd.setCursor(0, 1);
lcd.print("Zapisi cas");
myTone("ERR");
delay(4000);
return;
}
Serial.println("initialization done.");
root = SD.open("/");
if (root)
{
root.close();
}
else
{
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Card ERR");
lcd.setCursor(0, 1);
lcd.print("Zapisi cas");
myTone("ERR");
delay(4000);
}
/*----------------------------------------SD_END_setUp*/
digitalWrite(led_red, LOW);
digitalWrite(led_green, HIGH);
}
void loop()
{
vTaskDelay(10);
struct tm timeinfo;
if (!getLocalTime(&timeinfo))
{
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Time ERR");
lcd.setCursor(0, 1);
lcd.print("Zapisi cas");
myTone("ERR");
delay(4000);
}
else
{
if (sec == getTime("%S"))
{
rfidReader();
Serial.println("true");
}
else
{
printLocalTime();
sec = getTime("%S");
Serial.println("false");
}
}
}
void rfidReader()
{
// Reset the loop if no new card present on the sensor/reader. This saves the entire process when idle.
if (!rfid.PICC_IsNewCardPresent())
return;
// Verify if the NUID has been readed
if (!rfid.PICC_ReadCardSerial())
return;
MFRC522::PICC_Type piccType = rfid.PICC_GetType(rfid.uid.sak);
// Check is the PICC of Classic MIFARE type
if (piccType != MFRC522::PICC_TYPE_MIFARE_MINI &&
piccType != MFRC522::PICC_TYPE_MIFARE_1K &&
piccType != MFRC522::PICC_TYPE_MIFARE_4K)
{
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Wrong card");
lcd.setCursor(0, 1);
lcd.print("Zapisi cas");
myTone("ERR");
delay(4000);
return;
}
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Zivijo");
myTone("OK");
delay(2000);
String cardHex = printHex(rfid.uid.uidByte, rfid.uid.size);
prepareOut(cardHex);
// Halt PICC
rfid.PICC_HaltA();
// Stop encryption on PCD
rfid.PCD_StopCrypto1();
}
String printHex(byte *buffer, byte bufferSize)
{
String userid;
for (byte i = 0; i < bufferSize; i++)
{
Serial.print(buffer[i], HEX);
userid += String(buffer[i], HEX);
}
return userid;
}
void printLocalTime()
{
struct tm timeinfo;
if (!getLocalTime(&timeinfo))
{
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Time ERR");
lcd.setCursor(0, 1);
lcd.print("Zapisi cas");
myTone("ERR");
delay(4000);
return;
}
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(&timeinfo, "%d");
lcd.print(".");
lcd.print(&timeinfo, "%b");
lcd.print(".");
lcd.print(&timeinfo, "%Y");
lcd.setCursor(0, 1);
lcd.print(&timeinfo, "%H");
lcd.print(":");
lcd.print(&timeinfo, "%M");
lcd.print(":");
lcd.print(&timeinfo, "%S");
}
void saveSD(String text)
{
String monthText = getTime("%B");
String month = monthNum(monthText);
String year = getTime("%Y");
String file = month + "_" + year + ".txt";
char filename[file.length() + 1];
file.toCharArray(filename, sizeof(filename));
root = SD.open(filename, FILE_WRITE);
if (root)
{
root.println(text);
root.flush();
root.close();
}
else
{
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Card ERR");
lcd.setCursor(0, 1);
lcd.print("Zapisi cas");
myTone("ERR");
delay(4000);
}
}
String monthNum(String monthString)
{
if (monthString == "January")
{
return "1";
}
else if (monthString == "February")
{
return "2";
}
else if (monthString == "March")
{
return "3";
}
else if (monthString == "April")
{
return "4";
}
else if (monthString == "May")
{
return "5";
}
else if (monthString == "June")
{
return "6";
}
else if (monthString == "July")
{
return "7";
}
else if (monthString == "August")
{
return "8";
}
else if (monthString == "September")
{
return "9";
}
else if (monthString == "October")
{
return "10";
}
else if (monthString == "November")
{
return "11";
}
else if (monthString == "December")
{
return "12";
}
else
{
return "0";
}
}
String prepareOut(String cardHex)
{
String output = "";
String time = getTime("%H:%M:%S");
String year = getTime("%Y");
String day = getTime("%d");
String month = getTime("%B");
if (cardHex.length() != 8)
cardHex += " ";
output = cardHex + "|" + time + "|" + day + "." + monthNum(month) + "." + year;
saveSD(output);
}
String getTime(char *tags)
{
struct tm timeinfo;
if (!getLocalTime(&timeinfo))
{
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Time ERR");
lcd.setCursor(0, 1);
lcd.print("Zapisi cas");
myTone("ERR");
delay(4000);
}
char timeStringBuff[50];
strftime(timeStringBuff, sizeof(timeStringBuff), tags, &timeinfo);
String asString(timeStringBuff);
return asString;
}
void myTone(String type)
{
if (type == "OK")
{
digitalWrite(led_red, LOW);
digitalWrite(led_green, HIGH);
tone(BUZZER_PIN, 4000, 100, BUZZER_CHANNEL);
delay(100);
tone(BUZZER_PIN, 4000, 100, BUZZER_CHANNEL);
}
else
{
digitalWrite(led_green, LOW);
digitalWrite(led_red, HIGH);
tone(BUZZER_PIN, 4000, 500, BUZZER_CHANNEL);
}
}