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

 

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

 

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.

  • Programming Snapshot – Driving Data

    A connector plugged into the diagnostic port of Mike Schilli's cars sends current information such as speed, acceleration, and fuel economy via the mobile phone network to a cloud service. An app and a programmable API read out the data and provide stunning visualizations.

  • 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.

  • Tasmota

    Flashing IoT devices with new firmware lets you wield control and keep them out of the cloud.

  • FOSSPicks

    Graham checks out Sigil, Dragonfly Reverb, LabPlot 2.8, Node-RED, batufo, rg3d, and much more.

comments powered by Disqus
Subscribe to our Linux Newsletters
Find Linux and Open Source Jobs
Subscribe to our ADMIN Newsletters

Support Our Work

Linux Magazine content is made possible with support from readers like you. Please consider contributing when you’ve found an article to be beneficial.

Learn More

News