def pump_on(pump_pin=7, delay=1): init_output(pump_pin) f = open("last_watered.txt", "w") f.write("Last watered {}".format(datetime.datetime.now())) f.close() GPIO.output(pump_pin, GPIO.LOW) time.sleep(1) GPIO.output(pump_pin, GPIO.HIGH)
def Main(): try: # loop through each plant and call auto water plantList = plants.items() for plant in plantList: init_pins(plant[1]) while (True): for plant in plantList: auto_water(plant[1]) except KeyboardInterrupt: # If CTRL+C is pressed, exit cleanly: GPIO.cleanup() # cleanup all GPI
def init_pins(plant): outputPin = plant["pump_pin"] GPIO.setup(outputPin, GPIO.OUT) GPIO.output(outputPin, GPIO.LOW) GPIO.output(outputPin, GPIO.HIGH) inputPin = plant["water_sensor_pin"] GPIO.setup(inputPin, GPIO.IN)
def auto_water(delay=5, pump_pin=7, water_sensor_pin=8): consecutive_water_count = 0 init_output(pump_pin) print("Here we go! Press CTRL+C to exit") try: while 1 and consecutive_water_count < 10: time.sleep(delay) wet = get_status(pin=water_sensor_pin) == 0 if not wet: if consecutive_water_count < 5: pump_on(pump_pin, 1) consecutive_water_count += 1 else: consecutive_water_count = 0 except KeyboardInterrupt: # If CTRL+C is pressed, exit cleanly: GPIO.cleanup() # cleanup all GPI
def auto_water(plant, delay=5): consecutive_water_count = 0 pump_pin = plant["pump_pin"] water_sensor_pin = plant["water_sensor_pin"] print("Here we go! Press CTRL+C to exit") while 1 and consecutive_water_count < 10: time.sleep(delay) status = GPIO.input(water_sensor_pin) wet = status == 0 if not wet: if consecutive_water_count < 5: pump_on(pump_pin, 1) # record that plant was watered write_plant_watered(pump_pin) consecutive_water_count += 1 else: consecutive_water_count = 0
def Main(): try: GPIO.setmode(GPIO.BCM) GPIO.setwarnings(False) GPIO.setup(4, GPIO.OUT) GPIO.setup(17, GPIO.OUT, initial=GPIO.LOW) GPIO.setup(18, GPIO.OUT, initial=GPIO.LOW) GPIO.setup(21, GPIO.OUT, initial=GPIO.LOW) GPIO.setup(23, GPIO.IN, pull_up_down=GPIO.PUD_UP) GPIO.setup(15, GPIO.IN, pull_up_down=GPIO.PUD_DOWN) GPIO.setup(24, GPIO.IN, pull_up_down=GPIO.PUD_DOWN) GPIO.setup(26, GPIO.IN) while (True): if (GPIO.input(23) == False): GPIO.output(4, GPIO.HIGH) GPIO.output(17, GPIO.HIGH) time.sleep(1) if (GPIO.input(15) == True): GPIO.output(18, GPIO.HIGH) GPIO.output(21, GPIO.HIGH) time.sleep(1) if (GPIO.input(24) == True): GPIO.output(18, GPIO.LOW) GPIO.output(21, GPIO.LOW) time.sleep(1) if (GPIO.input(26) == True): GPIO.output(4, GPIO.LOW) GPIO.output(17, GPIO.LOW) time.sleep(1) except Exception as ex: traceback.print_exc() finally: GPIO.cleanup() #this ensures a clean exit
def init_output(pin): GPIO.setup(pin, GPIO.OUT) GPIO.output(pin, GPIO.LOW) GPIO.output(pin, GPIO.HIGH)
def get_status(pin=8): GPIO.setup(pin, GPIO.IN) return GPIO.input(pin)
# External module imp from EmulatorGUI.GPIO import GPIO #import RPi.GPIO as GPIO import datetime import time init = False GPIO.setmode(GPIO.BCM) # Broadcom pin-numbering scheme def get_last_watered(plant): try: f = open("last_watered.txt", "r") return f.readline() except: return "NEVER!" def get_status(pin=8): GPIO.setup(pin, GPIO.IN) return GPIO.input(pin) def init_output(pin): GPIO.setup(pin, GPIO.OUT) GPIO.output(pin, GPIO.LOW) GPIO.output(pin, GPIO.HIGH) def auto_water(delay=5, pump_pin=7, water_sensor_pin=8):
def __init__(self, time): self._time = time GPIO.setmode(GPIO.BCM) # Broadcom pin-numbering scheme
def getStatus(self, pin): GPIO.setup(pin, GPIO.IN) return GPIO.input(pin)
def pumpOn(self, pin, delay=1): self.initOutput(pin) GPIO.output(pin, GPIO.LOW) self._time.sleep(delay) GPIO.output(pin, GPIO.HIGH)
def updatePin(self, pin, value): GPIO.output(pin, GPIO.LOW)
def initOutput(self, pin): GPIO.setup(pin, GPIO.OUT) GPIO.output(pin, GPIO.LOW) GPIO.output(pin, GPIO.HIGH)
def cleanup(self): GPIO.cleanup() # cleanup all GPI
def pump_on(pump_pin=7, delay=1): GPIO.output(pump_pin, GPIO.LOW) time.sleep(1) GPIO.output(pump_pin, GPIO.HIGH)