The Cheapest WiFi Smart Switch: Flashing the Sonoff Basic

ConfigElectronicsHardwareHome AssistantNode-RedSmart Home

Written by:

Setting up a smart home can be a costly affair, if every device needed an expensive proprietary smart switch you would spend quite a bit of money. With a very tiny amount of soldering and some custom firmware, the Sonoff Basic model can be turned into a locally controlled MQTT enabled WiFi smart switch for about $7 each. These tiny little esp8266 based boards can be spliced into a power cord to add WiFi control to anything.

I have about a dozen of these and they work great. Because of how cheap they are, I have started adding them to some ridiculous devices. Today’s DIY WiFi retrofit: my coffee mug warmer.

I want my mug warmer to come on automatically when I enter my office in the morning, but only if I’ve made coffee already. But first, I need to flash some new firmware onto my Sonoff.


PARTS LIST

You will need:

FLASHING THE SONOFF

SOFTWARE

To communicate with the Sonoff, we hook it up to the  serial adapter, and plug that into the computer (you may or may not need a FTDI serial driver, depending on OS). I use the Arduino IDE to flash the new software.

In the Arduino Preferences, add the additional boards URL for the Boards Manager

Arduino IDE - Additional Boards

Arduino IDE – Additional Boards

http://arduino.esp8266.com/stable/package_esp8266com_index.json

Open the Boards Manager, and add the esp8266. Some versions of the Sonoff have issues with the newest version of the board, I recommend installing 2.3.0

Board Manager

Board Manager

HARDWARE

Here’s a diagram of what’s on the board:

Sonoff Basic esp8266 board

Sonoff Basic esp8266 board

Solder 4 or 5 header pins to the Sonoff Basic to the open through holes:

Sonoff Basic pinout

Sonoff Basic pinout

We needs pins on VCC, RX, TX, and GND (and the fifth GPIO14 if you want to add a DHT sensor). The pin closest to the button is the power (VCC).

Programmer – Sonoff 

  • 3V VCC – 3V VCC
  • TX – RX
  • RX – TX
  • GND – GND
  • GPIO14 – Data pin on the DHT22

Once you have the pins in place, connect a Dupont cable and attach it to the appropriate pins on the FTDI adapter. On my FTDI adapter, you can choose 3V or 5V with a jumper. Make sure yours is set to 3V for the esp8266.

!!! WARNING !!! Never connect the USB adapter and AC power at the same time, it’s dangerous!


FLASHING THE FIRMWARE

There are a lot of options for flashing the Sonoff. I am going to use the simple KmanSonoff. If you would like something more full featured, check out Sonoff-Tasmota. KmanSonoff is very lightweight and foolproof. It controls the switch, and a DHT22 humidity/temp sensor if you want. That’s it.

Download the KmanSonoff repository and the non-default MQTT library it uses, unzip both. Place the pubsubclient folder in your Arduino/libraries folder. Now open the KmanSonoff/arduino/KmanSonoff_v1.00sc INO file.

In config_sc.h, add your settings, which is mostly MQTT and the topic:

#define MQTT_SERVER "192.168.0.100" // Your mqtt server ip address
#define MQTT_PORT 1883 // Your mqtt port
#define MQTT_TOPIC "home/office/coffee_warmer" // Base mqtt topic
#define MQTT_USER "mqtt_user" // mqtt username
#define MQTT_PASS "mqtt_pass" // mqtt password

#define WIFI_SSID "wifissid" // Your WiFi ssid
#define WIFI_PASS "wifipass" // Your WiFi password

You can also enable the DHT22 if you’re using it.

RELATED >>  Presence Detection Part 2: Improving Presence with Node-Red

Now attach the FTDI programmer to the computer while holding down the button on the Sonoff. Holding down the button puts it in flashing mode. Now, in the Arduino IDE, select the appropriate port and the “Generic esp8266” option for the board. Set Flash Size to “1M (64K SPIFFS)” and Flash Mode to “DOUT” (note: some boards use DIO, use that if this doesn’t work for you).

Arduino Flash Settings

Arduino Flash Settings

Go to Sketch-> Upload. If all went well the LED will flash 4 times when it completes.

Flashing success

Flashing success

