Feature

Automated Garden Sprinkler System

Thomas Jackson

Issue 1, July 2017

This article includes additional downloadable resources.
Please log in to access.

Log in

As much as watering the garden might be a soothing sort of meditation for some, for others, it’s simply one of many necessary chores to keep a home looking nice. But it doesn’t mean we have to do it manually.

Using a Raspberry Pi, an OpenSprinkler breakout controller, and a little ingenuity, Thomas has created an intelligent IoT-connected watering system to keep the garden watered year-round. Although the garden may not need as much water in winter (as compared to summer), in Australia we can certainly go weeks or months without significant rain, so letting technology handle this task makes a lot of sense.

This installation has been used for in-ground watering, and can easily adapt above-ground sprinklers, drip-tube; just about any irrigation method. The system is equally suited to planter-pot micro-spray and other methods to keep your pots from drying out on a hot summer day, which can happen in just hours.

We’re definitely onboard for using technology to save time, and this certainly reclaims some time that could otherwise be lost. What made you embark on your automatic garden watering project?

Well I’m rather busy, so spending 30 minutes watering the garden with a hose isn’t a great use of my time. A sprinkler system on a standard timer doesn’t really account for the seasons either, so it’s easy to over- or under-water everything. Over the years, I’ve gained some experience hardware interfacing with Raspberry Pi in a few different projects, so I figured I’d apply my IT knowledge to a little home automation. I understand Python well, so I thought I’d give it a go. It ended up working so well that I set one up at my parents’ house too! They're both working really well.

You know it’s a success when someone else asks you for one too! How have you setup the network topology?

I setup a web server to do much of the “thinking”; the Pi basically then polls the web server and does what it’s told. I wrote the Python for the Pi from scratch. There’s some software with the OpenSprinkler that I could have made use of, but I really wanted to roll my own. I integrated it into an existing web server infrastructure that I’ve developed over time, so I can’t really share the PHP code itself; but I have included a sample XML response that the server produces, and the Python code that runs on the Pi. The web server also polls the BOM (Bureau of Meteorology) data feed (available in JSON format) for rain forecasts for my area. It doesn’t really matter if the forecasts aren’t 100% accurate – the plants aren’t going to mind if we get 10mm of rain instead of 20mm of rain! 

This seemed like a logical choice for the network setup. The live web server is data-centre connected, so it’s virtually guaranteed to be online all the time. It also provides a high speed connection for making requests to the BOM data feed. The Raspberry Pi can then sit safely behind my home firewall, and connects to the web server for updates (I setup private API authentication over HTTPS, so it’s secure too). If, for some reason, my home internet connection goes down and the Pi can’t connect to the server for an update, it just keeps going with the previously stored programme. 

Water control solenoids
The water control solenoids have to put up with humidity and temperature fluctuations.

That’s a great idea, we love the BOM polling to check the probability and amount of rainfall! It could also easily be adapted to incorporate any local water-restrictions if (or more accurately, when) we see another extended drought here in Australia too. If the Raspberry Pi can run autonomously, you must be storing data somehow. How did you handle that? 

I used the SQLite library for data storage. It is lightweight but powerful enough that it doesn’t make sense to try and “roll my own” storage system. The XML basically gets processed into SQLite database, then even if the hardware restarts from a power outage or something, it can pick up right where it left off.

Catering for a restart is a wise move – you don’t want a bug in the system turning the sprinklers on for hours because it got a little confused! On the topic of over-watering, have you incorporated any moisture-sensing technology into the system, as a second measure to help avoid that from occurring?

I wanted to keep it relatively simple. My garden has a lot of tolerance for too much or too little watering. If the plants look like they’re a bit dry, I can increase the “on” duration for the sprinklers via the web server. Likewise if the soil is perpetually saturated, I can reduce the duration too. I could run some sensors to various parts of the garden, but it’s healthy and it’s probably a little overkill. It would also require more digging... something I’m always keen to avoid!

Under house installation
The under-house installation is relatively straight forward, with several multi-core cables running to the solenoid bank.

It definitely sounds like the system is working well without the need for sensors. Was there much homework done before deciding on using an OpenSprinkler Pi (OSPi) hardware? 

It’s a proven piece of hardware, and by using the pre-rolled OSPi hardware you can almost get away without coding if you want to. It didn’t quite do what I wanted, so I wrote my own code, but that’s not entirely necessary either. The major benefit is that the GPIO on the Raspberry Pi breakout to a whole lot of switched outputs; they’re capable of driving the water solenoids directly from the board, so the hard work is done for you. It comes in a neat case, ready to roll with your own Pi. Then wiring is simple, there’s virtually no prototyping involved at a hardware level. The OSPi basically switches 24VAC, which is common in watering solenoids; so the actual watering hardware you choose is relatively flexible. 

Removing the hardware trial and error, and focusing on the code can definitely save time. Did it take long to get everything installed and working the way you wanted it to?

The bulk of my time really went into installing the sprinklers themselves, digging trenches, and that sort of thing. It’s back-breaking work digging trenches sometimes! My Python code isn’t going to win elegant code of the year or anything – it was done mostly after a day of digging – but it works well and hasn’t really been modified since I first wrote it.

Sometimes function is all you need. Your code is still commented and is reasonably clean – we’ve seen much worse! Thanks for taking us through your project. Is there anything you’re currently working on that you can briefly share?

My next project is probably whole house sound. I want to put speakers in the ceiling of most rooms and have multi-zone audio with a phone controller app or the like; something like Sonos would make it easy, but it’s much more fun to try and roll my own!

Definitely – the journey is the fun part for sure. Good luck with the project, we look forward to hearing more about it in the future.