Sonoff Complete Guide : Custom Firmware/Software OpenHAB MQTT (Using MAC)

Sonoff Complete Guide : Custom Firmware/Software OpenHAB MQTT (Using MAC)

Hey guys Matt here from and
in this video I am going to show you how to install custom firmware onto the Sonoff Dual
using a MAC. [Intro] Before I begin, in this video we will be using
a MAC to do all the software, if you are using a Windows computer then go watch my video
where I use a Windows computer. In the last video we left off with the Sonoff
Dual Powerstrip being fully built and running the stock firmware. If you haven’t seen that video there will
be a card on the screen or there will be a link to it in the description as well as on
my website. The stock firmware is great for if you want
something basic and want to use the ewelink eco system. But, if you have been following my channel
and are like me then you will want to connect this to your home automation system that you
have already established and love so much. So in this video we will be flashing custom
firmware that I wrote on to the Sonoff which will allow it to communicate with an OpenHab
server through MQTT. The reason we can do this is because the brain
of the Sonoff Dual is an ESP8266 which means we can treat it like a typical device I have
shown you to make and can flash it with the Arduino IDE. But, before we get into the flashing lets
go over the parts and tools we will need because there is just a little bit of hardware stuff
we need to do. For parts the only things we are going to
need is a female DuPont jumper cable, wall tac and a 4 pin female header. Then for tools we are going to need wire strippers,
wire cutters, solder, soldering iron, helping hands, PCB holder, small flat screwdriver,
regular flat screwdriver, Phillips screwdriver, dupont jumper cables and an arduino. You will understand why we need each tool
as we go along. The links to all these tools and parts will
be over on my website, the link is in the description. The first thing we have to do is take the
Sonoff out of the power strip we made in the last video. Before we continue make sure the main power
going into the device is unplugged. Anyway, to take it out first unscrew the small
white screws using the regular flat screwdriver. Then lift off the white wall plate the covers
up everything around the outlets. Now you should have access to the silver screws
that are holding down the outlets to the blue plastic enclosure, remove them using the Philips
screw driver. Next lift out the outlets and put them off
to the side. Then push the main power cable in a little
so we have more cable to work with inside. Take the Philips screwdriver and remove the
screw holding down the wire cover on the sonoff. Now take the small flat screwdriver and unplug
all the cables by pushing down on the white button and puling out the cable. Great the Sonoff is now removed! The second thing we have to do is solder two
parts to the Sonoff so we can program it. The first part we have to solder in is what
I like to call a flash wire and it is a wire that temporarily gets connected from gpio
0 to ground in order to put the esp8266 into programming mode. To do this we have take the Sonoff out of
it’s casing so, jam your finger nails or use a screwdriver and pop of the big housing. Next grab a Philips screwdriver and remove
the four silver screws holding down the PCB to the plastic part. Then take the female DuPont jumper wire and
cut of one end and then strip off a little insulation to reveal the copper. Now comes the very tight job of soldering
a little tiny wire onto a little tiny pad using a big soldering iron. Take the sonoff PCB and put it into the PCB
holder. What we have to do is solder this wire onto
this pad or side of the component that says 103 and is next to a yellow component. To solder in the wire first put some solder
on the wire. Second, if your exposed copper is long like
mine then shorten it using the wire cutters. Third hold the wire onto the side of the component
that I mentioned previously. Fourth grab the soldering iron and press down
a little on the cable so the solder flows and makes a nice connection. This is how it should look, make sure you
did not accidentally bridge any connections or components. Just as another measure of keeping it in place
I took some wall tac and put it over the connection. The next part we have to solder in is the
4 pin female header so we can communicate with the esp8266. First grab the header and put it into the
holes that say VCC, RX, TX and GND. Second put some flux onto the connections. Third grab your soldering iron and some solder
and solder it in. Finally, you can just take the PCB and screw
it back into the white plastic part with the 4 screws Great, that is it, those are all
the modifications we have to do to the Sonoff. Now all it needs is firmware and to be connected
to the home automation server. So let’s start off with the firmware. I recommend having my website open up so that
way you have all the steps and commands ready, and so you do not have to type in everything
you can just copy and paste. The first thing we are going to do is grab
the arduino and put a jumper cable from RES to GND. Then grab the Sonoff and connect all the pins
to their corresponding spots so, TX to TX, RX to RX, GND to GND. But unlike the devices we normally make on
this channel we have to connect VCC to 3V. Before we continue, connect the dupont wire
we soldered into an available male GND pin on the Sonoff. Once those are all connected plug the in the
USB cable from the arduino into the computer. Please note that in this video I will not
go over how to setup the arduino IDE and will assume that it is set up and you know how
to connect an arduino to it. If you do not know how to set it up or it
is not setup then go check out my Door sensor software video where I go in detail of the
entire process. Next we are going to head over to my site,
the link is in the description to the exact page and press the download MK-SonoffPowerStrip
Firmware. On the new page press download.Then go to
your finder and downloads folder and double click on MK-SonoffPowerStrip.ino. A pop up will come up asking if you want to
put it in a folder, click OK. It should bring up the code for the Sonoff
Power Strip, and there are only a few things we have to change. The first thing is the wifi settings which
are the ssid and password so change those according to your network. Please keep in mind that the esp8266 only
works on 2.4ghz so type in your 2.4ghz wifi ssid and password not your 5ghz. Also when adding the information only change
what is inside the quotation marks. Next set of parameters are the Web Updater
settings. The devices I designed are great because I
implemented a web user interface for each individual device so that way if you ever
have to flash new firmware you just go to its web address. The web address information is found at that
top of the code in the giant comment block. The first parameter is the hostname of the
device, usually I only change the last digit but since this is the first Sonoff Power strip
I will keep it as it is. Next is the update path and personally I don’t
change that. After that is the web user interface username
and password, these are the credentials you use to access the webpage because each device
is protected. The next set of parameters are for mqtt. The first one is the subscribeTopic and this
is the topic for which the device listens for commands or messages from the server. The next one is the MQTT Server Ip address
and this is simply the IP address of your home automation or openhab server. The last one is the Unique device ID and this
simply differentiates each device on the MQTT side, I usually just change the last digit
for every single device. That is it the code is ready to be flashed. Make sure the Arduino is plugged in and then
go to tools and make sure the Board: is Generic ESP8266 Module and the port is /dev/cu.wchusbserialXXXX. But, since this is a sonoff there are a couple
more parameters we have to check. Make sure the flash mode is DOUT and the flash
size is 1M (64K SPIFFS). Once those are good press the upload button,
it is the one with an arrow pointing to the right. When it is uploading you should see dots moving
at the bottom and some percents. After it is done uploading you should see
it say 100% and Done uploading. Let’s Test it! First unplug the Arduino USB cable from the
computer then unplug the dupont wires that go to TX and RX in between the arduino and
the Sonoff. Also, unplug the Flash wire. Finally plug the Arduino USB cable back into
the computer.To confirm that it flashed correctly and is working you can fire up MQTT.fx, connect
to the server and in the subscribe section type in # and press subscribe. If you do not have MQTT.fx then check out
my Home Automation Server Setup Guide link is in the description. Then click on the publish tab. In the topic bar type in the subscribeTopic
that we wrote in the Sonoff Power Strip device code that we flashed. Next in the message box type in C1ON and press
publish. The Red Light of the device should be on. Then in the message box delete what is there
and type in C1OFF and press publish. The device color should have changed to No
color. If you did then that means the device is connecting
with the server and has perfect communication. What we just did is manually send the MQTT
commands to the device. Since the Sonoff is not connected to AC power
at the moment the relays are not connected only the LED’s are, but, don’t worry about
this because if the LED’s work then the relays will work. If you want you can test both channels by
manually sending the commands just repeat the process of the commands and change the
number to match the channel you are testing 1 and 2. Now the device is complete and just needs
to be added to OpenHAB. But, before we do that let’s unplug the
cables going into the Sonoff from the Arduino and put the power strip back together. First put the white plastic part back on the
sonoff. Second connect the wires back to the sonoff. If you forgot which wire goes where then go
and watch my video where we make the power strip, again. Third screw the wire cover back onto the sonoff. Fourth tuck all the cables and outlets back
into the blue enclosure. Fifth screw the outlets down using the philips
screw driver. Sixth, put the white plate back on and screw
it down. Finally go ahead and plug the power strip
into the wall. Now, we can move on to adding it to openhab. Ssh into your pi or whatever your server may
be. The first thing we are going to do is create
the Sonoff Power Strip items. So type in sudo nano /etc/openhab2/items/home.items
and press enter, you may need to type in admin password. Then type in the comment //Sonoff Power Strip
then underneath that we are going to create 2 switches to control the Sonoff Power strip. Type in
Switch MKSonoffPowerStripChannel1 “Channel 1”[ “Switchable” ] {mqtt=”>[broker:MK-SmartHouse/utilities/MK-SonoffPowerStrip1:command:ON:C1ON],>[broker:MK-SmartHouse/utilities/MK-SonoffPowerStrip1:command:OFF:C1OFF]”}
Switch MKSonoffPowerStripChannel2 “Channel 2”[ “Switchable” ] {mqtt=”>[broker:MK-SmartHouse/utilities/MK-SonoffPowerStrip1:command:ON:C2ON],>[broker:MK-SmartHouse/utilities/MK-SonoffPowerStrip1:command:OFF:C2OFF]”} and press enter. Now let me go over the two items we created. They are the actual switches that will send
the mqtt commands and turn the relays on and off which will control the outlets. But let me go a little bit more in detail,
the first part is Switch and since this device is a power strip and we just turn the channels
on and off we use the switch type. The next part is the item name and I just
used its hostname without the dash and added the word channel with a number. After that is the label text and it is what
shows up in the interface and how it is formatted with the name. Next to that is the icon name which is what
picture shows up in the interface. Then lastly, we have the mqtt path to the
device in there is the subscribeTopic that we coded into the device. But as you can see there are two sets of MQTT
commands or lines for each device. This is because when the switch is turned
on through the interface it will send the CXON and when you turn it off it will send
CXOFF. X would be the number of the channel. Those two items are pretty much the same except
the number is incremented for each channel. That’s it for the items file. Now press control x then y and enter. Next up is the sitemap file so we can control
the device and our other switches. Type in sudo nano /etc/openhab2/sitemaps/home.sitemap
and press enter. It will bring up the sitemap. If you are following along with my series
then we have many different frames in our sitemap. I am going to create a new frame. So type in
Frame label=“Sonoff Power Strip” {
Switch item=MKSonoffPowerStripChannel1 Switch item=MKSonoffPowerStripChannel2
} and press enter, what we did is import the
items into the sitemap so we can control them from the user interface. Now press control x then y and enter. Before we go any further let’s confirm that
everything works so go to your web user interface and then Basic UI. You should see the 2 switches to control the
channels. If you turn on each channel switch then the
relays in the device should turn on. You will hear a click. If you turn the same channel switches off
then the relays should turn off and it will de click. Basically you should be able to control the
relays. Great! Now we know the channel switches work! Controlling the power strip through wifi is
awesome but let’s also make it so it can turn on by itself. What I mean by that is let’s create some
rules in openhab where it will turn on each channel at a certain time and turn off each
channel at a certain time. In terminal type in sudo nano /etc/openhab2/rules/home.rules
and press enter. In the file type in
//Turn on Sonoff power strip channels at 5 am
rule “Sonoff Power Strip On” when
Time cron “0 0 5 1/1 * ? *” then
sendCommand(MKSonoffPowerStripChannel1, ON) sendCommand(MKSonoffPowerStripChannel2, ON)
end Let me explain what this does, it turns on
both channels everyday at 5 am. Now, what if you did not want it to turn on
the channels everyday at 5 am but every other day or a different time. Openhab rules, utilises something called cron
time and will execute the rule based on what you set. Personally I go to
and set when I want something to happen then copy and paste the cron expression it gives
me. Let’s do an example, I am going to set it
to every 2 days at 6 am, then press generate cron expression. Now I am going to copy the expression it gave
me and paste it into the rules replacing the every day at 5. Next let’s move on to the turn of channels
rule. So paste into the rules file the following: //Turn on Sonoff power strip channels at 11
pm rule “Sonoff Power Strip On”
when Time cron ” 0 0 23 1/1 * ? *”
then sendCommand(MKSonoffPowerStripChannel1, OFF)
sendCommand(MKSonoffPowerStripChannel2, OFF) end What this does is turn off both channels everyday
at 11 pm. That is it for the rules. Now press control x then y and enter. That is it! The software is complete. This sonoff power strip is now ready for use! If you are interested in buying a sonoff I
will leave a link to it in the description. Alright thank you for watching and If you
have any questions leave them in the comments section below or head over to
where you have a better chance of it getting answered. Good Bye!

