Smart lawn sprinkler

In drought-struck states like California, United States, there are severe restrictions on water usage in certain parts of the state. For example: In summer, some cities passed an ordinance restricting water usage to 250 gallons per day. In such states, it is ridiculous to find lawn sprinklers going off the day before the rain. We are going to build a lawn sprinkler controller that only turns on when there is no rain predicted for the next day.

In order to build a smart lawn sprinkler, we need a flow control solenoid valve (for example, https://www.sparkfun.com/products/10456). Make sure that the valve can meet the water pressure requirements. This flow control valve can be interfaced to the Raspberry Pi Zero using a transistor switching circuit discussed in earlier chapters or the relay board we discussed earlier in this chapter.

  1. We will be making use of DarkSky API (https://darksky.net) to fetch the weather information. It provides a simple response format that could be used to determine if it was going to rain the next day.
  2. Sign up for a free account at the website and get a developer key from the console.
  3. According to the API documentation, the local weather information may be obtained as follows:
       https://api.darksky.net/forecast/[key]/[latitude],[longitude]
  1. The latitude and longitudinal coordinates can be obtained using a simple web search. For example, the request URL for Newark, CA is:
       URL = ("https://api.darksky.net/forecast/key" 
"/37.8267,-122.4233?exclude=currently,minutely,hourly")
  1. The response includes the current, minutely, and hourly forecasts. They can be excluded using the exclude parameter as shown in the preceding URL.
  2. Now, we need to turn on the sprinkler only if it is not going to rain the next day. According to the API documentation, the weather forecast is returned as a Data Point object. The data points include a field named icon that indicates whether it is going to be clear, cloudy, or rainy.
  3. Let's write a method check_weather() that fetches the weather for the week:
       def check_weather(): 
try:
response = requests.get(URL)
except Exception as error:
print(error)
else:
if response.status_code == 200:
data = response.json()
if data["daily"]["data"][1]["icon"] == "rain":
return True
else:
return False
  1. If the GET request was successful, which can be determined by the status code of the response, the json response is decoded using the json() method.
  2. The next day's weather information is available at data["daily"]["data"][1] (Print the response and verify it for yourself).
  3. Since the icon key provides a machine-readable response, it could be used to turn on the sprinkler. Hence, the check_weather() returns True if it is going to rain and vice versa.

We will let you figure out interfacing the solenoid valve using the GPIO pins. The earlier code sample is available for download along with this chapter as lawn_sprinkler.py.

Exercise for the reader:

We are making use of the next day's weather information to turn on the sprinkler. Go through the documentation and modify the code to account for current weather information.

Project enhancements: