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!
Parts list
-
Sonoff TH10 or TH16
-
A USB to TTL dongle CH340G (FT232RL or PL2303 based will also do), to flash the new firmware.
-
A 3.3V breadboard power supply (only required for testing, safer than using mains!)
Soldering on headers
The Sonoff comes with the power and tx/rx pins unpopulated, so you need to solder on a header.
-
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.
-
Solder on a 4-pin 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
- 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.
-
Download esptool from the github repository.
-
Download the latest stable build of espeasy from sourceforge.
-
Unzip espeasy into a directory.
-
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.
-
Install Arduino and the ESP8266 tools.
-
Configure Tools settings as below:
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 |