14 thoughts on “Sonoff Complete Guide : Custom Firmware/Software OpenHAB MQTT (Using MAC)

  • My Sonoff looks slightly different. It has a L e e L n n on the face of it. How do I hook the wires up? Do I use the N's as neutrals and the E's as grounds?

  • i dont have Port: "cu.wchusbserial14410" – i only have "cu.serial1" !? any ideas, i am using a single sonoff 10A and installed the C341SER-driver? thx

  • I started to do this with a sonoff but using home assistant i was using a single channel and it didn't have enough memory to flash. I now think I'll use your channel and openhab for my project but will I have the same problem?

  • The comments below at your code are right ?
    How would be with one channel ?

    if(channel1 && channel2) // channel1 true and channel2 true -> write 0011
    else if(channel1 && channel2 == false) // channel1 true and channel2 false -> write 0001
    else if(channel2 && channel1 == false) // channel1 false and channel2 true -> write 0010
    Serial.write(0x00); // channel1 false and channel2 false -> write 0000

  • thanks for the good video!
    I'm planning on using sonoff pow with tasmota firmware. the problem is that by pushing the button when powering the serial interface, I'm not able to go in update mode. can you explain how to easily turn on update mode on the pow version of sonoff? tks

  • Hi, thank you for the videos. I am trying to setup a sonoff dual R2 in Openhab2, but not without MQTT. Through HUE bridge, I have the problem when I turn on the switches, the relay close, and after 5 seconds, the satus change to OFF, although the relay stays closed. So I get a false satatus.. :/…. any solution? thanks.

    2018-04-02 01:15:14.670 [ome.event.ItemCommandEvent] – Item 'SonoffDUAL02_1' received command OFF
    2018-04-02 01:15:14.680 [vent.ItemStateChangedEvent] – SonoffDUAL02_1 changed from ON to OFF
    2018-04-02 01:15:16.216 [ome.event.ItemCommandEvent] – Item 'SonoffDUAL02_2' received command OFF
    2018-04-02 01:15:16.223 [vent.ItemStateChangedEvent] – SonoffDUAL02_2 changed from ON to OFF
    2018-04-02 01:15:24.580 [vent.ItemStateChangedEvent] – SonoffDUAL02_1 changed from OFF to ON
    2018-04-02 01:15:24.591 [vent.ItemStateChangedEvent] – SonoffDUAL02_2 changed from OFF to ON

  • i already did flash, and the Sonoff connects to the wifi, and relay works well when i have 3,3v connected to the sonoff, but when i connected just 220v, the relay doesn´t work. Anyone knows what is happened? thank you

  • So with this new software upload does that mean the computer have to stay on all the time in order for it to work? Or once Alexa is connected you won't need the computer to be on?

Leave a Reply

Leave a Reply

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

Copyright © 2019 Geted Tabs Online. All rights reserved.