Creating a WiFi-controlled Automatic Door Lock
This guide will show you how to use the Omega to build an WiFi-controlled automatic lock for any door with a single cylinder deadbolt.
We will be taking a regular deadbolt like this:
And making it wirelessly controllable using the Omega:
This guide will show you how to setup the physical mechanism to lock & unlock the deadbolt, and the software required to control the mechanism wirelessly.
- The Omega
- The Expansion Dock
- The Servo Expansion
- A Servo Motor
- A Casing to attach the motor to the deadbolt
- A MicroUSB Power supply
- Using the Servo Expansion
- Using Shell Scripts
- Familiarity with the Omega
A servomotor is a device that will position itself at a specific angular position based on a pulse-width modulated signal. The Onion Servo Expansion was specifically created to control servomotors. Using this combination, we will be able to turn a deadbolt lock at will.
The Actual Process
We are going to prepare the servo, accessories, and hardware. After that, we will configure the commands to activate the servo and then create an app that makes use of the command.
All of the code can be found on this GitHub repo: https://github.com/OnionIoT/smart-lock/tree/master/tutorial
Step 1: The Servo Motor
These motors turn using PWM signals. Our servo expansion has a pwm-exp function that can be used to generate these pulses. Most deadbolts lock and unlock at 90 degrees so modifications are not usually necessary. If your lock has to turn more than 180 degrees, you may want to modify your motor for continuous rotation.
Step 2: The Motor Casing
You will need a way to attach your motor to your door and turn the lock. You can make a casing for the motor that can attach to your door and hold the motor in place. A small piece of plastic in the shape of the lock’s bar should be attached to the motor. An easy way to obtain these is to 3D print them. These are the models we are using.
Step 3: Storing lock state using UCI
OpenWRT is installed on the omega so you are able to use UCI to store configurations on the omega. So, we will use this to store the status of the lock. If your lock needs to turn more than 180 degrees, this would prevent the motor from applying a force on already locked/unlocked doors.
Step 4: Controlling your servo through ubus
We will use the ubus to run commands from the app. For more information about the ubus, please take a look at our ubus tutorial series.
To setup a new ubus service: first navigate to
/usr/libexec/rpcd/ and then we can use a template to build the ubus service.
If you modified your servomotor, the sleep integer may have to be adjusted based on how much it is required to turn. Otherwise, you do not need conditional statements. The script we have basically runs the motor on channel 8 for ‘sleep’ seconds and set the state of the lock. If you are using UCI, you may want to add a function to force the motor to a position in case the lock status is incorrect. This may happen if someone uses a key to unlock a door so the status is still locked.
Our ubus service code can be found in the GitHub repo.
Step 5: Adding ubus Permissions
In order to run ubus commands outside of the terminal, you must first include the functions in the ubus permissions file.
In onion-console.json, add your ubus functions under read:ubus: and write:ubus:
Step 6: Create an app
Having an app to request functions is a lot easier and has a better UI than the terminal. There is an app template at
/www/apps/ which you can use to build an app. Copy the onion-template folder and give it an app name. Edit the app.json and icon.png files to register it. Set the icon value to “true” for your app to show up on the console.
Our example code for the app is located on GitHub. We are using the Polymer framework to create an HTML element. You may look in the files of other apps to see how your code should be organized.
This is what the UI looks like.
You are now finished. Attach your motor to your door and test it. Don’t forget to reboot your omega!
Using the Project
Simply press the buttons on your app to tell the servo to lock or unlock your door.
You may want to connect your app to the web so it can be used even when you’re not within range of the WiFi network to which the Omega is connected.