Introduction

The Chinese company Itead has produced quite a few interesting IOT devices recently. I already have a few ESP8266 SonOff devices in my home which are easily programmable with the Arduino IDE. Cheap, well made and easily hackable. I was quite excited to see they recently added a nice plug socket (the S20 WiFi Smart Socket, not quite as cheap but still well made and hackable) that can be switched via Wifi, especially because it contains an ESP8266 just like the SonOff switches. I ordered a few via Ali Express when they came out.

The switch is preprogrammed to use itead’s cloud service, but that’s not what we want. We want to program it ourselves. I’ll show you how to mod the socket to make it easily programmable from the Arduino IDE. I’ll also show how to make an plug-and-play FTDI adapter for it and finally I’ll show you how to easily program it with the event-driven Automaton framework.

The sockets contain an ESP8266 wifi-enabled micro controller. The button is connected to GPIO pin 0, the relay that switches the socket is connected to pin 12 and there’s an extra green status led (ring around the button) that’s connected to pin 13. When the relay is closed the led ring lights up blue.

There’s only a EU version of the socket on the Itead site, but other regional versions are available from Ali Express.

The PCB contains 4 pads marked VCC/Rx/Tx/GND to power the ESP8266 externally and program it via a serial cable. I modded my sockets to make these connections accessible from the outside of the socket without having to take it apart. The bottom of the socket looks like this:

Modification overview

My modification adds a female header that sticks out of the bottom half of the case.

I use a Sparkfun FTDI Basic 3v3 adapter (USB to UART) to interface with my laptop.

I could easily connect the socket’s header to the FTDI’s header with jumper wires, but I tend to forget pinouts and mix up Rx & Tx pins, so I made an small custom adapter instead that’s almost impossible to plug in incorrectly. The result looks like this when programming the socket.

Modifying the switch socket

This is a good time to warn you that we’re modifying a live high voltage AC device. I take no responsibility if your house burns down or you electrocute yourself or others with it.

First we’re going to modify the switch socket. The little red sticker on the bottom of the socket hides a phillips screw. Take it out and pry the two halves of the case apart. I can do it with my fingernails but YMMV. Now the inside with the PCB becomes visible.

On the lower left you can see the pads that we’re going to connect the header to. The square pad is marked VCC. The PCB is fixed to the bottom half of the case with two phillips screws, remove them and place the PCB upside down on your work surface.

Now solder the 4 pin female header on the lower left to the PCB so that it sticks out towards the bottom of the PCB like this.

Make a rectangular hole in the bottom half of the socket case to match the header. I used an electric drill with a 4mm bit. The result is not particularly pretty but it works and fortunately the socket is usually with its back to the wall.

Put the socket back together (make sure you line up the button and the led ring) and admire the result. It should look something like this:

Creating the FTDI adapter

The header pins that now stick out of your socket are almost in the right order for the Sparkfun FTDI basic, so it’s easy to create a custom adapter for our sockets. I used a piece of stripboard, a piece of straight male header (4 pins) to connect to the socket and a piece of angled male header (6 pins) to connect to the FTDI converter.

The connections are almost straight though, it’s just the GND that’s on the opposite side. I cut through the strip that connects to the DTR pin (1) on the FTDI and made a wire connection from pin 6 on the FTDI (GND) to the strip where the GND (top) pin of our socket header connects.

This is what it looks like when connected to the FTDI basic.

Programming the socket

Now we have the complete hardware setup required for programming the switch socket. Connect the FTDI interface with the adapter and plug the whole assembly in a PC with a USB cable. Keeping the button pressed while inserting the USB plug will place it into programming mode, ready for the Arduino IDE. Make sure you don’t plug the switch into an AC outlet while you’re programming it with the computer. Bad things could occur if you try that.

Install the proper ESP board files from here. Install the Automaton framework and the Automaton-Esp8266 extension from here and here. Select the Generic ESP8266 module from the Tools > Board menu. Set the correct serial port and set the baud rate to 115200.

Now you can enter the sketch below which transforms the iTead switch into a generic switch that can be controlled via a web browser.

#include <Automaton.h> #include <Atm_esp8266.h> #define PLUG_BUTTON 0 #define PLUG_LED 13 #define PLUG_RELAY 12 #define WIFI_SSID "SSID" #define WIFI_PASSWORD "PASSWORD" Atm_esp8266_httpd_simple server( 80 ); Atm_led relay; Atm_button button; void setup() { Serial.begin( 9600 ); delay( 200 ); Serial.println( "Starting up..." ); relay.begin( PLUG_RELAY ).blink( 200, 200 ); button.begin( PLUG_BUTTON ) .onPress( relay, relay.EVT_TOGGLE ); wifi.begin( WIFI_SSID, WIFI_PASSWORD ) // Connect to the WIFI network .onChange( true, [] ( int idx, int v, int up ) { Serial.print( "Connected to Wifi, browse to http://" ); Serial.println( wifi.ip() ); server.start(); }) .led( PLUG_LED, true ) .start(); server.begin() .onRequest( "/on", relay, relay.EVT_ON ) .onRequest( "/off", relay, relay.EVT_OFF ) .onRequest( "/blink", relay, relay.EVT_BLINK ) .onRequest( "/state", [] ( int idx, int v, int up ) { server.send( String( relay.state() ) ); } ) .onRequest() // Matches on everything .reply( "<!DOCTYPE html><html><body> <h1>LED control</h1> " // Default reply "<button onclick=\"window.location.href='/on'\">On</button>

" "<button onclick=\"window.location.href='/off'\">Off</button>

" "<button onclick=\"window.location.href='/blink'\">Blink</button>

" "</body></html>" ); } void loop() { automaton.run(); }

Change the SSID and PASSWORD values in the scipt and upload it to the switch plug. When the sketch runs it will display its IP address in the serial monitor. Use the ip address to access the controlling webpage.

Now unplug the programmer from the switch and instead plug it into a wall socket. Connect whatever device you want to control with it and control it via the webpage. The switch can be controlled by calling the URL http://1.2.3.4/cmd, replace cmd with either ‘on’, ‘off’, ‘blink’ or ‘state’ and replace 1.2.3.4 with the IP address of the module.