Controlling a cheap smart plug from Linux
Liberation
You could spend hundreds of dollars on specialized IoT appliances and fixtures, or you could just hack a smart plug and talk to it with your Linux system.
Have you ever wondered if you could use Linux to power your entire IoT house? Make it prepare your coffee in the morning – even if your coffee machine is older than you and does not have a CPU? Or maybe start the PC at work with the push of a button while you sit down at home to enjoy breakfast?
Homeowners spend hundreds of dollars on high-tech light bulbs and appliances that tie in with IoT networks. But what if you like your old appliances? What if you are concerned about the security and privacy issues of a full-blown IoT infrastructure, or maybe you just want to keep it simple while maximizing your flexibility for future customization.
The easiest and most foolproof version of an IoT network is a simple smart plug – a small device that plugs into an outlet and lets you control when the power flows. You can plug any electrical device into the smart plug and turn it on or off remotely – even if the device itself was never intended for IoT use.
This article describes how to control an inexpensive smart plug from a Linux system. Once you get control over the plug, you can use all the tools and powers of Linux to automate your electrical devices.
The solution described in this article requires a smart plug for each device, Linux, and an FTDI converter to program the smart plug. I'll use the Sonoff S20 smart plug from ITEAD as an example for this article. The Sonoff S20 [1] is a cheap Chinese smart plug that you can control via an Android application. I'll show you how to control the Sonoff S20 from an ordinary Linux computer.
For this solution, you'll need one FT232RL USB to TTL FTDI Controller [2] that can handle 3.3V. This part costs about US$4 online, and you only need one. You also need four differently-colored cables to connect the converter to the smart plug (Figure 1). The FT232RL USB to TTL FTDI controller has a switch that makes it output either 5V or 3.3V. Leave it on 3.3V; otherwise, you risk frying your smart plug or even your laptop.
The Concept
The procedure outlined in this article takes a little more trouble than most people are used to going to for a $12 smart plug, but hacks such as this one have a long history with the Linux community. This is a proof of concept, of course. A similar approach might work with a different smart plug, but you'll need to adapt the steps as necessary.
The basic idea is, you flash new firmware onto the smart plug that supports the open MQTT protocol [3], which is often used for IoT communication. Then you install the open source Mosquitto message broker [4], which speaks MQTT, onto your Linux computer. Mosquitto will manage communication between Linux and the smart plug. You can then enter a command for the smart plug directly into your web browser as a specialized URL. You can also use the Bash curl
command to send an HTTP-based command to the smart plug. I'll show you how to create rulesets that turn on the power at a specific time or after a specific interval. The ability to access the plug through Bash also means you can integrate the smart plug into scripts and add the scripts to auto-launch utilities such as cron.
Preparation
Insert a USB cable in the converter's USB slot and connect the four cables to four of the connecter's six pins. One cable should connect to the pin marked as GND, one to 5V, one to TXD, and the last one to RXD (Figure 2).
Inside the Smart Plug
Now you need to unscrew the Sonoff S20 smart plug. There is only one screw underneath the label. All you have to do after you remove the screw is pry open the plastic enclosure by inserting a butter knife or a screwdriver along the edges and gently separating them. Inside the casing, you will find a board pinned by two screws that need to be taken out so you can get access to the underside of the board. Right next to the smart plug's power button you will see four holes (Figure 3). These holes are important; you will need to connect them to the four free ends of the four differently-colored cables. For this you need four pins or four pieces of copper wire thin enough to fit through the holes. You need to solder these pins onto the board so the four cable ends can be fitted into these pins (Figure 4).
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
-
Halcyon Creates Anti-Ransomware Protection for Linux
As more Linux systems are targeted by ransomware, Halcyon is stepping up its protection.
-
Valve and Arch Linux Announce Collaboration
Valve and Arch have come together for two projects that will have a serious impact on the Linux distribution.
-
Hacker Successfully Runs Linux on a CPU from the Early ‘70s
From the office of "Look what I can do," Dmitry Grinberg was able to get Linux running on a processor that was created in 1971.
-
OSI and LPI Form Strategic Alliance
With a goal of strengthening Linux and open source communities, this new alliance aims to nurture the growth of more highly skilled professionals.
-
Fedora 41 Beta Available with Some Interesting Additions
If you're a Fedora fan, you'll be excited to hear the beta version of the latest release is now available for testing and includes plenty of updates.
-
AlmaLinux Unveils New Hardware Certification Process
The AlmaLinux Hardware Certification Program run by the Certification Special Interest Group (SIG) aims to ensure seamless compatibility between AlmaLinux and a wide range of hardware configurations.
-
Wind River Introduces eLxr Pro Linux Solution
eLxr Pro offers an end-to-end Linux solution backed by expert commercial support.
-
Juno Tab 3 Launches with Ubuntu 24.04
Anyone looking for a full-blown Linux tablet need look no further. Juno has released the Tab 3.
-
New KDE Slimbook Plasma Available for Preorder
Powered by an AMD Ryzen CPU, the latest KDE Slimbook laptop is powerful enough for local AI tasks.
-
Rhino Linux Announces Latest "Quick Update"
If you prefer your Linux distribution to be of the rolling type, Rhino Linux delivers a beautiful and reliable experience.