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.


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


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 personally bought a "Mi RF based keypad front door lock" off Amazon for about US$60. 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 should 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)
  • 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)

  • 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 however is sufficient voltage to both power the Wemos and trigger the locking / unlocking.

Step 1: Assembly


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.


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)

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):


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.


- 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.

<p>Awesome security system.</p>

About This Instructable




More by chimeranzl:RF Door Lock - Wemos (ESP8266) and OpenHAB HRV Temperature to OpenHAB Via ESP8266 (Serial code example!) 
Add instructable to: