0% found this document useful (0 votes)
6 views8 pages

TranTienDat 22110308 Lab4

This document provides a guide for creating a digital lock using NodeMCU or Wemos D1 with ESP8266 wifi and the Blynk smartphone application. It includes setup instructions for the Blynk app, circuit connections, and code implementation for controlling the lock and managing password inputs. Additionally, it outlines how to test the application once set up is complete.

Uploaded by

trantiendat1679
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views8 pages

TranTienDat 22110308 Lab4

This document provides a guide for creating a digital lock using NodeMCU or Wemos D1 with ESP8266 wifi and the Blynk smartphone application. It includes setup instructions for the Blynk app, circuit connections, and code implementation for controlling the lock and managing password inputs. Additionally, it outlines how to test the application once set up is complete.

Uploaded by

trantiendat1679
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

Guide for Digital lock with NodeMCU and Blynk

1. Requirements:
 Use NodeMCU or Wemos D1 with ESP8266 wifi
 Create an digital lock
 Send information about lock status to smart phone application (Blynk)
 Your app can open or close the lock

2. Setup Blynk on smartphone


How does our application work?
Go to Application store and Search for “Blynk”, install it! You got an app as below

Setting Blynk with new Project:

Notice:
After click Create project, the Authentication Key will send to your register email.
Add widgets: LCD and Lock/Unlock button

Click the “+” button on Blynk screen

3. Setup NodeMCU circuit


Circuit:
Note: you can do the same with Wemos D1 board and the 4x4 keypad.

Pin connection:
D0 – D6 connect to pin 7 to 1 of keypad (pin 7 on 1st of the left hand side, pin 1 on the right hand side )
D7 connect to LED
D8 connect to Servo

4. Code (You can see the code file on CMS)


#define BLYNK_PRINT Serial
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <Keypad.h>
#include <Servo.h>
#include <string.h>

// Create an instance lcd from WidgetLCD object


WidgetLCD lcd(V1);
// You should get Auth Token in the Blynk App.
char auth[] = "INPUT YOUR AUTHENTICATION KEY HERE";
// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "YOUR WIFI SSID HERE";
char pass[] = "YOUR WIFI PASSWORD";
// Define some constants and variables
const byte rows = 4;
const byte columns = 3;
int holdDelay = 700;
int n =3;
int state = 0;
char key = 0;
int pos = 0;
//Default password is 0000
String default_passwd = "0000";
//variable to store the user input for password
String input_passwd = "";
//Define keys for lock and unlock or change password function

char lock_key = '*';


char unlock_key = '#';
char change_pass_key = '-'; //press * key for more than 3 second
// Create an instance for servo motor
Servo servo_d8;

//Define characters matrix


char keys[rows][columns] =
{
{'1', '2', '3'},
{'4', '5', '6'},
{'7', '8', '9'},
{'*', '0', '#'},
};

//Define pins for every row of keypad


byte rowPins[rows] = {D0, D1, D2, D3};
//Define pins for every column of keypad
byte columnPins[columns] = {D4, D5, D6};
// Create an instance for our keypad
Keypad keypad = Keypad(makeKeymap(keys), rowPins, columnPins, rows, columns);

void setup()
{
// Debug console
Serial.begin(9600);

//Inititate Blynk
Blynk.begin(auth, ssid, pass);

//Setup LED
pinMode(D7, OUTPUT);
// connect signal pin of servo to pin number D7 on NodeMCU
servo_d8.attach(D8);
servo_d8.write(pos);
//Clear the LCD Widget and send status WELCOME!
lcd.clear();
lcd.print(0, 0, "WELCOME!");
delay(1000);
}
//Setup for Blynk to write data from App to Hardware
BLYNK_WRITE(V0)
{
int pinValue = param.asInt();

if(pinValue == 1)
{
digitalWrite(D7, HIGH);
servo_d8.write(90);
lcd.clear();
lcd.print(0, 0, "WARNING!");
lcd.print(0, 1, "UNLOCKED!");
}
else{
digitalWrite(D7, LOW);
servo_d8.write(0);
lcd.clear();
lcd.print(0, 0, "WELCOME!");
lcd.print(0, 1, "LOCKED!");
}
}

void loop()
{
Blynk.run();
Key_pressed();
}
// Define Unlock()
void Unlock()
{
digitalWrite(D7, HIGH);
lcd.clear();
lcd.print(0, 0, "WARNING!");
lcd.print(0, 1, "UNLOCKED!");
for (pos = 0; pos <= 180; pos += 1)
{
// tell servo to go to position in variable 'pos'
servo_d8.write(pos);
// wait 15 ms for servo to reach the position
delay(15); // Wait for 15 millisecond(s)
}
delay(3000); //open door 3s then close
//lock by servo_d8
for (pos = 180; pos >= 0; pos -= 1)
{
// tell servo to go to position in variable 'pos'
servo_d8.write(pos);
// wait 15 ms for servo to reach the position
delay(15); // Wait for 15 millisecond(s)
}
lcd.clear();
lcd.print(0, 0, "WELCOME!");
lcd.print(0, 1, "LOCKED!");
digitalWrite(D7, LOW);
}
// Keypad_press
void Key_pressed()
{
key = function_key(n);

if (key == unlock_key)
{
Serial.print("Input password: ");
input_passwd = input_password(4);
if (input_passwd == default_passwd)
{
//Unlock by servo_d8 and send alert to LCD
Unlock();
}
else
{
Serial.println("Wrong password!");
digitalWrite(D7, HIGH);
delay(500);
digitalWrite(D7, LOW);
delay(500);
digitalWrite(D7, HIGH);
delay(500);
digitalWrite(D7, LOW);
lcd.clear();
lcd.print(0, 0, "WARNING!");
lcd.print(0, 1, "WRONG PASSWORD!");
}
//Reset input_passwd
input_passwd = "";
key = 0;
}

if (key == change_pass_key)
{
default_passwd = change_password(4, default_passwd);
delay(2000);
key =0;
}
}
// Define function for key
char function_key(int n)
{
char temp = keypad.getKey();
if ((int)keypad.getState() == PRESSED)
{
if (temp != 0) {key = temp;}
}
if ((int)keypad.getState() == HOLD)
{
state++;
state = constrain(state, 1, n);
delay(holdDelay);
}
if ((int)keypad.getState() == RELEASED)
{
key += state;
state = 0;
}
delay(100);
Serial.println(key);
return key;
}
// Define function input_password
String input_password(int num_char)
{
String passwd = "";
//Serial.print("Input password: ");
do
{
char temp = keypad.getKey();
if (temp != 0) {Serial.print(temp); passwd += temp;}
delay(100);
}
while (passwd.length() < num_char);
Serial.println();
return passwd;
}
// Define function change_password
String change_password(int num_char, String current_passwd)
{
//Authenticate the old password:
Serial.print("Input old password: ");
String old_passwd = input_password(num_char);
if (old_passwd != current_passwd)
{
Serial.println("Password does not match! Nothing changes");
return current_passwd;
}
//New password
Serial.print("Input new password: ");
String new_passwd = input_password(num_char);
//Confirm passwd
Serial.print("Input new password again: ");
String confirm_passwd = input_password(num_char);

if (confirm_passwd == new_passwd)
{
Serial.println("Password has changed!!!");
return confirm_passwd;
}
else
{
Serial.println("Password does not match! Nothing changes");
return current_passwd;
}
}

5. Test (See demo on class)


Click on Play button on Blynk to start our apps

You might also like