Home automation with Xamarin and SignalR

[dropcap]F[/dropcap]or some time now I have been expanding my knowledge of developing software for mobile; and a few years ago I stumbled upon Xamarin (formally at the time known as MonoTouch).  I had developed some cool apps for clients using the product (including ING Bank, Leicestershire Tigers and more), and was able to leverage all of my past programming in C# to hit the ground running.

I decided to share an example of how I used Xamarin to develop a simple iPhone application for controlling a lighting circuit in my home office. Here is the full background:

Through some of my past projects I have had the privilege of playing with some pretty cool hardware, and one of those bits of kit specifically being a data acquisition device that provides digital inputs and outputs (there are quite a few variations of these available on the market) which can be communicated to over RS485.  I used a Serial to Ethernet adapter with these devices, and was able to turn this into a network  controllable device, addressing and communicating to the device using ModBus protocols.

The ModBus protocol is a serial communication protocol that has become a standard for communicating to industrial electonic devices, and for the purpose of my requirements allowed for a circuit to be turned on and off.

[image type=”rounded” float=”none” src=”http://www.ricmoore.co.uk/wp-content/uploads/2015/03/IMG_2725.jpg” info=”none” info_place=”top” info_trigger=”hover”]

[alert type=”danger” close=”false” heading=”Disclaimer”]Note.  I accept no responsibility for anyone wiring these devices up, you do so at your own risk[/alert]

I used the data acquisition module because I had it left over from another project, however I could achieve the same results using a Arduino with a network shield or Raspberry Pi.  I also used 12v relays suitable for switching 240v power.  If I were to be using Arduino I could have chosen a pre-built shield to provide relay outputs.

I also used a pre-existing library I had for the implementation of the ModBus protocol, however for those interested there are some examples over on CodeProject for creating your own; an example being: http://www.codeproject.com/Tips/16260/Modbus-TCP-class

Being fortunate enough to have the space available I converted my home office to allow control of various electronic circuits; these being heating and lighting (although I plan to bring this into my house in the future).  I created a voice activated interface to allow me to control the lights with various simple voice commands (delving into various voice SDK’s wont be covered in this post), and also the ability to control the device using my mobile phone through an application I developed using Xamarin.

Historically I would have created some sort of client / server polling method to create the methods in order to turn my lights on and off, however after watching a video by Scott Hansleman demonstrating SignalR to remotely control a webcam at his home and office at Xamarin Evolve Conference, I knew that there was now an awesome .Net library that could do what I needed.  Since seeing this video and his explanation of the technology, web sockets etc…, I knew I could put this to some great use.

After some play, I created a SignalR ASP.Net application and installed it onto my server, and then setup a client to connect to the SignalR server.  After getting this working and the ability to switch the lights on and off by simply sending commands, I soon realised that to really enjoy the use of this technology I would need another method of controlling the system I had created.

I opened up Xamarin, and imported the libraries I found for SignalR and was amazed that I was able to, within a couple of minutes, start controlling my lights and heating directly from my phone.

The UI and programming was so simple for the iPhone application that I wont show it here, however as I expand the application I will throw the code up on GitHub for anyone who is interested.

The following code shows a sample of how simple using SignalR is with Xamarin (SignalR is also now available in the components section of the Xamarin site, as well as up on NuGet):

Step 1. Create two objects of HubConnection and IHubProxy

HubConnection hubConnection;
IHubProxy chat;

Step 2. In your ViewDidLoad method initialise the hubConnection with your SignalR Application

//initialise the HubConnection
hubConnection = new HubConnection ("Your SignalR ASP.Net application");


//handle the connection event
hubConnection.StateChanged += delegate(StateChange obj) {

     if (obj.NewState == ConnectionState.Connected){
         //if connected enable the UI Buttons

         InvokeOnMainThread(enableButtons);

         Console.WriteLine("Connected to SignalR Server");

     }

}

//added custom headers in attempt to add some quick and dirty security
hubConnection.Headers.Add ("Username", "<USERNAME>");

hubConnection.Headers.Add ("Password", "<PASSWORD>");


chat = hubConnection.CreateHubProxy ("ChatHub");



//Start the connection
hubConnection.Start ().Wait ();


Step 3. In your method for your buttons call a method on the SignalR application

lightsOn.TouchUpInside += delegate(object sender, EventArgs e){
    chat.Invoke("Cabin", "Lights", "On");
}

With the above sample, this assumes that you have created a method on your SignalR application to handle the above call, and then communicate that back to your client that is controlling your lighting etc….

This solution wont work straight out the box, but should serve to explain how I went about achieving my home automation system.