Wire a Sonoff smart switch and install Tasmota firmware

getting started with a $5 wifi relay and open source firmware

29-10-2020 - 5 minutes, 35 seconds -
tasmota

This tutorial assumes you are using Linux. If you are not, you have bigger problems to address.

If you're just getting started with home automation, and you don't want to spend a ton of money, the Sonoff Basic is a great option. It's extremely easy to use (only consisting of a simple 10 amp relay and an ESP8266 WiFi chip), yet when flashed with the open source Tasmota firmware, you have almost limitless options. And best of all, it's under $5, even less when purchased in lots. Ebay, Aliexpress, ITEAD, you have many options to purchase one. I don't really recommend Amazon unless you want to pay double the price and need it soon.

In the box you'll find the switch itself and 4 screws.

At the bare minimum you'll need a phillips screwdriver, a small flat head screwdriver, an extension cord, 4 male to female jumper wires, a computer, the Tasmota firmware, and a USB to TTL/UART adapter that's capable of 3.3v. These adapters can be had for under a dollar online.

I highly recommend a few other things to make the process easier, especially if you plan to flash more than one switch.

These pin header receptacles are a few cents and make things a lot easier.

The Sonoff itself actually has 5 serial inputs but you will only be using the 4 closest to the black reset button. I believe the 5th is a gpio.

The easiest way to solder the header receptacle is to clean the pins, dip them in flux, and insert them into the top of the board. Turn the board over, tin your iron, and touch each pin with the iron. The solder should flow cleanly down each pin.

Attach all 4 wires to the USB adapter. Make sure your power is on the 3.3v and not the 5v or you will fry your switch.

Insert the male pins into the receptacle, being careful to keep track of which pins go where. Remember that Tx from USB will go to Rx on the board and Rx from the USB will go to Tx on the board.

Your setup should now look like the above. Go ahead and plug your USB into your computer. If your Sonoff is functional, you will see a green LED on the device start to blink.

Now that you have tested your switch, unplug the adapter from the USB, remove the 3v wire from the pin, and plug the adapter back into the USB. Hold down the black button on the switch and plug the 3v wire back onto the adapter. Now your switch should be in programming mode. There will be no light flashing this time.

You will need esptool to flash the actual firmware onto the device. Make sure you have python3 installed if you don't already.

$ sudo apt update && apt install python3

Next install esptool:

$ pip install esptool

If you prefer to build from source you can find the git repo here.

You will also need the actual Tasmota firmware. Get tasmota.bin here.

First backup the original firmware from your Sonoff. Make sure you know the correct port your adapter is plugged into. Mine is located at /dev/ttyUSB0.

$ sudo python3 esptool.py --port /dev/ttyUSB0 read_flash 0x00000 0x100000 fwbackup.bin

If all goes well, your output should be similar to this:

esptool.py v2.8 Serial port /dev/ttyUSB0 Connecting.... Detecting chip type... ESP8266 Chip is ESP8266EX Features: WiFi Crystal is 26MHz MAC: 5c:cf:7f:a7:39:58 Uploading stub... Running stub... Stub running... 1048576 (100 %) 1048576 (100 %) Read 1048576 bytes at 0x0 in 95.7 seconds (87.6 kbit/s)... Hard resetting via RTS pin...

You will have to reset your device after each stage by going through the same steps we used to put the switch into programming mode.

Next erase the flash.

$ sudo python3 esptool.py --port /dev/ttyUSB0 erase_flash

Your output should look like this:

esptool.py v2.8 Serial port /dev/ttyUSB0 Connecting.... Detecting chip type... ESP8266 Chip is ESP8266EX Features: WiFi Crystal is 26MHz MAC: 5c:cf:7f:a7:39:58 Uploading stub... Running stub... Stub running... Erasing flash (this may take a while)... Chip erase completed successfully in 2.5s Hard resetting via RTS pin...

Reset the device again and flash the tasmota.bin firmware:

$ sudo python3 esptool.py --port /dev/ttyUSB0 write_flash -fs 1MB -fm dout 0x0 ~/tasmota.bin
Output:

esptool.py v2.8 Serial port /dev/ttyUSB0 Connecting.... Detecting chip type... ESP8266 Chip is ESP8266EX Features: WiFi Crystal is 26MHz MAC: 5c:cf:7f:a7:39:58 Uploading stub... Running stub... Stub running... Configuring flash size... Compressed 580480 bytes to 399923... Wrote 580480 bytes (399923 compressed) at 0x00000000 in 35.3 seconds (effective 131.4 kbit/s)... Hash of data verified.

Leaving... Hard resetting via RTS pin...

That's it, firmware flashed. You can either leave the pin receptacle on or remove it.

An easy way to remove it or any multi-pin module is to turn the board upside down, attach the alligator clip from a helping hand to the component, and lay your iron sideways across the pins, careful not to touch any other points.

Lift up on the board and the receptacle should come right off.

Cut your extension cord wherever you wish, I like to put the module about a foot from the output.

You only need just under a cm of stripped wire as the inputs on the switch are very shallow.

Since you have your soldering iron out anyway, it's a good idea to tin the leads so there's no frays or stray wires that can cross. Working with mains power is nothing to take shortcuts on and you don't want to burn your house down because of a stupid mistake.

Don't forget to thread your wires through the top before you insert them into the board.

Your cord should have a ridge along the plastic of one wire. This is the neutral lead. Make sure the input and output are in the correct ports. There shouldn't be any copper showing if done correctly.

Screw everything back together and you're almost done. Just have to connect the switch to your WiFi.

Plug in your device to mains power, wait for the green light to start blinking and look for a Wifi AP named tasmota-xxxx, where xxxx is a random string of numbers. If you do not see the tasmota AP after a couple of minutes, press the black button 4 times to set the device into AP mode. Connect to the tasmota AP, and go the the address 192.168.4.1. Add your WiFi SSID and password and give it a relevant hostname, for instance the location or device it will be used with. Hit save and the switch will reboot. Check your router for the address asigned to it and enter that address. Now you can configure it however you like.