-
-
Save blissdismissed/080ea9f55b983138059ba9dc91f58251 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# External module imp | |
import RPi.GPIO as GPIO | |
import datetime | |
import time | |
init = False | |
GPIO.setmode(GPIO.BOARD) # Broadcom pin-numbering scheme | |
def get_last_watered(): | |
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): | |
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 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) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment