Posted on 21 Comments

#31: Sonoff Tasmota installation and configuration

Make the Sonoff smart power controller even more awesome by installing the Tasmota open source firmware.

Tasmota adds many new features, and allows you to integrate Sonoffs into an existing home automation system without relying on external cloud services. It includes MQTT support, Belkin WeMo emulation, easy configuration using a web browser, and you don’t even need a compiler or IDE to install it.

Step 1: Prepare hardware for flashing

To load new firmware onto a Sonoff, you need four pieces of hardware:

  1. A compatible Sonoff model or equivalent (more than 50 devices are supported by Tasmota)
  2. A USB-to-Serial converter that can run in 3.3V mode (don’t use a 5V converter!)
  3. Jumper wires or a programming adapter to connect the USB-to-Serial converter to the Sonoff
  4. A computer with WiFi. You can use a mobile phone for the WiFi steps if you prefer.

You can make the connections using jumper wires and the instructions in my previous video, or you can use my handy little programming adapter. Both methods do exactly the same thing: my programming adapter just makes it neat and easy.

Make sure the Sonoff is totally disconnected from any mains power.

Don’t proceed unless you have done that! Connecting your computer to a Sonoff while it is connected to mains is extremely dangerous.

Details of the electrical connection are shown in the previous episode, so check that out if you need more information.

Step 2: Download Tasmota

The Tasmota firmware and its documentation is available at github.com/arendst/Sonoff-Tasmota, including both firmware and pre-compiled binary releases. If you want to compile the code yourself that’s fine, but you don’t need to if you just want the latest version. The binary releases are at:

 github.com/arendst/Sonoff-Tasmota/releases

There is a big list of binaries for each release, which can be confusing if you don’t know which one you need. The binaries fall into several categories which are outlined on the releases page.

For most devices, you should choose the binary called “sonoff.bin” which includes all the features required for the majority of compatible hardware.

The binaries with 2-letter country codes appended, such as “sonoff-IT.bin”, have the exact same functionality as the standard “sonoff.bin” but with different languages for the user interface. If you want to run Tasmota in a language other than English, select the appropriate “sonoff-XX.bin” file.

Whichever version you select, download it to your computer.

I prefer to rename the binary file so that it includes the release version, such as “sonoff-6_4_1.bin”. That way if I come back to it later, I can see what version I downloaded.

Step 3: Download and install esptool

Esptool is a utility that can be used to read and write the flash memory on devices using the ESP8266 and ESP8285 microcontrollers, including all current Sonoff models. It’s written in Python so you will need a working Python environment on your computer.

You can download esptool from github.com/espressif/esptool.

Follow the instructions on that page to install it on your computer.

Step 4: Find serial device

Your USB-to-Serial converter connects to your computer by USB, and appears to the computer as a virtual serial port. The exact location of the virtual serial port will vary depending on the type of converter that you have, what operating system you use, and which physical USB port you plug the converter into.

If you have the Arduino IDE installed, a good way to find the location of the virtual serial port is to start the IDE, look at the ports list in Tools -> Port, then plug in the converter and check the ports list again to see if a new port has appeared.

On my Mac, the port appeared at /dev/tty.usbmodem14201, so I’ll use that in the following examples.

Step 5: Put Sonoff into bootloader mode

Disconnect power from the Sonoff, hold the GPIO0 pin low, and reconnect power. Then release GPIO0. This process is explained in detail in the previous video.

This will put the Sonoff into a mode where it waits for new firmware to be loaded.

OPTIONAL Step 5b: Back up original firmware

If you want to make a backup of the original firmware so you can restore it back to factory-original later, jump to the addendum near the bottom and then return here. Otherwise, carry on!

Step 6: Upload the Tasmota firmware

Open a terminal, and go into the directory where you have the Tasmota binary. Use esptool to push it to the Sonoff using the serial port location that you found earlier:

esptool.py -p /dev/tty.usbmodem14201 write_flash -fs 1MB -fm dout 0x0 sonoff-6_4_1.bin
  • The “-p” parameter sets the serial port
  • The “write_flash” command tells esptool to send new data to the flash memory
  • The “-fs” parameter is the flash size. Put this parameter in if you know it. If you don’t, you can omit this parameter and esptool will attempt to auto-detect the flash size for you
  • The “-fm” parameter is the flash mode to use
  • The “0x0” parameter tells esptool to start writing into flash from address 0
  • The final argument is the binary image to load

