Arduino attendance and electric door lock combo.

Hi, so I’ve been messing around with the Arduino Ethernet shield for a college project. The objective is to create an RFID based online attendance system which will store data in Google Spreadsheet. It’ll also double as a security door lock as well. The programs I’m using were taken online and they work fine separately. However, the problem came when I was trying to merge the two programs. There were no errors during compiling but the program didn’t seem to function.

Below is the merged version of both programs.

#include<SPI.h>
#include<MFRC522.h>
#include <Ethernet.h>
#define SS_PIN 4 //FOR mfrc522 SS PIN BECASUSE WE ARE USING BOTH ETHERNET SHIELD AND RS-522
#define RST_PIN 9
#define RELAY 3 //relay pin
#define No_Of_Card 4
#define ACCESS_DELAY 2000
#define DENIED_DELAY 1000
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
char server[] = "api.pushingbox.com";   //YOUR SERVER
IPAddress ip(192, 168, 137, 177);
EthernetClient client;     
MFRC522 mfrc522(SS_PIN,RST_PIN);
MFRC522::MIFARE_Key key; 
byte id[No_Of_Card][4]={
  {95,50,42,83},               //mfrc522 NO-1
  {153,178,60,64},             //mfrc522 NO-2
  {141,764,60,94}              //mfrc522 NO-3
};
byte id_temp[3][3];
byte i;
int j=0;


// the setup function runs once when you press reset or power the board
void setup(){
    Serial.begin(9600);
    SPI.begin();
    mfrc522.PCD_Init();
    pinMode(RELAY, OUTPUT);
    digitalWrite(RELAY, HIGH);
    Serial.println("Put your card to the reader...");
     Serial.println();
    for(byte i=0;i<6;i++)
    {
      key.keyByte[i]=0xFF;
    }
    if (Ethernet.begin(mac) == 0) {
      Serial.println("Failed to configure Ethernet using DHCP");
      Ethernet.begin(mac, ip);
    }
    delay(1000);
    Serial.println("connecting...");
 }

// the loop function runs over and over again forever
void loop()
{
    int m=0;
    if(!mfrc522.PICC_IsNewCardPresent())
    {
    return;
    }
    if(!mfrc522.PICC_ReadCardSerial())
    {
    return;
    }
    String content= "";  
  for(byte i=0;i<6;i++)
    {
     Serial.print(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " ");
     Serial.print(mfrc522.uid.uidByte[i], HEX);
     content.concat(String(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " "));
     content.concat(String(mfrc522.uid.uidByte[i], HEX));
     id_temp[0][i]=mfrc522.uid.uidByte[i]; 
               delay(50);
    }
    
     for(i=0;i<No_Of_Card;i++)
    {
            if(id[i][0]==id_temp[0][0])
            {
              if(id[i][1]==id_temp[0][1])
              {
                if(id[i][2]==id_temp[0][2])
                {
                  if(id[i][3]==id_temp[0][3])
                  {
                    Serial.print("your card no :");
                    for(int s=0;s<4;s++)
                    {
                      Serial.print(mfrc522.uid.uidByte[s]);
                      Serial.print(" ");
                     
                    }
                    Serial.println("\nValid Person");
                    Sending_To_spreadsheet();
                    Serial.println("Authorized access");
                    Serial.println();
                    delay(500);
                    digitalWrite(RELAY, LOW);
                    delay(ACCESS_DELAY);
                    digitalWrite(RELAY, HIGH);
                    j=0;
                              
                              mfrc522.PICC_HaltA(); mfrc522.PCD_StopCrypto1();   return; 
                  }
                }
              }
            }
     else
     {j++;
      if(j==No_Of_Card)
      {
        Serial.println("Not a valid Person");
        Sending_To_spreadsheet();
        Serial.println(" Access denied");
        delay(DENIED_DELAY);
        j=0;
      }
     }
    }
    
       // Halt PICC
    mfrc522.PICC_HaltA();
  
    // Stop encryption on PCD
    mfrc522.PCD_StopCrypto1();
 }

 void Sending_To_spreadsheet()   //CONNECTING WITH MYSQL
 {
   if (client.connect(server, 80)) {
    Serial.println("connected");
    // Make a HTTP request:
    client.print("GET /pushingbox?devid=<Your Device ID>&allowed_members=");     //YOUR URL
    if(j!=No_Of_Card)
    {
      client.print('1');
//      Serial.print('1');
    }
    else
    {
      client.print('0');
    }
    
    client.print("&Member_ID=");
    for(int s=0;s<4;s++)
                  {
                    client.print(mfrc522.uid.uidByte[s]);
                                  
                  }
    client.print(" ");      //SPACE BEFORE HTTP/1.1
    client.print("HTTP/1.1");
    client.println();
    client.println("Host: api.pushingbox.com");
    client.println("Connection: close");
    client.println();
  } else {
    // if you didn't get a connection to the server:
    Serial.println("connection failed");
  }
 }

The merging was mostly done in an assumption of what the code meant.

