This is a rewrite of an article I did for ”Linux Magazine”. Do you know those cheap Chinese smart plugs that you can turn on and off with the help of your smartphone? You can install Linux on them. Here's how.
The ITEAD SONOFF S20 is a fairly cheap smart plug that you connect to a wall plug and then stick some device in it and you can turn on and off said device via a smartphone app for iOS or Android. But if you want to do the same (and more) from your computer you can install Linux on it. Buy a SONOFF S20 and a $1-$2 FT232RL USB To TTL 3,3V FTDI. You will also need a screwdriver, four Dupont wires, a solderling iron, some pins and a MiniUSB cable.
If the FTDI controller has a 3,3V/5V switch turn it to 3,3V. Do not use 5V for this. Connect the Dupont cables to four pins:
Black to”GND”, orange to ”5V”, yellow to ”TXD” and white to ”RXD”.
Now use a screwdriver to open up the smart plug. There's a screw under the label on the back. Inside, near the single button, you will find four holes.
You will either need to solder a four pin connector to them or solder the wires directly to the holes.
Assuming you opted for pins connect the other ends of the Dupont cables coming out of the FTDI like so:
- GND to GND
- VCC to 5V
- TXD to RDX
- RDX to TXD
Now plug in your USB cable into your Linux laptop. The green light of the plug should turn on.
Push the button on the smart plug and keep it pushed then take out the USB cable. Wait two or three seconds then re-insert the USB cable. Wait two more seconds and you can release the button pressure. The LED should have now turned blue, meaning the SONOFF S20 entered its programable mode.
If you do a lsusb in your Terminal you should see the FTDI listed there:
Do a dmesg | grep tty in your console to see where exacly is your USB device located. In my case it was at /dev/ttyUSB0.
Now let's download a Python script that will help us write new Linux firmware to the Sonoff S20:
wget -c https://raw.githubusercontent.com/letscontrolit/ESPEasy/mega/test/esptool.py
The pyserial Python package is needed so also do a
pip install pyserial
Download this archive which contains the 2.0.0 dev 12 version of the ESPEasy firmware (although new version are available by now). Unzip it into a folder. inside, among other files, you will find one named ESPEasy_v2.0.0-dev12_normal_1024.bin. This is the one that we're going to use.
Make the Python script we're downloaded earlier executable with
chmod +x esptool.py
Assuming your USB device is on /dev/ttyUSB0 you can now write the firmware with
sudo ./esptool.py --port /dev/ttyUSB0 --baud 115200 write_flash 0x000 ESPEasy_v2.0.0-dev12_normal_1024.bin -fs 8m
Wait for the firmare to be flashed onto the smart plug:
When the process is done you can now disconnect the USB cable and reconnect it after a second or so. A new WiFi network named ESP_Easy_0 should appear. If it doesn't just give it a couple of minutes to be read by your router. You can connect to this new WiFi network with the default password configesp.
Now open up a browser tab and enter the local IP address 192.168.1.205. A webpage looking like this should be now visible:
You will be prompted to connect to your local WiFi SSID and you can pick yours from a list of available SSIDs:
You can now configure a new IP address for the smart plug, one that is on the same network as your router. You can also give the Sonoff S20 a name so you can identify it on your Wireless network list:
Now to make the smart plug accept commands from Linux.
Switch to the Devices tab and make the following changes to it:
Click Submit then go again to the Devices tab and now create another entry that looks like this:
The setting window differs a bit in the new version of the firmware but you can write down the settings above and use them the same way. In the end you should have two entries like this:
Now go to the Tools > Advanced tab and be sure you tick Rules. A new tab called Rules should now appear and in it copy/past the following:
On Button#State=1 do if [Relay#State]=0 gpio,12,1 else gpio,12,0 endif endon
Go to the Hardware tab and make it look like so:
Add HTTP support and (optionally) Telnet support by configuring them in the Controllers tab:
To control the smart plug from Linux we have to now create a set of rules for turning it on and off. Go to the Rules tab and copy/paste the following in there:
On start do gpio,12,1 //start endon On stop do gpio,12,0 //stop endon
After you submit these new rules you should be able to open up a new browser tab and turn the smart plug on by entering in the URL address bar
and off with
Substitute 192.168.1.22 with the IP address you gave to your Sonoff S20. You can aslo use curl from the command line to do the same:
curl http://192.168.1.22/control?cmd=event,start curl http://192.168.1.22/control?cmd=event,stop
If you want to precisely program scheduled actions you can do so with new sets of rules. Here's how I make coffee with the coffee machine connected to the smart plug. It takes seven minutes for my coffee machine to make a full pot of steamy coffee. So I entered in the Rules section the following:
On startcafea do gpio,12,1 // start making my morning coffee timerSet,1,420 // 60×7=420 seconds endon On stopcafea do timerSet,1,0 // you can halt the process before the 7 minutes pass endon On Rules#Timer=1 do gpio,12,0 // stop the coffee machine endon
Now if I invoke
from the command line the machine will turn on, will start making coffee and close itself after 7 minutes.
Here are other useful examples. Ruleset for starting the smart plug at 05:45 in the morning:
On Clock#Time=All,05:45 do gpio,14,0 endon
...or only on Sundays
On Clock#Time=Sun,08:30 do // Sundays at 08:00 in the morning gpio,14,0 endon
...or during certain times of the day:
On Pir#Switch=1 do If %systime% < 13:00:00 // start at 13:00 Gpio,16,1 Endif If %systime% > 16:24:00 // stop at 16:24 Gpio,16,1 Endif Endon
You can program your heater to turn on or off at certain times in Winter. I use it to turn on air conditioning at my Psychotherapist office at certain times on hot Summer days, before arriving there and before a client has scheduled a meeting with me. Another one I use at home to turn on via a BASH script my Raspberry Pi Linux cluster located in the other room
A Sonoff S20 goes for about $10 and it comes with US or EU plugs. There are line variations that you connect directly to your device's cables and they work on the same principle and the same Linux firmware.