Blynk Lock provides the convenience of monitoring, locking and unlocking a door from a distance. You do not need to walk to the door to open or close it.
Blynk Lock has the following features:
(1) It allows you to lock and unload a door remotely using a Blynk app.
(2) It shows door states (open or closed) on the Blynk app.
(3) It uses a LED to indicate if a door is open or closed.
(4) It sends a notification to the Blynk app whenever the door is open or closed.
(5) It shows a terminal with logs of door open and closed.
Blynk Lock has two parts. A hardware part which consists of the Blynk board, magnetic door switch set, door latch, servo, LED and a wooden bar which controls the door latch.
The second part is a Blynk app which shows the door states, open and close the door button and logs.
Follows are some videos recording while building the Blynk Lock:
(1) Blynk Lock - testing the door latch with servo video
(2) Blynk Lock - locking and unlocking door latch video
(3) Blynk Lock - showing door switch set, LED and door latch movements video
Connecting the componentsGet these parts: door latch, wooden bar to move the latch and box to house the latch and other parts.
Fix the door latch to the box and make a hole at big enough for the latch to move forward and backward.
Have the Blynk board, door switch set, LED, servo and alligator clips ready.
Connect all the above parts according to the diagram provided in the Schematics and circuit diagrams section.
The box with the latch, and door switch set and LED on the cover of the box.
The Blynk board and the App.
We will use the Arduino IDE to develop the app. Refer to the following resources to get to know how to prepare for the development environment:
(1) Blynk board Arduino Development Guide
(2) Blynk board Arduino Development 101
This project uses four widgets: Push (notification), Terminal, Button and Value.
Add the Push Widget
Push widget is under Notification section. After adding it, configure its setting as below. This diagram shows its setting on a Android device. It looks slightly different on an iOS device.
Add the Value Widget
Set the Value widget to V25. The widget will show the current state of the door switch sensor.
Add the Terminal Widget
Configure the Terminal widget to use pin V21. Set its setting the same as the following diagram.
Add the Button Widget
Set the Button widget to use pin V26. Configure it as a Switch. The widget is used to lock and unlock the door with a latch.
The Look of the App
Finally, the App should look like the following before running it.
Configure the Code
Cut and paste the source codes from the Code section below into your Arduino IDE. Change these lines of code.
char BlynkAuth[] = "blynk auth code"; // cloud
char WiFiNetwork[] = "wifi network";
char WiFiPassword[] = "wifi password";
Running the App
Once everything is ready, upload and run the project. Open the Serial Monitor by clicking the magnifying glass icon on the upper-right corner of your IDE. Some debug information will be displayed on the serial monitor.
Run your Blynk Lock app by clicking the arrow icon on the upper-right corner of Blynk.
Putting the door switches close to each other or pulling them apart. You will notice a few events should happen.
(1) When putting the switches close, the LED will be OFF, the door state will change to Close and the event will be logged on the Terminal. You may get a "door close" notification on Blynk.
(2) When pulling the switches apart, the LED will be ON, the door state will change to Open and the event will be logged on the Terminal. You may get a "door open" notification on Blynk.
From Blynk Lock app, you can remotely lock and unlock the door by pressing the Door Knob button. Checkout the video links above to find out how it works.
This configure the Blynk setting.
char BlynkAuth[] = "blynk auth code"; // cloud
char WiFiNetwork[] = "wifi network";
char WiFiPassword[] = "wifi password";
This setup the hardware pins.
#define LED_PIN 5
#define SERVO_PIN 12
#define DOOR_STATE_VIRTUAL V25
#define SERIAL_VIRTUAL V21
#define LOCK_BUTTON_PIN 0
#define BUTTON_VIRTUAL V26
This defines the terminal and a function that is called when device receives an update of Virtual Pin value of SERIAL_VIRTUAL which is V21.
WidgetTerminal terminal(SERIAL_VIRTUAL);
BLYNK_WRITE(SERIAL_VIRTUAL)
{
String incoming = param.asStr();
Serial.println(incoming);
// do other tasks
...
}
This reads the DOOR_SWITCH_PIN and update the door state and toggle the LED according to read value. It too prints logs to the terminal and send notifications if the state change interval is longer than the NOTIFICATION_LIMIT.
#define DOOR_SWITH_PIN 16
#define NOTIFICATION_LIMIT 60000
unsigned long lastDoorSwitchNotification = 0;
uint8_t lastSwitchState = 255;
BLYNK_READ(DOOR_STATE_VIRTUAL)
{
uint8_t switchState = digitalRead(DOOR_SWITCH_PIN); // Read the door switch pin
if (switchState) {
Blynk.virtualWrite(DOOR_STATE_VIRTUAL, "Close"); // Update virtual variable
digitalWrite(LED_PIN, 0);
}
else {
Blynk.virtualWrite(DOOR_STATE_VIRTUAL, "Open");
digitalWrite(LED_PIN, 255);
}
if (lastSwitchState != switchState) // If the state has changed
{
if (switchState) // If the switch is closed (door shut)
{
BB_DEBUG("Notified closed.");
if (lastDoorSwitchNotification && (lastDoorSwitchNotification + NOTIFICATION_LIMIT > millis()))
{
int timeLeft = (lastDoorSwitchNotification + NOTIFICATION_LIMIT - millis()) / 1000;
BB_DEBUG("Can't notify for " + String(timeLeft) + "s");
terminal.println("Door closed. Can't notify for " + String(timeLeft) + "s");
terminal.flush();
}
else
{
Blynk.notify("Door closed\r\nFrom: " + boardName + "\r\n[" + String(millis()) + "]");
terminal.println("Door closed!");
terminal.flush();
lastDoorSwitchNotification = millis();
}
}
else
{
BB_DEBUG("Notified opened.");
// Send the notification
if (lastDoorSwitchNotification && (lastDoorSwitchNotification + NOTIFICATION_LIMIT > millis()))
{
int timeLeft = (lastDoorSwitchNotification + NOTIFICATION_LIMIT - millis()) / 1000;
BB_DEBUG("Can't notify for " + String(timeLeft) + "s");
terminal.println("Door open. Can't notify for " + String(timeLeft) + "s");
terminal.flush();
}
else
{
Blynk.notify("Door open\r\nFrom: " + boardName + "\r\n[" + String(millis()) + "]");
terminal.println("Door opened!");
terminal.flush();
lastDoorSwitchNotification = millis();
}
}
lastSwitchState = switchState;
}
}
This code move the door latch between lock and unlock positions. Whenever the Blynk button (labeled Door Knob) is pressed, the virtual pin V26 will activate the toggle() function which will put the latch to "lock/unlock" position. A variable called pos is used to track the position.
Servo servo1;
int pos;
...
void toggle() {
if (pos == 0) {
pos = 15;
} else {
pos = 0;
}
servo1.write(pos);
delay(20);
}
BLYNK_WRITE(V26)
{
int pinValue = param.asInt(); // assigning incoming value from pin V26 to a variable
Serial.print("V26 Button value is: ");
Serial.println(pinValue);
toggle();
}
Comments