def init_app(): global uses_solar_data # See if any of our slots require solar data (sunrise, sunset) # this drives the slot builder that runs every morning at 12:01 AM uses_solar_data = check_for_solar_events() if uses_solar_data: print("\nSolar data enabled") # do we have long and lat values? if LOC_LAT or LOC_LONG: print("Lat and Long values exist") else: # then we can't run, and we need to terminate print("\nINVALID CONFIGURATION: Lat or Long values missing") sys.exit(1) get_solar_times() # build the daily slots array for today build_daily_slots_array() # are we supposed to be on? if is_on_time(): # then turn the relay on print("\nWhoops, we're supposed to be on!") relay.set_status(True)
def main_loop(): # initialize the relay, nothing will work until you do relay.init(RELAY_PIN) # Turn the relay off, just to make sure it starts off relay.set_status(False) while 1: # then toggle the relay every second until the app closes relay.toggle() # wait a second time.sleep(1)
def process_loop(): # initialize the lastMinute variable to the current time minus 1 # this subtraction isn't technically accurate, but for this purpose, # just trying to understand if the minute changed, it's OK last_time = inc_time(get_time_24(datetime.now()), -1) # infinite loop to continuously check time values while 1: # Is the button pushed? if btn.is_pressed: print("Detected button push") # Then toggle the relay relay.toggle() else: # otherwise... # get the current time (in 24 hour format) current_time = get_time_24(datetime.now()) # is the time the same as the last time we checked? if current_time != last_time: # No? OK, so the time changed and we may have work to do... print(current_time) # reset last_time to the current_time (so this doesn't happen until # the next minute change last_time = current_time # build the daily slots array every day at 12:01 AM # that's 1 (001) in 24 hour time if current_time == 1: # if one of the solar times is enabled if uses_solar_data: # populate our sunrise and sunset values for the day get_solar_times() # build the list of on/off times for today build_daily_slots_array() # otherwise just use the static slots we already have # finally, check to see if we're supposed to be turning the # relay on or off for slot in daily_slots: if current_time == slot[0]: relay.set_status(True) if current_time == slot[1]: relay.set_status(False) # wait a little bit, then check again time.sleep(.25)
import time import gpiozero import relay # set this variable to the GPIO pin the relay is connected to RELAY_PIN = 18 def main_loop(): # initialize the relay, nothing will work until you do relay.init(RELAY_PIN) # Turn the relay off, just to make sure it starts off relay.set_status(False) while 1: # then toggle the relay every second until the app closes relay.toggle() # wait a second time.sleep(1) if __name__ == "__main__": try: main_loop() except KeyboardInterrupt: # turn the relay off, just in case its on relay.set_status(False) print("\nExiting application\n") # exit the application sys.exit(0)