Contents:
- Hardware Requirements
- Software Requirements
- Step-by-Step Procedure
- — Step A
- — Step B
- — Step C
- —- Determine the Flash Size
- —- Backup / Download Official Firmware
- — Step D
- —- Erase Flash Memory
- —- Upload Official / Third Party Firmware
- Troubleshooting
Controlling Sonoff Smart Switches without internet over Local Area Network is hot topic for Sonoff enthusiasts. After loading any custom firmware to Sonoff, you cannot go back to the official firmware but now you can!!. Just backup your official firmware by following the instructions below and you can restore it later if you want. So Let’s get started..
Hardware Requirements:
- Sonoff Smart Switch
- FTDI USB To TTL 3.3V & 5.5V Serial Adapter + Cable
- Premium Female/Female Jumper Wires
- Personal Computer / Laptop
- WiFi Router
Software Requirements:
Step-by-Step Procedure:
Step A:
Download and install Python 2.x or 3.x from https://www.python.org/downloads. Download Esptool from https://github.com/espressif/esptool/releases. Extract the contents of esptool compressed file to C:\. All the contents of esptool should be under c:\esptool.
Step B:
- Open Command Prompt (Start > Run > cmd) and navigate to esptool folder.
1 |
cd c:/esptool |
- Install the esptool by issuing the command below.
1 |
python setup.py install |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
c:\esptool>python setup.py install running install running bdist_egg running egg_info creating esptool.egg-info writing requirements to esptool.egg-info\requires.txt writing esptool.egg-info\PKG-INFO writing top-level names to esptool.egg-info\top_level.txt writing dependency_links to esptool.egg-info\dependency_links.txt writing entry points to esptool.egg-info\entry_points.txt writing manifest file 'esptool.egg-info\SOURCES.txt' reading manifest file 'esptool.egg-info\SOURCES.txt' reading manifest template 'MANIFEST.in' writing manifest file 'esptool.egg-info\SOURCES.txt' installing library code to build\bdist.win-amd64\egg running install_lib running build_py creating build creating build\lib copying esptool.py -> build\lib copying espsecure.py -> build\lib copying espefuse.py -> build\lib creating build\bdist.win-amd64 creating build\bdist.win-amd64\egg copying build\lib\espefuse.py -> build\bdist.win-amd64\egg copying build\lib\espsecure.py -> build\bdist.win-amd64\egg copying build\lib\esptool.py -> build\bdist.win-amd64\egg byte-compiling build\bdist.win-amd64\egg\espefuse.py to espefuse.pyc byte-compiling build\bdist.win-amd64\egg\espsecure.py to espsecure.pyc byte-compiling build\bdist.win-amd64\egg\esptool.py to esptool.pyc creating build\bdist.win-amd64\egg\EGG-INFO copying esptool.egg-info\PKG-INFO -> build\bdist.win-amd64\egg\EGG-INFO copying esptool.egg-info\SOURCES.txt -> build\bdist.win-amd64\egg\EGG-INFO copying esptool.egg-info\dependency_links.txt -> build\bdist.win-amd64\egg\EGG-INFO copying esptool.egg-info\entry_points.txt -> build\bdist.win-amd64\egg\EGG-INFO copying esptool.egg-info\requires.txt -> build\bdist.win-amd64\egg\EGG-INFO copying esptool.egg-info\top_level.txt -> build\bdist.win-amd64\egg\EGG-INFO zip_safe flag not set; analyzing archive contents... esptool: module MAY be using inspect.trace creating dist creating 'dist\esptool-2.3.1-py2.7.egg' and adding 'build\bdist.win-amd64\egg' to it removing 'build\bdist.win-amd64\egg' (and everything under it) Processing esptool-2.3.1-py2.7.egg creating c:\python27\lib\site-packages\esptool-2.3.1-py2.7.egg Extracting esptool-2.3.1-py2.7.egg to c:\python27\lib\site-packages Adding esptool 2.3.1 to easy-install.pth file Installing espefuse.py-script.py script to C:\Python27\Scripts Installing espefuse.py.exe script to C:\Python27\Scripts Installing esptool.py-script.py script to C:\Python27\Scripts Installing esptool.py.exe script to C:\Python27\Scripts Installing espsecure.py-script.py script to C:\Python27\Scripts Installing espsecure.py.exe script to C:\Python27\Scripts Installed c:\python27\lib\site-packages\esptool-2.3.1-py2.7.egg Processing dependencies for esptool==2.3.1 Searching for ecdsa Reading https://pypi.python.org/simple/ecdsa/ Best match: ecdsa 0.13 Downloading https://files.pythonhosted.org/packages/f9/e5/99ebb176e47f150ac115ffeda5fedb6a3dbb3c00c74a59fd84ddf12f5857/ecdsa-0.13.tar.gz#sha256=64cf1ee26d1cde3c73c6d7d107f835fed7c6a2904aef9eac223d57ad800c43fa Processing ecdsa-0.13.tar.gz Writing c:\users\ahmed\appdata\local\temp\easy_install-8dtzxw\ecdsa-0.13\setup.cfg Running ecdsa-0.13\setup.py -q bdist_egg --dist-dir c:\users\ahmed\appdata\local\temp\easy_install-8dtzxw\ecdsa-0.13\egg-dist-tmp-q2eiv5 zip_safe flag not set; analyzing archive contents... Moving ecdsa-0.13-py2.7.egg to c:\python27\lib\site-packages Adding ecdsa 0.13 to easy-install.pth file Installed c:\python27\lib\site-packages\ecdsa-0.13-py2.7.egg Searching for pyaes Reading https://pypi.python.org/simple/pyaes/ Best match: pyaes 1.6.1 Downloading https://files.pythonhosted.org/packages/44/66/2c17bae31c906613795711fc78045c285048168919ace2220daa372c7d72/pyaes-1.6.1.tar.gz#sha256=02c1b1405c38d3c370b085fb952dd8bea3fadcee6411ad99f312cc129c536d8f Processing pyaes-1.6.1.tar.gz Writing c:\users\ahmed\appdata\local\temp\easy_install-lnc2v5\pyaes-1.6.1\setup.cfg Running pyaes-1.6.1\setup.py -q bdist_egg --dist-dir c:\users\ahmed\appdata\local\temp\easy_install-lnc2v5\pyaes-1.6.1\egg-dist-tmp-yeqyyk zip_safe flag not set; analyzing archive contents... Moving pyaes-1.6.1-py2.7.egg to c:\python27\lib\site-packages Adding pyaes 1.6.1 to easy-install.pth file Installed c:\python27\lib\site-packages\pyaes-1.6.1-py2.7.egg Searching for pyserial>=2.5 Reading https://pypi.python.org/simple/pyserial/ Best match: pyserial 3.4 Downloading https://files.pythonhosted.org/packages/cc/74/11b04703ec416717b247d789103277269d567db575d2fd88f25d9767fe3d/pyserial-3.4.tar.gz#sha256=6e2d401fdee0eab996cf734e67773a0143b932772ca8b42451440cfed942c627 Processing pyserial-3.4.tar.gz Writing c:\users\ahmed\appdata\local\temp\easy_install-mgemll\pyserial-3.4\setup.cfg Running pyserial-3.4\setup.py -q bdist_egg --dist-dir c:\users\ahmed\appdata\local\temp\easy_install-mgemll\pyserial-3.4\egg-dist-tmp-maag20 zip_safe flag not set; analyzing archive contents... Moving pyserial-3.4-py2.7.egg to c:\python27\lib\site-packages Adding pyserial 3.4 to easy-install.pth file Installing miniterm.py script to C:\Python27\Scripts Installed c:\python27\lib\site-packages\pyserial-3.4-py2.7.egg Finished processing dependencies for esptool==2.3.1 |
Step C:
Now We are just one step behind to download / upload any firmware to our Sonoff Smart Switch. Connect FTDI to Sonoff Smart Switch (For detailed information about each Sonoff Switch, visit Sonoff Smart Switch LAN Control without Internet) and start Sonoff device in programming mode. Now find the port under Control Panel > System > Device Manager > Ports at which Sonoff device is connected.
Determine the Flash Size:
1 |
esptool.py --port COM5 flash_id |
- Change COM5 to your own Port.
- Wait few seconds and it outputs the flash size as shown in picture below Detected flash size: 4MB.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
c:\esptool>esptool.py --port COM5 flash_id esptool.py v2.3.1 Connecting.... Detecting chip type... ESP8266 Chip is ESP8266EX Features: WiFi Uploading stub... Running stub... Stub running... Manufacturer: ef Device: 4016 Detected flash size: 4MB Hard resetting via RTS pin... |
Backup / Download Official Firmware:
1MB Flash:
1 |
esptool.py --port COM5 read_flash 0x00000 0x100000 image1M.bin |
4MB Flash:
1 |
esptool.py --port COM5 read_flash 0x00000 0x400000 image4M.bin |
- Change COM5 to your own Port.
- Change flash size parameter 0x100000 for 1MB and 0x400000 for 4MB flash.
- Wait a minute and firmware binary file will be downloaded and saved in to c:\esptool
It takes few seconds to upload firmware and output will be shown as follows.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
c:\esptool>esptool.py --port COM3 read_flash 0x00000 0x100000 image1M.bin esptool.py v2.3.1 Connecting.... Detecting chip type... ESP8266 Chip is ESP8266EX Features: WiFi Uploading stub... Running stub... Stub running... 1048576 (100 %) 1048576 (100 %) Read 1048576 bytes at 0x0 in 97.8 seconds (85.8 kbit/s)... Hard resetting via RTS pin... |
Step D:
Erase Flash Memory:
Erase the custom firmware from flash memory before uploading any new firmware.
1 |
esptool.py --port COM5 erase_flash |
- Change COM5 to your own Port.
It takes few seconds to erase firmware and output will be shown as follows.
1 2 3 4 5 6 7 8 9 10 11 12 |
c:\esptool>esptool.py --port COM3 erase_flash esptool.py v2.3.1 Connecting.... Detecting chip type... ESP8266 Chip is ESP8266EX Features: WiFi Uploading stub... Running stub... Stub running... Erasing flash (this may take a while)... Chip erase completed successfully in 9.6s Hard resetting via RTS pin... |
Upload Official / Third Party Firmware:
Determine the flash size as discussed in Determine the Flash Size section. Copy the official / any third party firmware binary file to c:\esptool folder if it is not already there and upload it by issuing the command below.
1MB Flash:
1 |
esptool.py --port COM5 write_flash -fs 1MB -fm dout 0x0 sonoff.bin |
4MB Flash:
1 |
esptool.py --port COM5 write_flash -fs 4MB -fm dout 0x0 sonoff.bin |
- Change COM5 to your own Port.
- Change flash size parameter -fs 1MB for 1MB and -fs 4MB for 4MB flash.
- Change sonoff.bin to the name of your firmware file inside c:\esptool folder.
It takes few seconds to upload firmware and output will be shown as follows.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
c:\esptool>esptool.py --port COM5 write_flash -fs 1MB -fm dout 0x0 image1M.bin esptool.py v2.3.1 Connecting.... Detecting chip type... ESP8266 Chip is ESP8266EX Features: WiFi Uploading stub... Running stub... Stub running... Configuring flash size... Flash params set to 0x0320 Compressed 1048576 bytes to 499096... Wrote 1048576 bytes (499096 compressed) at 0x00000000 in 44.3 seconds (effective 189.1 kbit/s)... Hash of data verified. Leaving... Hard resetting via RTS pin... |
Troubleshooting:
- Choosing Wrong COM Port will cause the following error.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
c:\esptool>esptool.py --port COM5 write_flash -fs 1MB -fm dout 0x0 image1M.bin esptool.py v2.3.1 Traceback (most recent call last): File "C:\esptool\esptool.py", line 2637, in <module> _main() File "C:\esptool\esptool.py", line 2630, in _main main() File "C:\esptool\esptool.py", line 2349, in main esp = ESPLoader.detect_chip(args.port, initial_baud, args.before, args.trace) File "C:\esptool\esptool.py", line 222, in detect_chip detect_port = ESPLoader(port, baud, trace_enabled=trace_enabled) File "C:\esptool\esptool.py", line 193, in __init__ self._port = serial.serial_for_url(port) File "build\bdist.win-amd64\egg\serial\__init__.py", line 88, in serial_for_url File "build\bdist.win-amd64\egg\serial\serialwin32.py", line 62, in open serial.serialutil.SerialException: could not open port 'COM5': WindowsError(2, 'The system cannot find the file specified.') |
- If following output is shown, Sonoff is not correctly connected to FTDI, Check wire configuration or terminals.
1 2 3 4 5 |
c:\esptool>esptool.py --port COM3 write_flash -fs 1MB -fm dout 0x0 image1M.bin esptool.py v2.3.1 Connecting........_____....._____....._____....._____....._____....._____....._____....._____....._____....._____ A fatal error occurred: Failed to connect to Espressif device: Timed out waiting for packet header |
- After restoring the official firmware, if led does not blink, It means you have problem with backup image
- either backup image is corrupt – Check 3.3v power supply during backup
- Chose wrong flash size during backup – See Determine the Flash Size Section
Great write up, thanks!
I have one residual question. Is it possible to write the official firmware, read from a 1M flash onto a 4M flash?
The reason I ask is that I have swapped the flash chips (from 1M with 4M flash) on most of my Sonoff basic’s. I would like to restore the original firmware onto the 4M flash chips.
I have two SONOFF basic switches both loaded with custom firmware. Using your backup/restore processes, I backed up factory firmware and restored to these two switches. The backup, erase, then restore worked perfectly. However, when I powered up both restored switches, nothing, no lights. What am I missing here. Does backup/restore only work with the same switch? Thanks for your guidance.
There may be the issue with flash size. Check Determine the Flash Size Section, updated today.
I have same issue with sonoff POW after succesfull backup/restore it is dead no light. The flash size is correct 4MB Source POW: C:\esptool-2.6>esptool.py –port COM3 flash_id esptool.py v2.6 Serial port COM3 Connecting…. Detecting chip type… ESP8266 Chip is ESP8266EX Features: WiFi MAC: 5c:cf:7f:92:2d:34 Uploading stub… Running stub… Stub running… Manufacturer: e0 Device: 4016 Detected flash size: 4MB Hard resetting via RTS pin… C:\esptool-2.6>esptool.py –port COM3 read_flash 0x00000 0x400000 image4M.bin esptool.py v2.6 Serial port COM3 Connecting…. Detecting chip type… ESP8266 Chip is ESP8266EX Features: WiFi MAC: 5c:cf:7f:92:2d:34 Uploading stub… Running stub… Stub running… 4194304 (100 %) 4194304 (100 %) Read… Read more »
For Martin do you need press button to your sonoff pow and connect power for programming and read.
This is error cpu not in programming mode.9
Programmatiin working. I change firmware ok . Test espurna and espeasy work. Revert original with new flash not work . Old flash is burned..
I have double backup. One 1mb and one 4mb. Memory flash is 4mbyte this is Pow sonoff.
Tedted ftdi and board ch340
If you keep getting this:
esptool.py v2.3.1
Connecting…….._____….._____….._____….._____….._____….._____….._____….._____….._____….._____
A fatal error occurred: Failed to connect to Espressif device: Timed out waiting for packet header
Try to disconnect the esp from the PC and start Sonoff device in programming mode again
Check your FTDI connections or cable. You might be connecting RX or TX lines inversely or your Sonoff device is not started in programming mode.
push the button when plugging ftdi usb cable and release it after 2 seconds
??? Dear not have solution? Change spiffs? Or other ? Have all file for upload? Only Boot ?
Pls help
I have dump for my original sonoff pow i have dumped 1mb and 4mb . My fflash type original is 25q32. I have burned original switch in the sonoff pow. And have replaced with 4.5V power supply. And have changed many time to original and espurna.Working fine for 60 day. If reprogramming flash a this day not work. Error md5. A this time have replaced flash with qh32bhig brand xmd, recovered by old lolin board. New test with new flash i have uploaded espurna and work fine. Router ap and work fine. If reinstall original sonoff pow firmware not work.… Read more »
Have you erased the flash? Have a look on to Erase Flash Memory. Moreover, are you using an FTDI with 3.3V logic to upload the binary file because esp8266 is not 5v tolerant. If problem persists, create and issue at https://github.com/esp8266/esptool/issues.
Upon investigation, we found the flash size issue. Check troubleshooting or Determine Flash Size Section above.
Hello dear i have bakup but not working? I have replaced new flash , uploaded my backup and not work. I have error boot @1000 or boot @81000
Help for backup?
Oter firmware working fine.
Please explain your problem and send the complete error message
Check troubleshooting section of post.
Hi,
Great article. Thanks for it.
But I have a problem in reading flash:
“A fatal error occured: Corrupt data, expected 0x1000 bytes but received 0xffd bytes”
Do you have any idea about this error?
Thanks
Hello. What are you going to do? Backing up Official Firmware?
Hi,
Yes, I want to make a backup.
Change your cable. If problem persists then flash memory might get damaged.