Corey919
Posts: 4
Joined: Mon Oct 09, 2017 1:22 am

I need some direction!!

Mon Oct 09, 2017 1:38 am

So I'm very new to python and programming. I have been trying to teach myself by writing little scripts here and there. Recently i have started trying to make my sprinklers automated using the RPi and a 8 relay module. Basically what i want to do is have my sprinklers start at the same time every day and run 3 stations and eventually it will run 6 to 7 stations. My irrigation water pressure sucks so it would have to run 1 station for an hour or so shut off the start the next station and so on. Any help or direction with this would be very appreciated Thanks.

Here is the code I have written so far. The times need to be adjusted and i need to add more stations but this was just to try and see if it would work.

Code: Select all

import datetime, time
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BOARD)
GPIO.setup(7, GPIO.OUT)
GPIO.output(7, 1)
GPIO.setwarnings(False)

n = 10

while n > 0:
    print(n)
    time.sleep(1)
    n-=1
    
print("Station 1 ON")

if n < 1:
    GPIO.output(7, 0)
    time.sleep(2)
    GPIO.output(7, True)
    GPIO.cleanup()
    print("Station 1 OFF")

pcmanbob
Posts: 6619
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: I need some direction!!

Mon Oct 09, 2017 10:10 am

HI.

Taking your code and changing it a bit results in this

Code: Select all

import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BOARD)
GPIO.setup(7, GPIO.OUT)
GPIO.setup(11, GPIO.OUT)

# set minutes sprinkler is on
spon = 1
delay = spon * 60

# station one control
GPIO.output(7, 1)
print (" Station 1 ON")
time.sleep(delay)
GPIO.output(7, 0)
print (" Station 1 OFF")

# station two control
GPIO.output(11, 1)
print (" Station 2 ON")
time.sleep(delay)
GPIO.output(11, 0)
print (" Station 2 OFF")

GPIO.cleanup()
You can use a simple sleep between the on and off for your relay commands, you just need to repeat the block of code for each sprinkler you want to add, and you should only use gpio.cleanup at the end of your program.
now this may not be the prettiest or most efficient way of doing this but it works and you can see how it progresses.

now this is another way of doing the same thing but more efficiently using the same code over and over for each station.

Code: Select all

import RPi.GPIO as GPIO
import time

GPIO.setmode(GPIO.BOARD)
GPIO.setwarnings(False)

# list of gpio in station order
iolist = [12,16,18,22,24,26]

# set up gpio 
for io in iolist:
    GPIO.setup(io,GPIO.OUT)
    
# set minutes sprinkler is on
spon = 1
delay = spon * 60

    
st = 1
for io in iolist:
    print (" Station", st, "ON")
    GPIO.output(io, GPIO.HIGH)
    time.sleep(delay)
    print (" Station", st, "OFF")
    GPIO.output(io, GPIO.LOW)
    st = st + 1
    
GPIO.cleanup()
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

Corey919
Posts: 4
Joined: Mon Oct 09, 2017 1:22 am

Re: I need some direction!!

Mon Oct 09, 2017 3:26 pm

Thanks pcmanbob. I'll have to try this later today. I'll be sure to post the results.

User avatar
Gavinmc42
Posts: 3605
Joined: Wed Aug 28, 2013 3:31 am

Re: I need some direction!!

Tue Oct 10, 2017 3:31 am

You could do this with Linux cron and shell scripts too.
I have found Python not that reliable running 24/7.
Much better to let the Linux Os run stuff at set times.
I'm dancing on Rainbows.
Raspberries are not Apples or Oranges

pcmanbob
Posts: 6619
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: I need some direction!!

Tue Oct 10, 2017 9:31 am

As the original poster was tying to gain some experience with writing python, this is a good project to start with lots of ways to do what he wants, and lots to learn along the way, once he has settled on his python program to run his required sprinkler sequence then yes cron is the right way to call the python program at the required time of day.

I agree you could do it all in bash scripts and cron but for someone starting to learn programming python is probably easier to learn.
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

Corey919
Posts: 4
Joined: Mon Oct 09, 2017 1:22 am

Re: I need some direction!!

Wed Oct 11, 2017 2:55 pm

Just wanted to post an update. pcmanbob your code worked great. adjusted the delay time and it works perfectly. Thanks for the help.

User avatar
addeswe
Posts: 27
Joined: Fri Jul 19, 2013 3:49 pm
Location: Sweden

Re: I need some direction!!

Tue Oct 17, 2017 2:49 pm

This is how I would write it. A list of ports that gets activated one by one. Set as output and then be set to high for 3600 seconds (1 hour). Then it loops back to the next gpio and runs again. With a little luck. This is what you wanted.

Code: Select all

import RPi.GPIO as GPIO
import time

# Set relay ports used at the rpi here:
ports = [7,11,9,13,25]
# Yeah, above this line. :-)

GPIO.setmode(GPIO.BOARD)

for active in ports:
  GPIO.setup(active, GPIO.OUT)
  GPIO.output(active, 1);
  print("Starting sprinkler " + str(ports.index(active)+1))
  time.sleep(3600)
  GPIO.output(active, 0);
  print("Stopping sprinkler " + str(ports.index(active)+1))

GPIO.cleanup()
Sorry, missed the post where there is almost identical code. I'll just leave this here to maybe inspire somebody else. :-)
“Knock, knock.” - “Who’s there?” - very long pause…. - “Java.”
If a python knocks on your door and says “Hi, I’m a vacuum cleaner!” it’s not true. That’s a python.

Return to “Python”