Uploading the binary takes about 30 seconds.

While esptool is doing the upload it will report useful information about the hardware in your Sonoff, including the MAC address. This can be extremely useful if you want to put a DHCP static lease into your router fix your Sonoff to a specific IP address. You can copy and paste the MAC address out of the terminal and save it in a document or spreadsheet for future reference if you want to.

Step 7: Connect to Sonoff via WiFi

Disconnect the Sonoff power power, then reconnect it. When the Tasmota firmware starts up and doesn’t find any existing configuration, it goes into a setup mode and creates its own WiFi network. The network will have a name similar to “Sonoff-6392”, with the 4 digits based on the last part of the unique MAC address of the Sonoff hardware.

On your computer or smartphone, go into WiFi settings and look for a network with a name similar to this. Make a note of the network name, because you will need it in a moment!

Connect to the Sonoff’s WiFi network without any username or password. Your computer should then automatically open a captive portal, with a WiFi configuration screen for the Sonoff.

Enter the WiFi settings for your normal network into the Sonoff. You can enter them directly, or you can click the “Show wifi networks” link to have the Sonoff scan for available networks so that you can select the right one and then enter the password manually.

Enter the details, and click “Save”.

The Sonoff will then reboot and attempt to connect to your normal WiFi network.

This will cause your computer to drop off the temporary WiFi network that was created by the Sonoff, and reconnect to your normal network.

Step 8: Connect to Tasmota configuration interface

Open a web browser, and enter the address of the Sonoff using the network name that you noted in Step 7 with “.local” added to the end. For example, the address could be something like

sonoff-6392.local

This will open a configuration interface where you can view information about the device, change its settings, backup and restore the configuration, install firmware updates, and control the output.

Many of the configuration options are explained in the video. Explore the menus and set up the Sonoff to suit your home automation system.

Addendum: Reconfiguring from scratch

If your home WiFi changes or you take the Sonoff to another location, it will keep trying to connect to the network that you originally configured and you won’t be able to control it.

If this happens, you don’t need to re-flash Tasmota. Instead, you can put it into WiFi recovery mode.

Tasmota can use the button on the Sonoff to put it into special modes. If you quick-press the button 4 times, it will restart the setup process and create its own WiFi network again just like in step 7. Then you can connect to its WiFi, and give it new details so it can connect to your main WiFi network again. Your other settings will be retained so you don’t need to set them again.

If you press and hold the button for more than 40 seconds, Tasmota will clear all its settings, reboot, create a WiFi network, and begin the setup process from scratch.

Other things you can do with the button are explained at github.com/arendst/Sonoff-Tasmota/wiki/Button-usage

Addendum: Backing up and restoring original EWeLink firmware

If you want to keep the option of returning the Sonoff to factory-original condition, you can download the original firmware out of its flash memory and save it for later use. Obviously this has to be done BEFORE you flash Tasmota onto it!

Do everything up to Step 5, but DON’T continue with Step 6. Instead, use esptool to determine the flash size of the Sonoff. Using your own serial port address, run a command like this:

esptool.py -p /dev/tty.usbmodem142101 flash_id

This will give a result that looks something like this:

jon@Jonathans-MacBook-Pro:~/Downloads$ esptool.py -p /dev/tty.usbmodem142101 flash_id
esptool.py v2.6-beta1
Serial port /dev/tty.usbmodem142101
Connecting….
Detecting chip type… ESP8266
Chip is ESP8266EX
Features: WiFi
MAC: dc:4f:22:2b:c8:63
Uploading stub…
Running stub…
Stub running…
Manufacturer: e0
Device: 4014
Detected flash size: 1MB
Hard resetting via RTS pin…

You can see the result “Detected flash size: 1MB” near the end.

The Sonoff will have reset back into normal mode, so put it back into bootloader mode in the usual way, then use a command like this to download the existing flash memory and save it in a local file:

esptool.py -p /dev/tty.usbmodem142101 read_flash 0x0 0x100000 sonoff-ewelink.bin

Pay attention to the value “0x100000” above. That example will read the flash starting at address 0, and continuing on to the 1MB point. If you have a 2MB flash, you’d use “0x200000”, for example.

On my laptop, it looks like this:

jon@Jonathans-MacBook-Pro:~/Downloads$ esptool.py -p /dev/tty.usbmodem142101 read_flash 0x0 0x100000 sonoff-ewelink.bin
esptool.py v2.6-beta1
Serial port /dev/tty.usbmodem142101
Connecting….
Detecting chip type… ESP8266
Chip is ESP8266EX
Features: WiFi
MAC: dc:4f:22:2b:c8:63
Uploading stub…
Running stub…
Stub running…
1048576 (100 %)
1048576 (100 %)
Read 1048576 bytes at 0x0 in 94.0 seconds (89.2 kbit/s)…
Hard resetting via RTS pin…

After it’s finished, you will have the original flash memory saved on your computer in a file called “sonoff-ewelink.bin”.

Then continue with installing Tasmota.

Later, if you change your mind and want to put the original firmware back in place, you can follow the normal steps for installing Tasmota but instead use the file you saved. The command will look similar to this, but with your serial port address substituted:

esptool.py -p /dev/tty.usbmodem142101 write_flash -fm dout 0x0 sonoff-ewelink.bin

21 thoughts on “#31: Sonoff Tasmota installation and configuration

  1. In Episode 21 there was a concern about the amount of Flash memory in the Sonoff basic. Is this still an issue and is the flash memory upgrade still suggested/needed for Tasmota over the air updates?

  2. Hi Jon,

    I’ve recently got some S26 Sonoffs and tried this process. Prior to this I’ve used an Arduino Uno and their IDE to flash the Sonoff Basic with success. I decided to try this as the binary file is readily available and I got some missing libraries when compiling Tasmota. The soldering is more problematical but following the work of others the S26 powers on (green LED flashes). I’m putting the device into programming mode (no LED when connecting) and I don’t seem to be able to flash the five S26 I have. Error is the “Timed out waiting for packet header” and I’m currently trying to hunt down the error. I moved from an Arduino to your USB to Serial adapter but still no joy. I’ve checked the Vout pin with a (cheap) multimeter and Vout is 3.25. I have also used a separate powered USBhub and may try using a LiPo to supply a little bit more (as power issues are known to cause this). I did notice that on the video you use 0 in the code but above that’s now in hex format. Would this cause the issue? I’m using High Sierra and have followed advice for this in esptool too. Any other advice?

    1. I’ve also tried all baud rates down to 9600… no success.
      🙁

      1. Did you get this worked out? I’ve just received some S26s (I haven’t used them before) so I’ll see if I can flash them.

        1. Hey Jon,
          Yes and thanks for getting back to me. I know you’re a busy guy. I got it sorted out. There were two things that it could possibly have been but as they changed them concurrently I’m not sure which did the trick. On the Tasmota github pages some users noted they had to up the available current from the FTDI. I used a Sparkfun Beefy 3 which provides up to 600mA. The one I bought from you is around 500mA (from memory) so it may not have been quite enough. The other thing was drivers. I installed FTDI drivers for another USB to Serial adapter. I removed these as I’d also read there can be conflicts between them and Apple’s own. I was pleased I’d overcome it so I didn’t go back and check with your device to see if it was just a case of a driver conflict.

  3. Great video, it was spot on!
    I have flashed sonoff.bin, v 6.4.1 on the new Sonoff Basic (RF R2 POWER V1.0), and configured mqtt to my own broker. However I have a problem with mqtt. Maybe I should ask in a Tasmota forum but since you obviously are using mqtt, I start with you: I set up a subscription (mosquitto_sub) in one terminal window (-t “+/DVES_DC7DE2_fb/+”), and use mosquitto_pub in another terminal window to send ON and OFF messages.
    When I send messages with mosquitto_pub the Sonoff turns off and on as it should, and the web interface also reflects the status.
    But 1: If I click on Toggle in the web interface the Sonoff reacts but there is NO MQTT message at all.
    But 2: If I press the button on the Sonoff device, the device switches state (as well as the web interface), but NO MQTT message at all!
    This leads me to think there is a bug in Tasmota 6.4.1. Or have I missed a setting somewhere?. I thought that I should ask you first if you have tested that? Maybe in another version?

  4. Just FYI, this may have been communicated, but just in case others have connection issues, I had to put sonoff in bootloader mode for every command so each time I ran a new command I disconnected the sonoff and reconnected it in bootloader mode.

  5. Hi Jonathan,
    I really like your superhouse.tv series on the web – thanks. Enjoy your break. I recently purchased your Freetronics USB to serial card and adaptor. I am using a Macbook Air running OSX 10.13.16 and using esptool.py v2.6 to download Tasmota firmware to a Sonoff TH16. I have found the serial port is tty.usbmodemFD121 and tried connecting to the Sonoff device. I’m now getting the old problem of “A fatal error occurred: Failed to connect to Espressif device: Timed out waiting for packet header”. It seems from various forums there can be a number of possible causes including: Sonoff not in flash mode, poorly rated 3.3V supply. I haven’t eliminated these, but do you know a reliable source of information that may help me to narrow down the cause(s) in my situation?
    Many thanks,
    Phil Kelloway.

    1. Hi Phil, perhaps one thing to check is whether the Sonoff is continuing to boot into normal mode. Does it still have the original eWeLink firmware installed? If so, do the normal process of connecting to the adapter and putting it into bootloader mode, then wait a minute and see if it’s booted normally (for example, associating with WiFi or toggling the output when you press the button) which would indicate it’s not gone into the correct mode.

      1. Hi Jon,
        Thanks heaps for your response.
        Well the sun has finally shone on my Tasmota download. Before I saw your reply I had a thought bubble about the 3.3.V supply rating and decided to give it a go on my desktop Mac (which is connected to a Belkin dock that has a blue USB port (3.0 if I recall correctly) with its own internal power supply). After installing Python, pyserial and ESPTOOL on the desktop I sent read_mac a go and got a connection and response. Then I tried read_flash and it wouldn’t connect. I noticed the message from the read_mac indicated hard reset of the Sonoff had occurred. Again jumping into the unknown I tried starting the Sonoff in programme mode again and it proceeded to read the flash as hoped, but wouldn’t open the target file because of permissions. So I started it via sudo and further success – the upload file was written. So finally I gave the write_flash a go and it worked. So now I’m into getting MQTT working but more of that later if I get into a hole again. Re your questions:
        I was pretty certain that Sonoff got into programming mode once I got it to respond to read_mac.
        Before that success if I restarted the Sonoff it worked fine in normal mode because I could see and control it from the eWelink app on my phone. It seems that you can do a lot via the ESPTOOL without blowing away the factory firmware – i.e. by just restarting the device and waiting for it to connect to the WiFi.
        Thanks again. I feel that I am starting to get somewhere but will proceed into the mist as methodically as I can. I’ll let you know of any successes or pitfalls once I get into MQTT. The videos are great.
        Cheers,
        Phil.

        1. Thanks for that info, Phil! I wouldn’t have thought of the USB port being a problem. That’s a helpful hint for future reference.

          I think it’s probably worth creating a topic for this on the SuperHouse forum, as a place to collect this sort of information.

          1. Hi Jon,
            Good idea. Other than scanning for information I’m not up on the forums. Who starts the topic on the Superhouse forum (i.e. me or thee)?
            I’m happy to do so if that is how it works.
            Cheers,
            Phil.

    2. Hi Phil,
      Just my thoughts having recently had an infuriatingly difficult time with some S26 wifi sockets. Previously, I have had no issues but these were troublesome for me. Be methodical and try to eliminate one thing at a time. If you do this you will eventually track it down. It can be difficult to find out the issue as although others are helpful on forums it can lead to a lot of “noise” and blind alleys. Good luck!

      1. Hi Nick
        Thanks for your suggestions. I’m on a pretty steep learning curve understanding modern computer parlance, setting up Home Assistant, MQTT, downloading firmware, etc. But I’ve had success at last – I sent Jon a long response to his reply to me explaining my findings. Put simply, it was insufficient 3.3V power supply rating from my Macbook Air’s USB. After installing python, pyserial, and the ESPTOOL on my desktop Mac Mini I connected to the USB on the Belkin docker attached to the Mac Mini and voila upload factory firmware and download Tasmota firmware works fine. So now I’m into getting MQTT working. Looking back at this one its so easy to loose message amongst all the noise but in the end method won out over madness. Thanks again.
        Cheers,
        Phil.

  6. Hi Jon, I’ve discovered in the latest firmware 6.5, mDNS Discovery is off by default, so the xxx.localhost doesn’t work for me, I have to check the IP address on via my router to access the sonoff web interface.

    1. That’s interesting, thanks. One other approach is to set the MQTT broker in the config before changing anything else, so that when the Sonoff switches to your main network it will immediately contact the broker. If you have an MQTT client watching the messages (even something like mosquitto_sub -t “#”) you’ll see Tasmota send its “hey, I’m alive” message which includes details such as the IP address.

  7. Hello Jonathan,
    Love your Tech-TV !
    Concerning local MQTT brokers: WHAT is the best one to setup securely, ( prefer FREE ) ; maybe a bridged system ; 1 controls all local comm’s and talking to another one that ‘s connected to the internet ; that one makes the use over 4G possible , and /or use external sensors and so … ?
    Running a PC 24/7 isn’t really hazard-proof (NOR with f.e. (the many) win10 updates) NOR is this that GREEN (power-hungry.)
    Maybe using a (or 2) fanless PC ( low power, and more robust with Industrial graded power supplies etc.) is a better solution / idea ???
    What do you think, and advice ?
    RPI3 is cheap, but not really a long-term stable solution, unless with some watchdogging and a standby backup system to autoswap in case of defect ( usually you’re on the road with Murphy ;o) when things get broken )

    Another question ONCE the MQTT brokers are setup : How do wel let Alexa controll/access the controls ? Is it best to let her only control and sync with the cloud-MQTT-part?
    Kind regards from Belgium, Europe.
    Ritchie

  8. First, thanks, took a little time and a bit of hard work, failed to crack it on Windows but Ubuntu won the day.

    Just a little add on.
    When I opened the case on my Basic Sonoff I managed to destroy the reset switch, well I broke the black push button off and therefore opened the switch.
    I have managed to “Bodge It” back together and it is now my “play with” Sonoff but you covered off the cleaver stuff really well, I managed to mess up the simple mechanical bit, do’oh!
    Keep up the awesome work.

  9. Hello Jonathan great work, really enjoying following your site many thanks!

    Wonder if you can shed any light on why I’m having difficulty saving the sonoff stock firmware using read_flash?
    Using > esptool.py –port COM5 –baud 92160 read_flash 0x0 0x100000 sonoff-ewelink.bin

    This returns >
    esptool.py v2.6
    Serial port COM5
    Connecting….
    Detecting chip type… ESP8266
    Chip is ESP8285
    Features: WiFi, Embedded Flash
    MAC: xxxxxxx
    Uploading stub…
    Running stub…
    Stub running…
    A fatal error occurred: Corrupt data, expected 0x1000 bytes but received 0xffe bytes

    Sonoffs are 1MB memory, I’ve tried different baud rates and different boards to no avail.

    I’m using your usb-serial adapter and sonoff connector, windows 10, python environment, Sonoff Rev 2 devices. I am able to used esptool.py for chip_id, read_mac and upload tasmota using write_flash – so assume all is good with com port, usb lead, baud rate, 3.3v and current etc.

    Just wondering if you/anyone has come across this issue? Probably academic as I expect I’ll migrate to tasmota and unlikely to go back to the stock firmware nevertheless I’d be interested to know why and if possible keep the option open to return to EWelink.

    Thanks James
    New Zealand

    1. There are so many variations of the Sonoff now that I wouldn’t trust the flash size: best to check it on the actual board. Try running “esptool.py -p COM5 flash_id” and look near the bottom for the reported flash size.

      1. Hi thanks
        I have run flash_id and it reported 1MB for all of my 4 basic sonoffs. I’ve tried multiple times with various combinations of baud rate, start and memory size using the read_flash command to no avail always getting something like:
        A fatal error occurred: Corrupt data, expected 0x1000 bytes but received 0xffe bytes.

        Unsure if it is a specific issue with rev 2 sonoff basic boards which uses ESP8285 and onboard flash memory compared to ESP8266. No problems uploading using write_flash and Tasmota.

        After a web search I have been able to back up the ewelink stock firmware using esp_tool.exe – a program found at the website below which runs on Windows 10. The site is in German language (translates well in chrome browser) but its not that difficult to follow/ understand if useful for anyone else having similar issues.

        https://forum.creationx.de/lexicon/index.php?entry/31-backup-sichern-der-original-firmware-sonoff-shelly-andere-esp8266-ger%C3%A4te/

        Thanks James
        New Zealand

Leave a Reply

Your email address will not be published. Required fields are marked *