Viktor S
Published © CC BY-NC-SA

Network Door Switch Using ESP8266

Connect your front door to the internet and unlock it with your smartphone.

BeginnerFull instructions provided6 hours8,713

Story

Read more

Custom parts and enclosures

bottom_v2.stl

top_v2.stl

bottom_v2.step

top_v2.step

full_unit.step

Schematics

IoT Door Lock - Schematics

Code

IoT Door Lock - Code

Arduino
/* 
 *  IoT Door Lock using ESP8266
 *  2/5/2018
 *  by Viktor Silivanov
 *  
 *  Controls a servo to press intercom button when specific URL is
 *  accessed on a local network.
 *  
 *  Modify Network Credential variable with your own.
 */

//********************************************************************************************************************************************************************
// Libraries
#include <ESP8266WiFi.h>
#include <SoftwareSerial.h>                 // Workaround to using serial monitor
#include <Servo.h> 

//********************************************************************************************************************************************************************
// Constructors
Servo             myservo;
WiFiServer        server(80);
SoftwareSerial    ESPserial(2, 3);          // RX | TX

//********************************************************************************************************************************************************************
// Network Credentials
const char* ssid =      "insert SSID";     
const char* password =  "insert PASS";

//********************************************************************************************************************************************************************
// Definitions
#define servo_pin   D4                      // D4 also controls the built in LED

//********************************************************************************************************************************************************************
// Global Variables
uint8_t released_position =   90;           // (degrees)
uint8_t pressed_position =    5;            // (degrees)
uint8_t arm_speed =           20;           // (milliseconds)
uint16_t pressed_time =       3000;         // (milliseconds)

//********************************************************************************************************************************************************************
// Code Begins
void setup(){
  //------------------------- Software Serial ----------------------------------------
  Serial.begin(9600);
  ESPserial.begin(115200);
  ESPserial.println("AT+IPR=9600");
  delay(1000);
  ESPserial.begin(9600);
  Serial.println("Ready");
  ESPserial.println("AT+GMR");
  delay(10);

  //------------------------- Wireless Initializations -------------------------------
  WiFiConnect();                                // Connect to WiFi network
  ServerBegin();                                // Begin Server
  
  //------------------------- Servo Setup --------------------------------------------
  // Reset servo to released position on boot up
  myservo.attach(servo_pin);
  myservo.write(released_position);
  delay(1000);
  myservo.detach();
}

//********************************************************************************************************************************************************************
// Connects to user's network credentials
void WiFiConnect(){
  Serial.println();
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(ssid);
 
  WiFi.begin(ssid, password);
 
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.println("WiFi connected");
  Serial.print(WiFi.localIP());
}

//********************************************************************************************************************************************************************
// Begins Server and returns local IP
void ServerBegin(){
  server.begin();
  Serial.println("Server started to: ");
  Serial.print("http://");
  Serial.print(WiFi.localIP());
  Serial.println("/");
}

//********************************************************************************************************************************************************************
// Servo Actuation
void MoveServo(){
  myservo.attach(servo_pin);
  
  // Slowly move the arm so it doesn't slip off the button
  for(uint8_t i = released_position; i > pressed_position; i--){    
    myservo.write(i);
    delay(arm_speed);                     // Alter this variable to increase/decrease speed
  }
  delay(pressed_time);                    // Hold the button down
  myservo.write(released_position);       // Return back to initial position
  delay(500);                             // Wait 0.5 second
  
  myservo.detach();                       // Detach servo from pin when not in use
}

//********************************************************************************************************************************************************************
// Main Loop
void loop() {
  digitalWrite(D4, HIGH);                 // Keep LED off when servo is not in use
  
  // Check if a client has connected
  WiFiClient client = server.available();
  if(!client){
    return;
  }
 
  // Wait until the client send a request
  Serial.println("Client request.");
  while(!client.available()){
    delay(1);
  }
 
  // Read the first line of the request
  String request = client.readStringUntil('\r');
  //Serial.println(request);
  client.flush();
   
  // Return the response
  client.println("HTTP/1.1 200 OK");
  client.println("Content-Type: text/html");
  client.println("");                               // do not forget this one
  client.println("<!DOCTYPE HTML>");

  // Servo arm presses button
  if(request.indexOf("/open") != -1){
    MoveServo();
  } 

  client.println("<html>");
  client.println("Welcome to the wireless door unlock page! <br>");
  client.println("Anyone on the local network can open the main door using the button below. <br>");
  client.println("<br> <a href=\"/open\">Open Door</a> <br>");
  client.println("</html>");
 
  delay(1);
  Serial.println("Request executed.");
  Serial.println("");
 
}

Credits

Viktor S

Viktor S

7 projects • 19 followers
Electrical Engineer & Electronics Hobbyist

Comments