Running EmonESP on the Sonoff S20 smart plug

This development is part of the work on demand side response explained in more detail in the forum post here: OpenEnergyMonitor Demand Side Response Development.

Its also documented in the EmonESP github, but I thought I would share it as a forum post as it may be a topic of more general interest:

This guide details how to use EmonESP on the Sonoff S20 smart plug. The SonOff can then be turned on and off via MQTT or/and used in conjunction with the emoncms demandshaper module for appliance scheduling based on the best time to use from a grid or/and carbon perspective. See:

Sonoff S20 programmer:

A standard USB to UART programing cable can be used to upload firmware to the SonOffS20 smart plug. Connect up power and RX/TX lines to the 4 pin programming connector on the plug:

To upload firmware, hold down the push button, reset the power to the plug (unplug and plug the programmer back in) and then keep holding down the push button until the firmware upload progress starts.

Safety: Disconnect the plug from the mains when programming
It’s advised not to connect to the mains when the case is open from a safety perspective for obvious reasons.

Firmware modifications:

Download EmonESP and switch to control_merge branch:

git clone
git checkout control_merge


Sonoff S20 smart plugs can have either the ESP8266 or ESP8285 core. I found I had to use the Arduino IDE in order to select the ESP8285 core, if someone knows how to get it working with platform io let me know.

Select in Arduino > Tools:

  • Generic ESP8285 Module
  • Flash Size “1M (512K SPIFFS)”

Compile and upload both the firmware and the sketch data.


  1. SmartPlug creates a WIFI access point, connect to access point, enter home WIFI network.

  2. To use the plug with the emoncms demand shaper module see the user guide at the bottom of the readme here:

  3. Alternatively to control the Sonoff S20 directly via MQTT. Enter the MQTT server details on the EmonESP configuration page.

To turn the plug on, publish a topic:message of the following format:

topic                     message
emon/smartplug1/status    1

To turn the plug off, publish a topic:message of the following format:

topic                     message
emon/smartplug1/status    0

smartplug1 corresponds to the node_name and node_id assigned in EmonESP:src.ino here:

That’s it, a relatively low cost WIFI smart plug controlled with MQTT using open source firmware. The Sonoff S20 plugs can be found on Amazon and Ebay for around £15.

EmonESP on the WIFI Relay module
The same firmware also runs on @Martin_Harizanov WIFI Relay module, change node_type to ‘wifirelay’ on this line: src.ino#L59


Personally I do not see this has any advantage over using Tasmota. Tasmota gives you far more control over the device, can be auto discovered and managed within Home Assistant etc etc etc. You also get precompiled bins to load if you wish.

I have a number of different Sonoff devices, Tasmota flashed and they work flawlessly.

I’d recommend buying direct from Itead as you still get great prices but often other sellers do not have the latest version so that can be a bit of a lottery (just got caught on that one :frowning:).