Pastebin launched a little side project called VERYVIRAL.com, check it out ;-) Want more features on Pastebin? Sign Up, it's FREE!
Guest

Agontile's Raspberry Pi based sprinkler controller

By: Agontile on May 28th, 2013  |  syntax: None  |  size: 3.27 KB  |  views: 1,330  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Agontile's Raspberry Pi based sprinkler controller
  2.  
  3. Uses hardware interface from OpenSprinkler Pi:  http://rayshobby.net/?p=5339
  4.  
  5. Note: I am not a professional programmer.  I know exactly enough about Linux to be dangerous.
  6.  
  7. My approach uses the Unix crontab as the timer for my sprinklers.  I placed the file "sprinkler-init.sh" in /etc/init.d and set it to run at startup.  This script gets the current time from the hardware clock, and uses it to set the system time.  After that, time is adjusted by the network NTP service, but this makes sure the clock is close even if the network is down.
  8.  
  9. (Note that my Pi is an early version.  The current version has different pins for its GPIO.  These scripts will need to be adjusted, as discussed here: http://rayshobby.net/phpBB3/viewtopic.php?f=28&t=51 )
  10.  
  11. The actual operation of the sprinklers is controlled by the two scripts, "sprinkler-on.sh" and "sprinklers-off.sh".  The 'on' script is called with a single argument, the number of the sprinkler circuit desired: "sprinkler-on.sh 1".  It will only allow one circuit to be on at a time.  The 'off' script does not take any arguments, and turns all circuits off together.  These two scripts were my "phase 1" installation.  They allowed me to manually install a crontab that would run my sprinklers at fixed times.
  12.  
  13. My "phase 2" was to fetch a weather forecast from the internet, using the script "get-forecast.sh".  I registered for a free 'developer' account at wunderground.com and got an API key.  If you want to use my script, you'll need to get your own free API key, and edit it into the two URLs in the script.  If you don't live in California zip-91205, you'll also probably want to change the "/CA/91205" part of the URLs to your own location.  The script doesn't program any sprinklers, it just fetches the forecast, and writes a single parameter into each of four tiny text files: "forecast-high", "forecast-precip", "forecast-sunrise-hour", and "forecast-sunrise-minute".  This script is run as a crontab entry every day at 4AM.
  14.  
  15. As of this time, the final pieces of the puzzle are "sprinkler-durations.txt" and "forecast-program.c".  Together, they read the forecast files, and create and install a custom crontab.  To allow other entries to exist unchanged in the crontab, I inserted 'start' and 'end' headers in my crontab, so the program would know what portion to update.  These headers must exactly match the code in the C source file.
  16.  
  17. The file "sprinkler-durations.txt" has a few parameters to make it easy to change the watering pattern.  Among them are the names of each sprinkler circuit.  These names must exactly match the names in the C file.
  18.  
  19. EDIT: Originally there was a warning here about the last line in main().  That has now been successfully tested, and the warning removed.
  20.  
  21. Files used:
  22. sprinkler-init.sh               http://pastebin.com/AAR65mtf
  23. sprinkler-on.sh                 http://pastebin.com/XBLMptiH
  24. sprinklers-off.sh               http://pastebin.com/Lcbm9UQC
  25. get-forecast.sh                 http://pastebin.com/wc4qHKvR
  26. sprinkler-durations.txt         http://pastebin.com/UJVgVs9u
  27. forecast-program.c              http://pastebin.com/JG5d46YM
  28.  
  29. To do:
  30. * Allow start times other than 'sunrise'
  31. * Allow multiple start times in same day
  32. * Fetch time from hardware clock sooner in boot sequence
  33. * Web page front end to create sprinkler-durations.txt