0

I would like to run my a function within my python program when the time is 7am. Almost like a when function when time == time:

I don't want to use cron because I want this to be internal function not executing the whole script

Here is my python script I have created:

#sprinkler.py file for sprinkler system
#this is the main file for the sprinklerOS

#functions to import for our test_run.py file
import RPi.GPIO as GPIO
import time
import argparse
import sys
import datetime
#how many zones the user/YOU have set up
zone_count = 10
#gpio pins on the raspberry pi for sprinkler system
pin = [12,7,16,11,18,13,22,15,32,29]

#set mode to board meaning the pin number on pi (1-40) instead of gpio number
GPIO.setmode(GPIO.BOARD)

GPIO.setwarnings(False)


#setting up all of the lights or relays as outputs(in a while loop)
for a in range(10):
        GPIO.setup(pin[a],GPIO.OUT)
        GPIO.output(pin[a], GPIO.LOW)



def run_sprinklers():
        for b in range(zone_count):
                run_time = 5
                GPIO.output(pin[b], GPIO.HIGH)
                time.sleep(run_time)
                GPIO.output(pin[b], GPIO.LOW)
                time.sleep(1)

I want to run_sprinklers() when the time is 7am Thank you in advanced

Mason Horder
  • 108
  • 11
  • 2
    You generally do this by putting that portion of the program into a separate task, and running that task with `cron` (under UNIX) or its Windows equivalent. Doing this with a busy-check process withing another Python process is generally not the best design. – Prune Jul 19 '19 at 18:53
  • 2
    Generally it's best to use an external scheduling system such as `cron` to execute scripts at specific times or at specific intervals. – John Gordon Jul 19 '19 at 18:53
  • Possible duplicate of [How do I get a Cron like scheduler in Python?](https://stackoverflow.com/questions/373335/how-do-i-get-a-cron-like-scheduler-in-python) - Plenty of hits with search using `python run function at specific time site:stackoverflow.com` – wwii Jul 19 '19 at 18:55
  • Agree with above. What platform is this running on? Is it an arduino (which has a continuous running loop) or an RPi which would use cron? – mankowitz Jul 19 '19 at 18:55
  • it is running on a raspberry pi – Mason Horder Jul 19 '19 at 23:11

1 Answers1

2

As others have mentioned in the comments, cron is probably the best solution. However if this is a persistent process and you cannot rely on a tool like cron the schedule module, https://github.com/dbader/schedule, might be of interest to you.

import RPi.GPIO as GPIO
import time
import argparse
import sys
import datetime
import schedule

#how many zones the user/YOU have set up
zone_count = 10
#gpio pins on the raspberry pi for sprinkler system
pin = [12,7,16,11,18,13,22,15,32,29]

#set mode to board meaning the pin number on pi (1-40) instead of gpio number
GPIO.setmode(GPIO.BOARD)
GPIO.setwarnings(False)


#setting up all of the lights or relays as outputs(in a while loop)
for a in range(10):
        GPIO.setup(pin[a],GPIO.OUT)
        GPIO.output(pin[a], GPIO.LOW)


def run_sprinklers():
        for b in range(zone_count):
                run_time = 5
                GPIO.output(pin[b], GPIO.HIGH)
                time.sleep(run_time)
                GPIO.output(pin[b], GPIO.LOW)
                time.sleep(1)


schedule.every().day.at("7:00").do(run_sprinklers)


while True:
    schedule.run_pending()
    time.sleep(1)

Mason Horder
  • 108
  • 11
Andrew Sledge
  • 10,163
  • 2
  • 29
  • 30
  • i like this solution, i have never heard of cron before so i did not know what to search. just a quick question, say i want to run run_sprinklers at 7am and 8pm how do i do that. is schedule still a good solution? – Mason Horder Jul 19 '19 at 19:56
  • just add another entry `schedule.every().day.at("20:00").do(run_sprinklers)` – Andrew Sledge Jul 20 '19 at 01:11
  • ok, thanks. i saw that after viewing the github link. thanks for the help – Mason Horder Jul 20 '19 at 14:28