Controlling a cheap smart plug from Linux


Article from Issue 225/2019

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.

Figure 1: The Sonoff S20 smart plug is available for both European and American-style outlets. This project also requires colored cables and the 3.3V FTDI FT232RL USB to TTL converter.

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.


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).

Figure 2: Black goes to GND, orange to the 5V pin, yellow to TXD, and white to RXD.

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).

Figure 3: Layout.
Figure 4: Solder four pins to the board.

Buy this article as PDF

Express-Checkout as PDF
Price $2.95
(incl. VAT)

Buy Linux Magazine

Get it on Google Play

US / Canada

Get it on Google Play

UK / Australia

Related content

  • Tasmota

    Flashing IoT devices with new firmware lets you wield control and keep them out of the cloud.

  • Home Assistant with MQTT

    Automating your four walls does not necessarily require commercial solutions. With a little skill, you can develop your own projects on a low budget.

  • ESPHome

    With an ESP32 or Raspberry Pi Pico W microcontroller board, you can easily create your own home automation devices. Thanks to ESPHome, you don't even have to be a programmer.

  • GPIO on Linux Devices

    The general purpose input/output interface is not just for small-board computers anymore: You can use GPIO on your Linux desktop or laptop, too, through the USB port.

  • Z-Wave Home Assistant

    Z-Wave components, a RaZberry module, and the free Home Assistant software make the Raspberry Pi a powerful smart home control center.

comments powered by Disqus
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