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

Sprinkler Control MQTT OpenHAB ESP8266

Sprinkler Control MQTT OpenHAB ESP8266

Demo: 

Hey guys Matt here from MKSmartHouse.com and in this video I am going to give you a demo of the sprinkler system we will be creating.
 
So what is this sprinkler system exactly well, it is a device that connects to our openhab server through MQTT and listens for commands to determine which sprinkler valve to turn on. If I pull out a mobile device and open up the openHAB app you can see that I now have 5 things in  the outside frame. The first 3 things are switches that say “Zone 1” “Zone 2” and “Zone 3 and what these switches do is turn on the sprinkler valves that are connected to the device. The next item is “20 Minute Cycle” and this one goes through and activates each zone individually for 20 minutes and then goes to the next zone. I will demonstrate Later. The next item is “Sprinkler System Schedule” and it controls whether or not the openhab system should do its pre programmed scheduled watering of the sprinkler system. So if I turn it off then it will not water by the schedule which is everyday at 5 am but, if I turn it on then it will water everyday at 5am. One cool thing that implemented in the rules and will show you in the software video is that whenever the system does its daily water then it will send you a notification before it starts watering to let you know what is going on. Now, let’s get into the demonstration of the device. I would just like to note that this device can control up to 3 zones but currently at my house I only have 2 so you will only see 2 sets of sprinklers activate on the lawn. Let’s start off by controlling each individual zone. First I am going to turn on Zone 1, and as you can see the sprinklers in zone came up and started watering the grass. Next I am going to turn off Zone 1 and turn on Zone 2 and just like Zone 1 you can see the sprinklers came up. If I did have my third zone up and running I could turn off Zone 2 and turn on zone 3 but currently I do not have the hoses connected to those sprinklers. The next thing I want to show you is the 20 minute cycle. So if I turn it on, you can see that zone 1 turned on and is watering. For demonstration purposes I changed it from 20 to 1 minute in the code so that way we don’t have to sit here and watch the sprinklers for 20 minutes and wastewater. I am going to speed up the video a little and as you can see Zone 1 now turned off and Zone 2 turned on. Now I am going to speed it up again and Zone 2 turned off and Zone 3 turned on but again it is not connected. Next, I am going to show you around the device starting with the valves that I have outside. So, outside I have three valves, the first 2 from the top are connected and the third one is not, it is there for future use. Anyway the 2 are connected to 3 conductor gray wire that goes inside the house and connects to the sprinkler system device so, let's go take a look at it. As you can see this is the device and it is nicely mounted to the wall. Starting from the top we have the device itself. Next we have all the wires coming from the box. Starting from left to right we have the 5 volt power wire and that is simply connecting into 5 volt power brick that is connect to an extension cord but ideally should be plugged directly into the wall. Next to that are the sprinkler valve wires. Then finally is the sprinkler valve power adapter wires which go to a power adapter to power the sprinkler valves when the device allows it. But, let's take a closer look at the control box. It is all housed in a nice plastic enclosure. Anyway if we open it up we can see the Custom PCB and its components. First up at the top left is the mode switch and we use this to either put the device into program mode or in run mode. Underneath to the left is the communication port and this is how we are going to program the device. Then under that is the 5v to 3.3v regulator. To the right of the regulator are the terminal blocks for 5v power, 3 sprinkler valve outputs and sprinkler valve power adapter. Above the terminals are the 3 relays to control the valves. And above that are diodes, transistors and resistors. If we flip the board over we will see the esp right in the middle. The esp8266 is the whole brains of the device and it is what connects to the openhab server through mqtt That’s it that is the entire device! In the next video I will show you all the hardware we will need and how to put it together. 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!

Sprinkler Control MQTT OpenHAB ESP8266

Hardware: 

 

Click to Download Sprinkler System Gerber

Hey guys Matt here from MKSmartHouse.com and in this video I am going to show you how to build the sprinkler system.
 
So, in this video we are going to go over all the parts required for the sprinkler system and then we are going to make it. To start off with the parts we are going to need 5 2 pin pcb terminals, 1 4 pin female header, male header strips that we can cut to length, 2 pin jumper, 6 330ohm resistors, 3 1N4001 Diodes, 3 2n2222 transistors, 5v to 3.3v regulator, ESP8266, Custom Printed circuit board more on that later, plastic enclosure, 3 5v relays, sprinkler valve power adapter, 2 conductor 22 Gauge Cable, Male usb connector and a 5v USB Wall adapter. The wall adapter can be any usb 5v adapter like a phone charger. For tools we are going to need a soldering iron with ST7 tip, flux, steel wire, solder, wall tac, PCB holder, helping hands, wire strippers, small wire cutters, small flat screwdriver, dremel, drill and a hot glue gun. That is everything that we will need. The links to all the parts and tools will be over on my website link is in the description. In the beginning I did say custom circuit board because to make this easier I designed a custom circuit board so that way not only will it be very stable and have strong connections with no guess work in assembling it but, also so I can make many of them really easily for my house. I have provided the gerber file in the description as well as where to get it made. Also, I have made a video on my website showing how to order the custom circuit board from iTead so that way there is nothing to be scared of and you know exactly how to order it.
 
Since all the connections are already made on the circuit board all we have to do is solder the components on to the board.
 
1. First let's take the circuit board and put it into the circuit board holder. If you look closely at the circuit board you will see that there are labels next to the component slots, when I say where to put the component that is what I will be referring to.
2. I like to solder from the less sensitive components to to most sensitive so, let's start off with the 2 pin terminals, take the terminals and put them in J1, J2, J3, J4 and J5. Then solder them in.
 
If you want to know how to solder or how I solder then check out my guide for the door sensor because in that video I went over it in detail.
 
3. Next take the 4 pin female header and put it into J6 and solder it in. If the leads get long and stick out cut them with wire cutters.
4. Then take a strip of the male header and using the wire cutters make a header that is 3 pins long.
5. Take that 3 pin male header and solder it into J7.
6. Now take the 3 1N4001 diodes and bend both side so the two leads are parallel to each other.
 
When soldering diodes you want to match up the line on the diode with the line on the PCB.
 
7. Solder the diodes into slots D1, D2, and D3.
8. Now take the 6 330 ohm resistors and bend their leads the same way you did the diodes, except when putting in resistors it does not matter which way you put it in the slots.
9. Put the resistors into slots R1, R2, R3, R4, R5, and R6 and solder them in.
 
I know in the video you see two different sets of resistors with different values, just ignore them I messed up when recording, just know that they all should be 330 ohm.
 
10. Next up is the transistors, so take the three 2N222 transistors and solder them into slots Q1, Q2, and Q3.
 
When soldering the transistors you will need to spread their leads in order for them to go into the slots.
 
11. After that we are going to solder in the relays so grab the 3 5 volt relays and solder them into slots Relay 1, Relay 2, and Relay 3.
 
When soldering the relays you may have to adjust there leads a little and also press down onto the relay in order for it to fit snug so you can solder it.
 
12. Next take the 5v to 3.3v regulator and match up its leads to the board and solder it in.
 
Great now we have one component left to solder and that is the esp8266.
 
13. I recommend putting flux on every single pad of the esp8266 and of the circuit board and then solder it in.
14. Now that everything is soldered take the 2 pin jumper and put it on the pins above PGM.
 
Now that the PCB is done lets move over to the enclosure because there are a couple of modifications we have to do it.
 
15. The first thing we have to do is make the standoffs shorter because with the relays the pcb is very tall so the case does not close. So grab a drill with a massive drill bit and simply drill each of the 4 standoffs down a little. I do not know exactly how much to drill down so just keep checking by putting the PCB in the case and trying to close it, if it closes then you are good if it does not close then keep drilling each stand off.
16. The second thing is make mounting holes, so grab a drill with a drill bit suitable for the screw you are going to be using for mounting, I am using a 4mm screw to mount the device so I grabbed a 4.5mm drill bit. Once you find your drill bit make two holes in the back of the case.
17. After that, I took a counter sink and countersunk the holes so that way when I mount the case the screws are close to flush with the case.
18. The third and last thing is we have to make a giant hole so we can plug the cables into the PCB through the case. To do this grab the PCB and put it in the case.
19. Then take a Dremel and barely engrave a line, marking where the holes on the terminal blocks are, this will be the guide we will use instead of keeping the PCB in the case with the risk of damaging components.
20. So take out the PCB and the use the Dremel and cut a line below the engraving.
21. Then take the Dremel and cut a line above the engraving.
22. After we have the two lines we have to remove the middle engraving so use the Dremel and stick it in the bottom cut and angle it up to cut that side loose, then do the same thing for the other side.
 
You should now have a giant hole in the side of the enclosure.
 
23. Put the PCB and see how it came out, you might have to go back in and fix the hole with the Dremel like I did.
 
Once you finished the hole the enclosure is now complete so you can leave the PCB in it.
 
Next we are going to work on the power cable to the device, not the sprinkler valve power cable because we will work on that in the final installation video. No, I am talking about the 5 volt power cable.
 
