Enzo

Thursday, June 27, 2013

Poor mans home automation...

Hi there,

this week I just have a short post and it's not related to UI at all...
I always wanted to control a power outlet with one of my Raspberry Pi's and was looking for a solution. After searching the web I found a very nice one called Netio 230B which is in principle a power strip with 4 power outlets and a RJ45 network connector. Means you could simply connect the power strip to your home network. The Netio comes with a little webserver that you could open in your browser and control the 4 power outlets. In addition you will also find an iOS and Android app that makes it possible to control the Netio 230B. Because I would like to control the Netio 230B with one of my Raspberry Pi's I needed the possibility to somehow talk to the Netio and lucky me they support access via telnet.
To be able to control the Netio I wrote a little Java class which looks as follows...

public class Netio {
  public static final boolean PORT_ON  = true;
  public static final boolean PORT_OFF = false;
  private Socket              netio;
  private PrintWriter         out;
  private BufferedReader      in;


  // ******************** Constructors **************************************
  public Netio() {

  }


  // ******************** Initialization ************************************
  public void init(final String IP, final int PORT) throws IOException {
    netio = new Socket(IP, PORT);
    out   = new PrintWriter(netio.getOutputStream(), true);
    in    = new BufferedReader(new InputStreamReader(netio.getInputStream()));
  }


  // ******************** Methods *******************************************
  public boolean login(final String USER, final String PASSWORD) throws IOException {
    if (null == out) return false;
    in.readLine();
    out.println("login " + USER + " " + PASSWORD + "\n");
    return in.readLine().startsWith("250 OK") ? true : false;
  }

  public void dispose() throws IOException {
    if (null != out) {
      out.println("quit\n");
      out.close();
    }
    if (null != in)    in.close();
    if (null != netio) netio.close();
  }

  public boolean isPortOn(final int PORT) throws IOException{
    if (null == out) return false;
    out.println("port list\n");
    String answer = in.readLine();
    switch(PORT) {
      case 1: answer = answer.substring(4, 5); break;
      case 2: answer = answer.substring(5, 6); break;
      case 3: answer = answer.substring(6, 7); break;
      case 4: answer = answer.substring(7, 8); break;
    }
    return answer.equals("1") ? true : false;
  }
  public boolean setPort(final int PORT, final boolean ON) throws IOException {
    if (null == out) return false;
    switch(PORT) {
      case 1: out.println("port list " + (ON ? "1" : "0") + "uuu\n"); break;
      case 2: out.println("port list u" + (ON ? "1" : "0") + "uu\n"); break;
      case 3: out.println("port list uu" + (ON ? "1" : "0") + "u\n"); break;
      case 4: out.println("port list uuu" + (ON ? "1" : "0") + "\n"); break;
    }
    return in.readLine().startsWith("250 OK") ? true : false;
  }
  public boolean interruptPort(final int PORT) throws IOException {
    if (null == out) return false;
    switch(PORT) {
      case 1: out.println("port list iuuu\n"); break;
      case 2: out.println("port list uiuu\n"); break;
      case 3: out.println("port list uuiu\n"); break;
      case 4: out.println("port list uuui\n"); break;
    }
    return in.readLine().startsWith("250 OK") ? true : false;
  }

  public boolean switchAllPortsOff() throws IOException{
    if (null == out) return false;
    out.println("port list 0000\n");
    return in.readLine().startsWith("250 OK") ? true : false;
  }

}

With this little class I'm able to switch each of the 4 power outlets on or off. The Netio 230B offers some really nice commands, one of them is the interrupt command. With this command you could interrupt a power outlet that is switched on. This is especially useful if you would like to restart a computer that hangs etc. To interrupt one of the 4 power outlets just call the interruptPort(int port) method.
At last I've added a method to switch off all ports at once which might be useful sometimes.
There are many more possibilities with the Netio 230B (for example program timers etc.) but for my use case it was enough to simply switch the power outlets with my Raspberry Pi.
I just thought this might be useful for one or the other...

That's it...so keep coding...

No comments:

Post a Comment