Controlling a cheap smart plug from Linux
Liberation
![](/var/linux_magazin/storage/images/issues/2019/225/controlling-a-smart-plug/coverstoryimage-1_225.png/751280-1-eng-US/CoverStoryImage-1_225.png1_medium.png)
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.
![](/var/linux_magazin/storage/images/issues/2019/225/controlling-a-smart-plug/figure-1/751283-1-eng-US/Figure-11_large.png)
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).
![](/var/linux_magazin/storage/images/issues/2019/225/controlling-a-smart-plug/figure-2/751286-1-eng-US/Figure-21_large.png)
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).
![](/var/linux_magazin/storage/images/issues/2019/225/controlling-a-smart-plug/figure-3/751289-1-eng-US/Figure-31_large.png)
![](/var/linux_magazin/storage/images/issues/2019/225/controlling-a-smart-plug/figure-4/751292-1-eng-US/Figure-41_large.png)
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.
![Learn More](https://www.linux-magazine.com/var/linux_magazin/storage/images/media/linux-magazine-eng-us/images/misc/learn-more/834592-1-eng-US/Learn-More_medium.png)
News
-
NVIDIA Released Driver for Upcoming NVIDIA 560 GPU for Linux
Not only has NVIDIA released the driver for its upcoming CPU series, it's the first release that defaults to using open-source GPU kernel modules.
-
OpenMandriva Lx 24.07 Released
If you’re into rolling release Linux distributions, OpenMandriva ROME has a new snapshot with a new kernel.
-
Kernel 6.10 Available for General Usage
Linus Torvalds has released the 6.10 kernel and it includes significant performance increases for Intel Core hybrid systems and more.
-
TUXEDO Computers Releases InfinityBook Pro 14 Gen9 Laptop
Sporting either AMD or Intel CPUs, the TUXEDO InfinityBook Pro 14 is an extremely compact, lightweight, sturdy powerhouse.
-
Google Extends Support for Linux Kernels Used for Android
Because the LTS Linux kernel releases are so important to Android, Google has decided to extend the support period beyond that offered by the kernel development team.
-
Linux Mint 22 Stable Delayed
If you're anxious about getting your hands on the stable release of Linux Mint 22, it looks as if you're going to have to wait a bit longer.
-
Nitrux 3.5.1 Available for Install
The latest version of the immutable, systemd-free distribution includes an updated kernel and NVIDIA driver.
-
Debian 12.6 Released with Plenty of Bug Fixes and Updates
The sixth update to Debian "Bookworm" is all about security mitigations and making adjustments for some "serious problems."
-
Canonical Offers 12-Year LTS for Open Source Docker Images
Canonical is expanding its LTS offering to reach beyond the DEB packages with a new distro-less Docker image.
-
Plasma Desktop 6.1 Released with Several Enhancements
If you're a fan of Plasma Desktop, you should be excited about this new point release.