Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
# 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)
@MauroNovellas

This comment has been minimized.

Copy link

commented Nov 30, 2017

I've got this error:

File "water.py", line 50, in
GPIO.output(pump_pin, GPIO.HIGH)
NameError: name 'pump_pin' is not defined

@benrules2

This comment has been minimized.

Copy link
Owner Author

commented Dec 4, 2017

You can see line 43 pump_pin is assigned a default value of 7. Can you confirm all lines 44-50 are indented properly so they're a part of "pump_on" ?

@billowbashir

This comment has been minimized.

Copy link

commented Nov 1, 2018

a brief explanation on how the code works would be nice

@20schmitztj

This comment has been minimized.

Copy link

commented Dec 12, 2018

Traceback (most recent call last):
File "<pyshell#1>", line 1, in
import water
ImportError: No module named 'water'

@20schmitztj

This comment has been minimized.

Copy link

commented Dec 12, 2018

what does this mean

@lfaino

This comment has been minimized.

Copy link

commented Mar 29, 2019

Hi,
I have a problem with your schema. How can you use an Analog output from the moisture sensor? I always get 1 either in water or without water?

Do you use any trick?
Cheers
Luigi

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.