24. First thing we have to do is take the 2 conductor 22 Gauge cable and cut it to length, I cut mine the same length as the sprinkler valve power adapter.
25. Then take one end of the wire and strip it to expose the 2 conductors.
26. Strip a little of the each conductor and then take wire boot and plastic boot of the usb connector and slide it over the 2 conductors.
27. Next take the helping hands and put the 22 gauge wire in one of them and the usb connector in the other.
28. Then solder the black wire to the far left pin and the red wire to the far right pin.
29. Now put some hot glue on the connector and slide over the plastic boot and the wire boot.
 
If you want you can connect the USB to the 5v wall adapter but do not plug it into the wall.
 
30. Almost done, strip the other end of the 22 gauge cable and then loosen the two screws on the 2 pin terminal above the 5v.
31. Then take the red wire and put it into the hole parallel to where it says + and the black into the hole above the - symbol and tighten the screws.
 
That is it for the hardware of the actual device now all it needs is some software which will be in the next video. 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!

Sprinkler Control MQTT OpenHAB ESP8266

Software: 

So ssh into your pi or whatever your server may be.
The first thing we are going to do is create the sprinkler system item.
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 //Sprinkler System then underneath that we are going to create 2 software switches to control the sprinkler system.
Type in: 
Switch SprinklerCycle "20 Minute Cycle" <water> [ "Switchable" ]
Switch SprinklerSystemSchedule "Sprinkler System Schedule" <calendar> [ "Switchable" ]
and press enter.
Now we are going to create the actual sprinkler system items for the zones type in:
Switch MKSprinklerSystemZone1 "Sprinklers Zone 1" <water> [ "Switchable" ] {mqtt=">[broker:MK-SmartHouse/utilities/MK-SprinklerSystem1:command:ON:Z1ON],>[broker:MK-SmartHouse/utilities/MK-SprinklerSystem1:command:OFF:Z1OFF],<[broker:MK-SmartHouse/utilities/MK-SprinklerSystem1/state:state:ON:Z1ON],<[broker:MK-SmartHouse/utilities/MK-SprinklerSystem1/state:state:OFF:Z1OFF]", autoupdate="false"}
Switch MKSprinklerSystemZone2 "Sprinklers Zone 2" <water> [ "Switchable" ] {mqtt=">[broker:MK-SmartHouse/utilities/MK-SprinklerSystem1:command:ON:Z2ON],>[broker:MK-SmartHouse/utilities/MK-SprinklerSystem1:command:OFF:Z2OFF],<[broker:MK-SmartHouse/utilities/MK-SprinklerSystem1/state:state:ON:Z2ON],<[broker:MK-SmartHouse/utilities/MK-SprinklerSystem1/state:state:OFF:Z2OFF]", autoupdate="false"}
Switch MKSprinklerSystemZone3 "Sprinklers Zone 3" <water> [ "Switchable" ] {mqtt=">[broker:MK-SmartHouse/utilities/MK-SprinklerSystem1:command:ON:Z3ON],>[broker:MK-SmartHouse/utilities/MK-SprinklerSystem1:command:OFF:Z3OFF],<[broker:MK-SmartHouse/utilities/MK-SprinklerSystem1/state:state:ON:Z3ON],<[broker:MK-SmartHouse/utilities/MK-SprinklerSystem1/state:state:OFF:Z3OFF]", autoupdate="false"}
and press enter.
 
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 replace what is in the outside frame with our new items.
 
type in:
    
Switch item=MKSprinklerSystemZone1
Switch item=MKSprinklerSystemZone2
Switch item=MKSprinklerSystemZone3
Switch item=SprinklerCycle
Switch item=SprinklerSystemSchedule
and press enter,
 
Now press control x then y and enter.
 
In terminal type in sudo nano /etc/openhab2/rules/home.rules and press enter. 
In the file type in:
//If sprinkler system schedule is on then water each zone at 5 am for 20 min
rule "Sprinkler System Schedule"
when
  Time cron "0 0 5 1/1 * ? *"
then
  if(SprinklerSystemSchedule.state == ON)
  {
      sendBroadcastNotification("SPRINKLER SYSTEM: Daily Watering Active")
      sendCommand(MKSprinklerSystemZone1, ON)
      createTimer(now.plusMinutes(20)) [|
           sendCommand( MKSprinklerSystemZone1, OFF)
           sendCommand( MKSprinklerSystemZone2, ON)
           createTimer(now.plusMinutes(20)) [|
               sendCommand( MKSprinklerSystemZone2, OFF)
               sendCommand( MKSprinklerSystemZone3, ON)
               createTimer(now.plusMinutes(20)) [|
                   sendCommand( MKSprinklerSystemZone3, OFF)
               ]
          ]
      ]
  }
end
 
