Home Automation Kit – Part 1 – The wonders of the .NET framework

Home Automation Kit – Part 1 – The wonders of the .NET framework

Ok, so I have been working on a new project at home to develop my first home automation kit. Why? The biggest reason being that I want to better manage my home energy use – plain and simple. So what do I need exactly? Well, I don’t have the entire exhaustive list planned out yet and it’s going to be a work in progress until I fine tune the kit and how it interacts with the appliances in my home.

What I do definitely know is that I need a base kit – a base station that talks to other devices in my home. And so, I will start off with this.

A typical base kit would consist of electronic board with an OS running on it that will allow it to performs multi-tasking and standard communication with devices that are managed. For practical purposes, I want to keep the system cost to a minimum.

So, I decided to use a Netduino 3 (with supported Wifi) as the base kit because it offers the advantage for quick prototyping using the .NET framework with minimal complexity for a total cost of 60 dollars.

The base kit will transmit all data to my PC at home and be managed and programmed through the internet from a custom iPhone app. In order to make sure the base kit is working properly, it will send it’s geo-location to the iPhone app. This article focuses on a geo-location based beacon to allow the iPhone to make sure that it is being operated at my home.

The C# code for the same has been provided below:


using System;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using Microsoft.SPOT;
using Microsoft.SPOT.Hardware;
using SecretLabs.NETMF.Hardware;
using SecretLabs.NETMF.Hardware.Netduino;
using System.IO.Ports;
using System.Text;

namespace HomeAutomationBaseKit
{
public class Program
{
private static SerialPort GPSerial = new SerialPort(SerialPorts.COM1, 9600);
private static OutputPort led = new OutputPort(Pins.ONBOARD_LED, false);

public static void Main()
{
GPSerial.DiscardInBuffer();
GPSerial.DiscardOutBuffer();
GPSerial.Parity = Parity.None;
GPSerial.StopBits = StopBits.One;
GPSerial.DataBits = 8;
GPSerial.BaudRate = 9600;
GPSerial.Handshake = Handshake.None;
GPSerial.ErrorReceived += new SerialErrorReceivedEventHandler(GPSerial_ErrorReceived);
GPSerial.DataReceived += new SerialDataReceivedEventHandler(GPSerial_DataReceived);
GPSerial.Open();

// write your code here
//blink the on board led five times
while (true)
{
led.Write(true);
Thread.Sleep(100);
led.Write(false);

Thread.Sleep(1000);
}
}

static void GPSerial_ErrorReceived(object sender, SerialErrorReceivedEventArgs e)
{
;
}

static void GPSerial_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
try
{

Thread.Sleep(100); //wait of 100 ms calculated as per the maximum 10 Hz data update cycle by the GPS unit.

string lat = string.Empty;
string longt = string.Empty;
string lat_dir = string.Empty;
string long_dir = string.Empty;
int BytesToRead = GPSerial.BytesToRead;
byte[] DataReceived = new byte[GPSerial.BytesToRead];
GPSerial.Read(DataReceived, 0, BytesToRead);

char[] cc = Encoding.UTF8.GetChars(DataReceived, 0, DataReceived.Length);

string outputFromGPS = new string(cc);

int StartIndex = outputFromGPS.IndexOf("$GPGGA");

if (StartIndex > 0)
{
outputFromGPS = outputFromGPS.Substring(StartIndex);
}

string[] values = outputFromGPS.Split(',');

//Analyzing the GPGGA values
if (values.Length > 6)
{
switch (values[3])
{
case "N":
lat_dir = "";
break;

case "S":
lat_dir = "-";
break;
}

switch (values[5])
{
case "E":
long_dir = "";
break;

case "W":
long_dir = "-";
break;
}

double latf = double.Parse(values[2]) / 100;
double longf = double.Parse(values[4]) / 100;

//locate decimal point
int latf_index = latf.ToString().IndexOf('.');
int longf_index = longf.ToString().IndexOf('.');

//degree part of the calculation
string latitude = lat_dir + latf.ToString().Substring(0, latf_index - 1);
string longitude = long_dir + longf.ToString().Substring(0, longf_index - 1);

//min and second part of calculation
latf = double.Parse(latf.ToString().Substring(latf_index + 1, 2)) / 60 + (double.Parse(latf.ToString().Substring(latf_index + 3)) / 100) / 3600;
longf = double.Parse(latf.ToString().Substring(latf_index + 1, 2)) / 60 + (double.Parse(latf.ToString().Substring(latf_index + 3)) / 100) / 3600;

latitude += latf.ToString();
longitude += longf.ToString();

Debug.Print(latitude + "," + longitude);
}

Debug.Print(outputFromGPS);
}
catch(Exception e1)
{
Debug.Print(e1.Message);
}
}

}
}

The next step with my home kit will be outlined and described in Part 2.

 

– Subramanian Ramachandran

Leave a Reply

Your email address will not be published. Required fields are marked *