I’m using an Arduino Uno R3 to house the program. And an MFRC522 RFID. Was wondering if could the fault be in the Arduino itself not being strong enough or in the program?

Circuit diagram.pdf (145 KB)

Below, is the separate program for sending online data and the door lock.

“Electric door lock program”

#include <SPI.h>
#include <MFRC522.h>
 
#define SS_PIN 4
#define RST_PIN 9
#define RELAY 3 //relay pin
#define ACCESS_DELAY 2000
#define DENIED_DELAY 1000
MFRC522 mfrc522(SS_PIN, RST_PIN);   // Create MFRC522 instance.
 
void setup() 
{
  Serial.begin(9600);   // Initiate a serial communication
  SPI.begin();          // Initiate  SPI bus
  mfrc522.PCD_Init();   // Initiate MFRC522
  pinMode(RELAY, OUTPUT);
  digitalWrite(RELAY, HIGH);
  Serial.println("Put your card to the reader...");
  Serial.println();

}
void loop() 
{
  // Look for new cards
  if ( ! mfrc522.PICC_IsNewCardPresent()) 
  {
    return;
  }
  // Select one of the cards
  if ( ! mfrc522.PICC_ReadCardSerial()) 
  {
    return;
  }
  //Show UID on serial monitor
  Serial.print("UID tag :");
  String content= "";
  byte letter;
  for (byte i = 0; i < mfrc522.uid.size; i++) 
  {
     Serial.print(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " ");
     Serial.print(mfrc522.uid.uidByte[i], HEX);
     content.concat(String(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " "));
     content.concat(String(mfrc522.uid.uidByte[i], HEX));
  }
  Serial.println();
  Serial.print("Message : ");
  content.toUpperCase();
  if (content.substring(1) == "95 50 42 83") //change here the UID of the card/cards that you want to give access
  {
    Serial.println("Authorized access");
    Serial.println();
    delay(500);
    digitalWrite(RELAY, LOW);
    delay(ACCESS_DELAY);
    digitalWrite(RELAY, HIGH);
  }
 
 else   {
    Serial.println(" Access denied");
    delay(DENIED_DELAY);
  }
}

“Online attendance program”

#include<SPI.h>
#include<MFRC522.h>
#include <Ethernet.h>
#define SS_PIN 4 //FOR RFID SS PIN BECASUSE WE ARE USING BOTH ETHERNET SHIELD AND RS-522
#define RST_PIN 9
#define No_Of_Card 3
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
char server[] = "api.pushingbox.com";   //YOUR SERVER
IPAddress ip(192, 168, 137, 177);
EthernetClient client;     
MFRC522 rfid(SS_PIN,RST_PIN);
MFRC522::MIFARE_Key key; 
byte id[No_Of_Card][4]={
  {142,76,58,42},             //RFID NO-1
  {153,178,60,64},             //RFID NO-2
  {141,764,60,94}              //RFID NO-3
};
byte id_temp[3][3];
byte i;
int j=0;


// the setup function runs once when you press reset or power the board
void setup(){
    Serial.begin(9600);
    SPI.begin();
    rfid.PCD_Init();
    for(byte i=0;i<6;i++)
    {
      key.keyByte[i]=0xFF;
    }
    if (Ethernet.begin(mac) == 0) {
      Serial.println("Failed to configure Ethernet using DHCP");
      Ethernet.begin(mac, ip);
    }
    delay(1000);
    Serial.println("connecting...");
 }

// the loop function runs over and over again forever
void loop(){
    int m=0;
    if(!rfid.PICC_IsNewCardPresent())
    return;
    if(!rfid.PICC_ReadCardSerial())
    return;
    for(i=0;i<4;i++)
    {
     id_temp[0][i]=rfid.uid.uidByte[i]; 
               delay(50);
    }
    
     for(i=0;i<No_Of_Card;i++)
    {
            if(id[i][0]==id_temp[0][0])
            {
              if(id[i][1]==id_temp[0][1])
              {
                if(id[i][2]==id_temp[0][2])
                {
                  if(id[i][3]==id_temp[0][3])
                  {
                    Serial.print("your card no :");
                    for(int s=0;s<4;s++)
                    {
                      Serial.print(rfid.uid.uidByte[s]);
                      Serial.print(" ");
                     
                    }
                    Serial.println("\nValid Person");
                    Sending_To_spreadsheet();
                    j=0;
                              
                              rfid.PICC_HaltA(); rfid.PCD_StopCrypto1();   return; 
                  }
                }
              }
            }
     else
     {j++;
      if(j==No_Of_Card)
      {
        Serial.println("Not a valid Person");
        Sending_To_spreadsheet();
        j=0;
      }
     }
    }
    
       // Halt PICC
    rfid.PICC_HaltA();
  
    // Stop encryption on PCD
    rfid.PCD_StopCrypto1();
 }

 void Sending_To_spreadsheet()   //CONNECTING WITH MYSQL
 {
   if (client.connect(server, 80)) {
    Serial.println("connected");
    // Make a HTTP request:
    client.print("GET /pushingbox?devid=<Your Device ID>&allowed_members=");     //YOUR URL
    if(j!=No_Of_Card)
    {
      client.print('1');
//      Serial.print('1');
    }
    else
    {
      client.print('0');
    }
    
    client.print("&Member_ID=");
    for(int s=0;s<4;s++)
                  {
                    client.print(rfid.uid.uidByte[s]);
                                  
                  }
    client.print(" ");      //SPACE BEFORE HTTP/1.1
    client.print("HTTP/1.1");
    client.println();
    client.println("Host: api.pushingbox.com");
    client.println("Connection: close");
    client.println();
  } else {
    // if you didn't get a connection to the server:
    Serial.println("connection failed");
  }
 }