If you get an “esp comm” error you either didn’t wire it correctly (check the RX / TX) or didn’t hold down the button while plugging in the programmer.

If you didn’t get 4 flashes at the end of the upload, or nothing is happening when you plug it in, check your board settings.

Make sure you put the board back into it’s case in the correct orientation (the Sonoff verison number is in the bottom right).

When you plug it into AC power, it will flash 4 times when it connects to the wifi.

CONNECTING TO HOME ASSISTANT

To integrate with Home Assistant, I just add a MQTT switch to my switches.yaml file:

- platform: mqtt
  name: "Coffee Warmer"
  state_topic: "home/office/coffee_warmer/stat"
  command_topic: "home/office/coffee_warmer"
  qos: 0
  payload_on: "on"
  payload_off: "off"
  retain: true

AUTOMATING!

My desired functionality is this: I want the mug warmer to turn on when I sit down at my office desk, but only if I’ve made coffee this morning. I have a Wemo enabled coffee maker that reports it’s state, a motion detector at my office desk, and the now smart mug warmer.

Using Home Assistant and Node-Red, let’s make this happen.

I’ve made a history stats sensor in Home Assistant to count how many times I’ve made coffee this morning (between 5am – 12pm). I drink a lot of coffee so this is kind of in the realm of things-I-don’t-really-want-to-know, but thanks technology.

- platform: history_stats
  name: Coffee This Morning
  entity_id: sensor.coffeemaker
  state: 'Brewing'
  type: count
  start: '{{ now().replace(hour=5).replace(minute=0).replace(second=0) }}'
  duration:
    hours: 7

Now I’m going to make a flow in Node-Red. When motion is detected in my office, check to see if it’s morning, then check to see if I made coffee. If so, turn on the mug warmer.

Turn on Mug Warmer Flow

Turn on Mug Warmer Flow

JSON:

[{"id":"17607704.59aa59","type":"server-state-changed","z":"45760804.d260f8","name":"Office Motion","server":"8ac3cd7f.58d3e","entityidfilter":"binary_sensor.office_motion","entityidfiltertype":"exact","haltifstate":"","x":130,"y":1800,"wires":[["8b5dd6af.2227a8"]]},{"id":"8b5dd6af.2227a8","type":"switch","z":"45760804.d260f8","name":"","property":"payload","propertyType":"msg","rules":[{"t":"eq","v":"on","vt":"str"},{"t":"eq","v":"off","vt":"str"}],"checkall":"true","outputs":2,"x":130,"y":1840,"wires":[["271d3505.48f2aa"],[]]},{"id":"34470c82.d2a044","type":"comment","z":"45760804.d260f8","name":"Mug Warmer On if I made AM Coffee","info":"","x":210,"y":1740,"wires":[]},{"id":"271d3505.48f2aa","type":"time-range-switch","z":"45760804.d260f8","name":"","lat":"37.79952","lon":"-122.26872","startTime":"05:00","endTime":"12:00","startOffset":0,"endOffset":0,"x":330,"y":1820,"wires":[["1a21b3e9.857a4c"],[]]},{"id":"1a21b3e9.857a4c","type":"api-current-state","z":"45760804.d260f8","name":"Coffee This Morning?","server":"8ac3cd7f.58d3e","halt_if":"","entity_id":"sensor.coffee_this_morning","x":540,"y":1820,"wires":[["b3402460.2ad4d8"]]},{"id":"b3402460.2ad4d8","type":"switch","z":"45760804.d260f8","name":"","property":"payload","propertyType":"msg","rules":[{"t":"gt","v":"0","vt":"num"}],"checkall":"true","repair":false,"outputs":1,"x":710,"y":1820,"wires":[["688eba8a.db0ab4"]]},{"id":"688eba8a.db0ab4","type":"api-call-service","z":"45760804.d260f8","name":"Turn on Mug Warmer","server":"8ac3cd7f.58d3e","service_domain":"switch","service":"turn_on","data":"{\"entity_id\":\"switch.coffee_warmer\"}","mergecontext":"","x":910,"y":1820,"wires":[[]]},{"id":"8ac3cd7f.58d3e","type":"server","z":"","name":"Home Assistant","url":"HASSURL","pass":"PASS"}]

