Controlling your WEMO switches

WEMO switches might don’t need an introduction. If you haven’t them sooner or later, you will. It’s just matter of time. In a nutshell, they are IoT devices that allow you from your smartphone or other smart means to turn on and off lights from anywhere.

At their core, they are UPNP devices. Which means knowing their protocol and syntax to make the calls you can interact with the devices via anything that you can code with. Today I choose Node JS.
If the work UPNP sounds foreign to you then here is your cue:
Universal Plug and Play (UPnP) is a networking technology used to discover devices and services on a network.  Chances are, if you have any sort of device on your home network it is probably a device that understands UPnP.  UPnP uses HTTP over UDP, SOAP, and XML for performing its various interactions.  Finding a device involves sending out a discovery packet (using SSDP) to a multicast address and waiting for responses.  Within the response, a device description and interface is defined.  From there, an application can use the interfaces provided by the device to configure or control it.  Furthermore, devices can subscribe to other devices to be notified when various state changes.
source - thanks Brendon!
There’s a great and kept in order library for Node JS called wemo-client. It gives you the basic essence, but it is enough to get you going. Of the shelf needs a bit of love and with this post I give you everything that you need to discover your switches, turn them on/off with some bonus features as well.
The wemo-client manages beyond the switches. However, the architecture in doing so is pretty much the same. Drop a line to Timon Reinhard for bringing all that together. Thanks, Tim!
Here is what we are going to put together:
  1. Install the wemo-client library
  2. Discover all the switches
  3. Store their information into a JSON file for later retrieval, so to avoid the discovery every time that you want to access to a specific switch
  4. Change the state of that switch
Assuming OS X as a platform (it works everywhere else the same though). From terminal type:
npm install wemo-client
That places the library into node_modules
The discover done using this
function discoverCompleted(deviceInfo) {
   var client = wemo.client(deviceInfo);
   // turn on the switch
   client.setBinaryState(1);
   // wait for the state to change and then exit
   client.on(‘binaryState’, function(value) {
       process.exit(0);
   }
  )};
 Finally to change the state of the switch you simply call this
client.setBinaryState(1);
To avoid to perform the discovery every time you can create the client object that contains the following information:
  1. IP Address of the switch
  2. Port number
  3. setup.xml
That can be done by serializing the deviceInfo object and then de-serialized and fed to wemo.load method. Therefore instead of using this
wemo.load(“http://192.0.1.85:49154/setup.xml”, discoverCompleted);
you would use this
var deviceInfo = JSON.parse(data);
wemo.load(deviceInfo, discoverCompleted)  (abstract look at the code)
DiscoverCompleted is the callback function that performs anything that you want to.
The code that put all this together is Github. Fork, share comment away.

Comments

Popular posts from this blog

Postgres on Synology

The Making of Basculo

Build an independent watch app - Part II