Developing the Internet of Things: Kwikset SmartCode Deadbolt and VeraLite Smart Home Controller
In this post explore how to rapidly build an application to create an Internet of Things Solution for home automation. The application will support swipe gestures to toggle the lock and unlock state of a Kwikset SmartCode Deadbolt and will interface with the REST API of a VeraLite Smart Home Controller.The REST API will be used to query the inital state of the doorlock and issue lock and unlock commands.
See how rapidly appmethod can build a multi-device connected app with REST components and only 8 lines of code!
KwikSet SmartCode Deadbolt with Home Connect Technology featuring Z-Wave
Kwikset offers an electronic deadbolt that features Z-Wave technology. This enables it to be controlled by a VeraLite Smart Home Controller.
The product page for the Kwikset Deadbolt describes the features:
The SmartCode Deadbolt with Home Connect Technology featuring Z-Wave enables the door lock to wirelessly communicate with other devices in home. The lock allows the user through a web enabled device to remotely check the door lock status, lock or unlock the door and receive text or email messages.
- The motor-driven deadbolt latch allows for true remote locking and unlocking.
- SmartKey cylinders feature a stainless steel side locking bar, racks and pins for increased strength and security
- Patented side locking bar technology offers improved security by protecting against lock bumping, an attack technique used to defeat conventional pin & tumbler locks
- Lifetime mechanical and finish warranty. 1 year electronics warranty.
VeraLite Smart Home Controller
The VeraLite Smart Home Controller is a connectivity hub that interfaces with all kinds of smart home automation Gadgets and Devices. Though this blog post will only cover connectivity to the Kwikset SmartCode Deadbolt, it would be very straight forward to extend functionality to other devices.
To do this, check out the full list of Devices supported at the MiCasaVerde Luup UPnP Variables and Actions wiki page. Here you can get the relevant REST API calls.
Before following this tutorial ensure that the VeraLite is properly configured and can control the Kwikset SmartCode Deadbolt.
Using appmethod's REST Debugger
REST Debugger is a tool that you can use to test and debug REST queries. In this case it will be used to test the REST API of the VeraLite Smart Home Controller.
The first step to communicating with the VeraLite is to obtain its IP address on the network. To obtain the IP address reference the Network Setup wiki entry from MicasaVerde. This blog post will refer to the IP address of your VeraLite Controller as <myip>.
The port of the VeraLite is 3480.
Open Rest Debugger, Tools -> REST Debugger
Ensure the Request Method is GET and enter the IP address and port into the URL field as such
Click Send Request. At the bottom of the REST Debugger window the Response should return 200:HTTP/1.1 200 OK. This is acknowledging that a successful connection to the VeraLite controller has been made.
To issue an unlock command over REST set the following in the URL field
To issue a lock command over REST set the following in the URL field
At this point, you should be able to use the REST Debugger to open and close the door lock.
Using the REST Debugger to Generate REST Components
A great feature of the REST Debugger is its ability to generate REST Components for you. This makes it very quick and easy to setup connectivity within your app to Gadgets and Devices that use a REST API.
The REST Components are powered by the Embarcadero REST Library, a framework for accessing REST-based web services (REST stands for Representational State Transfer).
Lock / Unlock Deadbolt
On the Request tab set the Method to GET and enter http://<myip>:3480 in the URL.
On the Parameters tab, enter data_request in the Resource field.
Each URL paramater needs to be entered as an entry to the Request Parameters. Click the Add button and add the URL variable and value as shown in the screen shot.
Once all the URL variables have been added, test the REST API call by clicking Send Request. By toggling the last URL variable, newTargetValue, from 0 to 1 you can test opening and closing the lock.
Click the Copy Components button.
The components are now stored in the clipboard and are ready to be pasted directly on the form designer.
Click on the RESTRequest1 component and double click the Params property (TRESTRequestParameterList) from the Object Inspector. This will open up a window that will allow you to inspect and change values for the URL variables inputted into the REST Debugger earlier.
Here you can manually toggle the newTargetValue between 0 and 1 to open and close the Kiwkset door lock.
Test that the REST API call is correctly set in the components by right clicking on the RESTRequest1 component and select Execute. If the newTargetValue is 1, the door lock will close, if it is set to 0 the door lock will open. Next, we will add functionality to our application to dynamically change this value.
Query Status of Deadbolt
In order to set the initial state of the app, we must query the KiwkSet Deadbolt as to whether or not it is locked or unlocked.
The following REST API call will return a JSON blob with the current status of VeraLite and the devices it is currently managing.
The returned JSON blob consists of a number of arrays containing devices each with an associated array of states. For this basic demo, I am directly addressing the locked/unlocked state of the deadbolt by setting devices.states in the JSON Root Element field under the Parameters tab. These indexes happens to work for the specific setup of the VeraLite I am connecting to in this demo. Using the REST Debugger one can spelunk through the JSON blob to determine the indexes of interest.
A more thorough approach would be to iterate through all devices in code to dynamically located a given device managed by the VeraLite controller.
Copy the components to the form designer as in the previous steps.
Build the App UI
Now that we have components that can issue lock and unlock commands and determine the state of the Kwikset Door lock, its time to build the UI for the app. The image below shows all the components that make up this app.
From the Tool Palette add a TActionList component and right click on the component and select Action List Editor.
Click the new action icon ( ) and select New Standard Action (Ctrl+Ins) in the pop up window, add both TNextTabAction and TPreviousTabAction
From the Tool Palette add a TGestureManager component to the Form Designer.
For an in depth look at how gestures work within appmethod please refrence the Gestures in FireMonkey doc wiki.
From the Tool Palette add a TTabControl component.
The TabControl is the parent to two TTabItems containing a lock and unlock image. The GestureManager component enables the tracking of swipe actions both with touch and the mouse. Upon gesture recognition the lock/unlock tabs will toggle. Each time the tab is toggled the RESTRequest component gets executed and the door will lock/unlock respectively.
We will add the functionality to do this in the end of this blog post. At this point of building the app, the swipe gestures are not functional.
To add tab items right click on the TabControl1 component from the Structure view and select Items Editor...
Add two TTabItems named TabItemLock and TabItemUnlock with Text property set to Lock/Unlock respectively.
Set the Align property of TabControl1 to Client. This will stretch the TabControl across the entire screen.
Set the ActiveTab property to TabItemLock and set TabIndex to 0.
Set the TabPosition to None. This will hide tab handle UI element.
For each of the TabItems, add a TImage from the Tool Palette.
For both Images set the name property to ImageLock/ImageUnlock.
Set the MultiResBitmap property to these images:
Set the Align property to Client.
Add a TToolBar from the Tool Palette to the Form Designer.
Set the Align property to Top and set the Name property to ToolBarHeader.
Add a TLabel nested under the ToolBarHeader and set its name property to LabelHeader and text property to Kwikset Door.
At this point, your project should look something like this:
Setting Inital State
We need to set the inital state of our app to reflect wether or not the doorknob is locked or unlocked.
The Status variable from the returned JSON blob is a 1 if locked and 0 if unlocked.
Right click on TabControl1 from the Structure view and and open the Items Designer. Ensure that Unlock is first and Lock is second.
On the Code View and set the following code on the TForm
Adding Swipe Functionality
Select the ImageLock component from the Structure view. It should be nested under the TabItemLock Component.
In the Object Inspector scroll down to the Touch property and expand it.
Set the GestureManager property to GestureManager1.
Expand the Gestures property then expand the Standard property and check Left.
Click the drop down menu to the right of the Left check mark and select NextTabAction. This value is being pulled in from the ActionList component.
Repeat this process for ImageUnlock but check off the Right Standard gesture and set the action to PrevTabAction.
Test the application and ensure that you can now swipe between the two images.
Add Door Lock Unlock Functionality
Select the TabControl1 component from the Structure view and open the Events tab from the Object Inspector.
Double click on the OnChange event property field. This will open the code view for the event handler that gets triggered whenever the tab control changes form one tab to another. This effectivly gives us a handle to have some code run whenever the app responds to a swipe command.
Enter the following code:
The active tab is queried and tested to check which tab has been switched to.
The RESTRequest1 component contains the URL variables for the REST API call. In this situation the value for newTargetValue (which is the 5th URL variable) is changed between 0 and 1 depending upon which tab was switched to.
Then the RESTRequest1 Execute method is called which sends out the URL string to the VeraLite for processing.
If you would like to try this app out yourself download the project files here.
With only 8 lines of code this app is able to recognize and respond to gesture input and then communicate with the VeraLite Controller via its REST API to lock and unlock a KwikSet SmartCode Deadbolt!
Please login first in order for you to submit comments