FREE Shipping on all orders over $99 & LIMITED TIME USE CODE "NEW" FOR 10% OFF

Control: Sonoff Power Strip Custom Firmware Windows

Sonoff Power Strip

Custom Firmware Windows: 

Hey guys Matt here from MKSmartHouse.com and in this video I am going to show you how to install custom firmware onto the Sonoff Dual using a Windows Computer.
 
Before I begin, in this video we will be using a Windows Computer to do all the software, if you are using a MAC then go watch my video where I use a MAC. 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.
1. Before we continue make sure the main power going into the device is unplugged.
2. Anyway, to take it out first unscrew the small white screws using the regular flat screwdriver.
3. Then lift off the white wall plate the covers up everything around the outlets.
4. 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.
5. Next lift out the outlets and put them off to the side.
6. Then push the main power cable in a little so we have more cable to work with inside.
7. Take the Philips screwdriver and remove the screw holding down the wire cover on the sonoff.
8. 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.
 
9. 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.
10. Next grab a Philips screwdriver and remove the four silver screws holding down the PCB to the plastic part.
11. 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.
 
12. 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.
 
13. To solder in the wire first put some solder on the wire.
14. Second, if your exposed copper is long like mine then shorten it using the wire cutters.
15. Third hold the wire onto the side of the component that I mentioned previously.
16. 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.
 
17. 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.
 
18. First grab the header and put it into the holes that say VCC, RX, TX and GND.
19. Second put some flux onto the connections.
20. Third grab your soldering iron and some solder and solder it in.
21. 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.
22. The first thing we are going to do is grab the arduino and put a jumper cable from RES to GND.
23. 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.
24. Before we continue, connect the dupont wire we soldered into an available male GND pin on the Sonoff.
25. 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.
 
26. 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. 
27. On the new page press download.
28. Then go to your file explorer and downloads folder and double click on MK-SonoffPowerStrip.ino.
29. 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.
 
30. 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.
 
31. 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.
32. Next is the update path and personally I don’t change that.
33. 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.
 
34. The first one is the subscribeTopic and this is the topic for which the device listens for commands or messages from the server.
35. The next one is the MQTT Server Ip address and this is simply the IP address of your home automation or openhab server.
36. 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.
 
37. 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 COMX where X is some number.
 
But, since this is a sonoff there are a couple more parameters we have to check.
 
38. Make sure the flash mode is DOUT and the flash size is 1M (64K SPIFFS).  
39. 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!
 
40. 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.
41. Also, unplug the Flash wire.
42. Finally plug the Arduino USB cable back into the computer.
43. 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.
 
44. Then click on the publish tab.
45. In the topic bar type in the subscribeTopic that we wrote in the Sonoff Power Strip device code that we flashed.
46. Next in the message box type in C1ON and press publish.
 
The Red Light of the device should be on.
 
47. 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.
 
48. But, before we do that let’s unplug the cables going into the Sonoff from the Arduino and put the power strip back together.
49. First put the white plastic part back on the sonoff.
50. 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.
 
51. Third screw the wire cover back onto the sonoff.
52. Fourth tuck all the cables and outlets back into the blue enclosure.
53. Fifth screw the outlets down using the philips screw driver.
54. Sixth, put the white plate back on and screw it down.
55. Finally go ahead and plug the power strip into the wall.
 
Now, we can move on to adding it to openhab.
 
56. 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.
 
57. So type in sudo nano /etc/openhab2/items/home.items and press enter, you may need to type in admin password.
58. Then type in the comment //Sonoff Power Strip then underneath that we are going to create 2 switches to control the Sonoff Power strip.
59. Type in
Switch MKSonoffPowerStripChannel1 "Channel 1" <poweroutlet> [ "Switchable" ] {mqtt=">[broker:MK-SmartHouse/utilities/MK-SonoffPowerStrip1:command:ON:C1ON],>[broker:MK-SmartHouse/utilities/MK-SonoffPowerStrip1:command:OFF:C1OFF]"}
Switch MKSonoffPowerStripChannel2 "Channel 2" <poweroutlet> [ "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.
 
60. Now press control x then y and enter.
 
Next up is the sitemap file so we can control the device and our other switches.
 
61. 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.
 
62. 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.
 
63. Now press control x then y and enter.
64. 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.
65. If you turn on each channel switch then the relays in the device should turn on.
 
You will hear a click.
 
66. 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.
 
67. In ssh type in sudo nano /etc/openhab2/rules/home.rules and press enter.
68. 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.
 
69. Personally I go to http://www.cronmaker.com/ and set when I want something to happen then copy and paste the cron expression it gives me.
70. Let’s do an example, I am going to set it to every 2 days at 6 am, then press generate cron expression.
71. 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.
 
72. 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.
73. 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 mksmarthouse.com/forum where you have a better chance of it getting answered. Good Bye!