Switch power outlets on and off with home automation tools

Click Clack

Article from Issue 189/2016
Author(s):

The "If This Then That" web service lets you simply click together home automation components, instead of laboriously integrating them via their APIs. However, you still have to open the hood for the nitty gritty where applicable.

What's that? There's a gathering storm, and suddenly, as if by magic, the outside shutters on the house are pulled in? Oh, this must be home automation at work! Unfortunately, it's not that easy to integrate individual components in a smart home into a coherent overall system; many manufacturers like to do their own thing and deliberately don't play well with gadgets manufactured by the competition.

WeMo

I recently bought the WeMo Switch [1] on a whim (Figure 1). It can be used to switch electrical devices on and off, both mechanically and by using the mobile phone app either via local WiFi or over the Internet.

Figure 1: The WeMo Switch powers electrical devices on and off using the WeMo app.

The remote switch generates its own WiFi signal with the identifier WeMo-xxx after plugging it into the wall socket. After downloading the iOS or Android mobile app (Figure 2) onto your smartphone and adjusting your WiFi setting to point to the temporary WeMo wireless signal, the app connects to the switch and prompts you to enter the SSID and password of your home WiFi.

Figure 2: The app controls the WeMo Switch over WiFi or the web.

The application sends the data to the minicomputer in the switch, which then accesses the Internet via the home WiFi and automatically logs into the Belkin WeMo service. Neither an email address nor registration is required – everything works anonymously (well, except that WeMo might know your home WiFi credentials).

After a successful setup, the app communicates with the WeMo switch either via WiFi or, if the mobile phone is outside its range, via the service provided by WeMo's parent company, Belkin, on the open Internet (Figure 3).

Figure 3: The smartphone app communicates with the WeMo Switch located behind a firewall via a server on the open Internet.

Tapping a green button on the app to deploy the switch gets old pretty quick, but you also have the option to flip it on or off programmatically in a scripting language, because who in their right mind can afford to take their hands off the keyboard just to turn on the light? In all seriousness, only by connecting the switch with sensors in the house and using a bit of programming logic can you create a "smart home" that, for example, turns on the outside garage light when it's both dark and your smartphone's Bluetooth signal is approaching.

On/Off Command

The WeMo switch runs a small web server connected to the home WiFi, so a simple Perl script (e.g., Listing 1) [2] can connect to and operate the mechanical switch with HTTP requests. The program uses the Power::Outlet::WeMo CPAN module, whose constructor receives the IP address of the switch and then communicates with the device's server behind the scenes as an HTTP client using the on() and off() methods.

Listing 1

wemo-onoff

01 #!/usr/bin/perl -w
02 use strict;
03 use Power::Outlet::WeMo;
04
05 my $lamp = Power::Outlet::WeMo->new(
06     host => "192.168.1.139" );
07
08 $lamp->on;
09 sleep 1;
10 $lamp->off;

The module also supports the methods switch() (for switching to the opposite state) and query() (for requesting the current state). The whole thing could be done pretty easily using a simple web client, but the module abstracts the underlying URLs, thus making programming convenient and tidy.

WeMo Vobiscum?

The wireless router dynamically assigns the switch an IP address. So, to make sure the script will find the device, even after a reboot, it is advisable to configure a static lease in the router by adding the device's MAC address. However, WeMo implements the UPnP protocol, so the script in Listing 2 can also learn its IP address with the CPAN module Net::UPnP::ControlPoint.

Listing 2

wemo-search

01 #!/usr/local/bin/perl -w
02 use strict;
03 use Net::UPnP::ControlPoint;
04
05 my $upnp = Net::UPnP::ControlPoint->new();
06
07 my @devices = $upnp->search(
08     st => 'upnp:rootdevice',
09     mx => 3 );
10
11 foreach my $device (@devices) {
12     print $device->getdevicetype(), "\n";
13     print $device->getfriendlyname(), "\n";
14     print $device->getssdp(), "\n";
15 }

As a search target, the script sets the parameter st to the value upnp:rootdevice, specifying that it is interested in all UPnP root devices in the network. Using the parameter mx and the value 3, it sets the maximum wait time to 3 seconds, until it aborts waiting for a device to report its status. At the time of testing, I had only a single UPnP device on my network; the output in Figure 4 shows, among other things, that the UPnP query found the switch's web server at IP 192.168.1.139.

Figure 4: The script launches a UPnP query on the WiFi and finds the WeMo switch at IP 192.168.1.139.

Because the switch can be actuated via the Internet as well as on the local LAN, the device can get around the local router's NAT firewall by speaking the STUN protocol [3] with a Belkin server on the Internet.

Of course, this isn't exactly safe, because a small error in the implementation could suddenly produce a huge dark net for remote-controlled electrical devices on the open Internet [4]. The operation of critical components such as ground-to-air defense systems, sauna heaters, or dentist drills must therefore be strongly discouraged.

Buy this article as PDF

Express-Checkout as PDF
Price $2.95
(incl. VAT)

Buy Linux Magazine

SINGLE ISSUES
 
SUBSCRIPTIONS
 
TABLET & SMARTPHONE APPS
Get it on Google Play

US / Canada

Get it on Google Play

UK / Australia

Related content

  • Programming Snapshot – Power Outages

    A power failure can cause the IQ of a smart home to plummet suddenly. An emergency power supply and a script on the SmartThings platform can prevent a total outage and inform the owner. The polyglot Perlmeister embarks on a foray into the territory of the Groovy scripting language in this issue.

  • Universal Plug and Play

    Universal Plug and Play provides an easy framework for seamless integration of network devices. Learn how to build your own UPnP solution using the open source BRisa framework.

  • Giada

    Giada is a small piece of musical software that can be used in many ways. Mainly designed for working with loops, Giada also can be used as a drum machine or be flexibly integrated into your workflow, thanks to VST and MIDI support.

  • Video Streaming

    Thanks to protocols such as UPnP and DLNA, video, audio, and photos are distributed to TVs, gaming consoles, PCs, and smartphones without the need for configuration. Linux as a central media server is a welcome guest.

  • Security and SOHO Routers

    Home and small office networks typically place their security in the hands of an inexpensive device that serves as a router, DHCP server, firewall, and wireless hotspot. How secure are these SOHO router devices? We're glad you asked …

comments powered by Disqus

Direct Download

Read full article as PDF:

Price $2.95

News

njobs Europe
What:
Where:
Country:
Njobs Netherlands Njobs Deutschland Njobs United Kingdom Njobs Italia Njobs France Njobs Espana Njobs Poland
Njobs Austria Njobs Denmark Njobs Belgium Njobs Czech Republic Njobs Mexico Njobs India Njobs Colombia