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)