Skip to content

Instantly share code, notes, and snippets.

Created September 20, 2017 23:07
Show Gist options
  • Star 13 You must be signed in to star a gist
  • Fork 10 You must be signed in to fork a gist
  • Save benrules2/6f490f3a0e082ae6592a630bd7abe588 to your computer and use it in GitHub Desktop.
Save benrules2/6f490f3a0e082ae6592a630bd7abe588 to your computer and use it in GitHub Desktop.
# 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():
f = open("last_watered.txt", "r")
return f.readline()
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
print("Here we go! Press CTRL+C to exit")
while 1 and consecutive_water_count < 10:
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
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):
f = open("last_watered.txt", "w")
f.write("Last watered {}".format(
GPIO.output(pump_pin, GPIO.LOW)
GPIO.output(pump_pin, GPIO.HIGH)
Copy link

I've got this error:

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

Copy link

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" ?

Copy link

a brief explanation on how the code works would be nice

Copy link

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

Copy link

what does this mean

Copy link

lfaino commented Mar 29, 2019

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?

Copy link

OkayTrain121 commented Jun 28, 2019

def pump_on(pump_pin = 7, delay = 1): time.sleep(1)

I believe you intended to use "delay" as the input to time.sleep()

Copy link

tlynnch commented Oct 7, 2019

Nice and simple, I like it. I like the external power source for the water pump in the design. Great example of using a relay in a circuit especially if the circuit is expanded to more than one plant and pump.

Shouldn't you use a MCP3008 analog to digital converter for the water sensor? Plus allows multiple moisture sensors.

A little code cleanup needed:
Introduce constants for pumpOn = GPIO.HIGH and pumpOff = GPIO.LOW for clarity and to easily change if the relay is on low and off high.

 6   init = false                          # variable init never used or changed 
43  def pump_on(pump_pin = 7, delay = 1):  # parameter delay is never used

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment