Articles Hierarchy

Articles Home » Arduino Projects » Sonoff - ESP

Sonoff - ESP

Sonoff S31

Arduino IDE / ESP8266 / MQTT + webserver + operation

Power-Meter digging

Sonoff S31 flashing

back Arduino IDE but now on RPI2



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

says:
* 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
http://arduino.esp8266.com/stable/package_esp8266com_index.json

*** 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.
https://github.com/ayushsharma82/WebSerial
JoaoLopesF/RemoteDebug
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
ingeniuske
xoseperez
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
esphome

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

sonoff_S31
TASMOTA flashed to S31

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



no, i not ready to flash the S31, BUT
i flash the ESP8266 board (very old node mcu v0.9 yellow) with
TASMOTA
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 http://192.168.4.1 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 https://www.python.org/downloads/
not forget to click ON [ add to PATH..]
( and can try to start python IDLE from the windows START )

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

22.2.2

info from steve and espressif

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

so in cmd just type
esptool.py
and get all help you need

backup:
esptool.py --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:
esptool.py --port COM11 --baud 115200 write_flash 0x00000 C:\path_to\Blink.ino.d1_mini.bin


GOOD MORNING THAILAND

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.
BUT:
-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 esp8266.com/wiki/ 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 [192.168.1.104] 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 [192.168.1.103] 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 [192.168.1.102] 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 fsck.repair=yes 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 fsck.repair=yes rootwait quiet splash plymouth.ignore-serial-consoles

optional:
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: https://pypi.org/simple, https://www.piwheels.org/simple
Collecting esptool
Downloading https://www.piwheels.org/simple/esptool/esptool-4.2.1-py3-none-any.whl (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 https://www.piwheels.org/simple/ecdsa/ecdsa-0.18.0-py2.py3-none-any.whl (142 kB)
|████████████████████████████████| 142 kB 520 kB/s
Collecting bitstring>=3.1.6
Downloading https://www.piwheels.org/simple/bitstring/bitstring-3.1.9-py3-none-any.whl (39 kB)
Collecting reedsolo<=1.5.4,>=1.5.3
Downloading https://www.piwheels.org/simple/reedsolo/reedsolo-1.5.4-cp39-cp39-linux_armv7l.whl (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:~ $ esptool.py
esptool.py v4.2.1
......
connect options

and what we want to burn? get:
http://ota.tasmota.com/tasmota/release/tasmota-sensors.bin.gz



connection?
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.
idea:

with board and 'clamp':




but when i try, the link not work,
also the port still not clear
/dev/serial0
/dev/ttyS0
/dev/ttyAMA0

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 )
esptool.py read_mac or esptool.py flash_id
-3- esptool.py --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
esptool.py --port /dev/serial0 read_mac

we talk on serial0pi@RPI2:~ $ esptool.py --port /dev/serial0 read_mac
esptool.py v4.2.1
Serial port /dev/serial0
Connecting....
Detecting chip type... Unsupported detection protocol, switching and trying again...
Connecting...
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:
esptool.py --port /dev/serial0 --baud 115200 read_flash 0x00000 0x400000 S31_backup.img
read/backup flash
pi@RPI2:~ $ esptool.py --port /dev/serial0 --baud 115200 read_flash 0x00000 0x400000 S31_backup.img
esptool.py v4.2.1
Serial port /dev/serial0
Connecting...
Detecting chip type... Unsupported detection protocol, switching and trying again...
Connecting...
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

wget http://ota.tasmota.com/tasmota/release/tasmota-sensors.bin.gz
gunzip tasmota-sensors.bin.gz

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

flash TASMOTA
pi@RPI2:~ $ esptool.py --port /dev/serial0 --baud 115200 write_flash 0x00000 tasmota-sensors.bin
esptool.py v4.2.1
Serial port /dev/serial0
Connecting...
Detecting chip type... Unsupported detection protocol, switching and trying again...
Connecting...
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.

Leaving...
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 192.168.1.6 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 ).



back Arduino IDE but now on RPI2



cd Downloads
wget http://kll.engineering-news.org/kllfusion01/downloads/arduinoinst_sh.txt
mv arduinoinst_sh.txt arduinoinst.sh
chmod +x arduinoinst.sh
./arduinoinst.sh




*** in preferences “Additional Boards Manager URLs” give the
http://arduino.esp8266.com/stable/package_esp8266com_index.json

*** in boards manager install ESP8266

*** libraries ArduinoJson install

*** libraries PubSubClient install

copy last project from laptop and verify here on RPI2
ESP8266_board_MQTT_fixIP_webserver_operation

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:

Pin______Function
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
NO CRYPTIC emulated EEPROM SHIT
also started it as a extra little sample code


for the real meter chip in the S31 use 'old'
wget https://github.com/ingeniuske/CSE7766/archive/refs/heads/master.zip

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.h
TasmotaSerial.cpp ( a special ESP soft serial / not arduino soft serial )
xnrg_02_cse7766.ino
well it does not end .. need more and more definitions