watering_from_nife_io.py 784 B

12345678910111213141516171819202122232425262728
  1. import RPi.GPIO as GPIO
  2. import time
  3. # Pin setup
  4. SENSOR_PIN = 17 # Soil moisture sensor pin
  5. PUMP_PIN = 27 # Relay + pump pin
  6. GPIO.setmode(GPIO.BCM)
  7. GPIO.setup(SENSOR_PIN, GPIO.IN)
  8. GPIO.setup(PUMP_PIN, GPIO.OUT)
  9. print("Smart Plant Watering System Started...")
  10. try:
  11. while True:
  12. soil_state = GPIO.input(SENSOR_PIN)
  13. if soil_state == 0: # Dry soil
  14. print("Soil is dry → Watering plant...")
  15. GPIO.output(PUMP_PIN, True)
  16. time.sleep(5) # Run pump for 5 sec
  17. GPIO.output(PUMP_PIN, False)
  18. print("Watering complete")
  19. else:
  20. print("Soil is wet → No need to water.")
  21. time.sleep(10) # Check every 10 sec
  22. except KeyboardInterrupt:
  23. print("Exiting program...")
  24. GPIO.cleanup()