Sunday, July 9, 2017

Watering the Garden with Raspberry Pi

If you've ever planted a garden or grass seed in the spring, you know that watering can be a time consuming task, and going out of town for a weekend can be disastrous for your new plants if the weather is hot and no one is around to water them.  More importantly, I just like to build things, so why not scratch that building itch and create an automated hose bib that can be scheduled to turn on and off using a Raspberry Pi?

My original approach to this project involved a watering system located near the garden, but I ran into several complications during the design phase.  The first issue arose trying to power the Pi as well as the electronic solenoid valve that would control the water flow.  Running wires across the back yard was not an ideal solution, and battery power was going to require frequent recharging.  I was also going to have to find a way to protect the electronics from the sun's UV rays as well as moisture, while at the same time allowing enough air-flow to let heat escape.  It occurred to me that locating all of the electronics inside the house and using them to control a hose bib on the side of the house solved both of these issues, and also added the flexibility to allow me to change the location of my automatic sprinklers or drip-watering-systems whenever necessary.

With this basic concept in mind, I broke the project into three segments: electronic hardware, plumbing hardware, and control software.  For the electronic hardware, I would need to be able to switch on and off a 12V fused power source that would supply an electric solenoid valve that would control the water.  This could easily be done by controlling a relay with a GPIO pin on the Pi.  For the plumbing hardware, I would need to tap into my water line and run it through the electric solenoid valve, and from there outside to a hose bib.  I would also need to be sure to include a check valve to prevent the back-flow of water from the outside, and there would need to be a way to drain any outdoor pipes to prevent freezing in the winter.  The control software would be the easiest.  All I needed was a short start-up script to set-up the GPIO pin, and then the timing of opening and closing the valve could be done with cron jobs.  With a plan in place it was time to get building.

Electronic Hardware

For this project I purchased a 12V / 3amp normally-closed brass solenoid valve to control the water flow.  You can buy a plastic one cheaper, but I had reservations about connecting a plastic water valve to my plumbing.  Basically all I needed to do was to connect a GPIO pin from the Pi to a relay that could then switch on/off the 12V power supply to the valve.  I went with a 4-channel relay module since I already had plans for future projects that would require additional relays.  I had a 12V / 30 amp power supply from previous projects on hand, which will provide more than enough current for this project, as well as anything I might need it for in the future.  The only thing left to plan was a fuse for the power to the valve.  For most people a simple in-line fuse holder would solve this problem, but I just so happened to have an old DC fuse panel lying around looking for a job to do, so I put it to use.  Here is the schematic for the final design:


In the above schematic I also drew in the relay board.  Obviously if you use the same relay board that I did, you don't need to build any of this, but I wanted to include it so that anyone using a different relay could adapt the circuit to their own needs.

Regarding the switches, I added the first switch to cut the 12V power so I don't have to pull the fuse every time I want to make adjustments to the set-up.  This particular switch has an LED that is powered via the same 12V that it switches, so I had to add a resistor to the ground leg on the switch to limit the current for the LED without limiting the current to the valve.  This was done by simply cutting the wire and adding the resistor in-line.  I also added a second switch to bypass the relay and open the valve without involving the Pi.  This is a standing "request" from my wife that anything I automate must still have a manual switch for operation.

One last, but very important note, when I finally had the whole set-up finished, I tested it out.  As soon as I sent power to the solenoid, my Pi emailed me that my smoke detector was alerting and gave me an audible alert that it was preparing to shut down.  What!?!?  These are all separate systems that should have nothing to do with each other except that the same Pi controls them all!  Well, since I had run my 12V lines along the same path as the jumpers for all of my GPIO pins, as soon as I switched the solenoid on, the electrical noise on the 12V lines interfered with the signals on the other GPIOs, causing a moment of havoc for the Pi. This could have been solved by rerouting my wires, or by trying to smooth things over with capacitors, but I happened to have some ferrite core noise suppression filters lying around, so I tried one out.  After testing several times, I can verify that this does in fact solve the problem.  With the noise filter there are no issues, without it the Pi goes a little nuts.  So keep this in mind when determining your cable management, or invest in some filters to solve the problem after the fact.

Plumbing Hardware


I hate plumbing, but it's a handy thing to know how to do.  I know just enough about it to make me dangerous, but that probably applies to a lot of things.  I won't go into too much detail here, because everyone's plumbing situation is going to be different, so there is no one-size-fits-all solution.  I am going to try to focus on the few components that will be universally required, and then just skip to the final result.  If you are not completely comfortable with altering the plumbing in your house, then you may want to look into another way to do this project.  Plumbers are expensive, so mistakes could be costly, not to mention messy.  You've been warned, so I accept no responsibility if you choose to proceed and accidentally flood your house.


Above is the basic set up for the plumbing piece.  Starting on the left, the first piece we need is a cut-off valve.  If my improvised plumbing leaks or brakes, or my Pi goes all Superman 3 on me, I need to have a way to cut off the water from it's source.  Next comes the solenoid valve that will be controlled by the Pi.  After that we need to use a check valve to prevent the water from the hose from draining back into my water pipes (this is probably required by code in most places).  Finally, I used a T-junction and another cut-off valve to allow for draining the outdoor pipes.  This valve will stay closed during normal operation, and will only be opened at the end of the season in order to drain the outdoor pipes so they don't freeze.  The outlet for this valve will lead to a sink basin in my shop so that I don't have to mess with sewer lines and traps.


