NederlandsKlik deze knop voor de Nederlandstalige website

Flashing Tasmota on Sonoff Basic R2 using Linux

The Sonoff Basic R2 is a good and cheap smart switch. With the accompanying app, most users will be satisfied. If you want more options, or you don’t want to depend on an app, you can put an alternative firmware on the switch. With this you can, for example, link your switch to Node-Red or Home Assistant (HASS.io) by using the MQTT protocol. In this blog, we will see how we can accomplish this by flashing Tasmota firmware on the Basic R2.

Flashing Tasmota: Prepare the Sonoff

First, we prepare the Basic for flashing:

  1. Update the standard firmware on the Sonoff to the latest version the eWeLink app
  2. Remove the power cables from the switch
  3. Open the housing

To make it easy to connect the FTDI adapter, you can solder a 4-pin header to the Basic PCB.

Flashing Tasmota on Sonoff Basic R2 using Linux - connections
Flashing Tasmota on Sonoff Basic R2 using Linux – connections

Then connect the FTDI adapter to the Sonoff according to the table below. Make sure the adapter is set to 3.3V, if the adapter is set to 5V the Sonoff can be damaged. Also, make sure that RX is connected to TX, and vice versa.

Basic R2 FTDI
GND GND
3.3V 3.3V
RX TX
TX RX
Flashing Tasmota on Sonoff Basic R2 using Linux - FTDI USB interface
Flashing Tasmota on Sonoff Basic R2 using Linux – FTDI USB interface

Then connect the FTDI adapter to the computer while holding down the push button on the Ssonoff. By pressing and holding the button, the switch starts up in programming mode.

Tasmota flashing: Install ESPtool

Install Python if not yet present. Most common Linux distributions are already equipped with Python.

Then download Esptool from https://github.com/espressif/esptool/releases. Extract the file and install Esptool:

sudo python setup.py install

Use the following command to find the USB port to which the FTDI interface is connected:

dmesg | grep tty

In this case, the port is “ttyUSB0”, according to the output:

[ 5723.651213] usb 2-1.4: FTDI USB Serial Device converter now attached to ttyUSB0

Then determine the flash memory size of the Sonoff:

esptool.py --port /dev/ttyUSB0 flash_id

If you see the error message: “A fatal error occurred: Failed to connect to Espressif device: Timed out waiting for packet header” then the Basic R2 has not started in programming mode. Restart the Sonoff with the button pressed.

In this case, the size is 1MB according to the output:

esptool.py v2.8
Serial port /dev/ttyUSB0
Connecting....
Detecting chip type... ESP8266
Chip is ESP8285
Features: WiFi, Embedded Flash
Crystal is 26MHz
MAC: 84:0d:8e:51:b8:65
Uploading stub...
Running stub...
Stub running...
Manufacturer: 51
Device: 4014
Detected flash size: 1MB
Hard resetting via RTS pin...

Back up the firmware

Bring the switch into programming mode by disconnecting and reconnecting the USB connection while holding down the push button on the Sonoff. Use one of the following commands to download a backup of the original firmware from the Sonoff to the hard disk.

For a Sonoff with 1MB flash memory:

esptool.py --port /dev/ttyUSB0 read_flash 0x00000 0x100000 firmware1MB.bin

For a version with 4MB flash memory:

esptool.py --port /dev/ttyUSB0 read_flash 0x00000 0x400000 firmware4MB.bin

Make sure you specify the correct port and memory size (0x100000 or 0x400000). In my case the correct command was:

esptool.py --port /dev/ttyUSB0 read_flash 0x00000 0x100000 firmware1MB.bin

If all goes well you will see approximately the following output:

esptool.py v2.8
Serial port /dev/ttyUSB0
Connecting....
Detecting chip type... ESP8266
Chip is ESP8285
Features: WiFi, Embedded Flash
Crystal is 26MHz
MAC: 84:0d:8e:51:b8:65
Uploading stub...
Running stub...
Stub running...
1048576 (100 %)
1048576 (100 %)
Read 1048576 bytes at 0x0 in 99.2 seconds (84.6 kbit/s)...
Hard resetting via RTS pin...

Then bring the switch back to programming mode by connecting the Sonoff while holding down the push button.

Clear the Flash memory

Before we can flash new firmware on the Sonoff Basic R2, we must first clear the memory. Use the following command for this:

esptool.py --port /dev/ttyUSB0 erase_flash

As a result, you see the following output:

esptool.py v2.8
Serial port /dev/ttyUSB0
Connecting....
Detecting chip type... ESP8266
Chip is ESP8285
Features: WiFi, Embedded Flash
Crystal is 26MHz
MAC: 84:0d:8e:51:b8:65
Uploading stub...
Running stub...
Stub running...
Erasing flash (this may take a while)...
Chip erase completed successfully in 5.0s
Hard resetting via RTS pin...

Tasmota flashing on the Basic R2

Return the switch to programming mode by connecting the Sonoff while holding down the push button.

Go to Tasmota on GitHub and download the file “tasmota.bin”, or possibly another version of your choice. Move the file to the folder where Esptool is located. You can then use one of the commands below to flash the firmware on the Basic R2.

For a switch with 1MB flash memory:

esptool.py --port /dev/ttyUSB0 write_flash -fs 1MB -fm dout 0x0 tasmota.bin

For a version with 4MB flash memory:

esptool.py --port /dev/ttyUSB0 write_flash -fs 4MB -fm dout 0x0 tasmota.bin

If everything goes well you will see the following output:

esptool.py v2.8
Serial port /dev/ttyUSB0
Connecting....
Detecting chip type... ESP8266
Chip is ESP8285
Features: WiFi, Embedded Flash
Crystal is 26MHz
MAC: 84:0d:8e:51:b8:65
Uploading stub.d 580480 bytes to 399923...
Wrote 580480 bytes (399923 compressed) at 0x00000000 in 35.5 seconds (effective 130.7 kbit/s)...
Hash of data verified.
Leaving...
Hard resetting via RTS pin…..
Running stub...
Stub running...
Configuring flash size...
Compressed 580480 bytes to 399923...
Wrote 580480 bytes (399923 compressed) at 0x00000000 in 35.5 seconds (effective 130.7 kbit/s)...
Hash of data verified.
Leaving...
Hard resetting via RTS pin…

Tasmota WiFi settings

Finally, we have to connect the Sonoff to the WiFi network. For that, we have to choose the network in Tasmota and fill in the corresponding form.

  1. Restart the Basic R2 by disconnecting and reconnecting the USB cable, the switch will then (for 3 minutes) be found as a WiFi access point.
  2. The LED on the Sonoff flashes; search for example with your phone or laptop the access point “tasmota-xxxx AP WIFI”.
  3. Connect to the Sonoff, open a browser and go to IP address 192.168.4.1.
  4. You can now have Tasmota scan WiFi networks. Choose the network that you want to connect to.
  5. Enter the network password and save the settings.

The Sonoff will now disconnect and establish a connection with the chosen WiFi network. In the router of the network, you will now see the switch as “tasmota-xxxx”. Open the corresponding IP address in a browser to further configure Tasmota. For more information, look at https://github.com/arendst/Tasmota/wiki/Initial-Configuration.

2 Comments