How to flash a Sonoff or other ESP8266 device

I am going to show you how you can make a great device out of a cheap Chinese Sonoff or any ESP8285 / ESP8266 device, so here it is.

I’m going to cover the software side of things. The hardware part is more specific to what exactly you want to achieve, so these will be in separate posts, stay tuned.

Jump to Tutorial

What are the options?

Depending on the goals you want to achieve and the devices you use daily, you want to choose the best firmware to use for your needs. Most tutorials go with one firmware and that’s it. I want to make sure you know all the options, or at least the good ones. All of them are Open Source. I am going to list just a few.

Tasmota

Tasmota works with a wide (probably the widest of all) range of products and that is one of the reasons it is the most popular solution. As with the other options, you can use Tasmota to program your device to be whatever you want it to. What sets Tasmota apart from the others is its wide support of devices AND support for almost any type of sensor.

Tasmota supports MQTT, HTTP, Serial and KNX but most often it is used when you want to use MQTT or HTTP. Updates can be done OTA.

MQTT is an ISO standard publish-subscribe-based messaging protocol. In order to use MQTT, you need a server acting as a MQTT Broker, which your MQTT devices use as the middleman connecting them.

ESPurna

ESPurna is very similar to Tasmota - it is open source, it supports a wide range of devices, it is highly configurable and supports MQTT and HTTP. It is better, because it supports REST, has better Web UI and is easier to integrate with Home Assistant. Updates can be done OTA.

Homekit Accessory Architect

Homekit Accessory Architect (formelly known as RavenCore) is an OpenSource firmware, supporting nothing but HomeKit. This makes it incompatible with Homeassistant, OpenHab, Alexa and others, so it only works with HomeKit-enabled devices - iPhones, iPads, Apple Watches, HomePod, Apple TV etc. Updates can be done OTA if you flash HAA firmware with OTA.

I’m an Android user. How do I do it?

Alexa and Google Home

For Android users, in my opinion you have 2 options (iOS users continue reading):

  1. Amazon’s Alexa
  2. Google Assistant
  3. HomeAssistant/OpenHab

Whichever one you prefer, the way to do it is to use OpenHAB. OpenHAB supports more than 200 different technologies and systems and thousands of devices.

The way that I’d do it would be to setup my OpenHAB server along with a MQTT Broker. Connect all the MQTT devices to that broker. Afterwards setup OpenHAB to support Google Assistant or Amazon Alexa.

In order for either to work, you need to have a Hub - Amazon Echo, Amazon Echo Dot or compatible Alexa speaker or solution, or Google Home or Google Home mini, depending on which one you want to use.

Of course, there is “a third” solution. You can build your own system working via HTTP or MQTT, but then you have to think about security, as you don’t want anyone else but you to control your devices.

Why I chose to go with HomeKit

Nice HomeKit image by Apple

HomeKit is a networking protocol supported by Apple and its devices. Unfortunately, device support is limited and the ones that support it are not among the budget options.

My personal goals were to build my smart home network around HomeKit and here’s why.

Slide from WWDC2017 session “What's New in HomeKit.” Image: Apple Slide from WWDC2017 session “What’s New in HomeKit.” | Image: Apple

It’s secure. When you add a device to your profile, it generates a unique token for that specific device which is used when the Home Hub is talking with the device. That way, the only device capable of sending and receiving anything from your smart home devices is the Home Hub (Apple TV / iPad / HomePod). From then on, the communication between your mobile device and the Home Hub is securely established via iCloud. No need to open ports in your network in order to access your devices remotely, that happens via the cloud.

I already have the hardware. I am deep into Apple’s ecosystem - I use iPhone, iPad, Apple Watch, Mac and others and they all work out of the box with HomeKit and have the Home app. If you are similar position, HomeKit is the way to go.

As with Alexa and Google Home, if you want to use HomeKit remotely, you need a hub set up. I am using an Apple TV for that, but you can also use an iPad or a HomePod.

I wrote a lot about my choice because choosing the firmware to go with is essential, depending on your goals.


Let’s get started

As I wrote above, I am going to use HAA. I am also going to be using a Mac, so some things may be done a bit differently in Windows.

There is a new version of this tutorial, where I explain an easier way to flash your device. Read it / Watch it here.

Shortcuts: JSON Strings / Setup in HomeKit