Let me explain what this does, it first checks for if the sprinkler system schedule switch is on and if it is then it will individually water each zone for 20 min everyday at 5 am. Also if you have myopenhab could connector connected then it will send you a notification on your device letting you know that it is activating the sprinklers. But if the switch is off then it will not activate the scheduled watering of everyday at 5 am for 20 minutes a zone. Now, what if you did not want it to water everyday at 5 am but every other day. Openhab rules, utilizeses something called cron time and will execute the rule based on what you set.
 
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.
 
Next let’s move on to the 20 minute cycle rule.
 
So paste into the rules file the following:
 
//If 20 Minute Cycle Switch Is On then water each zone for 20 min
rule "20 Minute Cycle"
when
  Item SprinklerCycle received update ON
then
      sendBroadcastNotification("SPRINKLER SYSTEM: 20 Minute Cycle Active")
      sendCommand(MKSprinklerSystemZone1, ON)
      createTimer(now.plusMinutes(20)) [|
           sendCommand( MKSprinklerSystemZone1, OFF)
           sendCommand( MKSprinklerSystemZone2, ON)
           createTimer(now.plusMinutes(20)) [|
               sendCommand( MKSprinklerSystemZone2, OFF)
               sendCommand( MKSprinklerSystemZone3, ON)
               createTimer(now.plusMinutes(20)) [|
                   sendCommand( MKSprinklerSystemZone3, OFF)
               ]
          ]
      ]
      sendCommand(SprinklerCycle, OFF)
end
 
What this does is first send you a notification that the 20 minute cycle is active and then it will individually water each zone for 20 minutes. That is it for the rules, all we did was add a scheduled rule for the sprinkler system and an instant on command rule.
 
Now press control x then y and enter.
 
sudo systemctl restart openhab2.service

Sprinkler Control MQTT OpenHAB ESP8266

Software Using Mac: 

 

Hey guys Matt here from MKSmartHouse.com and in this video I am going to show you how to setup the software for the sprinkler system using a MAC.
So in the last video we left off with the Sprinkler System Device being fully built so 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. I know this is the software video but, there are some hardware things that we need besides a computer. We are going to need an arduino of some kind preferably a UNO or a MEGA equivalent with its usb cable of course and male to male dupont jumper cables. Links to these items will be in the description or on the website.
1. The first thing we are going to do is grab the arduino and put a jumper cable from RES to GND.
2. Then grab the sprinkler system and connect all the pins to their corresponding spots so, TX to TX, RX to RX, GND to GND, and 5V to 5V and then connect the arduino to the computer
 
Before we continue, check to make sure the 2 pin jumper is above PGM. 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.  
3. 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-SprinklerSystem Firmware.
4. On the new page press download.
5.Then go to your finder and downloads folder and double click on MK-SprinklerSystem.ino.
6. 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 sprinkler system, and there are only a few things we have to change.
 
7. 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.
 
8. The first parameter is the hostname of the device, usually I only change the last digit but since this is the first sprinkler system I will keep it as it is.
9. Next is the update path and personally I don’t change that.
10. 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.
 
11. The first one is the outTopic and this is the topic for which the device sends commands or messages to on the server.
12. The next one is the MQTT Server Ip address and this is simply the IP address of your home automation or openhab server.
13. 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.
 
14. So go to tools and make sure the Board: is Generic ESP8266 Module and the port is /dev/cu.wchusbserialXXXX.
15. 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!
 
16. First unplug the dupont wires in between the arduino and the device.
17. Then take the 2 pin jumper and move it over so it is above RUN.
18. After that take the lid to the device and put it on.
19. Finally plug the device into the wall.
 
To confirm that it flashed correctly and is working you can fire up MQTT.fx,
20. 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.
 
21. Then click on the publish tab.
22. In the topic bar type in the subscribeTopic that we wrote in the sprinkler system device code that we flashed MK-SmartHouse/utilities/MK-SprinklerSystem1.
23. Next in the message box type in Z1ON and press publish.
 
The relay of the device should be on and you should have heard a click.
 
24. Then in the message box delete what is there and type in Z1OFF and press publish.
 
The relay of the device should be off and you should have heard a unclick. 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. If you want you can test all three relays by manually sending the commands just repeat the process of the commands and change the number to match the relay you are testing 1-3.
 
Now the device is complete and just needs to be added to OpenHAB.
 
25. So ssh into your pi or whatever your server may be.
The first thing we are going to do is create the sprinkler system item.
26. So type in sudo nano /etc/openhab2/items/home.items and press enter, you may need to type in admin password.
27. Then type in the comment //Sprinkler System then underneath that we are going to create 2 software switches to control the sprinkler system.
28. Type in: 
Switch SprinklerCycle "20 Minute Cycle" <water>
Switch SprinklerSystemSchedule "Sprinkler System Schedule" <calendar>
29. And press enter.
30. Now we are going to create the actual sprinkler system items for the zones type in:
Switch MKSprinklerSystemZone1 "Sprinklers Zone 1" <water> {mqtt=">[broker:MK-SmartHouse/utilities/MK-SprinklerSystem1:command:ON:Z1ON],>[broker:MK-SmartHouse/utilities/MK-SprinklerSystem1:command:OFF:Z1OFF]"}
Switch MKSprinklerSystemZone2 "Sprinklers Zone 2" <water> {mqtt=">[broker:MK-SmartHouse/utilities/MK-SprinklerSystem1:command:ON:Z2ON],>[broker:MK-SmartHouse/utilities/MK-SprinklerSystem1:command:OFF:Z2OFF]"}
Switch MKSprinklerSystemZone3 "Sprinklers Zone 3" <water> {mqtt=">[broker:MK-SmartHouse/utilities/MK-SprinklerSystem1:command:ON:Z3ON],>[broker:MK-SmartHouse/utilities/MK-SprinklerSystem1:command:OFF:Z3OFF]"}
31. and press enter.
 
Now let me go over the three 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 sprinkler valves. But let me go a little bit more in detail, the first part is Switch and since this device is a sprinkler system and we just turn the zones on and off we use the switch type. The next part is the item name and I just used its hostname without the number and added the word zone 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 ZXON and when you turn it off it will send ZXOFF. X would be the number of the zone. Those three items are pretty much the same except the number is incremented for each zone. Thats it for the items file.
 
32. Now press control x then y and enter.
 
Next up is the sitemap file so we can control the device and our other switches.
 
33. 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 replace what is in the outside frame with our new items.
 
34. So delete the demo switch and type in:
    
Switch item=MKSprinklerSystemZone1
Switch item=MKSprinklerSystemZone2
Switch item=MKSprinklerSystemZone3
Switch item=SprinklerCycle
Switch item=SprinklerSystemSchedule
35. and press enter,
 
what we did is import the items into the sitemap so we can control them from the user interface.
 
36. Now press control x then y and enter.
 
Before we go any further lets confirm that everything works
 
37. so go to your web user interface and then Basic UI.
 
You should see the 3 zone switches as well as the 20 Minute cycle and the Sprinkler system schedule switch.
 
38. If you turn on each zone switch then the relays in the device should turn on. You will hear a click.
39. If you turn the same zone switches off then the relays should turn off and it will de click.
 
Basically you should be able to control the relays. Great! Now that we know the zone switches work lets do something about the sprinkler cycle switch and the schedule switch, because right now they do not do anything.
 
40. In terminal type in sudo nano /etc/openhab2/rules/home.rules and press enter.
41. In the file type in:
//If sprinkler system schedule is on then water each zone at 5 am for 20 min
rule "Sprinkler System Schedule"
when
  Time cron "0 0 5 1/1 * ? *"
then
  if(SprinklerSystemSchedule.state == ON)
  {
      sendBroadcastNotification("SPRINKLER SYSTEM: Daily Watering Active")
      sendCommand(MKSprinklerSystemZone1, ON)
      createTimer(now.plusMinutes(20)) [|
           sendCommand( MKSprinklerSystemZone1, OFF)
           sendCommand( MKSprinklerSystemZone2, ON)
           createTimer(now.plusMinutes(20)) [|
               sendCommand( MKSprinklerSystemZone2, OFF)
               sendCommand( MKSprinklerSystemZone3, ON)
               createTimer(now.plusMinutes(20)) [|
                   sendCommand( MKSprinklerSystemZone3, OFF)
               ]
          ]
      ]
  }
end
 
Let me explain what this does, it first checks for if the sprinkler system schedule switch is on and if it is then it will individually water each zone for 20 min everyday at 5 am. Also if you have myopenhab could connector connected then it will send you a notification on your device letting you know that it is activating the sprinklers. But if the switch is off then it will not activate the scheduled watering of everyday at 5 am for 20 minutes a zone. Now, what if you did not want it to water everyday at 5 am but every other day. Openhab rules, utilizeses something called cron time and will execute the rule based on what you set.
 
42. 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.
 
43. Let’s do an example, I am going to set it to every 2 days at 5 am,
44. then press generate cron expression.
45. 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 20 minute cycle rule.
 
46. So paste into the rules file the following:
 
//If 20 Minute Cycle Switch Is On then water each zone for 20 min
rule "20 Minute Cycle"
when
  Item SprinklerCycle received update ON
then
      sendBroadcastNotification("SPRINKLER SYSTEM: 20 Minute Cycle Active")
      sendCommand(MKSprinklerSystemZone1, ON)
      createTimer(now.plusMinutes(20)) [|
           sendCommand( MKSprinklerSystemZone1, OFF)
           sendCommand( MKSprinklerSystemZone2, ON)
           createTimer(now.plusMinutes(20)) [|
               sendCommand( MKSprinklerSystemZone2, OFF)
               sendCommand( MKSprinklerSystemZone3, ON)
               createTimer(now.plusMinutes(20)) [|
                   sendCommand( MKSprinklerSystemZone3, OFF)
               ]
          ]
      ]
      sendCommand(SprinklerCycle, OFF)
end
 
What this does is first send you a notification that the 20 minute cycle is active and then it will individually water each zone for 20 minutes. That is it for the rules, all we did was add a scheduled rule for the sprinkler system and an instant on command rule.
 
47. Now press control x then y and enter.
48. Go back to the basic UI and turn on the 20 minute cycle switch.
 
If everything works right you should have gotten a notification and it should have turned on zone 1 then after 20 minutes zone 1 will turn off and zone 2 will turn on and then after another 20 minutes zone 2 will turn off and zone 3 will turn on.
 
49. Next turn on the sprinkler system schedule switch and then check if it correctly turned on the sprinkler system at the time you set up in the rules, you should also get a notification when it happens.
 
The schedule switch will stay on until you turn it off, it basically just turns the schedule on and off. That is it! The software is complete, now all we have to do is install the device in its final place which will be completed in the next final installation video. 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!

Sprinkler Control MQTT OpenHAB ESP8266

Software Using Windows: 

 

Hey guys Matt here from MKSmartHouse.com and in this video I am going to show you how to setup the software for the sprinkler system using WINDOWS.
So in the last video we left off with the Sprinkler System Device being fully built so 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. I know this is the software video but, there are some hardware things that we need besides a computer. We are going to need an arduino of some kind preferably a UNO or a MEGA equivalent with its usb cable of course and male to male dupont jumper cables. Links to these items will be in the description or on the website.
1. The first thing we are going to do is grab the arduino and put a jumper cable from RES to GND.
2. Then grab the sprinkler system and connect all the pins to their corresponding spots so, TX to TX, RX to RX, GND to GND, and 5V to 5V. and then connect the arduino to the computer.
Before we continue, check to make sure the 2 pin jumper is above PGM. 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.  
 
3. 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-SprinklerSystem Firmware.
4. On the new page press download.
5.Then go to your file explorer and downloads folder and double click on MK-SprinklerSystem.ino.
6. 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 sprinkler system, and there are only a few things we have to change.
 
7. 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.
 
8. The first parameter is the hostname of the device, usually I only change the last digit but since this is the first sprinkler system I will keep it as it is.
9. Next is the update path and personally I don’t change that.
10. 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.
 
11. The first one is the outTopic and this is the topic for which the device sends commands or messages to on the server.
12. The next one is the MQTT Server Ip address and this is simply the IP address of your home automation or openhab server.
13. 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.
 
14. So go to tools and make sure the Board: is Generic ESP8266 Module and the port is COMX.
15. 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!
 
16. First unplug the dupont wires in between the arduino and the device.
17. Then take the 2 pin jumper and move it over so it is above RUN.
18. After that take the lid to the device and put it on.
19. Finally plug the device into the wall.
 
To confirm that it flashed correctly and is working you can fire up MQTT.fx,
20. 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.
 
21. Then click on the publish tab.
22. In the topic bar type in the subscribeTopic that we wrote in the sprinkler system device code that we flashed MK-SmartHouse/utilities/MK-SprinklerSystem1.
23. Next in the message box type in Z1ON and press publish.
 
The relay of the device should be on and you should have heard a click.
 
24. Then in the message box delete what is there and type in Z1OFF and press publish.
 
The relay of the device should be off and you should have heard a unclick. 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. If you want you can test all three relays by manually sending the commands just repeat the process of the commands and change the number to match the relay you are testing 1-3.
 
Now the device is complete and just needs to be added to OpenHAB.
 
25. So ssh into your pi or whatever your server may be.
The first thing we are going to do is create the sprinkler system item.
26. So type in sudo nano /etc/openhab2/items/home.items and press enter, you may need to type in admin password.
27. Then type in the comment //Sprinkler System then underneath that we are going to create 2 software switches to control the sprinkler system.
28. Type in: 
Switch SprinklerCycle "20 Minute Cycle" <water>
Switch SprinklerSystemSchedule "Sprinkler System Schedule" <calendar>
29. and press enter.
30. Now we are going to create the actual sprinkler system items for the zones type in:
Switch MKSprinklerSystemZone1 "Sprinklers Zone 1" <water> {mqtt=">[broker:MK-SmartHouse/utilities/MK-SprinklerSystem1:command:ON:Z1ON],>[broker:MK-SmartHouse/utilities/MK-SprinklerSystem1:command:OFF:Z1OFF]"}
Switch MKSprinklerSystemZone2 "Sprinklers Zone 2" <water> {mqtt=">[broker:MK-SmartHouse/utilities/MK-SprinklerSystem1:command:ON:Z2ON],>[broker:MK-SmartHouse/utilities/MK-SprinklerSystem1:command:OFF:Z2OFF]"}
Switch MKSprinklerSystemZone3 "Sprinklers Zone 3" <water> {mqtt=">[broker:MK-SmartHouse/utilities/MK-SprinklerSystem1:command:ON:Z3ON],>[broker:MK-SmartHouse/utilities/MK-SprinklerSystem1:command:OFF:Z3OFF]"}
31. and press enter.
 
Now let me go over the three 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 sprinkler valves. But let me go a little bit more in detail, the first part is Switch and since this device is a sprinkler system and we just turn the zones on and off we use the switch type. The next part is the item name and I just used its hostname without the number and added the word zone 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 ZXON and when you turn it off it will send ZXOFF. X would be the number of the zone. Those three items are pretty much the same except the number is incremented for each zone. Thats it for the items file.
 
32. Now press control x then y and enter.
 
Next up is the sitemap file so we can control the device and our other switches.
 
33. 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 replace what is in the outside frame with our new items.
 
34. So delete the demo switch and type in:
    
Switch item=MKSprinklerSystemZone1
Switch item=MKSprinklerSystemZone2
Switch item=MKSprinklerSystemZone3
Switch item=SprinklerCycle
Switch item=SprinklerSystemSchedule
35. and press enter.
 
What we did is import the items into the sitemap so we can control them from the user interface.
 
36. Now press control x then y and enter.
 
Before we go any further lets confirm that everything works
 
37. so go to your web user interface and then Basic UI.
 
You should see the 3 zone switches as well as the 20 Minute cycle and the Sprinkler system schedule switch.
 
38. If you turn on each zone switch then the relays in the device should turn on. You will hear a click.
39. If you turn the same zone switches off then the relays should turn off and it will de click.
 
Basically you should be able to control the relays. Great! Now that we know the zone switches work lets do something about the sprinkler cycle switch and the schedule switch, because right now they do not do anything.
 
40. In ssh type in sudo nano /etc/openhab2/rules/home.rules and press enter.
41. In the file type in:
//If sprinkler system schedule is on then water each zone at 5 am for 20 min
rule "Sprinkler System Schedule"
when
  Time cron "0 0 5 1/1 * ? *"
then
  if(SprinklerSystemSchedule.state == ON)
  {
      sendBroadcastNotification("SPRINKLER SYSTEM: Daily Watering Active")
      sendCommand(MKSprinklerSystemZone1, ON)
      createTimer(now.plusMinutes(20)) [|
           sendCommand( MKSprinklerSystemZone1, OFF)
           sendCommand( MKSprinklerSystemZone2, ON)
           createTimer(now.plusMinutes(20)) [|
               sendCommand( MKSprinklerSystemZone2, OFF)
               sendCommand( MKSprinklerSystemZone3, ON)
               createTimer(now.plusMinutes(20)) [|
                   sendCommand( MKSprinklerSystemZone3, OFF)
               ]
          ]
      ]
  }
end
 
Let me explain what this does, it first checks for if the sprinkler system schedule switch is on and if it is then it will individually water each zone for 20 min everyday at 5 am. Also if you have myopenhab could connector connected then it will send you a notification on your device letting you know that it is activating the sprinklers. But if the switch is off then it will not activate the scheduled watering of everyday at 5 am for 20 minutes a zone. Now, what if you did not want it to water everyday at 5 am but every other day. Openhab rules, utilizeses something called cron time and will execute the rule based on what you set.
 
42. 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.
 
43. Let’s do an example, I am going to set it to every 2 days at 5 am,
44. then press generate cron expression.
45. 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 20 minute cycle rule.
 
46. So paste into the rules file the following:
 
//If 20 Minute Cycle Switch Is On then water each zone for 20 min
rule "20 Minute Cycle"
when
  Item SprinklerCycle received update ON
then
      sendBroadcastNotification("SPRINKLER SYSTEM: 20 Minute Cycle Active")
      sendCommand(MKSprinklerSystemZone1, ON)
      createTimer(now.plusMinutes(20)) [|
           sendCommand( MKSprinklerSystemZone1, OFF)
           sendCommand( MKSprinklerSystemZone2, ON)
           createTimer(now.plusMinutes(20)) [|
               sendCommand( MKSprinklerSystemZone2, OFF)
               sendCommand( MKSprinklerSystemZone3, ON)
               createTimer(now.plusMinutes(20)) [|
                   sendCommand( MKSprinklerSystemZone3, OFF)
               ]
          ]
      ]
      sendCommand(SprinklerCycle, OFF)
end
 
What this does is first send you a notification that the 20 minute cycle is active and then it will individually water each zone for 20 minutes. That is it for the rules, all we did was add a scheduled rule for the sprinkler system and an instant on command rule.
 
47. Now press control x then y and enter.
48. Go back to the basic UI and turn on the 20 minute cycle switch.
 
If everything works right you should have gotten a notification and it should have turned on zone 1 then after 20 minutes zone 1 will turn off and zone 2 will turn on and then after another 20 minutes zone 2 will turn off and zone 3 will turn on.
 
49. Next turn on the sprinkler system schedule switch and then check if it correctly turned on the sprinkler system at the time you set up in the rules, you should also get a notification when it happens.
 
The schedule switch will stay on until you turn it off, it basically just turns the schedule on and off. That is it! The software is complete, now all we have to do is install the device in its final place which will be completed in the next final installation video. 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!

Sprinkler Control MQTT OpenHAB ESP8266

Final Install: 

Hey guys Matt here from MKSmartHouse.com and in this video I am going to show you how to install the sprinkler system in your house.
 
First up let's go over the materials and tools we need to install this sprinkler system. For materials we are just going to need screws, black electrical tape and the sprinkler valve power adapter. As for tools we are going to need wire strippers, wire cutters, a screw gun with phillips bit, and a small flat screw driver. The links to all the materials and parts are over on my website, the link is in the description. Before we get to installing let’s talk about the actual sprinkler valves for a second, the valves I am using are Orbit sprinkler valves, the link to them is also on my website, but these valves have two leads that come out of them and they require at least 24v to trigger them and let the water through. Also, these valves are AC which means it does not matter the direction of the current. So, let's talk about how I have them wired. Like I mentioned in the demo video I am currently only using two valves, anyway of the valves I am using I have taken 1 lead from each of them and connected them together along with a yellow wire from a gray 3 conductor wire. Then I took each remaining lead from each valve and connected them to their own leads on the 3 conductor gray wire. This means on the gray 3 conductor wire is 1 common and 2 individual. The way I wired it just makes it use less wire because I only use three wires instead of four, but it is not the only way to wire it. With this sprinkler system I designed you can just directly connect each valve with its two leads to its own port on the device. I will talk more about this later when we connect the wires, I just wanted to give you an idea of my sprinkler valve setup in case you guys had any questions. Alright let’s get to installing the actual device. First we are going to connect the sprinkler valve power adapter. The power adapter I am using is a 25V AC power adapter from rainbird, link to it is on my website. This power adapter comes with a connector, so grab the wire cutters and cut it off. Then take the wire strippers and strip each lead. Once they are stripped then fold back each one so that way the exposed part is touching the insulation. Now grab the sprinkler system and using the small flat screw driver loosen the source terminal. Then take the power adapter wires and put them in the terminal red to plus and black to minus. After that use the small flat screwdriver and tighten the terminal. Now we are going to mount the device, so find a spot that is close to the sprinkler valve wire. Once you find a spot, pull out the PCB with the wires attached out of the case. Then grab your screw gun and screw in the 2 screws through the holes of the case. Great now the case is mounted! But, the screws are metal and the PCB sits really close to them, so put black electrical tape over the screws to reduce the risk of shorting out. You can now put the PCB back into the case. Now we are going to connect the sprinkler valve wires. So loose the terminals for the zones. As you recall from before I have three leads that go to my valves 1 common and two individuals. Since I have it this way I connected the yellow common wire to the minus side of the first terminal block. I connected it to that because all the minus sides of the terminal blocks are connected together as a common. Then I connected the individual wires to the plus side of the first and second terminal block. Another way of connecting the valves is to just take the leads from each valve and just connect them to the terminal blocks one lead into plus and one into minus. It does not matter which lead goes into plus and which one into minus. Once all the leads are tightened down take the lid and put it on the device. The last thing we have to do is plug it in so grab the 5 volt usb power brick and the sprinkler valve power adapter and plug them into power. Great! That is it for the installation! Now grab your phone and go outside and test it out, go through each zone and see if it works. If it works then that is it, the sprinkler system is now complete and fully installed! 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!

Sprinkler Control MQTT OpenHAB ESP8266

PyFlasher:
If you are on mac click the latest dmg file and on windows click the latest exe file