bfcw1997:
the program didn't seem to function.

Based on this description, my best guess is there's something wrong with the program.

Now if you would be a bit more generous in giving details of what it does, and how it is different from what you want it to do, we may actually point out where/how it goes wrong.

Looking at the locking program, I don’t see any access allowed for emergency services people.

Paul

wvmarle:
Based on this description, my best guess is there’s something wrong with the program.

Now if you would be a bit more generous in giving details of what it does, and how it is different from what you want it to do, we may actually point out where/how it goes wrong.

I see… Basically the electric door lock is supposed to unlock when the RFID tag is scanned. Every time an RFID tag is scanned, a timestamp will be sent to a Google spreadsheet I’ve made. Now it doesn’t seem to be doing either function. Checking the serial monitor, the program seems like its stuck somewhere.

Paul_KD7HB:
Looking at the locking program, I don’t see any access allowed for emergency services people.

Paul

I’ll take a look at that part of the program… I might have left out a few things while merging.

Will your program know if someone walks in and walks right back out? Or will they be counted as attending?

GoForSmoke:
Will your program know if someone walks in and walks right back out? Or will they be counted as attending?

No, it can't. There aren't any sensors for that. It only tracks when a designated RFID tag is scanned onto the module, after that it'll send a data to a premade Google Spreadsheet. The door lock program has a section which tells the RFID UID in the serial monitor.

I agree, it’s stuck somewhere. Perhaps you can note where it’s stuck. then tell us.

bfcw1997:
No, it can't. There aren't any sensors for that. It only tracks when a designated RFID tag is scanned onto the module, after that it'll send a data to a premade Google Spreadsheet. The door lock program has a section which tells the RFID UID in the serial monitor.

With 2 readers and some separation you can tell.

You might be safe counting the number of times the tag was read if all exits/entries are covered. Then an even # of times might be leaving and odd are entries.

From my time in school there are few places where anything you try will get tested so hard for everything including vandalism and theft.

Hi,
Ops circuit;


Tom... :slight_smile:

lastchancename:
I agree, it’s stuck somewhere. Perhaps you can note where it’s stuck. then tell us.

I think it stuck at the input phase of the program as shown in the attachment.
From my understanding, this should be the input and output part.

  Serial.println();
  Serial.print("Message : ");
  content.toUpperCase();
  if (content.substring(1) == "95 50 42 83") //change here the UID of the card/cards that you want to give access
    {
    Serial.println("Authorized access");
    Serial.println();
    delay(500);
    digitalWrite(RELAY, LOW);
    delay(ACCESS_DELAY);
    digitalWrite(RELAY, HIGH);
   }
   else   
   {
    Serial.println(" Access denied");
    delay(DENIED_DELAY);
  }
}

GoForSmoke:
With 2 readers and some separation you can tell.

You might be safe counting the number of times the tag was read if all exits/entries are covered. Then an even # of times might be leaving and odd are entries.

From my time in school there are few places where anything you try will get tested so hard for everything including vandalism and theft.

I have thought about making something like that… but due to time constraint, it became hard to make out the prototype…

Using Strings is not desirable in small memory chips like AVR - your program will probably run out of memory sooner or later.
delay() is also undesirable as it creates blocking code.

These are unlikely to be your immediate problem, but learn how to work around them.

You can find exactly where your code stalls, by putting Serial.print() statements around the code you think is lost. Then at runtime, you’ll see when it reaches each print() on the serial terminal.
Then you can explore that function to see why it’s not completing & returning.

GoForSmoke:
Will your program know if someone walks in and walks right back out? Or will they be counted as attending?

That is in the real world never seen as a serious issue. Swipe the card, person is present. Same for company staff: they have to time stamp their card when they arrive, and when they leave. But it doesn't see whether they're present in the meantime. Or in schools; attendance is taken by the teacher, see which students are physically present. But it doesn't tell whether they're mentally present, too.

lastchancename:
Using Strings is not desirable in small memory chips like AVR - your program will probably run out of memory sooner or later.
delay() is also undesirable as it creates blocking code.

These are unlikely to be your immediate problem, but learn how to work around them.

You can find exactly where your code stalls, by putting Serial.print() statements around the code you think is lost. Then at runtime, you’ll see when it reaches each print() on the serial terminal.
Then you can explore that function to see why it’s not completing & returning.

Noted. Will try revising some of the codes... Will try your method on finding the problem. Thanks!