ESP8266IOT Projects

RFID and IoT, ESP8266 RFID based Remote Access Door Lock, RFID IoT

RFID and IoT based Remote Access Door Lock System Introduction:

 

RFID and IoT, ESP8266 RFID based Remote Access Door Lock- In this tutorial, you will learn how to make RFID and IoT based Remote Access Door Lock system using Nodemcu ESP8266 Wifi module, MFRC522 RFID Module, Electronic Lock, and Blynk application. With the help of this project, you can remotely monitor your door lock from anywhere in the world with your iPhone or Android device.

RFID and IoT

In the cell phone app I added two tabs, one is used for the monitoring, each time a user swipes an RFID card a message is received.

The other tab is used for remote access control.

RFID and IoT

When all the buttons are turned ON it means the admin has given access to all the three users. If all the buttons are turned off, then the users won’t be able to open the door lock. In order to open the door lock, permission should be granted by the admin.


Note: For the practical demonstration and step by step explanation, watch the video tutorial given at the end of this article.

In this tutorial, we will cover,

  1. MFRC522 RFID module technical specifications and Pinout
  2. Complete circuit diagram explanation and finally
  3. Nodemcu ESP8266 Wifi Module Programming.

Without any further delay, let’s get started!!!

Amazon Links:

MFRC522 RFID module with tags:

Nodemcu ESP8266 Wifi Module:

12v Electronic Door Lock / Elock / Solenoid Lock:

Other Tools and Components:

Super Starter kit for Beginners

Digital Oscilloscopes

Variable Supply

Digital Multimeter

Soldering iron kits

PCB small portable drill machines

*Please Note: These are affiliate links. I may make a commission if you buy the components through these links. I would appreciate your support in this way!



RFID and IoT:

Radiofrequency identification system (RFID) is an automatic technology and aids machines or computers to identify objects, record metadata or control individual target through radio waves. Connecting RFID reader to the terminal of Internet, the readers can identify, track and monitor the objects attached with tags globally, automatically, and in real-time if needed. This is the so-called Internet of Things (IoT). RFID is often seen as a prerequisite for the IoT. The RFID and IoT technologies when mixed together, so many amazing monitoring and control system projects can be designed. With the help of this project, I will explain how to use RFID and IoT “Internet of Things” technologies together to make a remote access door lock control system. I have been using RFID modules in different Arduino based projects.

List of the RFID and Arduino based projects:

·         Arduino RFID Servo Motor Control system for Car Parking “MFRC522”

·        RFID & GSM based student Attendance Alert message to parents

·        RFID based students attendance system with message Alert

·        Bike anti theft system using MFRC522 RFID module

·        Raspberry Pi Home Automation using RC522 RFID, Smart Home

In this project, for the first time, I am using RFID with the Nodemcu ESP8266 Wifi module, just to explain how RFID and IoT can be used to build something amazing that can be used in real life.


About the MFRC522 RFID Module:

RFID and IoT

As you can see this is the MFRC522 RFID module. The MFRC522 is a highly integrated reader/writer IC for contactless communication at 13.56 MHz. This module has a total of 8 male headers which are clearly labeled as SDA, SCK, MOSI, MISO, IRQ, GND, RST, and 3.3V. As per the datasheet, the typical voltage is 3.3V while the maximum voltage is 3.6 volts. This module can be easily powered up using the Nodemcu ESP8266 Wifi Module.

About the Electronic Lock “E Lock”:

RFID and IoT

The electronic lock working principle is just like the Relay. It has a coil that can be energized and de-energized. We need 12 volts to control this electronic lock. As you can see I have cut the Red wire; by connecting and disconnecting these two wires the Electronic Lock can be controlled. These two wires will be connected with the relay common and normally open legs.


RFID and IoT based Remote Access Door Lock Circuit Diagram:

RFID and IoT

