FREE Shipping on orders over $49 USA $99 INTERNATIONAL

Sonoff Power Strip

Sonoff Power Strip

Hardware: 

 

Hey guys Matt here from MKSmartHouse.com and in this video I am going to be doing a complete guide to connecting and setting up the Sonoff.
 
In this video I am going to be using a Sonoff dual but you can apply the same concept to other Sonoff devices. For those of you who don’t know the Sonoff dual is a smart device that can control the AC power flow to devices or in the case of this video outlets. Yes that is right I am going to be using the sonoff dual with outlets to make a smart power strip. I should note though that this is not the power strip I normally use because the one I use is a little bit more DIY and with mine you can control three outlets. I will be making a video on my power strip very soon. But, the power strip we will be making in this video is great for if you want to control only two things. I should also note that in this video we will be connecting wires that will be used for high voltage but, if you follow along with my video exactly then you should be fine. Just as a disclaimer I am not responsible for anything you do. One last thing before we get started and that is for this video we will be using the stock firmware that comes with the sonoff but, in the next video we will be installing our own custom firmware that will connect to our OpenHAB server through MQTT.
Alright let's get started. The first thing we are going to do is go over the parts we need to make this smart power strip. First up on the list is a 14 guage power cable and we are going to use this to connect the Sonoff and the oultets to power you could use a power cable that you have laying around just make sure it is 14 guage and has ground. Next to connect the outlets to the sonoff and ground we need 14 guage stranded wire in Black, white and green, I chose those colors because they are the same colors inside the power cable. Next to house everything we are going to use a 3 gang electrical box preferably an extra deep one so you have more room to work, then to interface with the and connect devices to it we are going to use two electrical outlets, since the sonoff does not have a ground port we are going to need a 3 port push in wire connector to connect all the grounds together, to close off the power strip and give it a nice finish we are going to need an electrical cover plate, I am using a cover that you can choose the pieces for and put it together to make 1 big cover. Then finally we are going to need the star of the show and that is the sonoff dual, this is the device that will be the brains of the power strip. As you may have noticed that the electrical components that I showed are for US electric but you can do the same thing even if you are not in the US simply just replace the US electric with your country's electric components, standards and wire colors. So please research a little bit about your countries electric code before you continue. Next let’s move onto the tools we will need. We are going to need wire cutters, wire strippers, a phillips screwdriver and a small flat screw driver. I will leave links to all the parts and tools over on my website, the link is in the description.
 
With the parts and tools out of the way let's get started. To start off let’s take the Sonoff out of the box. It comes in a very nice blue box which on 3 sides it just says sonoff, on 1 side is some barcode, on another side is the specifications, and finally on the back is QR codes to download the app for it. To open the box you just pull out the flap on the side. Then you can pull out the Sonoff which is the only thing inside the box. Now let’s take a quick tour around the sonoff. On the front you will find a button and some information such as the wiring inputs. Then on the rest of the device is just blank white space. But if we go back to the front and remove the cover we will find the wire ports.
 
