In the summer when our garden is in full swing we tend to take vacation. I had to beg family and friends to water my garden and I'm particular so I just went and made my own garden watering system. Its a Raspberry Pi powered six channel watering system, it has a 24VAC transformer and solid state relays to control them. Everything is scripted in Python and the scheduling of how/when the script gets executed is run via cron job. Its a bit ridiculous and over designed but its on purpose, I plan to use this for a long time and as my gardening operations expand and grow it will fill the role without constant modification.
#!/usr/bin/env python
import RPi.GPIO as GPIO
import time
# ***CONFIGURATION STARTS HERE***
#time, in seconds, to run each channel
watering_time = [90, 90, 90, 90, 180, 120]
#select which channels to turn on; 1 = on, 0 = off/skip
channels = [1, 1, 1, 1, 1, 0]
#list all the GPIO numbers, in order, to run through/trigger
GPIO_list = [14, 15, 17, 18, 22, 23]
# Getting length of list
length = len(GPIO_list)
i = 0
# ***CONFIGURATION ENDS HERE***
if len(channels) != len(GPIO_list):
print("The 'channels' and 'GPIO_list' arrays are not the same lengths! Go fix it")
exit()
GPIO.setmode(GPIO.BCM)
#GPIO.setmode(GPIO.BOARD)
#The GPIO.BOARD option specifies that you are referring to the pins by the number of the pin on the plug - i.e the numbers printed on the board (e.g. P1) and in the middle of the diagrams below.
#The GPIO.BCM option means that you are referring to the pins by the "Broadcom SOC channel" number, these are the numbers after "GPIO" in the green rectangles
#setup the 120VAC switch separate from the solenoid outputs
GPIO.setup(4, GPIO.OUT) #120VAC Switch
GPIO.output(4, GPIO.LOW) #set to 120VAC switch LOW
#loop through 'GPIO_list' array. Setup all the GPIO and set them to LOW
while i < length:
GPIO.setup(GPIO_list[i], GPIO.OUT) #setup as output
GPIO.output(GPIO_list[i], GPIO.LOW) #set to LOW output
i += 1
#reset back to zero for the next loop
i = 0
time.sleep(0.100)
print('Turning AC power ON, Press CTRL+C to exit \n')
GPIO.output(4, GPIO.HIGH) #turn the solenoid ON
time.sleep(.100)
try:
#loop through 'GPIO_list' array
while i < length:
if channels[i] == 1:
print("Turn solenoid channel %d ON" % (i+1))
GPIO.output(GPIO_list[i], GPIO.HIGH) #turn the solenoid ON
time.sleep(watering_time[i])
print("Turn solenoid channel %d OFF \n" % (i+1))
GPIO.output(GPIO_list[i], GPIO.LOW) #turn the solenoid OFF
time.sleep(.100)
else:
print("Skipping channel %d \n" % (i+1))
i += 1
except KeyboardInterrupt: # If CTRL+C is pressed, exit cleanly:
print('Turning AC power off')
GPIO.output(4, GPIO.LOW) #turn the solenoid OFF
time.sleep(.100)
GPIO.cleanup() # cleanup all GPIO
exit()
#if exiting normally, without CTRL+C, shut off AC power and release all the GPIOs
print('Turning AC power off')
GPIO.output(4, GPIO.LOW) #turn the solenoid OFF
time.sleep(.100)
GPIO.cleanup() # cleanup all GPIO