Prerequisites:

  1. USB-to-Serial/TTL Adapter - Amazon / AliExpress
  2. Device
  3. Software

I will be using Sonoff Basic. Currently with HAA, the following ESP8266 boards are supported: Sonoff and Shelly and others or custom ESP8266, if you can write your JSON config.

First things first. Connect to the Sonoff via the TTL Adapter.

USB Serial - Sonoff
       GND -> GND
        TX -> RX
        RX -> TX
      3.3V -> 3V3

My helpful screenshot

After you’ve connected and set up your TTL Adapter let’s see how we can access it:

ls /dev

You should get a list of devices, disks and etc. We are looking for something like /dev/tty.usbserial This is the port we will be using to access the ESP chip.

Something important to know is that before each command we send to the chip, you have to reboot it in flash mode. To boot the device into flash mode, hold down the button for the first 8-10 seconds after powering it on. For other devices this procedure differs, you connect GPIO0 to Ground. (This is what pushing the button on the Sonoff Basic does)

Erasing Memory

Erase the flash:

esptool.py -p /dev/tty.usbserial erase_flash

Now that we have a clean plate. We need the firmware to put on it.

Downloading HAA and Flashing it

Download haaboot.bin, rboot.bin and blank_config.bin and put them in a folder HAA on your Desktop for example.

Go to the folder with the downloaded files:

cd ~/Desktop/HAA

Flash the device with HAABoot (make sure to specify the right serial port, for me it is /dev/tty.usbserial):

esptool.py -p /dev/tty.usbserial --baud 115200 write_flash -fs 1MB -fm dout -ff 40m \
0x0 rboot.bin 0x1000 blank_config.bin 0x2000 haaboot.bin

Initial Setup

You can now reboot the device without putting it into flash mode and it will go into Access Point mode. To set it up, you can connect to it via Wi-Fi using any smartphone or computer.

In recent versions of the HAA firmware, to access the setup page you need to open http://192.168.4.1:4567 in your browser while connected to the device via WiFi.

OTA Config

Configuration JSON Strings for Homekit Accessory Architect:

Sonoff Basic / SV / Mini / TH / POW:

{"c":{"l":13,"b":[{"g":0,"t":5}]},"a":[{"0":{"r":[{"g":12}]},"1":{"r":[{"g":12,"v":1}]},"b":[{"g":0}]}]}

Shelly 1:

Switch with external toggle

{"a":[{"0":{"r":[{"g":4}]},"1":{"r":[{"g":4,"v":1}]},"b":[{"g":5,"p":0},{"g":5,"t":0}]}]}

MagicHome RGB Controller

other devices

Now you can tap on Save and wait for the device to download the full firmware by itself.

Congratulations, you have a device flashed with Homekit Accessory Architect and support for HomeKit and OTA updates, yay!

Setup in HomeKit

To use and configure the device, you should add it to the Home app. To do that, open the Home app on any iOS device and tap on the + button, and then Add Accessory. Point your camera to one of the images bellow:

HomeKit Pairing QR

HomeKit Pairing Image

If you see a new device, go ahead and add it (first time adding takes a bit). If you don’t see it, most probably the device is still configuring itself. If you can turn it’s relay on and off using the button, it should be ready. After the device is paired, its led will turn on for a few seconds.

If the device can’t connect to the wi-fi in the first 5 minutes after boot or you have entered invalid JSON, it will go into AP or Setup mode. You can check your JSON string here.

Now that you have a new HomeKit device, you can access it from within your home network or anywhere around the world if you have internet and a HomeKit Hub.

Additional HAA configuration

If you are using a Sonoff Basic, most likely what we’ve done so far is enough. Still, there is a lot more configuration options we can and in some cases have to configure.

To enter setup mode, by default, you must quickly toggle any action switch/button 8 times (physical or from Home App). Afterwards if the device is connected to your network, you can access it via its ip (port 80) or hostname, which is the same as the default device name.

ex: HAA-123AB -> http://haa-123AB.lan or http://haa-123ab.local

More about HAA’s configurable options you can see in their wiki.

Flashing your device by following my instructions you do it at your own risk. Opening some devices might void their warranty. Furthermore, flashing HAA removes your old firmware and if you want to get it back, you need to re-flash that manually.

Make sure to subscribe to the channel, so you don’t miss my future videos on smart home stuff and other useful tutorials.