I already have a flow to turn off my office when I leave, so that will also turn the mug warmer off. See my post on basic lighting automations with Node-Red if you’re interested in that flow. Since this appliance involves a heating element, I’m going to set up a secondary auto-off if it’s left on for more than 3 hours.

Auto-Off After 3 Hours

Auto-Off After 3 Hours

JSON:

[{"id":"c266179c.461328","type":"server-state-changed","z":"45760804.d260f8","name":"Mug Warmer","server":"8ac3cd7f.58d3e","entityidfilter":"switch.coffee_warmer","entityidfiltertype":"exact","haltifstate":"","x":130,"y":1940,"wires":[["55b9c7d3.92e3a8"]]},{"id":"ec46f198.b5833","type":"comment","z":"45760804.d260f8","name":"Turn Off After 3 Hours","info":"","x":160,"y":1900,"wires":[]},{"id":"55b9c7d3.92e3a8","type":"switch","z":"45760804.d260f8","name":"","property":"payload","propertyType":"msg","rules":[{"t":"eq","v":"on","vt":"str"},{"t":"eq","v":"off","vt":"str"}],"checkall":"true","repair":false,"outputs":2,"x":290,"y":1940,"wires":[["ca2c5756.362d18"],["77659ee6.cecd5"]]},{"id":"ca2c5756.362d18","type":"stoptimer","z":"45760804.d260f8","duration":"3","units":"Hour","payloadtype":"num","payloadval":"0","name":"","x":490,"y":1940,"wires":[["329c9d6b.6aece2"],[]]},{"id":"77659ee6.cecd5","type":"change","z":"45760804.d260f8","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"STOP","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":260,"y":2000,"wires":[["ca2c5756.362d18"]]},{"id":"329c9d6b.6aece2","type":"api-call-service","z":"45760804.d260f8","name":"Turn off Mug Warmer","server":"8ac3cd7f.58d3e","service_domain":"switch","service":"turn_off","data":"{\"entity_id\":\"switch.coffee_warmer\"}","mergecontext":"","x":740,"y":1940,"wires":[[]]},{"id":"8ac3cd7f.58d3e","type":"server","z":"","name":"Home Assistant","url":"HASSURL","pass":"PASS"}]

I now have the world’s smartest coffee warmer. Truly solving the big problems here.

6 Replies to “The Cheapest WiFi Smart Switch: Flashing the Sonoff Basic”

  1. Afan says:

    Please excuse this comment without any contribution. Just wanted to tell you that I really like this blog ?

  2. Kuan Yew says:

    Maybe you could help me out here a bit. What if I’m building a new house, so the Wifi & the MQTT isn’t up yet on the new house. But I want these Sonoffs (Basic, 4CH and T1) to be embedded in the walls of the house. Is it possible to do the changing of passwords and wifi thingy AFTER the house is ready?

    Or do I have to put in fake passwords and IDs, now then only use the same passwords later when the Internet connection is up in the new house?

    • brad says:

      You can flash the firmware over-the-air AFTER you’ve done the initial flashing via serial connection. They would need to be connected to WiFi to do this.

      If it were me I would get an old access point, set up a test network, and do the initial set up on the Sonoffs and get them connected to that. After they’re installed in the house, turn on the test wifi and then flash them all OTA with the correct settings.

      • Kuan Yew says:

        Thanks for the detailed reply. Will try out your suggestions and see how it goes. As for the old access point, that’s a bit hard as the house is a new build.

        • Craig Larson says:

          Brad, thanks for the post.

          Kuan, if you haven’t built that house yet put the Sonoff switches in the fixtures or lighting circuits. Then at the switches you only need pushbuttons. This means that you can save all the wiring that goes back and forth from the circuit to the switches. However you will need to have some limited power to the switches. But the range of options including battery, DC converters, or even a closer AC source. I just built before this tech was available. The wiring required by three-way switches was entirely unnecessary. With the Sonoffs you could control circuits/fixtures with as many switches as you want… and you can move them around! And you can move them where ever you want. Nice. The new gen of Sonoffs have 433MHz alternative in case your WiFi goes down.

  3. […] post The Cheapest WiFi Smart Switch: Flashing the Sonoff Basic appeared first on DIY […]

Leave a Reply

Your email address will not be published. Required fields are marked *