Created
June 5, 2017 02:35
-
-
Save smoriarty21/d25eee4b527fcdefe979afbce209a98a to your computer and use it in GitHub Desktop.
Automated Plant Watering
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
import time | |
import RPi.GPIO as GPIO | |
MOISTURE_SENSOR_PIN = 4 | |
RELAY_PIN = 17 | |
def setupPinOutput(pin): | |
GPIO.setup(pin, GPIO.OUT) | |
def turnRelayOn(pin): | |
GPIO.output(pin, 0) | |
def turnRelayOff(pin): | |
GPIO.output(pin, 1) | |
def setupGPIO(): | |
GPIO.setmode(GPIO.BCM) | |
def setPinInput(pin): | |
GPIO.setup(pin, GPIO.IN) | |
def readPin(pin): | |
return GPIO.input(pin) | |
def main(): | |
setupGPIO() | |
setPinInput(MOISTURE_SENSOR_PIN) | |
setupPinOutput(RELAY_PIN) | |
turnRelayOff(RELAY_PIN) | |
while(True): | |
reading = readPin(MOISTURE_SENSOR_PIN) | |
if (reading): | |
print('We need to water!') | |
turnRelayOn(RELAY_PIN) | |
time.sleep(30) | |
turnRelayOff(RELAY_PIN) | |
time.sleep(450) | |
else: | |
print('We do not need to water!') | |
turnRelayOff(RELAY_PIN) | |
time.sleep(30) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment