Following a couple of my prelude IBLEs published here and here, this project takes the first step towards building a basic version of a functional Home Automation Hub.
I've used several different technologies in an effort to make sense of how I may be able to use all the things that I have learned in the past and the new things that I continue to learn as days progress.
Therefore, this Automation Hub is comprised of the following components:
A SQL Server 2012 database that:
A Real-time ASP.NET SignalR Hub Web Application that:
A User facing HTML SignalR Dashboard client that:
A Python SignalR background service application running on the Raspberry Pi 2.0 that:
An Arduino IR Transmitter Sketch that:
If the target appliance is in the vicinity of the IR Transmitter, then the appliance (may) react to the IR signal transmitted by the Arduino
NOTE
Although, the target appliance that I use in this demonstration does react to IR signals, you may want to read this section of my other IBLE for reasons why I say that the appliance (may) react to the IR signal .
Time to roll.
This instructable takes off with some of the work done previously which also resulted in my last IBLE.
So, before we step into what we need for this IBLE, it's recommended that you read this instructable for some background on how :
Following the completion of this IBLE, I deployed an ASP.NET IR Code Recorder web application that would:
It's this SQL database that forms one of the components of the Home Automation Hub elaborated in this IBLE.
NOTE
The IR Code Recorder Web application does not form part of the discussion here for the following reasons:
Therefore details on this project would be a topic for another IBLE
A functioning Raspberry Pi 2.0 - I recommend installing Ubuntu Mate as it has a richer set of features including OpenLibre Office which by the way was indispensable in documenting this instructable, right there on the Raspberry Pi.
In addition, the Pi , you'll need the following externals:
To get all of the pieces together, the following software setup will have to be installed and running:
On the Raspberry Pi, you will need to install the following:
A Windows machine with the following development environment installed:
A Windows Internet Information Server (IIS) Hosting environment:
NOTE
All instructions are applicable to the Python 2.7.x version . Version 3.0 may require rewrites
The attached schematic shows the structure of a basic SQL Server database used in this application and contains just two tables.
Table AutoHubCode
The two important columns in this table are:
AutoCodeKey- stores the user-friendly name of the Code key
AutoCodeVal- stores the raw IR Code sequence
In this case, a Python client in constant communication to the Hub receives the IR code sequence and transmits it over Serial Port to the Arduino UNO
Table AutoHubLog
As mentioned, I've used SQL Server 2012 as my database platform of choice . You can recreate this simple design on a different database platform such as MySQL, Oracle, etc.
Nevertheless, the SQL Script to create this database has been attached here.
NOTE
The ASP.NET SignalR Hub Web Application jointly comprises of the following components as indicated in the attached schematic:
Section 1 - The SignalR Hub that receives requests from and responds to client
Sections 2,4 - The HTML client web page and it's style sheet that collectively form the front end of the Automation system and issues commands to the Automation Hub
Section 3 - The jQuery SignalR APIs used by the HTML client to communicate to the Automation Hub
Section 5 - The SignalR Hub does not directly communicate to the database . It does so via intermediate classes generated using the Entity Framework
These classes abstract the database details from the front end application
Section 6 - The Database service class that helps perform the Read-Write operations on the SQL Database (described previously) by using Entity Framework classes
ASP.NET and SignalR are Microsoft technologies and this tutorial will walk you through on how a simple SignalR application is built and deployed.
What I've built here is based on the basics acquired from this tutorial. When deployed, the application should look similar to the web page shown in the second picture
NOTE ON THE CODE
A ZIP file containing a stripped down version of the code has been attached
The folder structure is as shown in the visual - however, all of the framework classes, and jQuery scripts have been removed to reduce the size of the attachment
The recommendation is that this code be used as a guide because when you create a new SignalR Web application by following the tutorial link above, the latest jQuery libraries and ASP.NET framework classes will be added automatically
Also, the references to the jQuery scripts in the index.html page will have to be changed to reflect the latest version of the jQuery SignalR client libraries that will be automatically added when you build your Web application.
Finally, the connection string will have to be changed to match your database in the files named like Web.config*
While the HTML SignalR Client is a front facing User Interface, the Python Client is a back end service application whose main function is to receive the IR Code transmitted by the Hub and route it to the Arduino UNO over Serial communication.
The code attached is self-explanatory and is documented enough to describe its functionality.
As shown in the composite screen shot, the HTML Client and the Python Service client communicate through the SignalR Hub as follows:
The Arduino circuit as shown in the visuals is pretty simple for this system and is therefore described briefly:
NOTE
In practice, the Arduino and the Pi could be jointly connected to a powered USB hub strong enough to drive the Pi, the Arduino and also transmit a strong signal via the IR LED
If the system has been set up correctly, then you should be able to bring up the HTML client page on your phone or tablet and control your appliance with the buttons on your HTML client page.
The visuals above show the Home Automation System in action once it's setup.
Since publishing this IBLE, I've extended the interface by capturing a few IR Codes from my VIZIO LED TV
As shown side by side with the factory TV Remote in the first visual, few essential functions of this remote have been built into the Web UI accessed via my tablet
Subsequent visuals show the tablet in the foreground with the TV in the back responding to commands issued from the Web interface:
In all the tests, the Gray area alongside the dashboard on the tablet screen displays the command issued by the client, and the response sent back by the remote SignalR Hub
This system can be extended by adding more codes captured from different systems. While this part is easy, there are two other factors that you will have to take into consideration.
Enhancement 1 (Quick) : Working with IR Signals of different lengths
The result? The LED TV does not turn on because the IR Code Buffer has been corrupted by the extra 20 codes not cleaned up from the previous operation!
Fix 1 (the easy way out ,not recommended)
Alter the Arduino Sketch as follows:
Change the following function calls in in the loop(){} function
transmitIRCode() ; to <br>transmitIRCode(c);
Make changes to the signature of the above function:
void transmitIRCode(int codeLen)<br>{<br> //RAWBUF constant replaced with codeLen<br> IRTransmitter.IRSendRaw::send(IRCodeBuffer, codeLen, 38);
}While this is easy, the array never really gets completely cleared and therefore this is not a very clean solution
Fix 2 (Not hard, recommended)
Declare an additional variable at the very top of the Arduino Sketch, after the comments section:
unsigned int EMPTY_INT_VALUE;
Add this to the top of the setup() function:
//Capture the natural state of an empty unsigned integer variable<br>EMPTY_INT_VALUE = IRCodeBuffer[0];
Scroll down and add a new function to the sketch immediately after the transmitIRCode() function:
void clearIRCodeBuffer(int codeLen)<br>{<br>//Clear all codes from the array<br>//NOTE: setting the array elements to 0 is not the solution!<br>for(int i=1;i<=codeLen;i++)<br>{<br>IRCodeBuffer[i-1]=EMPTY_INT_VALUE;<br>}<br>}Finally, call new function above at the following location in the loop() function:
... //Reset - Resume reading Serial Port<br>clearIRCodeBuffer(c);<br>...
This is a more cleaner approach as it actually resets all the locations in the IR Buffer array that were populated by the most recent IR Code signal without leaving anything to chance.
Enhancement 2 (More involved): Repeating IR Signal Transmission for certain devices
The Fix in Concept has been discussed here as it's a bit more involved and will need testing.
Adding the repeat functionality to the Ardunio Sketch will mean that you will have to flash the Sketch each time you add a new device to your Home Automation System
Instead, adding this fix to the HTML SignalR client and the Python SignalR Service application makes the solution a lot more flexible. And this could be achieved in principle as follows:
Modify the SignalR HTML client to transmit repeat information to the Hub
Open the index.html and embed the repeat value in the HTML button like so: value="SMSNG-SB-PWR-ON" would become value="SMSNG-SB-PWR-ON_2_1000"
Where, 2 is the repeat value and 1000 is the delay value in milliseconds between the two repeat signals
When you click on this button, the SignalR hub will receive the Key Code+Repeat_Spec
Modify the SignalR Server side methods to parse out only the Key Code:
Modify the Python SignalR Service Application to transmit signals using the Repeat values:
Open the Python client and modify the following two functions:
def print_command_from_hub(buttonId, cmdSrc): # parse the repeat code from the buttonId value
def transmitToArduino(IRSignalCode,delim,endPrefix): # set up a while or for loop to transmit the signal at the desired frequency
As is the case with systems built the very first time, this one has a couple of issues that came out during testing.
Issue 1: Firing commands in rapid succession with delays less than a second between button clicks caused the system to become unresponsive after responding for the first couple of times.
That said, I've noticed that my TV does not respond well to its factory remote - therefore the very nature of IR communication of my TV may be a contributing factor as well.
Issue 2: The HTML screen stops responding to button clicks after a long period of inactivity
SECURITY CONCERNS
This system has been designed for local (home) network use only and does not have the necessary security safeguards in place to be used over the internet!
Therefore it's recommended that the SignalR Hub be deployed to a local machine on your local/home network.
Thanks for reading my IBLE and I hope you have fun!