Controlling the world with nodejs

Nodejs + home automation = scifi and good espresso

Skrivet av
Kristoffer Yi FredrikssonDigital strateg11 apr, 2014

This is part II in my attempt to create a nodejs home automation server/app for plugwise.

Here is a quick recap: My espresso machine takes about 45 minutes to warm up. I want it to know when I’m on my way home and automatically turn itself on about 45 minutes before I enter my front door. Thanks to 24HR, I got sponsored with some plugwise plugs to begin my automation project.


I always try to approach my side/hobby projects as any other project I do for our clients. I strongly believe in approaching whatever you want to do in a way that you can reuse it, scale it or make it open source. It forces you to think a little more and write better code.

So, with that in mind, and with my goal to make a simple system for controlling appliances, the first natural step is to make an API that is easy to understand and use. The second step is to create a server that uses the API and the third is to let my phone talk to my server and tell it where I am. This part will focus in the API.

The plugwise system works by talking to each relay, or circle, through a wireless mesh-network. Each relay has a MAC address and through it you can power on or off or read the power usage.

So you have a couple of endpoints with unique identifiers that you want to control and read.

A good way to start making an API is to look at what people find familiar. And one of the most familiar syntaxes in the javascript community is jquery.

So if you in jquery want to read the value of an dom element, you simply write $(“#foo”).val();. Since we have the same kind of reference for our appliances, we can create an API that works similarly:

Since we are working in nodejs/javascript, the most obvious thing to implement is to have each command call a callback whenever it's ready.

The simple serial communication from your computer to the relays won't let you just send commands and receive them through callbacks, so that part has to be built into the API. You have to create a queue that only sends one command until it gets its result (I tried to send many and wait, but it didn't work so well). Also, if you want to simulate a jquery style, you’ll have to call the callback with the correct scope so you can continue easely:

So far...

Here is how the API currently works:

I really like the simplicity of this. With 4 rows of code you are able to control whatever appliance you want. Of course, to have full use of it you will have to write more, but at least this makes it very simple to understand.

Currently, my API will only allow to power on and off and read the information of a relay. Power usage isn't implemented yet.

If you want to try it out just install it with npm (npm install plugwisejs) or check it out on github

That's it for now. Next time I’ll write more about the server. Currently I have a working version that at least shows all appliances and lets me control them.