import machine
import utime
from RPi_I2C_driver import i2c_lcd
from RPi.GPIO import GPIO
# Define GPIO pins
relay_pin = 17
moisture_sensor_pin = 18
# Initialize the relay and moisture sensor
relay = GPIO(relay_pin, GPIO.OUT)
moisture_sensor = machine.Pin(moisture_sensor_pin, machine.Pin.IN)
# Initialize the LCD
lcd = i2c_lcd.i2c_lcd(0x27, 1, 2, 16)
lcd.backlight()
def water_plants():
lcd.lcd_clear()
lcd.lcd_display_string("Soil is dry.", 1)
lcd.lcd_display_string("Watering plants...", 2)
relay.value(1)
utime.sleep(10) # Run the pump for 10 seconds (adjust as needed)
relay.value(0)
lcd.lcd_clear()
lcd.lcd_display_string("Watering complete.", 1)
while True:
if moisture_sensor.value():
lcd.lcd_clear()
lcd.lcd_display_string("Soil is moist.", 1)
else:
water_plants()
utime.sleep(3600) # Check moisture every hour (adjust as needed)