Linux Format

Automatic plant waterer

Water your plants when the weather forecast suggests no rain.

-

You will need...

Having to dig out the ol’ watering can and potter about the garden might be some people’s idea of bliss, but it’s not very 21st century. Besides think of the time you can recoup for another hacking project. In our final Raspberry Pi project in this feature, we’re going to automate the whole watering of plants with a Pi linked to a weather forecast service and an add-on board that is connected to a pump.

To kick off, we start by soldering connection­s to the terminals of our pump. These can be secured with a hot-glue gun or heat shrink. You’ll need to use more wire on the barrel jack screw terminals and make a note of which is plus (+) and minus (-). On the Piface Relay Plus, locate relay 3 and insert the GND (-) of your power into the COM terminal and also one of the pump connection­s. Locate the NO (Normally Open) terminal and insert both of the remaining wires. Next, you’ll need to attach the Piface Relay Plus board to your Pi and boot to the desktop. To install the software for your Piface board and use openweathe­rmap with Python 3, open XTerminal and type: $ sudo apt-get update && sudo apt-get install python3pif­acerelaypl­us $ sudo pip-3.2 install pyowm

Open Python 3 IDLE via the programmin­g menu and create a new file. Save your project as garden_manager.py. We start the code by importing the Piface, pyowm and time libraries with import pifacerela­yplus, time, pyowm . Next, we create a variable called key and store our API key from http://openweathe­rmap.org.

We now need to create two functions: our first function controls the pump attached to Piface. This function we’re calling pump and it takes one argument: how long it should water the garden. def pump(time):

pfr = pifacerela­yplus.PiFaceRela­yPlus(pifacerela­yplus. RELAY) pfr.relays[6].toggle() time.sleep(time) pfr.relays[6].toggle()

We use a variable, pfr to shorten the function call for using a relay. Then we toggle the relay on and off, depending on its current state. We then pause using time.sleep() , permitting the water to flow, before we toggle the relay off.

Our second function retrieves the weather forecast for the next 24 hours. It takes two arguments: our location and the number of days to forecast. We then create a variable to store our openweathe­rmap API key, and a further two variables contain the output of the forecast functions. def forecast(x,y): owm = pyowm.OWM(key) fc = owm.daily_forecast(x,y) f = fc.get_forecast() We use a for loop to iterate over the forecast data. This feature comes into its own when used to forecast weather for multiple days: for weather in f:

rain_forecast = str(weather.get_status()) Finally, the function uses an if…else statement to check the forecast. If rain isn’t forecast this informatio­n is printed to the shell before calling the pump() function. If rain is forecast,

Newspapers in English

Newspapers from Australia