// Install from the Library Manager: // "MFRC522" (GithubCommunity) and // "LiquidCrystal I2C" // Board: "ESP32 Dev Module" #include #include #include #include // The RC522's SDA and RST pins; SCK, // MISO, MOSI are 18, 19, 23 on the ESP32 const int SS_PIN = 5; const int RST_PIN = 4; // The LCD's I2C pins const int SDA_PIN = 21; const int SCL_PIN = 22; // The same card again within this (ms) const unsigned long REPEAT_GUARD = 3000; // A message stays on screen this long const unsigned long MESSAGE_TIME = 3000; // The known cards: number and name. // A new card's number shows in the Serial // Monitor; add it here (a name is at most // 16 letters). struct Person { const char* uid; const char* name; bool inside; }; Person people[] = { {"01 02 03 04", "Ayse", false}, {"11 22 33 44", "Burak", false}, {"55 66 77 88", "Elif", false}, }; const int PEOPLE = sizeof(people) / sizeof(people[0]); MFRC522 rfid(SS_PIN, RST_PIN); // I2C address: 0x27 on most, 0x3F on some LiquidCrystal_I2C lcd(0x27, 16, 2); String lastUid = ""; unsigned long lastReadAt = 0; unsigned long messageAt = 0; bool showingMessage = false; int insideCount() { int count = 0; for (int i = 0; i < PEOPLE; i++) { if (people[i].inside) count++; } return count; } // The card's number: "01 02 03 04" String readUid() { String uid = ""; for (byte i = 0; i < rfid.uid.size; i++) { if (i > 0) uid += " "; if (rfid.uid.uidByte[i] < 0x10) uid += "0"; uid += String(rfid.uid.uidByte[i], HEX); } uid.toUpperCase(); return uid; } // The main screen: waiting for a card void showIdle() { lcd.clear(); lcd.print("Scan your card"); lcd.setCursor(0, 1); lcd.print("Inside: "); lcd.print(insideCount()); } void setup() { Serial.begin(115200); Wire.begin(SDA_PIN, SCL_PIN); lcd.init(); lcd.backlight(); SPI.begin(); rfid.PCD_Init(); // A reader that does not answer reads // as version 0x00 or 0xFF byte version = rfid.PCD_ReadRegister(MFRC522::VersionReg); if (version == 0x00 || version == 0xFF) { Serial.println("RC522 not found, check the wires."); lcd.print("No RC522!"); lcd.setCursor(0, 1); lcd.print("Check the wires"); while (true) { delay(1000); } } Serial.println("Attendance ready: scan your card."); showIdle(); } void loop() { // When the message is over, main screen if (showingMessage && millis() - messageAt > MESSAGE_TIME) { showingMessage = false; showIdle(); } if (!rfid.PICC_IsNewCardPresent()) return; if (!rfid.PICC_ReadCardSerial()) return; String uid = readUid(); rfid.PICC_HaltA(); // A card left on the reader counts once if (uid == lastUid && millis() - lastReadAt < REPEAT_GUARD) { return; } lastUid = uid; lastReadAt = millis(); lcd.clear(); showingMessage = true; messageAt = millis(); int found = -1; for (int i = 0; i < PEOPLE; i++) { if (uid == people[i].uid) found = i; } if (found < 0) { lcd.print("Unknown card"); lcd.setCursor(0, 1); lcd.print(uid.substring(0, 16)); Serial.print("Unknown card: "); Serial.println(uid); return; } // A known card: out if it was inside, // in if not Person& person = people[found]; person.inside = !person.inside; lcd.print(person.name); lcd.setCursor(0, 1); lcd.print(person.inside ? "IN " : "OUT "); lcd.print("Inside:"); lcd.print(insideCount()); Serial.print(person.name); Serial.print(person.inside ? " came in" : " left"); Serial.print(", now "); Serial.print(insideCount()); Serial.println(" inside"); }