As you can see the circuit diagram is very simple, let’s start with the MFRC522 RFID module, as you can see the 3.3v pin of the RFID module is connected with the Nodemcu ESP8266 Wifi module, the RST pin of the RFID module is connected with the digital pin 2, GND pin of the RFID module is connected with the ground pin of the Nodemcu module, the IRQ pin is not connected, the MISO pin is connected with the digital pin D6, MOSI pin is connected with the digital pin D7, SCK pin of the RFID module is connected with D5, and lastly the SDA pin of the MFRC522 RFID module is connected with the digital pin D4 of the Nodemcu ESP8266 Wifi Module.

The 12V Electronic door lock is controlled using a one channel relay module. As you can see the GND wire of the electronic door lock is connected with the ground of the power supply. While the 12V wire of the electronic lock is connected with the common of the relay module and the normally open leg of the relay module is connected with the 12v power supply. No by turning ON and turning OFF this relay module the electronic door lock can be opened and closed. The relay module is controlled using the D0 pin of the Nodemcu ESP8266 Wifi module.

Finally, this is the regulated 5V power supply based on the LM7805 voltage regulator. This 5V regulated power supply is used to power up the Nodemcu Module. J1 is the female power jack and this is where we connect the 12v power supply. Two 470uf capacitors are connected at the input and output sides of the voltage regulator. A 330-ohm resistor is connected in series with a 2.5V led, this is a current limiting resistor. A wire from the output of the voltage regulator is connected with the Vin pin of the Nodemcu Module and a wire from the power supply ground is connected with the ground pin of the Nodemcu module. So, that’s all about the circuit diagram, now let’s make the blynk application, and follow the same exact steps.

Note: this old version of the Blynk app is no more functional. For the blynk mobile App setup and Blynk.cloud dashboard setup ready my article on the New Blynk V2.0. In this article I have explained how to migrate your projects from Blynk 1.0 to the new Blynk V2.0. You can also watch the video.

Blynk Application Designing for Remote Access Door Lock:

I always first start with the Blynk application designing, this way I know which virtual pins I have to use in the programming. Moreover, this also helps me in checking my code, as I keep testing my project. So, first let’s start with the Blynk application designing.

  • First of all, open the blynk application.
  • Click on the new project and enter the project name as “IoT RFID”.
  • Click on the choose device and select Nodemcu.
  • Make sure the connection type is set to WIFI.
  • Finally, click on the create button, an authentication token will be sent on your email id, which later will be used in the Nodemcu programming.
  • Click anywhere on the screen and search for the tabs and add it.
  • Click on the tab and add two tabs, with names MONITORING AND REMOTE ACCESS.
  • While the MONITORING tab is selected, click on the screen and search for the terminal widget and it.
  • Now click on the terminal widget and select the virtual pin V2 and set the add newline to yes.
  • Now select the REMOTE ACCESS tab.
  • Click on the screen and add three buttons.
  • Click on the first button, set the name, and select the virtual pin V3, select the button mode as SWITCH. Select the font size as per your requirement.
  • Click on the second button, set the name, and select the virtual pin V4, select the button mode as SWITCH. Select the font size as per your requirement.
  • Click on the third button, set the name, and select the virtual pin V5, select the button mode as SWITCH. Select the font size as per your requirement.

In this project, I considered three users, ENG. FAHAD, ELECTRONIC CLINIC, and JAMSHAID KHAN.

RFID and IoT

Still, if you find it hard to follow, you can watch the video tutorial given at the end of this article.

MFRC522 RFID Interfacing with Nodemcu:

RFID and IoT

I connected the MFRC522 RFID Module and the 12V electronic door lock as per the circuit diagram already explained. Now let’s have a look at the Arduino programming.

Download Libraries

RFID and IoT based Remote Access Door Lock system Nodemcu ESP8266 Programming:

// RFID and IoT

#include <SPI.h>
#include <MFRC522.h>
#define BLYNK_PRINT Serial
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>

#define SS_PIN 4  // sda
#define RST_PIN 2

int elock = D0;    

