"Latching circuit" with RFID tags

Hi guys, I'm currently working on a project where people need to scan their RFID tags when entering and then re-scan it when exiting. All the data is sent to an MQT broker and then sent on Telegram using Node-Red.

So my problem is that I need Arduino to know when a tag is scanned (entering) and re-scanned(exiting).

Any advice would be helpful :slight_smile:

Libraries :
ArduinoMQTTClient.h

WiFiNINA.h

SPI.h

MFRC522.h

Hardware :
1x Arduino MKR WiFi 1010

1x RFID-RC522

(Other componenents such as LEDs and piezo buzzer are not listed).

Wiring :
Arduino D2 : Red LED
Arduino D3 : Green LED
Arduino D4 : Piezo buzzer
Arduino D5 : Piezo buzzer
Arduino D6 : RC-522 RST
Arduino D8 : RC-522 MOSI
Arduino D9 : RC-522 SCK
Arduino D10 : RC-522 MISO
Arduino D11 : RC-522 SDA
Arduino GND : RC-522 GND
Arduino Vcc : RC-522 3.3V

Pin 4 and 5 are both connected to the piezo because the MKR board is a 3.3 V board and thats the max voltage output on the pins. The Piezo requires atleast 5 volts

Code :

//Include libraries
#include <ArduinoMqttClient.h>
#include <WiFiNINA.h>
#include <SPI.h>
#include <MFRC522.h>

//RFID Pins
#define RST_PIN 6
#define SS_PIN 11

//RFID reader instance
MFRC522 mfrc522(SS_PIN, RST_PIN);
MFRC522::MIFARE_Key key;

//Wifi configuration
const char ssid[]         = "XXX";
const char pass[]         = "XXX";

//MQTT configuration
const char broker[]       = "XXX";
int port                  = XXX;
const char user[]         = "XXX";
const char password[]     = "XXX";
const char topicUS[]        = "XXX/User";
const char topicKA[]        = "XXX/Info";

/*
RFID Tags :
E370XXXX = Tag 1
B3D5XXXX = Tag 2
E361XXXX = Tag 3
*/

//MQTT and WiFi clients configuration
WiFiClient wifiClient;
MqttClient mqttClient(wifiClient);

void setup() {

  //Pin setup
  pinMode(2, OUTPUT); //Red LED
  pinMode(3, OUTPUT); //Green LED
  pinMode(4, OUTPUT); //Piezo buzzer
  pinMode(5, OUTPUT); //Piezo buzzer

  //Serial monitor
  Serial.begin(115200);
  delay(1500);

  //SPI interfacing
  SPI.begin();

  //Connecting to WiFi
  Serial.print("Connecting to WiFi, your SSID :");
  Serial.print(ssid);
  while (WiFi.begin(ssid, pass) != WL_CONNECTED) {
    Serial.print(".");
    delay(5000);
  }
  Serial.println();
  Serial.println("Succesfully connected to WiFi!");
  Serial.println();

  //Connecting to MQTT broker
  Serial.print("Connecting to MQTT broker, your adress : ");
  Serial.println(broker);
  mqttClient.setUsernamePassword(user, password);
  if (!mqttClient.connect(broker, port)) {
    Serial.print("Connection failed, error code = ");
    Serial.print(mqttClient.connectError());
    while (1)
      ;
  }
  Serial.println("Succesfully connected to MQTT broker");
  mqttClient.beginMessage(topicKA);
  mqttClient.print("Arduino connected!");
  mqttClient.endMessage();
  Serial.println();

  //Reader initialisation
  mfrc522.PCD_Init();
  mfrc522.PCD_DumpVersionToSerial();
  Serial.println();
}

void loop() {   

  //Keeps MQTT Connected
  KeepAliveMQTT();

  digitalWrite(3, HIGH);
  //Search for new tags
  if (!mfrc522.PICC_IsNewCardPresent()) {
    return;
  }

  //Read tag info
  if (!mfrc522.PICC_ReadCardSerial()) {
    return;
  }


  //Extract UID 
  MFRC522::PICC_Type piccType = mfrc522.PICC_GetType(mfrc522.uid.sak);

  //Print UID to serial monitor 
  Serial.print(F("Tag UID : "));
  Serial.println();
  mfrc522.PICC_HaltA();

  RFIDTags();
  UserInter();
}

//Keeps the connection between Arduino and MQTT
void KeepAliveMQTT(){
  delay(1000);
  mqttClient.beginMessage(topicKA);
      mqttClient.print("Keep Alive");
      mqttClient.endMessage();
}

void RFIDTags(){
  
  char UIDString[32] = "";
    Convert(mfrc522.uid.uidByte, 4, UIDString);
    Serial.println(UIDString);
    mfrc522.PICC_HaltA();
    Serial.println();
    
  int user1state = strcmp(UIDString, "E370XXXX");
  int user2state = strcmp(UIDString, "B3D5XXXX");
  int user3state = strcmp(UIDString, "E361XXXX");
  
  if(user1state == 0){
      mqttClient.beginMessage(topicUS);
      mqttClient.print("User1");
      mqttClient.endMessage();
      UserInter();
                            
   } 
  else if(user2state == 0){
      mqttClient.beginMessage(topicUS);
      mqttClient.print("User2");
      mqttClient.endMessage();
      UserInter();
      
  }
  else if(user3state == 0){
      mqttClient.beginMessage(topicUS);
      mqttClient.print("User3");
      mqttClient.endMessage();
      UserInter();
          
  }
}

//LEDs and piezo control
void UserInter(){
      digitalWrite(3, LOW);
      digitalWrite(2, HIGH);
      digitalWrite(4, HIGH);
      digitalWrite(5, HIGH);
      delay(400);
      digitalWrite(4, LOW);
      digitalWrite(5, LOW);
      delay(1000);      
      digitalWrite(2, LOW);
      digitalWrite(3, HIGH);  
}

//Converting Byte[]>>Char
void Convert(byte array[], unsigned int len, char buffer[]){
   for (unsigned int i = 0; i < len; i++){
      byte nib1 = (array[i] >> 4) & 0x0F;
      byte nib2 = (array[i] >> 0) & 0x0F;
      buffer[i*2+0] = nib1  < 0xA ? '0' + nib1  : 'A' + nib1  - 0xA;
      buffer[i*2+1] = nib2  < 0xA ? '0' + nib2  : 'A' + nib2  - 0xA;
      }
   buffer[len*2] = '\0';
}


If you have any other improvements for my code that would be very helpful.

Emirhan

a lower cost alternalive to the MKR WiFi 1010 would be an ESP32 with onboard WiFi, Bluetooth clasic and BLE

Well I have some ESP32 boards at home.I will definilety compare both of the boards to see if one of them is better or not...

if you do a web search for esp32 rfid you will get plenty of links

I guess I will use two buttons "entering" and "exiting" instead of a latching circuit which knows if a tag is scanned for entering or exiting

How many different tags will the system be dealing with ?

30 tags will be used permanently and 30 more for visitors...

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