About a week ago I received a ITEAD Sonoff, which is a nifty little device, for the surprisingly low price of only $4.85 (currently typing, normal price $6).
There are two versions, one with and one without RF, I went for the one without, because I will only be using it over WiFi.
Why the Sonoff?
The Sonoff isn’t the first of its kind on the market, so why this, and not one of the other?
The choice of this all comes down to how much control you want to have over your items, and how much you want others to just take care of it for you. As default the Sonoff comes pre-loaded with firmware, and can be controlled from a smartphone from the app for it. This is pretty much the same deal with other devices on the market, but with the Sonoff you are able to take it a step further.
Inside the Sonoff, there is an ESP8266 hidden, and this takes care of everything, and any one spending just a few minutes on this blog, have most likely found this IC mentioned at least once already. Not only does it have a chip easy to program, ITEAD was also nice enough to break the pins out, needed for flashing it. And this is exactly what I am about to do!
Before ordering the Sonoff, I had been doing a bit of reading on how to flash it, and found it very confusing, but I ordered it, and hoped it would all make sense when I could actually look at it, sadly, it was just as confusing after getting it. So instead I closed all the pages, and decided to give it a shot myself, and it turned out to be simpler than I thought.
Before doing anything, disconnect the Sonoff from mains.
First thing you need to do, is to solder a header to the 1×5 row of holes.
These go to the TX and RX pins on the ESP8266, and are also used to provide 3v3 and GND to run the circuit.
Then it should look something like on the picture above.
The connection closest to the button at the top, is 3v3, underneth TX, then RX and last GND. The last connection is not one we are going to use for this.
To attach the programmer, first make sure it is set to 3V3, then connect it as following:
Sonoff (counting from top to bottom)
Then hold the button down on the Sonoff, and attach the programmer to your computer. Holding the button down while applying power to the ESP8266 will bring it into flash mode, which will allow you to put new firmware onto it.
My choice was to put NodeMCU onto it, which went without a hitch.
After having put another firmware onto the ESP8266, we would probably want to be able to control the relay again, and for that we need the pin number.
Spare (pin 5)
The LED on the board is actually a red/green bi-color led, but the red side is only connected to the RF module, which in my version isn’t mounted.
After putting NodeMCU on mine, I modified some of my old code to match the pins on the Sonoff. This code will make it connect to a MQTT borker, turn the relay on and off, flash the led on activity, and allow the button on the Sonoff to act as a toggle switch for the relay too.
-- 0 = WiFi status + mqtt connect
-- 1 = MQTT offline
-- 2 = Free
-- 3 = Free
-- 4 = Free
-- 5 = MQTT activity
-- 6 = Button debounce
mqttBroker = "192.168.1.30"
mqttUser = "none"
mqttPass = "none"
wifi.sta.config ("SSID", "PASSKEY")
-- Pin which the relay is connected to
relayPin = 6
-- Connected to switch with internal pullup enabled
buttonPin = 3
buttonDebounce = 250
gpio.mode(buttonPin, gpio.INPUT, gpio.PULLUP)
-- MQTT led
-- Make a short flash with the led on MQTT activity
if (gpio.read(mqttLed) == 1) then gpio.write(mqttLed, gpio.HIGH) end
tmr.alarm(5, 50, 0, function() gpio.write(mqttLed, gpio.HIGH) end)
m = mqtt.Client("Sonoff-" .. deviceID, 180, mqttUser, mqttPass)
m:lwt("/lwt", "Sonoff " .. deviceID, 0, 0)
ip = wifi.sta.getip()
print ("MQTT reconnecting to " .. mqttBroker .. " from " .. ip)
tmr.alarm(1, 10000, 0, function()
-- Pin to toggle the status
buttondebounced = 0
gpio.trig(buttonPin, "down",function (level)
if (buttondebounced == 0) then
buttondebounced = 1
tmr.alarm(6, buttonDebounce, 0, function() buttondebounced = 0; end)
--Change the state
if (gpio.read(relayPin) == 1) then
print("Was on, turning off")
print("Was off, turning on")
-- Update status to MQTT
if (gpio.read(relayPin) == 0) then
m:publish("/home/".. roomID .."/" .. deviceID .. "/state","OFF",0,0)
m:publish("/home/".. roomID .."/" .. deviceID .. "/state","ON",0,0)
-- On publish message receive event
m:on("message", function(conn, topic, data)
print("Recieved:" .. topic .. ":" .. data)
if (data=="ON") then
elseif (data=="OFF") then
print("Invalid command (" .. data .. ")")
-- Subscribe to MQTT
m:subscribe("/home/".. roomID .."/" .. deviceID,0, function(conn)
print("MQTT subscribed to /home/".. roomID .."/" .. deviceID)
tmr.alarm(0, 1000, 1, function()
if wifi.sta.status() == 5 and wifi.sta.getip() ~= nil then
m:connect(mqttBroker, 1883, 0, function(conn)
print("MQTT connected to:" .. mqttBroker)
mqtt_sub() -- run the subscription function