vincent wong
Published © GPL3+

Blynk Lock

A lock which allows remote locking and un-locking. An user does not need to walk to the door to open or lock a door.

IntermediateFull instructions provided1,964
Blynk Lock

Things used in this project

Hardware components

SparkFun Blynk Board - ESP8266
SparkFun Blynk Board - ESP8266
×1
Alligator Clips
Alligator Clips
×1
Jumper wires (generic)
Jumper wires (generic)
×1
Door Latch
×1
SparkFun Magnetic Door Switch Set
×1

Software apps and online services

Blynk
Blynk
Arduino IDE
Arduino IDE

Story

Read more

Schematics

Blynk Lock Diagram

Code

blynk_lock.ino

C/C++
#define BLYNK_PRINT Serial    // Comment this out to disable prints and save space
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <Servo.h>  // servo library

#ifdef DEBUG_ENABLED
#define BLYNK_PRINT Serial
#define BB_DEBUG(msg) {\
  Serial.print("[" + String(millis()) + "] "); \
  Serial.println(msg); }
#else
#define BB_DEBUG(msg)
#endif

////////////////////
// Blynk Settings //
////////////////////

char BlynkAuth[] = "blynk auth code";  // cloud
char WiFiNetwork[] = "wifi network";
char WiFiPassword[] = "wifi password";

///////////////////////
// Hardware Settings //
///////////////////////
#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


Servo servo1;
int pos;

void unlock() {
  if (pos == 0) {
    pos = 15;
    servo1.write(pos);
    delay(20);
  }
}

void lock() {
  if (pos == 15) {
    pos = 0;
    servo1.write(pos);
    delay(20);
  } 
}

void toggle() {
  if (pos == 0) {
    pos = 15;
  } else {
    pos = 0;
  }

  servo1.write(pos);
  delay(20);
}

/* 12 12 12 12 12 12 12 12 12 12 12 12 12
 12 Experiment 12: Terminal            12
 12 Widget(s):                         12
 12  - Terminal: V21, On, On           12
 12 12 12 12 12 12 12 12 12 12 12 12 12 */
String emailAddress = "";
String boardName = "Blynk Lock";

WidgetTerminal terminal(SERIAL_VIRTUAL);

BLYNK_WRITE(SERIAL_VIRTUAL)
{
  String incoming = param.asStr();
  Serial.println(incoming);
  
  if (incoming.charAt(0) == '!')
  {
    String emailAdd = incoming.substring(1, incoming.length());
    for (int i=0; i<emailAdd.length(); i++)
    {
      if (emailAdd.charAt(i) == ' ')
        emailAdd.remove(i, 1);
    }
    terminal.println("Your email is:" + emailAdd + ".");
    emailAddress = emailAdd;
    terminal.flush();
  }
  if (incoming.charAt(0) == '$')
  {
    String newName = incoming.substring(1, incoming.length());

    boardName = newName;
    terminal.println("Board name set to: " + boardName + ".");
    terminal.flush();
  }
}


/* 14 14 14 14 14 14 14 14 14 14 14 14 14
 14 Experiment 14: Push                14
 14 Widget(s):                         14
 14  - Push: Off, On (iPhone)          14
 14  - Value: DoorState, V25, 1sec     14
 14  - Button: PushEnable, V26, Switch 14
 14 14 14 14 14 14 14 14 14 14 14 14 14 */
#define DOOR_SWITCH_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
  // Pin 16 is pulled low internally. If the switch (and door) is open,
  // pin 16 is LOW. If the switch is closed (door too), pin 16 is HIGH.
  // LOW = open
  // HIGH = closed
  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;
  }  
}



BLYNK_WRITE(V26)
{
  int pinValue = param.asInt(); // assigning incoming value from pin V26 to a variable
  // You can also use:
  // String i = param.asStr();
  // double d = param.asDouble();
  Serial.print("V26 Button value is: ");
  Serial.println(pinValue);

  toggle();
}

void setup()
{
  // Initialize hardware
  Serial.begin(9600); // Serial
  pinMode(LED_PIN, OUTPUT); // LED output

  // Set up the pin 16 door switch input:
  pinMode(DOOR_SWITCH_PIN, INPUT_PULLDOWN_16);
  lastSwitchState = digitalRead(DOOR_SWITCH_PIN);

  // Initialize Blynk
  Blynk.begin(BlynkAuth, WiFiNetwork, WiFiPassword);
//  Blynk.begin(BlynkAuth, WiFiNetwork, WiFiPassword, IPAddress(192,168,1,4));

  servo1.attach(SERVO_PIN);
  pos = 15;
  servo1.write(pos);

  
}

void loop()
{
  // Execute Blynk.run() as often as possible during the loop
  Blynk.run(); 


}

Credits

vincent wong

vincent wong

56 projects • 159 followers

Comments

Add projectSign up / Login