Flashing custom Firmware to a Sonoff TH10/16

This is a tutorial about how to flash custom firmware onto the ESP8266 in the Sonoff Wireless Smart Switch. The Sonoff is a low cost mains relay with a fully customisable ESP8266 inside. Great for home automation hacking!

sonoff

Parts list

Soldering on headers

The Sonoff comes with the power and tx/rx pins unpopulated, so you need to solder on a header.

  1. Open up the Sonoff and locate the 4 pins for the header. They’re on the bottom left corner of the TH10/16 board, and towards the center on older boards.

  2. Solder on a 4-pin header.

header

To test, while disconnected from the mains, power the device with 3.3v connected to VCC and GND connected to 0v, just running the vanilla firmware. You should see the blue led light and flash a bit. I had to use a breadboard power supply for this step, as the USB to TTL adapter used for flashing doesn’t deliver enough current to power the full device.

Connecting the ESP8266 for flashing

  1. Using the USB to TTL dongle, connect:
Pin Sonoff USB
1 VCC 3V3
2 tx RX
3 rx TX
4 GND GND

Pin numbering is from bottom to top - the board has VCC and GND labelled, but not tx and rx in between. Note: TX on the Sonoff connects to RX on the dongle and vice versa.

Ensure VCC is 3.3v, not 5v. The ESP8266 will not take 5v.

Powering on for flashing

Power on the device by first holding down the tall white button, and whilst this is held plugging in the USB dongle, then releasing the white button. The ESP8266 will now be powered up in flashing mode (there’s no lights or anything to indicate this though).

Installing espeasy firmware

The ESPEasy firmware offers a great pre-built web interface with plenty of functionality to integrate with home automation over HTTP and MQTT.

  1. Download esptool from the github repository.

  2. Download the latest stable build of espeasy from sourceforge.

  3. Unzip espeasy into a directory.

  4. To flash the firmware:

$ ./esptool.py --port /dev/ttyUSB0 --baud 460800 write_flash --flash_size=8m -fm dio 0 ~/Downloads/ESPEasy_R120/ESPEasy_R120_1024.bin

The ESP8266 has 8Mbit (1MByte) of flash memory, so select the _1024.bin variant of the ESPEasy firmware.

Configuration of it is all performed over Wifi. For next steps using ESPEasy, check their detailed wiki.

Installing an Arduino sketch

Alternatively you can install your own Arduino sketch.

  1. Install Arduino and the ESP8266 tools.

  2. Configure Tools settings as below:

arduino

Useful pins

GPIO In/Out Connection Level
1 In Button
12 Out Red LED and Relay Low=on
13 Out Blue LED Low=on
14 In/Out Phono jack

Other useful links

Comments

blogroll

social