Smart Door Lock

The idea of controlling my home door lock from remote was in my mind for a while.

Recently I purchased an Amazon Echo Dot device and I wanted to write my own skills.

I also wanted to practice mechanical design for 3D printing and this idea combines all my wishes.

I will try to describes the project and explain what I did, why I did it and how I did it.

Mechanical Design

Turning the lock

My home door is heavy and when I lock the door, it anchored in several places.
It takes a little force to lock and unlock so a geared servo motor like the MG995 seemed good choice.

Problem:  The servo spins only 180 degrees and I need few tuns to lock and unlock the door.
Solutions:

  • Hack the servo so it will spins 360 degrees
  • Buy a version of this servo that spins 360
  • Plan gears in a way that 180 degrees will be enough to lock/unlock

I’m not a mechanical engineer and I don’t know much about gears (you will see later).
I already had MG995 servo motor so I chose to hacked it.
There are many tutorials out there for this hack, this is the one I used:

Ater the hack, the motor does not stop at 180 degrees, it continues to speed forever.
I can set the direction of the motor by sending a signal for angles from 0 to 90, and from 90 to 180 to the other direction.

3D Printed mount

I used Autodesk Fusion 360 for the 3d printed parts.
There are many tutorials out there and you can start making designs really fast.
Also, for hobbyist you get can one year license for free.

From within the house there is a “fixed key”.

Problem: I did not want to remove this key or make any changes in the door.
I live in a rented apartment so I cant the risk of damaging the door.
Without any modification to the door, is it possible to connect the lock mechanics?

Solution: This “key” can be taken off the door.  There is a little spring that pushes a needle
to a hole in the key. If you push the needle you can then take out the key.
My idea was to connect the gear to this hole to, from the outer side.

The second gear will be connected to the servo, and all will mount on the door handle.
Mounting it on the handle makes it possible to lock and unlock the door manually.
When you open the door with the handle, you disconnect the gears from each other and
then it’s possible to turn the lock without any resistance from the servo motor.
This is what came out:

You can see in right photos what I described with the handle.

I’m not a mechanical engineer and I know very little about gears.
I do know that if you want to increase the torque, you need to build a gears ratio greater than 1:1.
I drew these big gears out of my head without any formulas or calculation like you should do when you
plan gears. That’s why the teeth are so big. I started with one tooth and then duplicated it.
The big gear has 26 teeth and the little gear has 13 teeth, so it’s 2:1 ratio (doubling the torque).

Some more photos:

You can find the stl files here:
https://gitlab.com/itaysp/alexa-door-lock/tree/master/STL

Firmware

After building the mechanics, I wanted to control it from remote – my phone or even Alexa.
First and simple method I thought about was to take an ESP8266 and run a webserver.
Then I could control the servo with HTTP requests and maybe build a nice web UI.

Thinking a little more and I decided to use MQTT messaging protocol instead.
MQTT is a little more complex, you need a broker server and use a library for the mqtt client,
but for the long term, if I want to build a smart home with many devices, this is a good infrastructure.

The ESP8266 is still used here for the connecting the servo to my smart home and running a MQTT client.
I used the WeMos D1 Mini board because it’s small, has a usb-serial interface already so it’s easy to flash a
firmware without any additional circuit or components, it’s cheap, and I had few lying around.

I used the ESP8266 Core for Arduino to write the firmware because it’s very fast and there are good
MQTT libraries for Arduino.
For the MQTT client I used the Pubsubclient library.
I started a Mosquitto MQTT broker on a raspberry pi, but there are good free MQTT broker services
like cloudmqtt which I can recommend from a past experience.

From the examples and the documentation of the Pubsubclient library it’s easy to start
and subscribe to an mqtt topic.
First you need to write all the server details you have.
clientId must be a unique identifier.
topic is the topic we subscribe to for controlling the door lock.

/* Mqtt Settings */
const char* mqtt_server = "YOUR_MQTT_SERVER_ADDRESS";
const char* clientUsername = "YOUR_MQTT_SERVER_USERNAME";
String clientId = "esp-door-client";
const char* clientPass = "YOUR_MQTT_SERVER_PASSWORD";
const uint16_t port = YOUR_MQTT_SERVER_PORT;
const char* topic = "DoorLock";

When a message is received with the topic I subscribed to,  I read it and then
using strcmp function check if it’s was lock or unlock message.
For large smart home network it will be wiser to use opcodes with switch statements instead.
Because I have here only 2 options and speed/efficiency is not important right now, I used
command words for simplicity.

lock and unlock are global booleans flags.
The lock/unlock will take place in the main loop according to these flags.

//Mqtt message received
void callback(char* topic, byte* payload, unsigned int length) {
 char buff[16];
 uint8_t i;

 Serial.print("Message arrived [");
 Serial.print(topic);
 Serial.print("] ");

 for (i = 0; i < length; i++) {
  Serial.print((char)payload[i]);
  buff[i] = (char)payload[i];
 }
 buff[i] = 0; //Termination char for working with strcmp

 if(strcmp(buff,"lock") == 0){
  lock = true;
 }
 else if((strcmp(buff,"unlock") == 0)){
  unlock = true;
 }

}

The door lock/unlock function will initialize the servo, write a position that now only
means speed and direction, wait a few seconds and then detach the servo.
I’m detaching the servo because after I hacked it, it’s hard to make it stop.
Setting it to 90 degrees position should make it stop, but it doesn’t.
Detaching it will stop sending pulses and it will stop moving.

I used the Arduino servo library to control the servo.
For the lock and unlock timing, I just played with it a little until I found the right
time for the servo to complete the unlock and the lock.

//Door unlock function
void doorLock(){
 //init the servo
 door_servo.attach(SERVO_PIN);

 //start pulsing the servo (for direction and speed)
 door_servo.write(DOOR_LOCK_SPEED);

 //wait duration seconds for the door to lock
 myDelay(DOOR_LOCK_DURATION);

 //stop sending pulses
 door_servo.detach(); 
}

The delay function is not the regular Delay because it blocks interrupts.
I like to write my own delay function.
In the ESP8266 there is a watchdog enabled by default so when writing such a function
it’s importat to remember feeding it.

//My delay function
void myDelay(uint32_t ms){
 uint32_t t = millis();
 while((millis() - t) < ms)
 {
  ESP.wdtFeed(); //feed the watchdog to avoid reset of the mcu
 }
}

You can find the complete firmware here:
https://gitlab.com/itaysp/alexa-door-lock/blob/master/DoorLock/DoorLock.ino

Note that you need to modify it with your wifi credentials, and this firmware
includes the code for the Arduino OTA update option so you can remove it
if you don’t need it.

Alexa Skill

After having the door lock controlled by MQTT (from my phone/
web browser with MQTT client) ,it’s time for the Alexa skill.

I decided to build  it with Node.js because I kept hearing about it and this was a good
time to start using it.

There’s a good step by step guide from Amazon for building a skill using Node.js:
https://github.com/alexa/skill-sample-nodejs-howto

And there’s the Alexa Skills Kit SDK for Node.js
https://github.com/alexa/alexa-skills-kit-sdk-for-nodejs

Reading both and looking at some of these examples, I built the skill.

I used the MQTT.js client library to publish messages to the server.

There is not much I can add after you read the tutorials and look thru the examples.
You can find the source code and the speech assets of my skill  here:
https://gitlab.com/itaysp/alexa-door-lock/tree/master/AlexaSkill

If you want to use/modify it and create a skill, just follow the first link –
it explains all the steps you need in to do in the Developer Portal and the AWS Lambda.

The project’s gitlab:
https://gitlab.com/itaysp/alexa-door-lock

 

 

 

 

 

2 thoughts on “Smart Door Lock”

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.