Timed relay activation and billing using rfid card

I kindly need your help with the project I am working on. The project is supposed to activate the relay for 5 seconds using a predefined rfid card. The card value is 1000000 and for every use/ relay activation the figure is reduced by 500. I am supposed to get a visual feedback using the serial monitor but the code is failing to give results.
The code is below:

#include <MFRC522.h>
#include <SPI.h>

#define RST_PIN 0 // RST Pin connected to GPIO 0
#define SS_PIN 2 // SS Pin connected to GPIO 2
#define RELAY_PIN 12 // Relay Pin connected to GPIO 12

MFRC522 mfrc522(SS_PIN, RST_PIN); // Create MFRC522 instance

unsigned long initialBalance = 1000000; // Initial balance
unsigned long remainingBalance = initialBalance; // Remaining balance

unsigned long cardUsageTimeout = 0; // Timeout for card usage relay activation
const unsigned long relayActivationTime = 5000; // Relay activation time in milliseconds

void setup() {
Serial.begin(9600); // Initialize Serial Monitor
SPI.begin(); // Init SPI bus
mfrc522.PCD_Init(); // Init MFRC522

pinMode(RELAY_PIN, OUTPUT); // Set relay pin as output
digitalWrite(RELAY_PIN, LOW); // Set relay initial state as low

Serial.println("Scan your RFID card to deduct $500.");
Serial.print("Initial Balance: $");
Serial.println(initialBalance);
}

void loop() {
// Look for new cards
if (mfrc522.PICC_IsNewCardPresent() && mfrc522.PICC_ReadCardSerial()) {
String cardID = String(mfrc522.uid.uidByte[0], HEX) +
String(mfrc522.uid.uidByte[1], HEX) +
String(mfrc522.uid.uidByte[2], HEX) +
String(mfrc522.uid.uidByte[3], HEX);

if (cardID.equals("bac47b3")) {
  // Check if the relay activation timeout has passed
  if (millis() >= cardUsageTimeout) {
    // Deduct $500 from the balance
    remainingBalance -= 500;
    
    // Activate the relay for 5 seconds
    digitalWrite(RELAY_PIN, HIGH);
    delay(relayActivationTime);
    digitalWrite(RELAY_PIN, LOW);
    
    // Update the card usage timeout
    cardUsageTimeout = millis() + relayActivationTime;
    
    // Display initial and remaining balance
    Serial.print("Initial Balance: $");
    Serial.println(initialBalance);
    Serial.print("Remaining Balance: $");
    Serial.println(remainingBalance);
  } else {
    Serial.println("Relay is still active. Please wait before using the card again.");
  }
} else {
  Serial.println("Unauthorized card detected.");
}

// Halt PICC
mfrc522.PICC_HaltA();

}
}

I moved your topic to an appropriate forum category @bbiraj.

In the future, please take some time to pick the forum category that best suits the subject of your topic. There is an "About the _____ category" topic at the top of each category that explains its purpose.

This is an important part of responsible forum usage, as explained in the "How to get the best out of this forum" guide. The guide contains a lot of other useful information. Please read it.

Thanks in advance for your cooperation.

1 Like

very vauge statement
what microcontroller are you using?
upload a schematic of the wiring?
upload a copy of the serial monitor output (as text not a screen image) with comments indicating what output you expect

I am using esp8266. The wiring is as below
ESP8266 GPIO RFID-READER RELAY
D3 RST D8
D4 SDA -------
D5 SCK ------
D6 MISO --------
D7 MOSI -------
D8 ------- IN
GND GND GND
3.3V 3.3V 3.3V

SERIAL MONITOR OUTPUT- NO OUTPUT-NOTHING IS DISPLAYED

corrections made- relay pin redefined to gpio 15, D8

the pin values in post 4 don't appear to match the code in post 1?
you should at least get your inital Serial prints appearing (I assume you have the correct serial monitor baudrate selected)
noted you have the MFRC522 reset pin

#define RST_PIN 0 // RST Pin connected to GPIO 0

connected to GPIO0 which may be causing boot problems
have a look at esp8266-pinout-reference-gpios
which particular ESP8266 module are you using ? give a link to where you purchased it?

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.