This is part I of my series about connecting plugwise and nodejs. Part II is here.

I recently started (yet another) project. I want to automatically have a warmed up espresso machine when I get home. My espresso machine (Rancilio Silvia) takes about 45 minutes to warm up, which means that I need to tell the machine to switch on when I’m on my way home from work.

Now, I could do it with timers, or have a web interface that I manually interact with, but that feels very wrong.

Instead, I want to make a solution that somehow “knows” that I’m coming home soon, and turns the machine on.

In essence, thats my project. Or at least my first step. Or do I mean a part of a step?. Anyhow, I will try to cover the different steps in some blog posts the coming months. This post is #1.

##Goals The ultimate goal is to control my espresso machine with some kind of smart engine. Besides that, these are my goals:

  • Create a nodejs API to control plugwise circles (power on and off, read power usage, etc)
  • Make the API work asynchronous but making sure commands are run properly
  • Create a REST api when the basic API is doneCreate a configurable rule system for controlling appliances
  • Create a web interface the system

##Initial preparations First of all, I need to create a bridge between the computer and the real world. There are a couple of good hardware solutions to control appliances and lamps from a computer, and I decided to go with Plugwise. I like their hardware, their finish and that each “circle” (i.e. each control point) can both read power usage and switch an appliance on or off. What I don’t like about it is that they only have a Windows app to control and read their system. Then again, if they had a nodejs API already, half my work would have been finished and I wouldn’t have anything to do.

Luckily for me, some people have analyzed the protocol used by plugwise, and created som unix utilities for communicating with the different parts. There are php APIs and python APIs already out there. But since I’m a nodejs fan, I HAVE to create a nodejs API. And to create the API I first need to communicate with the different parts of the plugwise system.

##Step A After reading the excellent plugwise unleashed document by Marteen Damen I understood that the communication is done by a serial port (a virual one, since the control stick is USB) and how the different basic commands work. I also used the python utilities to test that I acually could talk to my circles.

I had some initial problems because my os x lion didnt reconized the usb stick. But after installing the correct drivers on my mac from Future Technology Deviced International, I could control a lamp with the python scripts. (by the way, it’s a magic feeling to send commands from the command line and control something in the real world).

As soon as I got it working, I created a new nodejs project (i.e. I created a new folder), installed serialport with npm (I love npm, npm install serialport), installed crc (npm install crc) to make checksums for the protocol and I began to test.

After the usual errors and typos I had some code that could control an appliance. Here is a rough example:

plugwise.init();

setTimeout(function(){

    turnOff(appliances.espressoMachine);.

    setTimeout(function(){
        turnOn(appliances.espressoMachine);.
        process.exit();
    }, 5000);

}, 1000);

I would never have guessed that after a couple of hours I was already controlling my machine with nodejs. It’s amazing that so many people have made it so easy with different packages to get directly into whatever you want to do. Need to communicate with a serial port? Install serialport. Need to make checksums? install crc. It couldn’t be simpler.

So, this “proof of concept” was a success. I have some ugly code that already communicates with plugwise. Next step is turning it to a beautiful API with callbacks and stacks.

If you want to look at my rough example code, you can download it here. I’ll put the API on github when its presentable.