So that is the basic design.  To the right is the fully installed product.  To tap into the waterline I opted to slice into one of my newer Pex waterlines, which is much easier than trying to mess with 70 year old galvanized water pipe.  For the line that continues outside I opted to use galvanized steel water pipe because I wanted something more rigid to support the solenoid valve, and because you can't run Pex outside.  If you do something similar and use steel pipe outside, be sure to paint it to keep it from rusting.


Control Software

With everything in place, all I needed to do was to write a couple of short scripts to control the solenoid valve.  This is the easy part.  Here are the two python scripts called water-on.py and water-off.py:

 #!/usr/bin/env python  
   
 # water-on.py  
   
 import RPi.GPIO as GPIO  
 from time import sleep  
   
 RELAY_PIN = 26  
   
 GPIO.setmode(GPIO.BCM)  
 GPIO.setup(RELAY_PIN, GPIO.OUT)  
   
 GPIO.output(RELAY_PIN, 0)  
 sleep(1200)  
 GPIO.output(RELAY_PIN, 1)  

 #!/usr/bin/env python  
   
 # water-off.py  
   
 import RPi.GPIO as GPIO  
   
 RELAY_PIN = 26  
   
 GPIO.setmode(GPIO.BCM)  
 GPIO.setup(RELAY_PIN, GPIO.OUT)  
 GPIO.output(RELAY_PIN, 1)  

For the water-on.py script I added in a 20 minute timer before automatically turning the water back off as a fail-safe.  As always, once the scripts are created using a text editor, they need to be made executable by using

chmod +x water-on.py
chmod +x water-off.py

I need to make sure that the GPIO pin that controls the relay isn't floating when the Pi reboots, so I need to run the water-off.py script on boot.  I am going to do this by adding the following line to the /etc/rc.local file:

sudo /home/pi/water/water-off.py

This time I am also going to make the file executable without having to specify the whole path to the file by copying it to the /usr/bin folder like so:

cp /home/pi/water/water-on.py /usr/bin/water-on
cp /home/pi/water/water-off.py /usr/bin/water-off

Notice I dropped the .py when I added the file to /usr/bin.  This is not necessary, but in Linux land file extensions are not required, and it saves me a little bit of typing.  So now the water can be turned on with a simple command.

water-on

But the point of this was not to be able to turn on the water by typing a command, we could do that more easily by flipping a switch.  The goal is to be able to schedule the water to turn on and off automatically.  For this we will use Linux's built in scheduling system called Cron.  To add a task to the cron table we use the following command:

crontab -e

 The first time you edit the cron table you will be prompted to pick a text editor.  I prefer vim, but it's purely a matter of personal taste.  When the file is opened for the first time there is nothing but lines of comments explaining how Cron works.  Since the directions are built into the cron table, I won't go into too much detail here.  To turn the sprinklers on for 20 minutes every morning at 6:00am you would add the following two lines to the end of the cron table:

00 6 * * * water-on &
20 6 * * * water-off

Remember that I added a fail-safe to the water-on script to have it shut off after 20 minutes, so the water-off isn't 100% necessary, but it seems like good practice.  If you wanted to run the sprinklers for more than 20 minutes you could either edit the sleep line of the water-on script, or just add another line to the cron table to turn the water back on after 20 minutes.

So that's it.  Now every morning my Pi will wake up at 6am sharp and take care of my watering for me.  Sure you can buy an off-the-shelf product that will do the same thing, but what's the fun in that?  If anyone needs any help adapting this walk-through to your own projects just let me know in the comments and I'll do my best to help.


7 comments:

  1. This is super dope, thanks for posting. I'm looking to do something similar with for anti-mosquito spraying. There's products out there, but where's the fun in that?

    ReplyDelete
  2. nice project. A small question though. When there was/has been a rain shower, what do you do. Esp. when from home. I trust you have remote access to your RP.

    ReplyDelete
    Replies
    1. Yes, I have remote access, but the sprinklers usually kick on at 6:00 am, so usually I am in bed hitting the snooze button when it's watering. I currently have some soil moisture sensors on order. When they get here I am going to modify the scrip for watering to check the soil moisture to see if watering is necessary before turning on the sprinklers. I plan to update this post after I make the change. Good question. Thanks for your interest.

      Delete
  3. This is awesome love to read this instructive information.I have also remote access.But yeah the sprinklers usually kick on early in the morning but anyhow it depends allot situation,If the air is dry, they will need more water. The warmer the temperature is, the more water they need.I have a beautiful garden with an working garden irrigation system and i always love to care my raspberries.

    ReplyDelete
  4. Hello, i think that i saw you visited my blog so i came to “return the favor”.I am trying to find things to improve my website!I suppose its ok to use some of your ideas!! norlog pond

    ReplyDelete
  5. Do I need to change the current AC to DC?

    ReplyDelete
    Replies
    1. Yes, the Raspberry Pi requires 5V DC, and the electric solenoid requires 12V DC.

      Delete