Last edited by greenbreakfast, 2016-04-22 20:20:09
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.
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.
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
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.
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.
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.
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.
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:
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.
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!
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.