Today I present a tutorial that was proposed to me by Bernard, a reader of DIY Projects. In this tutorial, Bernard explains how he hacked the ESP8266 from a connected SONOFF S20 plug. SONOFF specializes in the manufacture of power supply (Wi-Fi controlled plug, power supply …), relay card, etc. These devices are built around a Wi-Fi module ESP8266. SONOFF also markets its products are the bare card form to integrate into DIY home automation projects. SONOFF thought of the hackers leaving the serial port accessible on the map. To control its plug connected from Jeedom, Bernard installed the firmware ESP Easy. He also used the Rule system presented here to program the command button available on the facade. A very big thank you to Bernard for sharing his experience and the quality of his tutorial.
Home automation is gradually being democratized with the continuous appearance of new connected objects.
On the communication protocol, two technologies stand out, wifi and bluetooth, allowing direct communication with smartphones without having to go through a gateway (ex RF433, Zwave, …). Wifi has the advantage of a wider range often allowing to cover the whole of a house, but it allows above all to communicate via Internet thanks to the domestic box and its integrated router.
So everywhere, out of home, one can interact with objects connected by wifi. But the constraining side of connected objects is that each manufacturer has its dedicated app on the smartphone. The idea is to move towards wifi links easily recognized by home automation solutions. For example, Jeedom, a free open source home automation solution, continually develops new interfaces thanks to its “plugins” concept.
And for wifi there is the Espeasy plugin that allows to interact with the equipment equipped with a wifi module Esp8266 “flashed” with the firmware Espeasy.
In general, WiFi modules operate with proprietary firmware, and the ESP8266, a wifi module that is very popular among makers, can be flashed with many firmware, especially ESPEasy, which offers a user-friendly web interface. One of the basic components of the connected house is the power socket controlled to: simulate presence (light, music, …), trigger auxiliary heating, turn on the radio for its alarm clock, …
The object of this article is to transform a conventional wifi plug into a super communicating plug and capable of managing in addition various sensors: temperature, pressure, humidity, luminosity, …
On the market it seems that the SONOFF S20 is currently the only wifi plug equipped with a module Esp8266.
How to flash ESP Easy firmware on the SONOFF S20 plug
If you discover ESP Easy, I refer you to this general article before going further.
Necessary material :
The SONOFF S20 plug is supplied and configured for the E-Welink app. The flashing ESP Easy will obviously overwrite its initial configuration.
First remove the plug: remove the screw on the back (hidden by the red label), then unclip.
A connection area with 4 terminals (VCC, RX, TX, GND) is located on the electronic board; The marking is not systematic.
Next, a 4-pin header on the board must be welded to form the flash connector (welds on the reverse side), and for the applications described below.
The most tricky operation if one can say is to connect the socket properly to your PC via the FTDI serial converter with the following diagram:
|Side SONOFF S20||Side FTDI|
|VCC||VCC (be careful to set the FTDI jumper to 3.3 V)|
Install the Espeasy firmware on your PC (download the R120 version from https://sourceforge.net/projects/espeasy/)
In deziping one obtains:
Double click to launch flash.cmd
You must fill in 3 fields:
- Comport: No USB port of your PC to which the FTDI is connected (on Windows: Settings -> Connected devices -> Device Manager -> Ports)
Tip: If you have the Arduino IDE, open the IDE and go to Tools -> Port
- Flash size: 1024 (memory size of the Esp8266)
- Build: 120 (Espeasy version)
- Then Enter
Before uploading Espeasy, you must configure the Esp8266 in flash mode: before connecting to VCC, press the button on the socket (the GPIO 0 will be set to GND) and keep the power on during power-up.
Uploading is fast:
If the flashing fails, the FTDI may not supply sufficient current. In this case, power the Esp8266 VCC with a separate 3V power supply (eg 2 x 1.5V batteries) with GND pooling.
The youtube video demonstrates how to change and flash the SONOFF S20 plug.
Once the flashing is finished, Espeasy must be started and its main functionalities must be known. Everything is explained in this tutorial.
I nevertheless draw attention to the step where the screen appears with “Proceed to main config”, do not forget to reconnect on its Wifi network before launching Proceed to main config.
Now that Espeasy is installed how to drive the operation of the plug, first with the Espeasy web interface then with Jeedom.
The plug has a relay and a pushbutton for manual operation.
The relay is activated by the GPIO 12 (1 = ON, O = OFF) and the push button acts on the GPIO 0 (pressed = 0, released = 1).
To test the relay, use the “Command” function in “Tools”: enter “gpio, 12,1” then Submit; The relay slams. With gpio, 12.0 the relay returns to idle.
Let’s test the push button: nothing happens. This is normal, you have to create a program with the GPIO 0. For this we will use the programming editor Rules and create Devices.
Creation of 3 Devices:
- SWITCH, linked to the pushbutton
- GPIO12, linked to the relay
- GPIO2, virtual device for programming needs
Devices GPIO2 and GPIO12 have been created to have status feedback from associated GPIOs.
By activating the relay via the gpio command, 12.1, the device GPIO12 changes to state 1.
Let’s move on to the programming logic: pressing the pushbutton must change the state of the GPIO12 regardless of the event that created its initial state.
To familiarize yourself with Rules I recommend reading this post and https://www.letscontrolit.com/wiki/index.php/ESPEasy#Rules_page
Here’s the script you just paste into the Rules field:
on GPIO#Sw12=1 do gpio,2,1 endon on GPIO12#Sw12=0 do gpio,2,0 endon on SWITCH#Switch=1 do if [GPIO2#Sw2]=0 gpio,12,1 else gpio,12,0 endif endon
Or this from Jonathan
on SWITCH#Switch=0 do gpio 12,0 endon on SWITCH#Switch=1 do gpio 12,1 endon
One tests: it works, and the blue LED lights up when the relay is activated.
But our catch has other resources that Esp. Remember the 4 terminals of the flash connector: VCC, GND, TX, RX.
These terminals are available to power sensors, indeed TX and RX are respectively connected to the GPIO1 and GPIO3.
The list of sensors and equipment managed by Espeasy is already quite consistent: https://www.letscontrolit.com/wiki/index.php/ESPEasy#Supported_Sensors.2FActuators
These sensors and various equipment are powerful and cheap (a few euros on Amazon, Ebay, Aliexpress, …)
Note that if you configure the GPIO1 and GPIO3 in SDA and SCL (I2C communication) you can connect several sensors or devices using this communication mode: for example a BME280 weather sensor (pressure, temperature, humidity) and a 4×20 LCD or Oled SSD.
Indeed Espeasy differentiates the I2c addresses of each device (I2C Scan function from the Tools menu).
On the practical level, a 3.5 mm jack with 4 connectors can be used to properly connect the sensors.
Thus, the advantage of a SONOFF S20 thus modified can be seen, for example, in order to independently regulate the temperature and / or humidity of a room with heating equipment, a dehumidifier; The lighting according to the brightness.
An example of weather sensor tested on a SONOFF module, and the S20 jack modified with a jack.
But the best is to integrate our instrumented plug into a home automation solution.
Here again Espeasy is very user-friendly because it can communicate through many protocols: HTTP, MQTT, ThingSpeak, …
The same process obviously applies to all SONOFF products integrating a wifi module ESP8266
In home automation wifi offers a very effective mode of communication in both flow and range. Thanks to the appearance of open components such as ESP8266 it becomes relatively easy to boost the capacities of cheap basic products such as a connected plug and integrate them in open domotic solutions like Jeedom, Domoticz or Home Assistant.
- The firmware ESP Easy Mega arrives on ESP32, test on the Wemos LoLin32 Lite, novelties of the firmware
- #Hack: SONOFF S20 plug to make it a super intelligent plug with ESP Easy
- ESP Easy: flash the firmware with esptool.py on ESP8266 (GitHub)
- ESP Easy: firmware ESP8266 to create connected and home automation objects without programming
- ESP Easy: flash the firmware with esptool.py on ESP8266 (GitHub)
- Domoticz: Lua script to create a remote OLED display with ESP Easy