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
-
Red Hat Enterprise Linux 9.5 Released
Notify your friends, loved ones, and colleagues that the latest version of RHEL is available with plenty of enhancements.
-
Linux Sees Massive Performance Increase from a Single Line of Code
With one line of code, Intel was able to increase the performance of the Linux kernel by 4,000 percent.
-
Fedora KDE Approved as an Official Spin
If you prefer the Plasma desktop environment and the Fedora distribution, you're in luck because there's now an official spin that is listed on the same level as the Fedora Workstation edition.
-
New Steam Client Ups the Ante for Linux
The latest release from Steam has some pretty cool tricks up its sleeve.
-
Gnome OS Transitioning Toward a General-Purpose Distro
If you're looking for the perfectly vanilla take on the Gnome desktop, Gnome OS might be for you.
-
Fedora 41 Released with New Features
If you're a Fedora fan or just looking for a Linux distribution to help you migrate from Windows, Fedora 41 might be just the ticket.
-
AlmaLinux OS Kitten 10 Gives Power Users a Sneak Preview
If you're looking to kick the tires of AlmaLinux's upstream version, the developers have a purrfect solution.
-
Gnome 47.1 Released with a Few Fixes
The latest release of the Gnome desktop is all about fixing a few nagging issues and not about bringing new features into the mix.
-
System76 Unveils an Ampere-Powered Thelio Desktop
If you're looking for a new desktop system for developing autonomous driving and software-defined vehicle solutions. System76 has you covered.
-
VirtualBox 7.1.4 Includes Initial Support for Linux kernel 6.12
The latest version of VirtualBox has arrived and it not only adds initial support for kernel 6.12 but another feature that will make using the virtual machine tool much easier.