The Cheapest WiFi Smart Switch: Hacking the $5 Sonoff

The Chinese iTead Sonoff switches are definitely the cheapest way to get a smart switch integrated into a controller like Home Assistant. They cost about $5 if you order from overseas for the basic model. A power sensing version is available, and it’s easy to and a temperature/humidity sensor yourself. Even if you were to order the parts yourself (an esp8266, relay, optional DHT22) you can’t do it for less. For basic, less than 10amp devices they are very reliable and economical.

All we need to do is flash it with an open source firmware that uses MQTT instead of the cloud based service they ship with. There are several options, but I like the extremely simple and stable Sonoff-HomeAssistant.

Stuff Needed:

First step is to open up the switch and solder in a 5 pin connector to the GPIOs on the board. For the standard switch that looks like this:

Then connect those to a USB FTDI adapter, like this. Note the RX and TX are crossed.

3V3 3V3 / VCC

That’s the hardware side. Now for software, we will be flashing using the Arduino IDE. Download the code for Sonoff-HomeAssistant and use the appropriate version for your Sonoff. You will need to change the default name and add your wifi settings. In the Arduino IDE, use these settings:

Screen Shot 2017-08-27 at 6.05.02 PM

Plug your USB FTDI converter in while holding down the button on the Sonoff while it turns on. Then when your settings are set to the above, hit the Upload button. It should successfully upload.

Now wire in your device. If all went well, you should see it connect to your MQTT and start sending messages. To add it as a switch in Home Assistant, change your topics to match your settings:

  - platform: mqtt
    name: "Living Room"
    state_topic: "home/sonoff/living_room/1/stat"
    command_topic: "home/sonoff/living_room/1"
    qos: 0
    payload_on: "on"
    payload_off: "off"
    retain: true