Overview:
In this mini project, we will learn how to build an IoT based biometric door lock system using Wemos D1 Mini (ESP8266). The ESP8266 WiFi Module will collect the fingerprint data from the user and send it over the internet to update a google spreadsheet to record the door entry event by the user. The door lock will only open and update record when the user is recognized.
Hardware and Software components:
Hardware Used:
Software Used:
- Arduino IDE
Application Description:
- Wemos D1 Mini ESP8266
The WeMos D1 Mini is one of the best ways to develop and build ESP8266 projects. This very small board comes with a Micro-USB interface. It can be powered directly from the USB port, and it works very well with the Arduino IDE.
Specification:
- Input Voltage: 7-12V recommended, 6-15V Maximum, (or can be run directly using the integrated USB port.)
- Digital Input/Outputs: 9 digital input/output pins, all pins support interrupt/PWM/I2C/one-wire except D0
- Analog Inputs: 1 x 10-bit resolution input.
- TTL Serial Ports: 1 (Additional Serial ports can be implemented using SoftwareSerial)
- Maximum Output Current: 12mA per pin source current, 20mA sink current MAXIMUM
- Flash Memory: 4 Megabytes
- SRAM: 2KB
- EEPROM:1KB
- Size: 3cm x 2.5cm x 0.7cm
- Weight: 5 Grams
- USB Port: Micro USB using CH340G driver
Pinout:
- Optical fingerprint sensor (AS608-R3070)
The AS608-R307 fingerprint module is an integrated fingerprint processing module integrating optical path and fingerprint processing. It features a small size, low power consumption, a simple interface, high reliability, fast identification speed, good adaptability of dry and wet fingers, and fast fingerprint searching speed.
The module's communication interface is UART/serial.
Specifications:
- Product Name: AS608-R307 Fingerprint Module
- Resolution: 500dpi
- Supply current: <60mA
- Supply voltage: 3.3V
- Fingerprint image entry time: <1.0 seconds
- Peak current: <60mA
- Window area: 15.3x18.2MM
- Characteristic file: 256 bytes
- Template file: 512 bytes
- Storage capacity: 1000 pieces
- Communication Interface: USB/UART
Pinout:
- Red Wire to 3.3V
- Yellow wire is Serial TX
- White wire is Serial RX
- Black wire is ground
- 6V/12V Solenoid Electromagnetic lock standard/mini
Specifications:
- Product Name: 99-S12 smart cabinet electromagnetic lock
Product size: 3.3x2.7x1.7cm/1.3x1.06x0.67in
Product weight: 35g
Product category: 6V/12V universal
- 5V/10A 1 Channel Relay Module (with Optocoupler / without Optocoupler)
This is a 1 channel Relay module 5v, which can be used to control various appliances an equipment with a load current capacity of not more than 10A. It can be controlled directly from microcontrollers like Raspberry Pi, Arduino, AVR, PIC, DSP, ARM, or TTL logic.
To isolate the microcontroller from the relay, an optocoupler is included in the relay module. With this configuration, the microcontroller doesn’t have any physical connection with the relay, it just uses the LED light of the Optocoupler IC to activate the relay. This solution saves your microcontroller from high voltage spikes for both VAC/VDC.
Specification and Features:
- 1 channel relay board
- Operating Voltage 5V
- Max Current : 20mA
- Equipped with high-current relay, AC250V 10A ; DC30V 10A.
- Standard TTL Level logic controlled (Raspberry Pi, Arduino, AVR, PIC, DSP, ARM, or TTL logic).
- For control of module, need 5V power and logic operation at 5V.
Pinout:
- 12V battery / adapter
Specification:
- 12V / 3A (36W) Power adapter
- Ultra-small size, lightweight, streamline, fully sealed, and easy to carry
- Low-cost design, high reliability
- Built-in over-voltage, over current, and short circuit protection
Note: Instead of adapter, you can also use other sources like 12V Li-ion battery or lead acid battery, or AC-DC converter
Hardware Set-up:
Connect Optical fingerprint sensor (ASR08-R307) and Wemos D1 Mini
Take note: R307 fingerprint module requires 3.3V.
- ESP8266 3.3V – R307 VCC
- ESP8266 D3 – R307 White wire RX
- ESP8266 D2 – R307 Yellow wire TX
- ESP8266 GND – R307 GND
Next, connect the Wemos D1 Mini and relay module. Then connect the relay to solenoid and 12V supply.
- ESP8266 5V – Relay VCC
- ESP8266 D5 – Relay IN
- ESP8266 GND – Relay GND
Relay module with Solenoid and 12V supply.
- Relay NO – 12V battery (+)
- Relay Common – Solenoid pin 2 (+)
- Solenoid pin 1 (-) – 12V battery (-)
Google Spreadsheet
Software Set-up
- Google Spreadsheet Creation
- Login to your Gmail account and open Google Drive.
- Create a new folder, name it and open the folder.
- Inside the folder right click and click on Google Sheets.
- Google sheet will be created inside the folder, Spreadsheet will open in new tab and name the spreadsheet according to your wish.
- Specify the fields in spreadsheet tables like id, time, date and value.
- Go to Extensions, click on Apps Script (in new tab Apps Script will open).
- Name the Apps Script and write the below code in a script editor.
here's the sample code of app script:
function doGet(e) {
Logger.log( JSON.stringify(e) );
var result = 'Ok';
if (e.parameter == 'undefined') {
result = 'No Parameters';
}
else {
var sheet_id = '13wuYJhvpvmVq-bZAmrexRCWbgutRfB4lO-W6wg3Bm5I'; // Spreadsheet ID
var sheet = SpreadsheetApp.openById(sheet_id).getActiveSheet();
var newRow = sheet.getLastRow() + 1;
var rowData = [];
var Curr_Date = new Date();
rowData[0] = Curr_Date; // Date in column A
var Curr_Time = Utilities.formatDate(Curr_Date, "Asia/Manila", 'HH:mm:ss');
rowData[1] = Curr_Time; // Time in column B
for (var param in e.parameter) {
Logger.log('In for loop, param=' + param);
var value = stripQuotes(e.parameter[param]);
Logger.log(param + ':' + e.parameter[param]);
switch (param) {
case 'name':
rowData[2] = value; // Temperature in column C
result = 'temp Written on column C';
break;
}
}
Logger.log(JSON.stringify(rowData));
var newRange = sheet.getRange(newRow, 1, 1, rowData.length);
newRange.setValues([rowData]);
}
return ContentService.createTextOutput(result);
}
function stripQuotes( value ){
return value.replace(/^["']|['"]$/g,"");
}
- Just change your sheet_id or spreadsheet id at your Google spreadsheet link; the highlighted one is the sample for spreadsheet id.
- And you can change the time zone of your current time
- After that, save the code
- Then, go to deploy and click the new deployment
- Under the new deployment click the select type and enter the web app, then, click the deploy.
- Now select anyone and click on the Deploy button.
- Now select the Gmail account in which you have created the Google sheet.
- Now click on the Go to Untitled project (unsafe).
- Now click on the allow button to give access to the account
- Then copy the deployment id which will be used in the code.
-
Take note: The URL copied will be something like this: https://script.google.com/macros/s/
AKfycbylLixMqGG5V6rwkZF81upglHpMVDV4Kh1O6vYJWzQtWZouW2I8CCssI6VFmsmfzsA7_w
- The above link is in the form of: https://script.google.com/macros/s/<gscript id>/exec
- Here, gscript id is: AKfycbylLixMqGG5V6rwkZF81upglHpMVDV4Kh1O6vYJWzQtWZouW2I8CCssI6VFmsmfzsA7_w
- Now we will be using the above link to push the data to our Google Sheets:
https://script.google.com/macros/s/AKfycbylLixMqGG5V6rwkZF81upglHpMVDV4Kh1O6vYJWzQtWZouW2I8CCssI6VFmsmfzsA7_w/exec
- Paste the above link to the new window and hit enter:
- Then you should see message like this. If this appears, then your cells are updated:
- Updating the cell
- Now the Google Sheets part is done.
Arduino Code:
Note: import the libraries needed first
#include "adafruit_fingerprint.h"
#include "softwareserial.h"
#include "esp8266wifi.h"
#include "esp8266httpclient.h"
const char* ssid = "cprcrn"; //your wifi network
const char* password = "@Dpnddlnd26lj"; //your wifi pasword
const char* host = "script.google.com";
const int httpsPort = 443;
bool state = true;
WiFiClientSecure client;
String GAS_ID = "AKfycbyznGzSrZLNjzsRujHWkI833pNeIpnVECw1ks_QW7Jub8F6wfNvxaPnIuGTD3j88A9n"; //spreadsheet id
SoftwareSerial mySerial(D2, D3);
Adafruit_Fingerprint finger = Adafruit_Fingerprint(&mySerial);
const int relay = D5; // relay pin attached to D5 of esp8266 wemos
int passDelay = 5000; // 5 seconds delay to pass the door
String name = " ";
void setup() {
pinMode(relay, OUTPUT);
digitalWrite(relay, HIGH); // solenoid lock closed by default
Serial.begin(115200);
Serial.println("Automatic door lock system");
finger.begin(57600);
Serial.print("connecting to ");
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500); Serial.print("."); // print.... till not connected }
Serial.println("");
Serial.println("WiFi connected");
Serial.println(ssid);
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
if (finger.verifyPassword()) {
Serial.println("Fingerprint sensor ready for operation!"); Serial.println();
} else {
Serial.println("Did not find fingerprint sensor. Check your wiring...");
Serial.println();
while (1) { delay(1);}
}
Serial.println("Provide your finger to activate door lock....");
}
}
void loop() {
if(finger.fingerID) {
Serial.print("!!");
Serial.println(finger.fingerID);
name = "Jade";
if(finger.confidence >= 60) {
Serial.print("Someone who enter: ");
Serial.println(name);
delay(passDelay);
}
finger.fingerID = 0;
}
if (getFingerPrint() != -1) {
Serial.println("OPEN");
digitalWrite(relay, LOW);
delay(passDelay);
digitalWrite(relay, HIGH);
Serial.println("CLOSED");
delay(50);
Serial.println("\n\nProvide your finger to activate door lock....");
}
client.setInsecure();
Serial.print("connecting to ");
Serial.println(host);
if (!client.connect(host, httpsPort)) {
Serial.println("connection failed");
return;
}
String url = "https://script.google.com/macros/s/AKfycbyznGzSrZLNjzsRujHWkI833pNeIpnVECw1ks_QW7Jub8F6wfNvxaPnIuGTD3j88A9n/exec?name=Jade";
Serial.print("requesting URL: ");
Serial.println(url);
client.print(String("GET ") + url + " HTTP/1.1\r\n" +
"Host: " + host + "\r\n" +
"User-Agent: BuildFailureDetectorESP8266\r\n" +
"Connection: close\r\n\r\n");
Serial.println("request sent");
while (client.connected()) {
String line = client.readStringUntil('\n');
if (line == "\r") {
Serial.println("headers received"); break;
}
}
String line = client.readStringUntil('\n');
if (line.startsWith("{\"state\":\"success\"")) {
Serial.println("esp8266/Arduino CI successfull!");
} else {
Serial.println("esp8266/Arduino CI has failed");
}
Serial.print("reply was : ");
Serial.println(line); Serial.println("closing connection");
Serial.println("==========");
Serial.println();
}
// returns -1 if failed, otherwise returns ID #
int getFingerPrint() {
int p = finger.getImage();
if (p != FINGERPRINT_OK) return -1;
p = finger.image2Tz();
if (p != FINGERPRINT_OK) return -1;
p = finger.fingerFastSearch();
if (p != FINGERPRINT_OK) return -1;
// found a match!
Serial.print("Found ID #");
Serial.println(finger.fingerID);
return finger.fingerID;
}
Code breakdown:
#include "ESP8266WiFi.h"
#include "ESP8266HTTPClient.h"
We also need to import the libraries needed for the fingerprint sensor:
-
#include "Adafruit_Fingerprint.h"
#include "SoftwareSerial.h"
The following line sets software serial on pins D2 and D3. Pin D2 as RX (yellow wire), and Pin D3 as TX (white wire).
-
SoftwareSerial mySerial (D2, D3);
Then, we create a an Adafruit_Fingerprint object called finger on the serial pins we’ve set previously.
Adafruit_Fingerprint finger = Adafruit_Fingerprint(&mySerial);
After that enter the Wi-Fi credential and password.
-
const char* ssid ="cprcrn" //your wifi network
- const char* password ="@Dpnddlnd26lj" // your wifi password
Initialize the pins of the Relay:
-
const int relay = D5;
Set a 5 seconds delay to open and closed door lock:
-
int passDelay = 5000;
In this statement: If connection is successful, it shows the IP address in serial monitor
-
Serial.println("");
Serial.println("WiFi connected");
Serial.println(ssid);
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
loop()
In the loop(), the code checks if the fingerprint is in the record. If yes, the door lock is opened, and closed after 5 seconds. You can adjust the delay "passDelay".
To connect to Google Sheets, initialize the Google app script or google script id, then paste the deployment id as value for "String GAS_ID":
To send the data from Wemos D1 Mini to Google sheet:
if(finger.fingerID) {
Serial.print("!!");
Serial.println(finger.fingerID);
name = "Jade";
if(finger.confidence >= 60) {
Serial.print("Someone who enter: ");
Serial.println(name);
delay(passDelay);
}
finger.fingerID = 0;
}
if (getFingerPrint() != -1) {
Serial.println("OPEN");
digitalWrite(relay, LOW);
delay(passDelay);
digitalWrite(relay, HIGH);
Serial.println("CLOSED");
delay(50);
Serial.println("\n\nProvide your finger to activate door lock....");
}
client.setInsecure();
Serial.print("connecting to ");
Serial.println(host);
if (!client.connect(host, httpsPort)) {
Serial.println("connection failed");
return;
}
String url =
"https://script.google.com/macros/s/AKfycbyznGzSrZLNjzsRujHWkI833pNeIpnVECw1ks_QW7Jub8F6wfNvxaPnIuGTD3j88A9n/exec?name=Jade";
Serial.print("requesting URL: ");
Serial.println(url);
client.print(String("GET ") + url + " HTTP/1.1\r\n" +
"Host: " + host + "\r\n" +
"User-Agent: BuildFailureDetectorESP8266\r\n" +
"Connection: close\r\n\r\n");
Serial.println("request sent");
while (client.connected()) {
String line = client.readStringUntil('\n');
if (line == "\r") {
Serial.println("headers received");
break;
}
}
String line = client.readStringUntil('\n');
if (line.startsWith("{\"state\":\"success\"")) {
Serial.println("esp8266/Arduino CI successfull!");
} else {
Serial.println("esp8266/Arduino CI has failed");
}
Serial.print("reply was : ");
Serial.println(line);
Serial.println("closing connection");
Serial.println("==========");
Serial.println();
}
Video demonstration:
Conclusion:
We have used the Wemos D1 Mini, a small ESP8266 Wifi module, as the main controller to detect fingerprints and open a door lock and send data to the cloud by updating Google sheets entry record. This mini project will be helpful in providing enhanced security and restrictions. The concept is simple, this project can be further improved by using actual door locks or gate motorized systems.
References:
- https://cdn-learn.adafruit.com/downloads/pdf/adafruit-optical-fingerprint-sensor.pdf
- https://microcontrollerslab.com/5v-single-channel-relay-module-pinout-working-interfacing-applications-datasheet/
- https://www.electroniclinic.com/send-data-from-google-spreadsheet-or-google-sheets-to-esp8266-or-esp32/
- https://createlabz.store/products/35-029?_pos=23&_sid=e350241b8&_ss=r
- https://createlabz.store/products/30-004?_pos=1&_sid=fae703467&_ss=r
- https://createlabz.store/products/20-017?_pos=1&_sid=873b4bf61&_ss=r