


Relay coding
Code: Select all
import RPi.GPIO as GPIO
import time
channel = 21
# GPIO setup
GPIO.setmode(GPIO.BCM)
GPIO.setup(channel, GPIO.OUT)
def motor_on(pin):
GPIO.output(pin, GPIO.HIGH) # Turn motor on
def motor_off(pin):
GPIO.output(pin, GPIO.LOW) # Turn motor off
if __name__ == '__main__':
try:
motor_on(channel)
time.sleep(1)
motor_off(channel)
time.sleep(1)
GPIO.cleanup()
except KeyboardInterrupt:
GPIO.cleanup()
Soil coding
#!/usr/bin/python
import RPi.GPIO as GPIO
import time
#GPIO SETUP
channel = 14
GPIO.setmode(GPIO.BCM)
GPIO.setup(channel,GPIO.IN)
def callback(channel):
if GPIO.input(channel):
print ("DRY SOIL")
else:
print ("WATERY SOIL")
GPIO.add_event_detect(channel, GPIO.BOTH, bouncetime=300) # let us know when the pin goes HIGH or LOW
GPIO.add_event_callback(channel, callback) # assign function to GPIO PIN, Run function on change
# infinite loop
while True:
time.sleep(1)