Next grab the 14 gauge power cable and strip off some of the black insulation. To do this use the blades on the wire strippers to score the black insulation and sort of twist the black insulation free and remove it from the cable. Then if your power cable’s leads are not stripped then strip them to expose the copper. Next take the electrical enclosure and stick the power cable through one of the holes. I used a screwdriver to push the flap up a little so I can stick the cable in easier. Once it is in pull it so there is maybe about 10in inside the enclosure, this is to make connecting the sonoff easier which is the next thing we are going to do. So grab the power cables and first connect the black wire or in other words the live wire into the Input L port on the sonoff. To connect the cables into the sonoff take the small flat screwdriver and push down on the white button and then slide the cable into the hole. Then take the white or neutral wire and put it in the port labeled N. Now we have one wire left and this is the green ground wire. There is no where to connect this on the sonoff so take the 3 port push in connect and push the green wire into it, we will also push the outlet ground wires into it later. Great, now the sonoff is connected to the power cable. Next grab your phone it could be Android or ios and go to the app store. In the search type in eWeLink and press search then click install next to the one with an e in the icon. Once it is installed open up the app. Allow it to send notifications and on the screen press next. After that it will ask you to log in, if you have an account then log in otherwise like me you will press register. First enter your email address and press next. On the next page enter the verification code they sent to the email address you typed in previously, and type in a password 2 times and press register. It will say “Registered successfully, press OK. It will then bring you back to the log in page where you will log in with email address and password you used in the registration procces and click log in. Once you are logged in it will bring you to the devices page, where you will click on the plus button to add a device. Next plug the power cable into your home’s power. You should see some lights come on in the sonoff that means it is powered on! Next hold down the button on the Sonoff for 7 seconds or until the light starts flashing rapidly on it. Now go back to the mobile device and choose the option that says AP and press next. Now exit out of the app and go into your settings and then wifi. Click on the device that says ITEAD and some letters and numbers. It will then prompt you for a password, type in 12345678 and press join. Once you have connected to the device go back into the eWeLink app and click next it should then automatically load your wifi network, it takes to closest wifi network to that device, if the one loaded is not your wifi network then type your wifi network in. In the password field type in the password for your wifi and press next. The device will now try to connect to your wifi network. Once it is done it will ask you to name the device, I am going to keep it how it is and press complete. It will then say “Added successfully” press OK. The device is now added! You can try out the device by turning on and off channel 1 and channel 2. You should hear clicks come out of the device. Awesome the device is all setup! Now lets finish this up and put it all together. But. before we continue unplug the power cable from your home’s power. Next grab the rolls of 14 gauge stranded cable and cut two pieces of 12 inch cable from each roll of each color. So you should have 2 12 inch pieces of black white and green wires. Then grab the wire strippers and strip all the ends of all the wires to expose the copper. But, on one end it should be stripped according to the specifications for the outlets you will be using. For example on my outlets it gives a little template on how long the expose copper should be so, I stripped it to that. Then on the other side it should be stripped half an inch or a little bit more than 1 centimeter. Once all the wires are stripped we will begin to connect them to the outlets. So grab an outlet and a green wire. For all the connections on the outlets use the side of the wire that was stripped to the specification for the outlet. First take the green wire and twist the copper part, so it is more together and more solid. Then take a screwdriver and wrap the copper part of the wire around the screwdriver to make it into a hook. After that grab the outlet and hook the green wire around the green screw of the outlet. Using the philips screw driver tighten down the screw as much as you can to make a really secure connection. Then take the white wire and just like the green wire twist the copper part and wrap it around the screw driver. But, unlike the green wire hook it over the silver screw, on the outlet it should say white wire on that side and using the screwdriver tighten it down as much as it allows. I also like to screw down the other unused silver screw so that way it is not sticking out. Next take the black wire or live wire, twist it and wrap it around the screwdriver. Then hook it onto the gold screw and tighten it down. Like the silver screw I like to screw down the unused gold screw. Awesome! This outlet is done! Now repeat the same process for the other outlet! Once both outlets are done we are going to connect them to the sonoff. First grab the sonoff and one outlet. Then take the white wire and connect it to the Port labeled N on the sonoff. Next take the black wire from the outlet and connect it to the port labeled Out1 L on the sonoff. Now we are left with the ground wire so push it into the push in wire connector. After that Grab the next outlet and connect it the same way, white wire into the N port except connect the black wire into the Out2 L port on the sonoff. Also, push the green wire into the push in wire connector with the rest of the ground wires. All of the wiring is now done! All that is left is to tidy everything up! Grab the wire cover for the sonoff and screw it on using the philips screw driver. Then pull the power cable back out of the blue enclosure so that way there is no extra black cable inside and so the sonoff is sitting nicely inside at the bottom. After that take one outlet and twist it along with the wires and slightly screw it into the enclosure. The reason we twist it is to shorten the wires and make them more compact. Then take the other outlet and do the same thing. Once both outlets are in you can align them and tighten their screws down. As you can see if we take a look everything is all nice and tidy inside the enclosure so let's close it all up. Take the pieces for the plate and put them together to make a nice big plate. Then take the plate and test fit it over the outlets, you may need to adjust the outlets, luckily I did not have to. My blank plate required a metal riser so I screwed that into the empty spot. Finally take the plate and using the white screws that come with the plate screw it into the enclosure. Great! The device is now done, let’s test it out! I first plugged the power strip into my home’s power and the connected lights into each of the outlets. As you can see if I turn on each channel each light turns and the same thing is for if I turn each channel off. Awesome! The power strip is fully complete and working! In the next video we will be flashing custom firmware to the device so it will work with MQTT and our openHAB server. I would just like to mention again that all the links to all the materials and tools are available on my website which the link to is 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. Good Bye!

Sonoff Power Strip 

Custom Firmware Mac:

 

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 MAC.
 
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.
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 finder 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 /dev/cu.wchusbserialXXXX.
 
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 terminal 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!

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!

Sonoff Power Strip 

Product Links:
14 AWG Power Cable: http://amzn.to/2w2wKhr
Green 14 AWG Wire FT: http://amzn.to/2widk7q
Black 14 AWG Wire FT: http://amzn.to/2hkMbOv
White 14 AWG Wire FT: http://amzn.to/2uVKDxF
Electrical Encloseure: http://amzn.to/2uVbo5y
Edge Outlet Piece: https://goo.gl/Fw7F9E
Blank Piece: https://goo.gl/tw3CZZ
3 Port Push In Wire Connector: http://amzn.to/2w2RQfu
Sonoff device: http://amzn.to/2hrGHl3
DuPont Female Jumper Cable: http://amzn.to/2fAA9QI
4 Pin Female Header: http://amzn.to/2fB3GK1