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
TX RX
RX TX
GND GND

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:

switch:
  - 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

10 Favorite DIY IoT Projects using the esp8266

So many good uses for these chips, here are a few I have bookmarked for later projects.

  1. ESP Easy – by far the easiest way to connect a bunch of cheap sensors to an esp8266. I have about 6 of these doing various things.
  2. Bed Occupancy Sensor – FSR sensor for pressure and a simple automation to determine bed occupancy
  3. WiFi Candle Using multiple WS2812 addressable LEDs to create a simple lamp that flickers like a candle.
  4. Notification Flag – Raise a flag based on a simple condition. Would be nice to combine in tandem with…
  5. Mailbox Sensor – Notify when mailbox is opened.
  6. DIY Milight Hub – I’ve been using this and it works great. Control as many cheap Chinese RGB bulbs as you want.
  7. Simple Water Alarm – If water shorts the wires, it wakes a sleeping esp8266 and sounds the alarm
  8. Door/Window Monitor – Along the same lines, a simple magnetic switch monitors the status of a window or door.
  9. Smart Power Strip – Retrofitting a power strip with 2 banks of controllable outlets, using 3D printed parts.
  10. Weather Station Display – Another super cool 3D printed project that plugs in and displays info on a small screen.