Sonoff - ESP
Posted by kll on August 25 2022 17:32:28
Sonoff S31

Arduino IDE / ESP8266 / MQTT + webserver + operation

Power-Meter digging

Sonoff S31 flashing

back Arduino IDE but now on RPI2

stop and check fridge

Sonoff S31

starting with a simple cheap home assistant device
Sonoff S31 On Off switch and power meter
( here for thailand use the US AC plug ( but 3pin) and use 220VAC )
even it writes about being for 120VAC 60Hz ( usa market )
but i read somewhere that statement is to be ignored.
on the other side for usa there is a S40.

i buy online for 242 THB / 6.77 EUR / delivered to my gate / cash payment /

step 1: is just use it as a power-meter with its mobile app
++ scan with mobile get linked to
eWeLink smart home app : install: 84 MB

* email
get email verification code
* give code
* password
* password
can add devices

add: give password for your WIFI
it scans
put device into plug and press 5sec the button
look the fast blue blinking
and find the installed device S31TPB(US)

* when switch on red LED ON
??? see 206.7VAC to 224VAC???
plug in FAN
/ 3 / see 0.23A
/ 1 / see 0.17A

the daily consumption works ( after some time... ) but only as kWh for day /100 days back?
while i read about Sonoff PWR2 had a hourly trend?
but its not available here anymore.
there is also a option to record power usage with a app start stop operation.

after check YOU-TUBE how to open it pls see: here

* CSE7766 power-meter
* ESP8266
so need to check if i can flash it
- - open it and find the pins
- - have a 3v3V serial-USB adapter

also read like: "pins labeled DTX and DRX? I got a i2c xxx working"
means i could connect the ADS1115 to get also VDC readings
like from a smart PS
but the big question remains, safety, as the Sonoff ESP seems not to be isolated...
could be on 220V??

Arduino IDE / ESP8266 / MQTT + webserver + operation

so why is that in the Arduino category?
on a old laptop install arduino ide 1.8.19

*** in preferences “Additional Boards Manager URLs” give the

*** in boards manager install ESP8266

*** i select a original ESP8266 ( i have for test ) board

*** libraries EspMQTTClient install

and try a mqtt code.
using my RPI3 as mqtt broker and also as Node-Red server with SQLite3 historic database
*** libraries ArduinoJson install info here
and build with it a PSemulation same as i did on RPI in python ( send DC Volt and OnOff setpoints ) and watch AC Amp and AC Watt change.
emulation as we test here the mqtt / operation / other end RPI node-red &SQLite data collection /
but without talking to the S31 power-meter chip CSE7766 in the S31
( i think i found a library for it? ingeniuske/CSE7766 )
it's just a emu / simulation.

with Arduino ide connected ESP8266, and serial monitor open:

and try do MQTT and webserver
and add webserver PS operation:

while that makes only sense with a test of a other project,
back to my MQTT Cloud
while the webserver operation would be a LOCAL remote operation ( inside your LAN WIFI )
there via mqtt cloud would be the remote remote operation in the node-red server web site / dashboard:
where it looks like:

could try a real measuring project
( i have not done over years with this ESP because its Ain is poor )

but now i have the I2C linked ADS1115
so with resistors i might be able to get
** VDC (12V to 3v3 range V divider ) ,
** ADC ?manganin shunt? in the minus path ( S31 has one for the AC current )
+ calculate WDC
estimate a WAC is difficult, the switching PS efficiency is good, but actually depends on the output voltage,
because with very small VDC the rectifier diode dominates the losses,
and efficiency goes down to 0 ( at VDC ~ 0 ) with at VDC ~ 12V it can be 0.95.
but when i used the ADS1115 it was RPI python, now i need a ESP arduino library for it.

>> may be an other time

