Samstag, 23. Dezember 2017

Sonoff S20 with EasyESP and OpenHAB and Alexa


I had some time and tried to make Alexa toggle the Sonoff S20 power switch that I recently bought  on Ebay for less than 20 Euro. It uses the well known ESP8266 WLAN chip. I have never tested the original firmware from Sonoff - I flashed it with EasyESP right away.

1. Flash S20 with Easy ESP Firmware 

There is a pretty good desciption that I followed:


I will not repeat everything, but just add some points. FIRST: Disconnect the 220V/110V power before opening the device and keep it disconnected also during the flashing procedure!! See the image for the pins, which were not labeled on my S20 board. Then connect VCC, RxD,TxD,GND to the same pins on your USB-Flash stick.

PINs of Sonoff S20
Now download EasyESP - I used the stable version R120. Note, that if you are on MacOS, like I am, you cannot use the provided batch file. The easiest solution for me was to use ESPTool.py. If you have python already installed,  simply enter "sudo pip install esptool" in you terminal window.

First, put the ESP into flash mode: hold the red button on the S20 board while connecting it to the USB-flash device. To write the firmware you have to first find out the name of your USB device. Look for a device named similar to /dev/tty.wchusbserialfd120. Then enter the following line: 

sudo esptool.py --port /dev/tty.wchusbserialfd120 write_flash 0x00000 ESPEasy_R120_1024.bin


Writing the firmware should only take a few seconds. Afterwards, the S20 will reboot and boot automatically into AP mode. Look for a WLAN called "ESP-0" and connect to it. It will ask for WPA password, enter "configesp". Open your browser and you will find a webpage where you can enter the SSID and password of your WLAN network. EasyESP will now try to connect - write down the IP it will display after is successfully connected. Then reboot and connect your computer to your home WLAN again. Enter the IP you just wrote down into your browser and you will get to the ESPEasy configuration webpage.
Flash USB-Programmer

See the above link on how to configure the GPIOs for your S20 in ESPEasy. After this, you should be able to switch power on/off using the button on the S20, and also using a simple web command (replace the IP with your S20 IP):


No worry if the button does not seem to work correctly - between two button presses you have to pause for about 2 seconds (I am not sure why). 

2. Configure Openhabian
Most people suggest to use an MQTT server to bind your ESPEasy device to your OpenHAB server. I found a simpler way by simply using the HTTPBinding in OpenHAB.  So, first install the HttpBinding in OpenHab, then add the following item to your default.items file:

Switch powerSwitch "Power Switch" ["Switchable"] {http=">[ON:POST:http://192.168.178.101/control?cmd=GPIO,12,1] >[OFF:POST:http://192.168.178.101/control?cmd=GPIO,12,0]"} 
Also add this to your default.sitemap file:

Switch item=powerSwitch label="Power Switch" 

You should now be able to switch the S20 Power Plug from OpenHab, using the OpenHAB Basic UI.

3. Bind to Alexa

There is an official OpenHAB-Skill for Alexa - however, I was not able to make it work. Instead I used the Hue-emulation service that comes with OpenHAB. Simply set the["Switchable"] tag on your item and it will appear in the Alexa app,  after you search for new devices for your Smart Home. You may now use an Alexa command to switch on your power plug.