Replacing the ITEAD Sonoff firmware

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).

sonoff_wifi_wireless_smart_switch_4__1

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!

Lets flash!

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.

2016-05-22 14.13.54

 

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:

Programmer Sonoff (counting from top to bottom)
3V3 1
TX 2 (RX)
RX 3 (TX)
GND 4
5

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.

Pin functions

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.

Function GPIO NodeMCU
Button 0 3
Relay 12 6
Green LED 13 7
Spare (pin 5) 14 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.

My code

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.

-- Timers
-- 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"

deviceID="bugzapper"
roomID="1"

wifi.setmode(wifi.STATION)
wifi.sta.config ("SSID", "PASSKEY")


-- Pin which the relay is connected to
relayPin = 6
gpio.mode(relayPin, gpio.OUTPUT)
gpio.write(relayPin, gpio.LOW)


-- Connected to switch with internal pullup enabled
buttonPin = 3
buttonDebounce = 250
gpio.mode(buttonPin, gpio.INPUT, gpio.PULLUP)


-- MQTT led
mqttLed=7
gpio.mode(mqttLed, gpio.OUTPUT)
gpio.write(mqttLed, gpio.HIGH)

-- Make a short flash with the led on MQTT activity
function mqttAct()
	if (gpio.read(mqttLed) == 1) then gpio.write(mqttLed, gpio.HIGH) end
	gpio.write(mqttLed, gpio.LOW)
	tmr.alarm(5, 50, 0, function() gpio.write(mqttLed, gpio.HIGH) end)
end

m = mqtt.Client("Sonoff-" .. deviceID, 180, mqttUser, mqttPass)
m:lwt("/lwt", "Sonoff " .. deviceID, 0, 0)
m:on("offline", function(con)
	ip = wifi.sta.getip()
	print ("MQTT reconnecting to " .. mqttBroker .. " from " .. ip)
    tmr.alarm(1, 10000, 0, function()
		node.restart();
	end)
end)


-- 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
			gpio.write(relayPin, gpio.LOW)
			print("Was on, turning off")
		else
			gpio.write(relayPin, gpio.HIGH)
			print("Was off, turning on")
		end
		
		mqttAct()
		mqtt_update()
	end
end)


-- Update status to MQTT
function mqtt_update()
	if (gpio.read(relayPin) == 0) then
		m:publish("/home/".. roomID .."/" .. deviceID .. "/state","OFF",0,0)
	else
		m:publish("/home/".. roomID .."/" .. deviceID .. "/state","ON",0,0)
	end
end
 
-- On publish message receive event
m:on("message", function(conn, topic, data)
	mqttAct()
    print("Recieved:" .. topic .. ":" .. data)
		if (data=="ON") then
		print("Enabling Output")
		gpio.write(relayPin, gpio.HIGH)
	elseif (data=="OFF") then
		print("Disabling Output")
		gpio.write(relayPin, gpio.LOW)
    else
		print("Invalid command (" .. data .. ")")
    end
	mqtt_update()
end)


-- Subscribe to MQTT
function mqtt_sub()
	mqttAct()
	m:subscribe("/home/".. roomID .."/" .. deviceID,0, function(conn)
		print("MQTT subscribed to /home/".. roomID .."/" .. deviceID)
    end)
end

tmr.alarm(0, 1000, 1, function()
	if wifi.sta.status() == 5 and wifi.sta.getip() ~= nil then	
		tmr.stop(0)
		m:connect(mqttBroker, 1883, 0, function(conn)
			gpio.write(mqttLed, gpio.HIGH)
			print("MQTT connected to:" .. mqttBroker)
			mqtt_sub() -- run the subscription function
		end)
	end
 end)
263 Comments

Add a Comment

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