i see a nice thing, the so called web-serial where instead of debug to USB can debug to a extra web-page.
so i might try that one day, but here it gives me a total different idea:
i have MQTT running ( and a node-red SQLite database storage on the traffic )
i should just debug to MQTT.
but suddenly i think i know nothing about MQTT and TOPIC
again my existing structure:
mtopic: "KLL/site/get" from device to world
stopic: "KLL/site/set" from world to device
with "KLL/+/get" i get data from all "sites" to data-base ( to sort it out later by "select from table where 'topic' like "KLL/siteX/get")
now i need to understand / test if "KLL/site/get" is exclusive "KLL/site/get/DEBUG"
OR "KLL/+/get" is actually same like "KLL/+/get/#" means inclusive ? good question ha ?
after i learn that i need to decide if i want DEBUG msg stored into database?
anyhow add i MUST use like
{ rec:0, dev:"DeviceToDebug", rem:"function measure rev 0.9.3", msg:"what a mess" }

and why is that so important?

the S31 has a ESP
* without usb interface, need a 3v3 usb programmer,
anyhow it might be 'live' on 220VAC
you NEVER connect to it by hardware if the AC is connected!
? in CSE7766 datasheet they show a opto coupler to the micro controller serial UART in ?out?,
but Sonoff did not do it that way?

hope i made clear what i am up to?

Power-Meter digging

writing my own Arduino code for ESP8266 here makes only sense if i can get
the lib for the power-meter installed
only then i could flash own firmware to a S31 powermeter -- MQTT
above is all old stuff, i see basic only use read, but not UART write to sensor ( like for calibration.. )
check more Tasmota cse7766

LOL, while i check on that TASMOTA code i stumbled over xnrg_30_dummy.ino
a emulated power meter what is basically that what i did with python for the PSemulator >> MQTT
and again with arduino >> ESP8266 >> MQTT same
only i had more remote operation included.

Sonoff S31 flashing

back to Sonoff S31
what Sonoff devices are doing is after pairing with the phone app the
phone and the S31 via your router communicate with Sonoff HOME/server AWS / EU /
( so not directly with one another )
with your ISP down it does not work. even your LAN is still good.
on the other side this way you can switch onoff from all over the world ( if your phone is online ).

now for the home automation that working principle is unwanted..
so they use many common devices and flash it with own code:

Tasmota vs ESPhome

( that is one of the things i also want to try, flash Tasmota ( to my ESP board and to S31 ) )

interesting checking Tasmota device list:
Sonoff S31 OK
Sonoff S40 BAD
or you can buy a thing what already has Tasmota
more easy

TASMOTA flashed to S31

how to open and flash Sonoff S31
more info to the internals:

no, i not ready to flash the S31, BUT
i flash the ESP8266 board (very old node mcu v0.9 yellow) with
using from laptop .. usb cable
( win 10 / edge || chrome browser ( firefox not work for this ))
info here
Tasmota Web Installer - flash Tasmota using a Chrome based browser for ESP82XX and ESP32

-1- select newest tasmota sensor version, install, and
-2- via local/LAN IP config as S31 with MQTT to RPI Broker

now after flashing you can directly ( via USB / serial ) setup your local WIFI connection ( ssid & password / get local IP )

or without that, tasmota will come up in AP mode:
! at start for short time ?1min? only !
Search for a Wi-Fi AP named tasmota_XXXXXX-####
(where XXXXXX is a string derived from the device's MAC address and #### is a number)
no password needed.

open in a web browser on the smartphone
where you also can set your WIFI credentials.. to bring it into your LAN.
( tasmota pre-flashed devices are to be shipped like that. )
i try to find it later, after i give wifi credentials /
no, not see the AP mode at boot, even try to power off the router ( so tasmota esp can not login at boot )
it still not show the AP wifi ssid?
? open might be that you need to press the button on power up to get the AP menu? or press it 40 sec for reset?
? also not seem to work.

( oh, there are lots of boot options about that, ( if you are in, by serial and wifi, you can adjust )
works as go menu console and give
Reset 1
** config erased and can get AP ... wifi config again
( but yes, you need to be IN to do it, anyhow its clean to ship the device)

but first i need to know what is the default ( in the provided .bin file )

so to learn i have to flash it again ( web flash laptop edge browser ) but stop when it wants to setup my wifi
remove USB from laptop / plug into charger / have mobile ready to scan close wifi ssid's
find tasmota_xxx / select No password / can give you wifi ssid password / restart
find tasmota IP in your network via mobile FING
browser connect IP and see config screen

* * here back to first web flash

* * here starts the work:
+ + select sonoff S31 ( for powermeter )
+ + select MQTT ( config your broker and topic header )
and check it:

and on RPI3 ( what is also my mqtt broker )
make a python tool to look at what the Tasmoto S31 sends:

very good, sadly it was just with the ESP board and not with the original Sonoff S31.

are we ready for the real S31?
no... i think about backup the original sonoff image
and here we try ( sure again with the ESP board only )
on win 10 laptop :
install python from
not forget to click ON [ add to PATH..]
( and can try to start python IDLE from the windows START )

open cmd window
python --version
pip --version
python.exe -m pip install --upgrade pip
pip --version


info from steve and espressif

pip install esptool
it ends up in c:\Users\USER\AppData\Local\Programs\Python\Python310\Scripts

so in cmd just type
and get all help you need

backup: --port COM11 --baud 115200 read_flash 0x00000 0x400000 backup.img
( this 'stub... running' takes long )

burning a other image ( like one made by arduino ) should be like: --port COM11 --baud 115200 write_flash 0x00000 C:\path_to\Blink.ino.d1_mini.bin


it's sonoff_S31: OPENING TIME
i not have the camera or lighting equipment to show it / better see all that tutorial or videos.

so, as you see, you see nothing,
i can not identify anything, ESP chip?, sensor chip?
now i see some drawing that for the sensor CSE7766 there should be a 1mOhm 1% current shunt resistor manganin ( Mn Cu )
with ! 3 ! connectors ? for sense amp to 'VIP' and 'VIN' and sense volt to 'V2P' via 1k / 1M ohm divider.
-a- that is from the sensors datasheet and not what Sonoff build
-b- i can't see it
( no idea if i could open the thing so i would see also the downside of the board? )
so for circuit and how the board is connected to the 3 AC pins and where the shunt resistor is?
here see it:

but the connectors: ( to USB - serial programmer )
-1- VCC 3v3
-2- RX >> TX
-3- TX >> RX
-4- nc
-5- nc
-6- GND

and now it happens as usual:
i found my old USB-serial adapter
and i found my original blog where i used it as a terminal on RPI pins
( where i could login to RPI the OLD way and even power it with the 5V from USB PC .. )
also see this original image about it from adafruit:

so its unusable for talk AND power the ESP ( need 3v3 ?)
stop here and order a new one,
unless i use the RPI for it?? see or instructables and blog.3d-logic

for the power requirements / esp8266 boards might have a 5v to 3v3 0.6 A
but the ESP is said to use max 90mA when sending WIFI.

i use: [router + + ethernet hub ]
RPI4 [] for internet ... ( i would not dare to touch the header pins / it's in a alu box with noisy fan anyhow / )
it boots PINN SD to USB3 sweet M2 SSD drive to 4 systems
make own 'pinnwheel' command to boot around remotely.

RPI3 [] for develop
usually have a USB3 SSD drive but
/ now use it for node-red & sqlite3 DB / optional local mqtt broker / on a slow USB 8GB stick system
( ++ where we tested to read with win32imager that usb stick / PC zip from 8GB down to 3.2GB / cloud share / burn SD using win32imager / boot / enlarge filesystem / mqtt crosstalk usa/thailand with using hivemq broker free account )

RPI2 [] for tinkering / install BLINKA .. use AD1115 I2C to RPI pins

so here - RPI2: i gone install what i need to talk / flash ESP8266 of Sonoff S31
-a- check RPI
cat /boot/cmdline.txt
console=serial0,115200 console=tty1 root=PARTUUID=424dcdad-02 rootfstype=ext4 rootwait quiet splash plymouth.ignore-serial-consoles

this i might have to check / change
sudo raspi-config
The serial login shell is disabled │
The serial interface is enabled

pi@RPI2:~ $ cat /boot/cmdline.txt
console=tty1 root=PARTUUID=424dcdad-02 rootfstype=ext4 rootwait quiet splash plymouth.ignore-serial-consoles

sudo apt-get install minicom

minicom -b 155200 -o -D /dev/serial0 ?/dev/ttyS0? but NOT AMA0..
stop with [ctrl][a][x][yes]

-b- check python: python 3.9.2 pip 20.3.4

-c- esptool
sudo pip install esptool
Looking in indexes:,
Collecting esptool
Downloading (259 kB)
|████████████████████████████████| 259 kB 180 kB/s
Requirement already satisfied: pyserial>=3.0 in /usr/lib/python3/dist-packages (from esptool) (3.5b0)
Requirement already satisfied: cryptography>=2.1.4 in /usr/lib/python3/dist-packages (from esptool) (3.3.2)
Collecting ecdsa>=0.16.0
Downloading (142 kB)
|████████████████████████████████| 142 kB 520 kB/s
Collecting bitstring>=3.1.6
Downloading (39 kB)
Collecting reedsolo<=1.5.4,>=1.5.3
Downloading (921 kB)
|████████████████████████████████| 921 kB 266 kB/s
Requirement already satisfied: six>=1.9.0 in /usr/lib/python3/dist-packages (from ecdsa>=0.16.0->esptool) (1.16.0)
Installing collected packages: reedsolo, ecdsa, bitstring, esptool
Successfully installed bitstring-3.1.9 ecdsa-0.18.0 esptool-4.2.1 reedsolo-1.5.4
pi@RPI2:~ $

pi@RPI2:~ $ v4.2.1
connect options

and what we want to burn? get:

ESP8266_____|_____Raspberry Pi
GND--------------|--------GND [6]
3.3V--------------|--------3.3V [1]
RXD--------------|--------UART_TXD [8]
TXD--------------|--------UART_RXD [10]

i see internet many ways to connect to that S31 ESP board
+ solder wires ( de-solder after flash )
+ solder header pins ( looks good, but de-solder a header might be more tricky
+ use this small clamps looks easy ( but not have any )
nice would be a way to have a permanent header there,
but space in that plastic box is limited.

- - ok tasmota might need to flash one time only and can update via WIFI,
but i want ( after a tasmota test ) go back to my own arduino project
means many uploads required.

i try something ? a clamp for 4 pins?
7 pins angled header,
0 is for board-stop ( pushed DOWN) ,123xx6 ( 4,5 pulled UP) with female female wires to RPI header
the perf board is for depth-stop inside the clamp ( not soldered at all )
the clamp? my wife might recognize it.

with board and 'clamp':

but when i try, the link not work,
also the port still not clear

also try to test with minicom, but even more bad because more parameter ?
and i not even know if original SonoffS31 talks there? at boot? at button press? ??

so how to narrow it down?
-1- ok solder new wires
-2- try with a easy command ( instead read write all flash ) read_mac or flash_id
-3- --before no_reset will skip DTR/RTS control signal
( only have 4 wires anyhow no RTS DTR wires )

ok, with soldered wires that's better, as i see a blue led
its bad as it never goes steady
SO CLEAR the wiring connection must be good, the 3v3 must be stronger as a RPI2 provides?
i now think that there is color on that 6 test/solder points, so cleaning or soldering just the pads
might make it possible to use my 'clamp' too?? i test if i buy a new one.

/dev/ttyS0 NO PORT
/dev/ttyAMA0 NO PORT

/dev/serial0 NO SERIAL DATA

ok disconnect 3v3 wire, keep press button and power up again, and try read something from it --port /dev/serial0 read_mac

we talk on serial0pi@RPI2:~ $ --port /dev/serial0 read_mac v4.2.1
Serial port /dev/serial0
Detecting chip type... Unsupported detection protocol, switching and trying again...
Detecting chip type... ESP8266
Chip is ESP8266EX
Features: WiFi
Crystal is 26MHz
MAC: 48:3f:da:28:e5:2f
Uploading stub...
Running stub...
Stub running...
MAC: 48:3f:da:28:e5:2f
Hard resetting via RTS pin...

try backup: --port /dev/serial0 --baud 115200 read_flash 0x00000 0x400000 S31_backup.img
read/backup flash
pi@RPI2:~ $ --port /dev/serial0 --baud 115200 read_flash 0x00000 0x400000 S31_backup.img v4.2.1
Serial port /dev/serial0
Detecting chip type... Unsupported detection protocol, switching and trying again...
Detecting chip type... ESP8266
Chip is ESP8266EX
Features: WiFi
Crystal is 26MHz
MAC: 48:3f:da:28:e5:2f
Stub is already running. No upload is necessary.
4194304 (100 %)
4194304 (100 %)
Read 4194304 bytes at 0x00000000 in 386.3 seconds (86.9 kbit/s)...
Hard resetting via RTS pin...

that is the JOB: burn TASMOTA into a Sonoff S31

gunzip tasmota-sensors.bin.gz

and burn it: --port /dev/serial0 --baud 115200 write_flash 0x00000 tasmota-sensors.bin

pi@RPI2:~ $ --port /dev/serial0 --baud 115200 write_flash 0x00000 tasmota-sensors.bin v4.2.1
Serial port /dev/serial0
Detecting chip type... Unsupported detection protocol, switching and trying again...
Detecting chip type... ESP8266
Chip is ESP8266EX
Features: WiFi
Crystal is 26MHz
MAC: 48:3f:da:28:e5:2f
Stub is already running. No upload is necessary.
Configuring flash size...
Flash will be erased from 0x00000000 to 0x000aafff...
Compressed 697376 bytes to 493078...
Wrote 697376 bytes (493078 compressed) at 0x00000000 in 44.1 seconds (effective 126.6 kbit/s)...
Hash of data verified.

Hard resetting via RTS pin...
pi@RPI2:~ $

unsolder wires ( or have a death-wish ) and test

there was a question about S31 fails / restart setpoints...
i can not answer, but as i burned TASMOTA i try:
-1- power up S31 Tasmota
-2- find with mobile " search WIFI" ssid ( AP mode )
-3- give my WIFI login
-4- get local IP
-5- config Tasmota menu and set S31
-6- test with FAN /speed 1/2/3 powermeter ok
( the power meter calibration should be inside the CSE7766 ( UART write or factory defaults! )
so not lost with flasing Tasmota to ESP8266 of S31)
-7- acc /tasmota/doc/ following defaults:
** SetOption0 1
** SaveData 1
** PowerOnState 3
so that should store current setpoints ( like power ON aka relay energized ) regulary and set after boot
-8.1- power off power to S31 ( simulate power fail ) LEDs OFF, FAN OFF
-8.2- power on >> FAN RUNs
so restart from last setpoint works good with TASMOTA ( default settings ).

i used it to monitor some power-hungry devices at my home...
but also in a followup project here

back Arduino IDE but now on RPI2

cd Downloads
mv arduinoinst_sh.txt
chmod +x

*** in preferences “Additional Boards Manager URLs” give the

*** in boards manager install ESP8266

*** libraries ArduinoJson install

*** libraries PubSubClient install

copy last project from laptop and verify here on RPI2

Done compiling
Executable segment sizes:
ICACHE : 32768 - flash instruction cache
IROM : 301912 - code in flash (default or ICACHE_FLASH_ATTR)
IRAM : 27881 / 32768 - code in IRAM (IRAM_ATTR, ISRs...)
DATA : 1548 ) - initialized variables (global, static) in RAM/HEAP
RODATA : 4560 ) / 81920 - constants (global, static) in RAM/HEAP
BSS : 26376 ) - zeroed variables (global, static) in RAM/HEAP
Sketch uses 335901 bytes (35%) of program storage space. Maximum is 958448 bytes.
Global variables use 32484 bytes (39%) of dynamic memory, leaving 49436 bytes for local variables. Maximum is 81920 bytes.

again but use
[sketch][export compiled binary]

ok, with this trick i can write Arduino code on any computer,
make the .bin and use SFTP
** like from windows use FileZilla ( i never was able to install SFTP server on windows )
** on/between RPI's i use the MC
copy it to the RPI2 to burn it to S31 ESP via UART.

and the status of the Arduino project
download PC USB ESP9266board for test
i am very happy that this is working again, i doubted my code... the PC made the USB connect disconnect music every 2 seconds
uploads failed because USB port missing..
phuuu.. i retired the USB cable and all good

now there are lots of differences between the former PSemulator
and the new S31 code, starting slowly:

now we need the hardware def for the S31:

GPIO00__Push Button (HIGH = off, LOW = on) for toggle relay
GPIO12__Relay and its status LED
GPIO13__Green LED (HIGH = off, LOW = on)
GPIO01__RX pin (for power sensor)
GPIO03__TX pin (for power sensor)

first got to get the S31 Lite functionality
( no power meter )
-1- LED on after connect WIFI and MQTT Broker
-2- drive RELAY at
-2.1- local push button ( also 'flash' button )
-2.2- local web site button operation
-2.3- remote node-red mqtt button operation
i failed already at the local push button try own debounce code ( test on ESPboard where it looks like the pb is not working ? )
the mqtt debug was of some help, but too slow... but problem with flash button on nodemcu board is big, used also for usb chip...
so my tests not make any sense, until i burn the real S31 with this LITE version.

now as a side step. it has been 5 years 2017/2018 i used SPIFFS with ESP
there has been some updates, so i start again with LITTLEFS
-a- need that /tools/ dir with unzip this
-b- need the project/data dir with file

-c- need write LITTLEFS
** begin / read from file / end code
** write changed settings back to file ( on change? or timer? )
-d- and i want it to be a readable 'settings.json' file
also started it as a extra little sample code

for the real meter chip in the S31 use 'old'

as i read that the serial print would interferre with the communication to the power meter
i delete all serial diagnostig prints....
anyhow that not compile ( about the softserial init )

now try to get some files from TASMOTA git:
TasmotaSerial.cpp ( a special ESP soft serial / not arduino soft serial )
well it does not end .. need more and more definitions

stop and check fridge

and now i stopped the whole projects ( health limits my desk / work hours )
loaded TASMOTA
desoldered the wires
and do what i bought it for:

check on some consumers starting with

a 10 year old SAMSUNG fridge

well it was a lemon when we bought it,
on the first day the compressor failed and we got a replacement build in, good, fast service!

but when some friend told me about his electric bill ( half ) i got thinking...
do 2 people houshold mean double electric bill?

the first readings on the fridge
( and a fluctuating mains voltage 210 .. 230 VAC )
about 110 .. 120 W OK
that not sound so big, slowly understanding what makes the cost? its the runtime ( dutycycle )

so a bad fridge ( with bad means ? less insulation ? )
not need more Watt, but worst case it runs 24/7 at it's compressor Watt.

so i started to make a little spread sheet ( google drive sheets )
( sorry i still call it EXCEL even i dislike the company )
and listed daily the TASMOTA reported last day kWh
( actually not knowing how S31 knows what is a day? )

over some days i see 2.2 .. 2.5 kWh/d ( with weather data: 22 .. 30 degC and a fridge setting of 4 ?of 6 ? )
and i calculated
* duty cycle ( 0.76 to 0.88 of 1 )
* kWh / month ( to compare it to a monthly bill up to 250 kWh )
* cost per month ( 30% of my monthly bill !!)
* kWh / year ( 800 .. 900 kWh )
* cost per year ( and the electric cost per all 10 years would be a new small motocycle )

ok, will run it some days longer and then try a other fridge setting like ?2?
that check might be worthless as the winter begin is expected...
here ( thailand north ) with 10degC less, even YOU would not call that 'winter'
- - -
so, changing the fridge settings to 2 give me 2.3 kWh/d instead 2.5 kWh/d ( AVG )
with little bit (-1degC ) cooler weather,
not what i hoped.

small remark:
here in thailand the electric is very cheap ( many water dam & electric power stations )
but that not stops me from thinking / caring / how to save energy .
i pay for a small private house
** contract PEA / 1 phase counter / 4.06 THB / kWh ( about 11 euro cent )
a monthly bill of ( only ) 27 euro for 2 people
+ 1 fridge
+ 1 aircon ( rare use / 1 inverter type )
+ 3 of 8 fan running
+ 1 washing machine ( every day use / only cold / inverter type )
+ 2 utility water pump ( rare use )
+ 3 TV ( & computer ( shutdown after use ))
+ rice cooker / caffee machine / micro wave / air frier / water kettle /
- gas oven ( from gas bottle ) for most cooking
+ all LED lamps ( 7 .. 10 Watt )
+ 3 phone charging
+ standby devices? need to check

so how to compare it's 800 .. 900 kWh / year
with new energy classification EU 2021
examples from LG:
LG GBB92STBAP class A 110 kWh/year ( 1500 EURO )
LG GBB92MCABP class B 157 kWh/year ( 1300 EURO )

now that sounds crazy!