GeekHouse

Musings of a Geek Wannabe

Controlling The World from the IPad

No Comments »

I recently acquired an IPAD and in my quest to take over the world control my immediate environment I figured that it makes a pretty good tool. Unfortunately without jail breaking the device or paying lots of money for a SCADA app, there aren’t a lot of development possibilities for the IPAD.

After a bit of research I managed to find a app called Net Remote. It’s essentially a remote control app which connects to a tcp server on the network. Whenever a button is pressed it sends a message to the server and the server performs an action. Another great feature of this app is that you can create custom layouts for it, basically design the remote with your own custom buttons and designs.

The only drawback of this application is that the server side applications are created for a mac (no windows/linux) and also don’t really have the functionalities that I am interested in, so I decided to develop my own.

To make the process easier I created a small basic class that creates a socket and listens for incoming commands from the remote. It generates an event for each incoming action. This class is by no means complete (error checking,etc) but you get the idea on how it works.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using System.Windows.Forms;

namespace tcpservertest
{

public class tcpserver
{
  private TcpListener tcpListener;
  private Thread listenThread;

  // delegate declaration
  public delegate void OnIncomingActionHandler(object sender, ActionArgs Aa);

  public event OnIncomingActionHandler OnIncomingAction;

  public tcpserver(int PortNumber)
  {
    this.tcpListener = new TcpListener(IPAddress.Any, PortNumber);
    this.listenThread = new Thread(new ThreadStart(ListenForClients));
  }

  public void StartServer()
  {
    this.listenThread.Start();
  }

  public void StopServer()
  {
    this.listenThread.Abort();
  }

  private void ListenForClients()
  {
    this.tcpListener.Start();

    while (true)
    {
      // blocking
      TcpClient client = this.tcpListener.AcceptTcpClient();

      Thread clientThread = new Thread(new ParameterizedThreadStart(HandleClientCom));
      clientThread.Start(client);
    }

  }

  private void HandleClientCom(object client)
  {
    TcpClient tcpClient = (TcpClient)client;
    NetworkStream clientStream = tcpClient.GetStream();

    byte[] message = new byte[4096];
    int bytesRead;

    while (true)
    {
      bytesRead = 0;

      try
      {
        bytesRead = clientStream.Read(message, 0, 4096);
      }
      catch
      {
        // socket error
      }

      if (bytesRead == 0)
      {
        // client has been disconnected
        break;
      }

      ASCIIEncoding encoder = new ASCIIEncoding();
      ActionArgs Aa = new ActionArgs(encoder.GetString(message, 0, bytesRead));

      if (this.OnIncomingAction != null)
        OnIncomingAction(this, Aa);
    }
  }

}

public class ActionArgs : System.EventArgs
{
  private string message;

  public ActionArgs(string m)
  {
    this.message = m;
  }

  public string Message()
  {
    return message;
  }
}
}

Enjoy :)

Share on Facebook

xPL on OpenWrt Router

No Comments »

I’ve got a Asus WL-520GU wireless router which I have previously hacked to run OpenWrt as per these instructions. I figured it would be cool if I could turn it into a small xPL server for my home automation network. This would involve putting a xPL hub on it and then developing some xPL applications and connect some xPL devices. The USB port on the router would make it alot easier to add more devices and storage space.

To get a xPL hub on there I decided to use xPL4Linux which is a xPL libary (with hub) written by Gerald Duprey. I’ve been using it on my linux home server for a while now and it works great. Since the router is now loaded with linux and the xPL Hub is pretty ligthweight I figured with the right toolchain it should work.

I’m not going into too much detail as you can get more information on your router on the OpenWrt site and the Wifi Radio Project Site.

cd ~
mkdir OpenWrt
cd OpenWrt
svn co https://svn.openwrt.org/openwrt/trunk/ .
./scripts/feeds update -a
make prereq
make menuconfig
make world

After having a working toolchain for my OpenWrt router, all I had to do was modify the MakeFile in the original xPL4Linux library, compile and copy to router.
Voila, phase 1 completed. 1 running xPLHub with some xPL tools (as found in the examples folder of the library)

root@OpenWrt:~# uname -a
Linux OpenWrt 2.4.35.4 #3 Mon Dec 1 17:35:57 PST 2008 mips unknown
root@OpenWrt:~# ./xPL_Logger
00/01/03 05:27:43 [xPL_MSG] TYPE=xpl-stat, SOURCE=cdp1802-logger.C0A8005000wthu2t, TARGET=*,, TYPE=app
00/01/03 05:28:00 [xPL_MSG] TYPE=xpl-cmnd, SOURCE=cdp1802-xplsend.default, TARGET=*,, TYPE=basic
00/01/03 05:28:00 [xPL_MSG] TYPE=xpl-cmnd, SOURCE=cdp1802-xplsend.default, TARGET=*,, TYPE=basic
00/01/03 05:28:01 [xPL_MSG] TYPE=xpl-cmnd, SOURCE=cdp1802-xplsend.default, TARGET=*,, TYPE=basic
00/01/03 05:28:01 [xPL_MSG] TYPE=xpl-cmnd, SOURCE=cdp1802-xplsend.default, TARGET=*,, TYPE=basic
00/01/03 05:28:02 [xPL_MSG] TYPE=xpl-cmnd, SOURCE=cdp1802-xplsend.default, TARGET=*,, TYPE=basic
00/01/03 05:28:02 [xPL_MSG] TYPE=xpl-cmnd, SOURCE=cdp1802-xplsend.default, TARGET=*,, TYPE=basic
00/01/03 05:28:28 [xPL_MSG] TYPE=xpl-stat, SOURCE=gkh-smtp.tompc, TARGET=*,, TYPE=app
00/01/03 05:28:44 [xPL_MSG] TYPE=xpl-stat, SOURCE=cdp1802-logger.C0A8005000wthu2t, TARGET=*,, TYPE=app

Share on Facebook

Hello world!

No Comments »

Welcome to my new Blog. Hopefully you see some articles and informative rants about my projects …

Stay Tuned

Share on Facebook
www.flickr.com