Cultivate Curiosity, Inspire Imagination.

Arduino Tutorial: ESP32 Blynk RFID App Lock

Introduction

This tutorial explains how to construct an ESP32-based Smart Lock System. Using an ESP32 module, an RFID reader and a Solenoid lock, this system allows you to lock and unlock a Smart Lock with an RFID smart card or via an app on the cloud.

 

 

 

Hardware Required

You may purchase the individual components here:

  1. WiFi Bluetooth Module ESP32-WROOM-32E
  2. Relay Module 1 Channel 5V
  3. RFID / NFC Module PN532 13.56MHz
  4. Power Adapter 12V 2.5A Safety Mark
  5. Jumper Wire Male-Male 40x
  6. Solenoid Door Lock

 

Connecting the Hardware

Connect the RFID/NFC Module to the corresponding pins on the ESP32. There are 6 pins on the RFID/NFC Module that you will need to connect to the corresponding pins on the ESP32, namely, the SCK, MISO, MOSI, SS, VCC and GND. Connect these pins to the corresponding pins for 18, 19, 23, 5, 3V3 and GND on the ESP32.

 

Flip the switch on the RFID/NFC module to SPI mode. This allows the RFID/NFC module and the ESP32 module to send and receive data between each other.

The ESP32 operates at 3.3 volts and cannot control the Solenoid lock directly because the Solenoid Lock is a 12-volt device. Thus, we set up a one-channel relay to allow the ESP32 to control the Solenoid lock. Connect the Relay module to the ESP32 as seen in the picture below.

  1. The In pin on the Relay connects to pin 2 of the ESP32. We will be sending a signal from the ESP32 to this pin to activate the relay.
  2. The GND pin on the Relay connects to the GND pin of the ESP32.
  3. The VCC pin on the Relay connects to the 5V pin of the ESP32 to supply power to it.

We will connect the Solenoid Lock to the Relay module and program the ESP32 to send a signal to it to unlock it. Connect the Solenoid lock to the Relay module and the ESP32 as seen in the picture below.

  1. The NO pin on the Relay connects to the Solenoid lock to supply power to unlock it.
  2. The COM pin on the Relay connects to the 12V pin on the ESP32. To unlock the Solenoid lock, we will be powering the ESP32 using an external power source rather than via USB cable to a computer.
  3. The grounding wire on the Solenoid lock connects to the GND pin on the ESP32.

Here's how the project should look like after it is assembled.

 

Install the ESP32 Extension for Arduino

Install the ESP32 extension for Arduino. Tutorial on how to install can be found here

 

Discovering The Unique Identifier of the RFID Card

To use the RFID/NFC module, we will need to install its libraries. The RFID/NFC module uses the NFC Data Exchange Format (NDEF) format, a standard that operates across all NFC devices. To install its libraries on Arduino, visit https://github.com/Seeed-Studio/PN532. Follow the instructions under “Getting Started” to install the library.

Open the example from PN532 > iso14443a_uid. We’re going to find out the UID of our RFID card.

Select the Arduino board from Tools > Board > ESP32 Dev Module. Then select the port from Tools > Port > xxx. Upload the program and open the serial monitor. You should see this if your connection is correct:

Tap the RFID card on the reader. The reader will read the card and display its UID.

Copy this UID as we will be using it in the next step.

 

Setting up Blynk Cloud

Blynk.io is a cloud-based IoT solution that allows us to control our IoT devices remotely. Register for an account at Blynk.cloud. Log in and create a new template.

Give your new template a title and select the ESP32 board with WiFi connection. Click done.

Click the “Web Dashboard” tab and drag a switch and a LED into the dashboard.

Create a Virtual Pin Datastream V0 with data type integer

We will do the same for the LED, this time using V1.

Rename the title of the LED to "Unlock".

Your dashboard is now ready! Click save to confirm.

Next, we will create our device. Click on the search icon on the left and click “New Device”.

Done! Your dashboard and device is ready! Take note of the Template ID, Device Name and AuthToken as we will be using it in our program.

To be able to use the Blynk library to program the ESP32 App Lock, follow the instructions to install the Blynk library for the Arduino IDE here.

 

Programming The ESP32

Open the codes from our previous RFID Smart Lock tutorial. We will be making some modifications for us to connect to Blynk cloud and control it remotely from the Blynk Dashboard.

The source code for this sketch can be downloaded at Kuriosity's Github Repository at the end of this tutorial.

Paste the Blynk credentials we got earlier when setting up our dashboard.

#define BLYNK_TEMPLATE_ID "<PASTE TEMPLATE ID HERE>"
#define BLYNK_DEVICE_NAME "Smart RFID Lock"
#define BLYNK_AUTH_TOKEN "<PASTE AUTH TOKEN HERE>"

Include the required libraries for WiFi, Blynk and PN532.

#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>
#include <SPI.h>
#include <PN532_SPI.h>
#include "PN532.h"Serial.print("UID Value: ");

Key in your WiFi SSID and Password so the ESP32 can go online.

// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "<ENTER YOUR WIFI SSID HERE>";
char pass[] = "<ENTER YOUR WIFI PASSWORD HERE>";
char auth[] = BLYNK_AUTH_TOKEN;}

Create an object for SPI PN532, using pin 5 as the chip select.

PN532_SPI pn532spi(SPI, 5);
PN532 nfc(pn532spi);

In the setup function, add a command to connect to the Blynk server.

Blynk.begin(auth, ssid, pass);
// You can also specify server:
//Blynk.begin(auth, ssid, pass, "blynk.cloud", 80);
//Blynk.begin(auth, ssid, pass, IPAddress(192,168,1,100), 8080);

In the main loop, we will update Blynk server when an authorized tag is present. V0 updates the switch and V1 updates the LED. Then call Blynk.run() and we’re done!

if (memcmp(uid, valid, 4) == 0) {
Blynk.virtualWrite(V0, 1);
Blynk.virtualWrite(V1, 1);
Serial.println("Welcome Kuriosity!");

// activate relay to unlock door
digitalWrite(relayPin, HIGH);
// hold door unlock for 3s
delay(3000);
// deactivate relay to lock door
digitalWrite(relayPin, LOW);
Blynk.virtualWrite(V0, 0);
Blynk.virtualWrite(V1, 0);
}
else {
Serial.println("UNAUTHORIZED!");
}
Serial.println();

// Wait 1 second before continuing
delay(1000);
}

Blynk.run();

Select the ESP32 Dev Module. Then select the port from Tools > Port > xxx. Once done, upload the program.

Go back to Blynk dashboard and click on the search icon. You should see your device online if your Blynk credentials are correct. Click on your device to see the dashboard.

You can now control your solenoid lock remotely and view status in real-time. Click the switch or tap your card to see the dashboard update!

Finally, install the RFID Smart Lock device on a cabinet, door, or gate that you wish to secure!

 

Resources