MFRC522 mfrc522(RST_PIN, SS_PIN);        // Create MFRC522 instance.
char auth[] ="iyzplTOdDMzqRJN7wo_LVlobNBbzjO21";

char ssid[] = "ZONG MBB-E8231-6E63";
char pass[] = "08659650";

SimpleTimer timer;
int fflag = 0; 
int eflag = 0; 
int jflag = 0; 
WidgetTerminal terminal(V2);

void setup() {
        Serial.begin(9600);        // Initialize serial communications with the PC
        Blynk.begin(auth, ssid, pass);
      pinMode(elock,OUTPUT);
      digitalWrite(elock, LOW);
        SPI.begin();                // Init SPI bus
        mfrc522.PCD_Init();        // Init MFRC522 card
        //Serial.println("Scan a MIFARE Classic PICC to demonstrate Value Blocks.");
          timer.setInterval(1000L, iot_rfid);
}

void loop() {
        
  timer.run(); // Initiates SimpleTimer
  Blynk.run();

       
}

void iot_rfid()
{

        // Prepare key - all keys are set to FFFFFFFFFFFFh at chip delivery from the factory.
        MFRC522::MIFARE_Key key;
        for (byte i = 0; i < 6; i++) {
                key.keyByte[i] = 0xFF;
        }
        // Look for new cards
        if ( ! mfrc522.PICC_IsNewCardPresent()) {
                return;
        }

        // Select one of the cards
        if ( ! mfrc522.PICC_ReadCardSerial()) {
                return;
        }
        // Now a card is selected. The UID and SAK is in mfrc522.uid.
        
        // Dump UID
        Serial.print("Card UID:");
        for (byte i = 0; i < mfrc522.uid.size; i++) {
               Serial.print(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " ");
               Serial.print(mfrc522.uid.uidByte[i], DEC);
        } 
        Serial.println();

        // Dump PICC type
        byte piccType = mfrc522.PICC_GetType(mfrc522.uid.sak);
    //    Serial.print("PICC type: ");
//Serial.println(mfrc522.PICC_GetTypeName(piccType));
        if (        piccType != MFRC522::PICC_TYPE_MIFARE_MINI 
                &&        piccType != MFRC522::PICC_TYPE_MIFARE_1K
                &&        piccType != MFRC522::PICC_TYPE_MIFARE_4K) {
                //Serial.println("This sample only works with MIFARE Classic cards.");
                return;
        }
        // defining Cards here 
        
        if( ((mfrc522.uid.uidByte[0] == 66) && (mfrc522.uid.uidByte[1] == 71) && (mfrc522.uid.uidByte[2] == 176) && (mfrc522.uid.uidByte[3] == 30)) && (fflag == 1) )
        {
         Serial.println("Eng. Fahad");
         Blynk.virtualWrite(V2, "Eng. Fahad" );
         digitalWrite(elock, HIGH); 
         delay(5000); 
         digitalWrite(elock, LOW); 
        }
  
        else
        if(( (mfrc522.uid.uidByte[0] == 00) && (mfrc522.uid.uidByte[1] == 47) && (mfrc522.uid.uidByte[2] == 115) && (mfrc522.uid.uidByte[3] == 137))&& (eflag == 1) )
        {
         Serial.println("Electronic Clinic"); 
         Blynk.virtualWrite(V2, "Electronic Clinic" );
         digitalWrite(elock, HIGH); 
         delay(5000); 
         digitalWrite(elock, LOW); 
        }
        else
        if( ((mfrc522.uid.uidByte[0] == 198) && (mfrc522.uid.uidByte[1] == 69) && (mfrc522.uid.uidByte[2] == 34) && (mfrc522.uid.uidByte[3] == 75))&& (jflag == 1) )
        {
         Serial.println("Jamshaid Khan");
         Blynk.virtualWrite(V2, "Jamshaid Khan" );
         digitalWrite(elock, HIGH); 
         delay(5000); 
         digitalWrite(elock, LOW); 
        }
 
        else 
        Serial.println("unregistered user");
  
}

