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
-
Systemd Fixes Bug While Facing New Challenger in GNU Shepherd
The systemd developers have fixed a really nasty bug amid the release of the new GNU Shepherd init system.
-
AlmaLinux 10.0 Beta Released
The AlmaLinux OS Foundation has announced the availability of AlmaLinux 10.0 Beta ("Purple Lion") for all supported devices with significant changes.
-
Gnome 47.2 Now Available
Gnome 47.2 is now available for general use but don't expect much in the way of newness, as this is all about improvements and bug fixes.
-
Latest Cinnamon Desktop Releases with a Bold New Look
Just in time for the holidays, the developer of the Cinnamon desktop has shipped a new release to help spice up your eggnog with new features and a new look.
-
Armbian 24.11 Released with Expanded Hardware Support
If you've been waiting for Armbian to support OrangePi 5 Max and Radxa ROCK 5B+, the wait is over.
-
SUSE Renames Several Products for Better Name Recognition
SUSE has been a very powerful player in the European market, but it knows it must branch out to gain serious traction. Will a name change do the trick?
-
ESET Discovers New Linux Malware
WolfsBane is an all-in-one malware that has hit the Linux operating system and includes a dropper, a launcher, and a backdoor.
-
New Linux Kernel Patch Allows Forcing a CPU Mitigation
Even when CPU mitigations can consume precious CPU cycles, it might not be a bad idea to allow users to enable them, even if your machine isn't vulnerable.
-
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.