Switch power outlets on and off with home automation tools
Click Clack
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.
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.
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).
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.
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
(incl. VAT)
Buy Linux Magazine
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.
News
-
Juno Computers Launches Another Linux Laptop
If you're looking for a powerhouse laptop that runs Ubuntu, the Juno Computers Neptune 17 v6 should be on your radar.
-
ZorinOS 17.1 Released, Includes Improved Windows App Support
If you need or desire to run Windows applications on Linux, there's one distribution intent on making that easier for you and its new release further improves that feature.
-
Linux Market Share Surpasses 4% for the First Time
Look out Windows and macOS, Linux is on the rise and has even topped ChromeOS to become the fourth most widely used OS around the globe.
-
KDE’s Plasma 6 Officially Available
KDE’s Plasma 6.0 "Megarelease" has happened, and it's brimming with new features, polish, and performance.
-
Latest Version of Tails Unleashed
Tails 6.0 is based on Debian 12 and includes GNOME 43.
-
KDE Announces New Slimbook V with Plenty of Power and KDE’s Plasma 6
If you're a fan of KDE Plasma, you'll be thrilled to hear they've announced a new Slimbook with an AMD CPU and the latest version of KDE Plasma desktop.
-
Monthly Sponsorship Includes Early Access to elementary OS 8
If you want to get a glimpse of what's in the pipeline for elementary OS 8, just set up a monthly sponsorship to help fund its continued existence.
-
DebConf24 to be Held in South Korea
Busan will be the location of the latest DebConf running July 28 through August 4
-
Fedora Unleashes Atomic Desktops
Fedora has combined its solid distribution with rpm-ostree system to make it possible to deliver a new family of Fedora spins, called Fedora Atomic Desktops.
-
Bootloader Vulnerability Affects Nearly All Linux Distributions
The developers of shim have released a version to fix numerous security flaws, including one that could enable remote control execution of malicious code under certain circumstances.