// in Blynk app writes values to the Virtual Pin 3
BLYNK_WRITE(V3)
{
   fflag = param.asInt(); // assigning incoming value from pin V3 to a variable
  // Blynk.virtualWrite(V2, fflag );

}


// in Blynk app writes values to the Virtual Pin 4
BLYNK_WRITE(V4)
{
   eflag = param.asInt(); // assigning incoming value from pin V4 to a variable

}

BLYNK_WRITE(V5)
{
   jflag = param.asInt(); // assigning incoming value from pin V5 to a variable

}



RFID and IoT based Remote Access Door Lock system Nodemcu ESP8266 Program Explanation:

Before you start the programming, first of all, make sure that you download all the necessary libraries.

Download Libraries:

So, first of all, I started by including the libraries needed for the RFID and IoT based Remote Access Door Lock system.

#include <SPI.h>

#include <MFRC522.h>

#define BLYNK_PRINT Serial

#include <ESP8266WiFi.h>

#include <BlynkSimpleEsp8266.h>

Next, I defined pins for the SS, RST, and Electronic Lock.

#define SS_PIN 4  // sda

#define RST_PIN 2

int elock = D0;

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

This is the authentication code that was sent via email; I simply copied and paste it over here.

char auth[] =”iyzplTOdDMzqRJN7wo_LVlobNBbzjO21″;

This is the router name.

char ssid[] = “ZONG MBB-E8231-6E63”;

This is the password.

char pass[] = “08659650”;

Next, I defined a timer.

SimpleTimer timer;

fflag, eflag, and jflag are variables of the type integer, which will be used as the flags. All the flags are assigned zeros, which means that by default the users have no access. So, the flags value can be zero or one. 0 means no access while 1 means that the use can control the electronic lock using the RFID tag.

int fflag = 0;

int eflag = 0;

int jflag = 0;

The terminal widget is assigned the virtual pin V2.

WidgetTerminal terminal(V2);


Inside the void setup() function. I activated the serial communication for debugging purposes. While 9600 is the baud rate. The electronic lock is set as the output and is turned OFF using the digitalWrite() function. iot_rfid is a user-defined function that is executed after every 1 second. The RFID code is placed inside this function.

void setup() {

        Serial.begin(9600);

        Blynk.begin(auth, ssid, pass);

        pinMode(elock,OUTPUT);

        digitalWrite(elock, LOW);

        SPI.begin();                // Init SPI bus

        mfrc522.PCD_Init();        // Init MFRC522 card

        //Serial.println(“Scan a MIFARE Classic PICC to demonstrate Value Blocks.”);

        timer.setInterval(1000L, iot_rfid);

}

Then starts the void loop() function. As you can see inside the void loop() function we have only two functions the timer.run() and Blynk.run().

void loop() {

  timer.run(); // Initiates SimpleTimer

  Blynk.run();

}

The iot_rfid() function is a user-defined function, which has no return type and does not take any argument as the input. 99% of this code is exactly the same as used in my previous projects which I have already explained in very detail, links to the related RFID based projects are given in the description. In this code, I did a very little modification……… I added these flags….

void iot_rfid()

