← Back to the chapter · Previous detail · Next detail →
Technical detail · Capstone Projects: Agriculture and Environment
Smart watering decision rule
4.4.1.1 Smart Watering Algorithm
def should_water(soil_moisture, weather_forecast, last_watered):
"""
Intelligent watering decision algorithm.
Args:
soil_moisture: Current soil moisture (0-100%)
weather_forecast: Dict with rain_probability, temp
last_watered: Datetime of last watering
Returns:
(should_water: bool, duration_seconds: int)
"""
# Define thresholds (plant-specific)
DRY_THRESHOLD = 30 # Water if below this
WET_THRESHOLD = 70 # Never water above this
RAIN_THRESHOLD = 60 # Don't water if rain likely
MIN_INTERVAL_HOURS = 6 # Minimum time between watering
# Check if too soon since last watering
hours_since = (datetime.now() - last_watered).total_seconds() / 3600
if hours_since < MIN_INTERVAL_HOURS:
return (False, 0)
# Check if rain is expected
if weather_forecast['rain_probability'] > RAIN_THRESHOLD:
return (False, 0) # Let nature do the work
# Check soil moisture
if soil_moisture > WET_THRESHOLD:
return (False, 0) # Already wet enough
if soil_moisture < DRY_THRESHOLD:
# Calculate watering duration based on how dry
deficit = DRY_THRESHOLD - soil_moisture
duration = min(deficit * 2, 60) # Max 60 seconds
return (True, duration)
return (False, 0)