Introduction: Wifi to RF - Door Lock

Overview

This instructable will give you the ability to lock / unlock your front door via your home automation software (such as OpenHAB or HomeAssistant) The image above shows a sample screenshot of the OpenHAB iPhone app. Alternatively you can utilise the MQTT messaging as described below in almost any system you wish. YAML code for using ESPHome in HomeAssistant also provided.

Assumptions

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

  • OpenHAB or HomeAssistant (free open source home automation software) running, although as mentioned, it should work with any home automation software that can include an MQTT binding (or ESPHome with HA). Alternately, you can alter the code yourself to suit your own needs.
  • Either 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) or ESPHome addon for Home Assistant
  • 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 lock or spend a little more on a decent model :-) )

Introduction

This project specifically shows you how to use an RF based door lock KEY FOBto be considerably more useful by converting it to work wirelessly, therefore giving you the ability to lock or unlock your door via your home automation system, a web browser, your phone (via the OpenHAB or HA 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 which became rather frustrating. I've since swapped out that unit for a Windsor branded electronic door lock of which the batteries last up to 2 years. Alternately, albeit a lot more work, consider running DC power through the core of your door to the lock)

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 chip) 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 transistors etc, 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, you will also need a 5V voltage regulator (such as an LM7805) plus 2 x capacitors, 10V 0.33uF and a 10V 0.1uF or similar in size (as per the LM7805 datasheet)
  • 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

The key fob itself typically uses 2 x 3V batteries in series (6V Vcc) Therefore, using a 5V supply with a capacitor however is sufficient voltage to both power the Wemos and trigger the locking / unlocking.

If you have a keyfob that works off a single 3V battery, then you should be able to add a 3.3V voltage regulator in your circuit to drop voltage close to the 3V required. A 3V battery actually outputs around 3.1V, and most circuits have some sort of higher voltage tolerance, so in this instance 3.1V to 3.3V is +6%. Combine that with the fact the Wemos is drawing current, so voltage is likely to be even less. If in doubt measure with a multimeter and if possible, check with the lock vendor the maximum voltage (or maximum tolerance) the keyfob will accept, as I take no responsibility if your keyfob stops working! Finally, as an alternate solution setup a voltage divider circuit instead.

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 Wemos and the Key fob, so 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. D1 and D2 are GPIO (general purpose input/output) pins that are designated as OUTPUT pins in the code.

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. The capacitor will charge up and when triggering, drain the capacitor rather than direct DC power)

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 (required for OpenHAB or HomeAssistant without ESPHome)

Launch the Arduino IDE. Download and open the code (alternate link HERE) Ensure the additional boards are included in the IDE, see here on how to setup. You will then need to ensure the correct board is installed and loaded for your project (Tools, Boards, Board Manager - search "esp8266" and install) as well as the correct COM port selected (Tools, Port, COM...). You will also need the appropriate libraries PubSubClient and ESP8266Wifi installed (Sketch, Manage Libraries, Include Library...)

Next, 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. If you do not have an MQTT broker installed, I recommend using Mosquitto. Download for Windows or Linux here.

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


ESPHome (for HomeAssistant)

Add ESPHome to HomeAssistant if you haven't already (Settings, Add-ons) and add a New Device. Name it frontdoorlock (or similar) and click Next. If you haven't setup ESPHome before, read the documentation, but the short answer it to connect the Wemos to your PC via USB and upload the ESPHome binaries to the chip.

Once the new device setup is complete, click EDIT ...



At the bottom of the frontdoorlock.yaml file (usually under 'captive portal:'), append the following text:

# Switch Action
switch:
  - platform: gpio
    name: "Unlock Door"
    icon: 'mdi:lock-open'
    id: unlock_door
    pin: D2
    interlock: [lock_door]
    on_turn_on:
      - delay: 1000ms
      - switch.turn_off: unlock_door

  - platform: gpio
    name: "Lock Door"
    icon: 'mdi:lock'
    id: lock_door
    pin: D1
    interlock: [unlock_door]
    on_turn_on:
      - delay: 1000ms
      - switch.turn_off: lock_door

# Switch Toggle
cover:
  - platform: template
    name: "Front Door Lock"
    id: front_door_lock
    open_action:
      - switch.turn_on: unlock_door
    close_action:
      - switch.turn_on: lock_door
    stop_action:
      - switch.turn_off: lock_door
      - switch.turn_off: unlock_door
    optimistic: True
    assumed_state: True


Save the file and X to close. Now click the 3 dots and install the new code.



Once uploaded and ESPHome on the Wemos checks into HA, the new Entity will be available for you to add to your HA console. Setup similar to the following:


Test the LOCK and UNLOCK functionality in Home Assistant but toggling the new Front Door Lock device

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 any other device that has subscribed to that topic. 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

(-t means 'topic', -m means 'message', -v means 'verbose' output)

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

'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, such as the "mqttbroker:" part which refers to your configured MQTT broker name.

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

Step 3: Solder Up the Components to a PCB

I ended up soldering all components up to a PCB, just using wires and/or solder tracks to join all components. I then got an old DC wall wart and cut the wire from it slightly shorter, then ran that to a connector block that I'd soldered to the PCB. Power in then goes off to the 5V voltage regulator / capacitors and necessary components. I simply blue-tacked the back of the PCB to the wall wart, and plugged it into a power point inside one of my cupboards. Its been running now for about 9 months without any issues!