Introduction: Wifi to RF - Door Lock

Overview

This instructable will give you the ability to lock / unlock your front door via OpenHAB (free home automation software), such as via an iPhone as highlighted in the image above. Alternately you can utilise the MQTT messaging as described below in almost any system you wish.

Assumptions

This instructable assumes you already have (or will setup):

  • OpenHAB (free open source home automation software) running, although it should work with any home automation software that can include an MQTT binding. Alternately, you can alter the code yourself to suit your own needs.
  • Mosquitto MQTT binding installed and configured with OpenHAB (MQTT is a messaging subscribe/publish type protocol that is lightweight and great for communication between devices)
  • That you have already fitted an RF based front door keypad lock that comes with at least one key fob remote (the majority do) These can be bought relatively cheap on Amazon (approx ~US$60, although the batteries on this model only last ~4 months so see if you can provide DC that powers the door!)

If you do not run OpenHAB and an MQTT broker, see this excellent article on the MakeUseOf web site

Introduction

This project specifically shows you how to use an RF based door lock KEY FOBto be considerably more useful by integrating it with your home automation system (or even without OpenHAB, you can control via MQTT publish messages directly from your PC, or adopt the code to whatever you want) You can therefore lock or unlock your door via a web browser, your phone (via the OpenHAB app) or even schedule it to auto-lock at night using rules in the home automation software. For this project, I bought a "Mi RF based keypad front door lock" off Amazon for about US$60.

(EDIT: the batteries on this particular brand of door lock do not last that long! I only got 3-4 months use out of it before I had to change the batteries. I've since swapped out that unit for a Windsor branded electronic door lock of which the batteries last up to 2 years)

For that they provide an RF (radio frequency operated) door lock with built-in keypad and a key fob for remotely locking / unlocking. With RF however, the key fob needs to be in range of the door lock. This project will make use of the key fob by integrating it with a Wemos (IOT board with ESP8266) to do the locking / unlocking wirelessly, therefore controllable from anywhere you have an Internet connection.

What do I need?

First, purchase and install the RF based keypad lock. You must ensure it comes with a key fob! Test the fob and ensure that by pressing the buttons, it will lock or unlock the door before starting. The majority of these door locks should be based on standard door sizes and lock offsets so if you already have a deadbolt (like I did) replacing it is exceptionally easy.

To create the wireless controller, you will need to source the following parts:

  • Wemos D1 mini V2 (has an ESP8266 wireless CHIP built-in) or just an ESP8266 CHIP should do (I do not recommend the ESP-01 version for this project, as the GPIO 0 pin needs to be held high on power up in order for it to boot from flash, however doing this will cause the door lock to trigger when the ESP8266 powers up potentially unlocking your front door! While there are workarounds we can do for this, such as using timers or NPN/PNP transistors and a capacitor, for the low cost of a Wemos and fewer components, we will aim for the simpler approach)
  • A 5V DC power source to power the Wemos and the Keyfob (batteries will no longer be required) If you are powering the Wemos using the 5V VCC pin (rather than the USB port) and the DC power source is higher than 5V, then you will also need a 5V voltage regulator (such as an LM7805) plus 2 x capacitors, 10V 1uF and a 10V 10uF or similar in size.
  • Two 2N7000 or similar MOSFETs (these will be used to switch the keyfob, one for locking, the other for unlocking. The 2N7000 is a very common and very cheap enhancement type N-Channel MOSFET so should be very easy to source)

  • Two 10K ohm resistors (these will be used as pull-down resistors for each of the MOSFETs so the door lock / unlock is not triggered on power up)

  • Depending on your keyfob, you may also need to wire in another capacitor (such as a 10V 220uF or similar in size) to help boost power to the keyfob . See reasons why later in this article.

  • Tools needed: side cutters, single core wire, a soldering iron, flux and optionally a multi-meter

Furthermore (regarding the ESP8266-01) it is a 3.3V device whereas the Wemos is a 5V powered device so a voltage regulator would be required. Conversely, 3.3V alone is not enough to power the key fob, as the key fob itself usually uses 2 x 3V batteries in series (6V VCC) Using a 5V supply with some capacitors however is sufficient voltage to both power the Wemos and trigger the locking / unlocking.

Step 1: Assembly

Soldering

Assembling the components is very simple. First, rip open the key fob - remove the batteries and case as you only need the circuit board inside. Next solder a wire on each side of the legs of the key fob button. Do this for both the "lock" button and the "unlock" button. Next, turn the fob over and solder a wire on the + and - pads at the back of the fob, as shown in the images. The larger pad is +, the smaller is -

Note: The 5V power supply will power both the Keyfob and the Wemos, the fob batteries are no longer required.

Assembly

Provide 5V Vcc and GND to the Wemos and add the MOSFETs and resistors to the breadboard as per the image. The 10K ohm resistors should run from GND to the gate (center pin) of the MOSFETs. This will pull the voltage to ground stopping the MOSFETs being triggered on power up.

Next run a wire from D1 on the Wemos to the gate of the first MOSFET and D2 on the Wemos to the gate of the second MOSFET.

Finally, plug the keyfob wires onto the breadboard, Vcc to 5V rail, GND to GND, then the wire on the negative side of the button to the source side of each MOSFET and the positive side of the button to the drain side of each MOSFET as shown in the image (use a multimeter if unsure of polarity)

(EDIT: The replacement Windsor door lock I mentioned earlier, has a keyfob that consumed slightly more voltage than the old keyfob. This caused the Wemos to effectively stop responding due to the voltage drop to it, when the door was locked/unlocked. If you experience the same issue, then to remedy wire in a 220uF (or similar) capacitor just before the +/- wiring that goes out to the keyfob)

Now plug the USB cable into the Wemos and the other end of the cable into your computer.

(The code will output pin D1 "HIGH", triggering the MOSFET to turn on allowing voltage to travel from drain to source and therefore, "switching" the button on for 1 second before bringing the output pin "LOW" again and switching it off. In effect, it is simply simulating a key press of the button via code)

Arduino IDE

Launch the Arduino IDE. Download the code (alternate link HERE) and open it in your project. You will need to ensure the correct board is installed and loaded for your project as well as the correct COM port selected (Tools, Port). To get the Wemos board included in your sketch, see this article.

You will need to alter the following lines of code, and replace with your own SSID and password for your wireless connection. Also, change the IP address to point to your own MQTT broker.

// Wifi
const char* ssid = "your_wifi_ssid_here";
const char* password = "your_wifi_password_here";
// MQTT Broker IPAddress
MQTT_SERVER(192, 168, 222, 254);

Once changed, verify your code then upload to the Wemos / ESP8266 board via a USB cable.

Step 2: Testing and OpenHAB Configuration

MQTT Testing

MQTT is a "subscribe / publish" messaging system. One or more devices can talk to an "MQTT broker" and "subscribe" to a certain topic. Any incoming messages from any other device that are "published" to that same topic, will be pushed out by the broker to all devices that have subscribed to it. It is an extremely lightweight and simple to use protocol and perfect as a simple triggering system such as the one here. See the flow diagram for a rough idea of how it works.

For testing, you can view incoming MQTT messages from the Wemos to your MQTT broker by running the following command on your Mosquitto server (Mosquitto is one of many MQTT Broker software available). This command subscribes to incoming keepalive messages:

mosquitto_sub -v -t openhab/frontdoor/status

You should see inbound messages coming in from the Wemos every 30 seconds or so with the number "1" (meaning "I'm alive") If you see constant "0's" (or no response) then there is no communication. Once you see the number 1 coming in, then it means the Wemos is communicating with the MQTT broker (search "MQTT Last Will and Testament" for more information on how this works, or see this really good blog entry)

Once you have proved the communication is functional, you can now track the actual "trigger" (lock/unlock) message. First subscribe to the following topic.

mosquitto_sub -v -t openhab/frontdoor/trigger

Now open a second command line window and run the following publish command, sending the topic a message of either "LOCK" or "UNLOCK". You should see the corresponding message appear in the first window and you should also see the red LED light flash on the keyfob and the door mechanism locking or unlocking as required.

mosquitto_pub -t openhab/frontdoor/trigger -m LOCK


Note: If the door unlocks when you send lock or locks when you send unlock command, simply change the D1 and D2 wires around



OpenHAB configuration

The following changes are required to OpenHAB:

'items' file:

Switch frontdoorTrigger "Front Door" (gDoors) { mqtt=">[mqttbroker:openhab/frontdoor/trigger:command:ON:LOCK],>[mqttbroker:openhab/frontdoor/trigger:command:OFF:UNLOCK]" }
Number frontdoorStatus "Front Door [MAP(status.map):%d]" (gDoors) { mqtt="<[mqttbroker:openhab/frontdoor/status:state:default]" }

'sitemap' file:

Switch item=frontdoorTrigger mappings=[ON="Lock", OFF="Unlock"]
Text item=frontdoorStatus

Contents of 'status.map' file (in transform folder):

0=Down
1=Alive
-=unknown

You may need to change the above openhab configuration slightly to suit your own setup.

The final image shows the components on a PCB. In this case, because I am using a DC power supply that is > 5V (in my case 9V) the board also includes an LM7805CV voltage regulator as well as a 0.33uF capacitor for power in side and 0.1uF capacitor on the power out side to help smooth and stabilise the voltage. Otherwise the rest of the connections are the same as described earlier.

Troubleshooting

- If you have issues where the Wemos continuously locks or unlocks the door (eg: sends an RF signal and the LED light of the key fob stays on) then check your VCC and GND wires to the Wemos. Chances are, one or both of these are not connected properly.

Comments

author
sny2ksa (author)2017-08-29

Hi,

I try to build a basic project using espeasy + RPI3 with openhabian 2,1 and mosquito. everything is going fine but relay not working, I can turn on and off relay using espeasy commands using web browser all screens shots are attache can some one tell me where i am wrong.

additionally i want to add timer on this relay with save the relay state during power failure

esp easy mega controller settings.jpgesp easy mega devices settings.jpgesp easy mega jog settings.jpgesp easy mega main settings.jpgesp easy mega rules settings.jpgmessages.jpgopenhab items.jpgopenhab sitemap.jpg
author
javierz14 (author)2017-06-20

Hi, I'm trying to do something similar. It is possible to add a physical button to Wimos for LOCK or UNLOCK. And modify the status also in OPENHAB. It would be a physical control just like the SWITCH in openhab. Thanks greetings.

author
chimeranzl (author)javierz142017-08-01

Yes, of course - this should be very simple to do. You would have to define another a GPIO pin as an input, wire in a switch, then when the switch is flicked on or off (lock or unlock) have the Wemos update the MQTT message. OpenHAB would then detect the change, and update accordingly.

author
tannermeans made it! (author)2017-07-01

Phenomenal writeup!

First time soldering in ~5 years but messily got it all to work. My soldering iron started corroding pretty bad while finishing the GND wire. Thanks a lot! Range on the remote is ~40 feet for me through walls.

I'd add: it much easier to solder the remote into the prototyping board if you attach ~2cm pins instead of wires on the remote's button leads, allowing it to stand on its own in a breadboard or once soldered. I cut these off the MOSFETs, but you might be able to use the clipped tails from resistors as well.

I also used a mini D1 "wemos compatible" chip from amazon instead of the offical wemos D1 and it works just fine.

Make sure to require authentication to your MQTT broker! They're easily discoverable.

IMG_20170701_132733.jpgIMG_20170701_132740.jpg
author
chimeranzl (author)tannermeans2017-07-01

That's awesome nice work! Glad you found it helpful, thanks for the tips. My soldering is a lot more messy tbh, and using wires a lot smarter in hindsight! Personally I didn't bother with auth because I have a separate SSID and network for my IOT devices, that sits separate to usual "user" based devices.

author
tannermeans (author)chimeranzl2017-07-01

+1 on the separate network!

author
DIY Hacks and How Tos (author)2016-07-31

Awesome security system.

About This Instructable

9,037views

156favorites

License:

More by chimeranzl:Wifi to RF - Door LockHRV Temperature to OpenHAB Via ESP8266 (Serial code example!)
Add instructable to: