FREE Shipping on orders over $49 USA $99 INTERNATIONAL

Control: Sprinkler Control MQTT OpenHAB ESP8266 Software Mac

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!