{

        // Prepare key – all keys are set to FFFFFFFFFFFFh at chip delivery from the factory.

        MFRC522::MIFARE_Key key;

        for (byte i = 0; i < 6; i++) {

                key.keyByte[i] = 0xFF;

        }

        // Look for new cards

        if ( ! mfrc522.PICC_IsNewCardPresent()) {

                return;

        }

        // Select one of the cards

        if ( ! mfrc522.PICC_ReadCardSerial()) {

                return;

        }

        // Now a card is selected. The UID and SAK is in mfrc522.uid.

        // Dump UID

        Serial.print(“Card UID:”);

        for (byte i = 0; i < mfrc522.uid.size; i++) {

               Serial.print(mfrc522.uid.uidByte[i] < 0x10 ? ” 0″ : ” “);

               Serial.print(mfrc522.uid.uidByte[i], DEC);

        }

        Serial.println();

        // Dump PICC type

        byte piccType = mfrc522.PICC_GetType(mfrc522.uid.sak);

    //    Serial.print(“PICC type: “);

//Serial.println(mfrc522.PICC_GetTypeName(piccType));

        if (        piccType != MFRC522::PICC_TYPE_MIFARE_MINI

                &&        piccType != MFRC522::PICC_TYPE_MIFARE_1K

                &&        piccType != MFRC522::PICC_TYPE_MIFARE_4K) {

                //Serial.println(“This sample only works with MIFARE Classic cards.”);

                return;

        }

        // defining Cards here

So, this condition means, if the RFID card identification number is 66 71 176 30 and the fflag value is 1 then send a message to the serial monitor and terminal widget on the blynk app and Open the door for 5 seconds. When the fflag value is 0 the door will not be opened. So the fflag value can be 0 or 1 which is controlled using the blynk app. Similarly, for the remaining two conditions.

  if( ((mfrc522.uid.uidByte[0] == 66) && (mfrc522.uid.uidByte[1] == 71) && (mfrc522.uid.uidByte[2] == 176) && (mfrc522.uid.uidByte[3] == 30)) && (fflag == 1) )

        {

         Serial.println(“Eng. Fahad”);

         Blynk.virtualWrite(V2, “Eng. Fahad” );

         digitalWrite(elock, HIGH);

         delay(5000);

         digitalWrite(elock, LOW);

        }

        else

        if(( (mfrc522.uid.uidByte[0] == 00) && (mfrc522.uid.uidByte[1] == 47) && (mfrc522.uid.uidByte[2] == 115) && (mfrc522.uid.uidByte[3] == 137))&& (eflag == 1) )

        {

         Serial.println(“Electronic Clinic”);

         Blynk.virtualWrite(V2, “Electronic Clinic” );

         digitalWrite(elock, HIGH);

         delay(5000);

         digitalWrite(elock, LOW);

        }

        else

        if( ((mfrc522.uid.uidByte[0] == 198) && (mfrc522.uid.uidByte[1] == 69) && (mfrc522.uid.uidByte[2] == 34) && (mfrc522.uid.uidByte[3] == 75))&& (jflag == 1) )

        {

         Serial.println(“Jamshaid Khan”);

         Blynk.virtualWrite(V2, “Jamshaid Khan” );

         digitalWrite(elock, HIGH);

         delay(5000);

         digitalWrite(elock, LOW);

        }

        else

        Serial.println(“unregistered user”);

}

The following three functions are used to send values 0 and 1 using the buttons through virtual pins V3, V4, and V5. These values are then stored in fflag, eflag, and jflag; depending on the value stored in these flags, the access is given to the users. 0 means no access while 1 means giving access to the person.

// in Blynk app writes values to the Virtual Pin 3

BLYNK_WRITE(V3)

{

   fflag = param.asInt(); // assigning incoming value from pin V3 to a variable

  // Blynk.virtualWrite(V2, fflag );

}

// in Blynk app writes values to the Virtual Pin 4

BLYNK_WRITE(V4)

{

   eflag = param.asInt(); // assigning incoming value from pin V4 to a variable

}

BLYNK_WRITE(V5)

{

   jflag = param.asInt(); // assigning incoming value from pin V5 to a variable

}

In the end, I successfully tested my project “RFID and IoT based Remote Access Door Lock system”.

RFID and IoT

Watch Video Tutorial:

 

Engr Fahad

My name is Shahzada Fahad and I am an Electrical Engineer. I have been doing Job in UAE as a site engineer in an Electrical Construction Company. Currently, I am running my own YouTube channel "Electronic Clinic", and managing this Website. My Hobbies are * Watching Movies * Music * Martial Arts * Photography * Travelling * Make Sketches and so on...

One Comment

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button