← Back to Course
Weather-Based Irrigation Decisions
💧 Weather-Based Irrigation Decisions
🌤️ What You'll Learn:
- 📊 Make irrigation decisions based on weather conditions
- 🌧️ Automatically skip watering when rain is detected
- 🌡️ Adjust watering duration based on temperature and humidity
- 💰 Save 30-50% more water by factoring in weather forecasts
📊 Decision Matrix Using Weather Data
-
🌧️ Rain detected in last 6 hours
→ ✓ Skip irrigation (rain provides natural watering)
💧 Water saved: 100% of scheduled amount -
🌡️ Temperature > 35°C + No rain
→ ⚠️ Increase watering by 50% (high evaporation)
💧 Without adjustment: 30-40% water lost to evaporation -
💨 Humidity < 30% + No rain
→ ⚠️ Plants stressed - water immediately + increase duration by 25%
💧 Low humidity causes rapid transpiration (plants lose water faster) -
🌤️ Forecast rain + soil moisture > 50%
→ ✓ Skip irrigation (rain expected soon)
💧 Prevents over-watering and runoff -
⚠️ Soil moisture < 30% + No rain forecast
→ ⚠️ WATER IMMEDIATELY - critical stress level
💧 Yield loss if delayed: 15-25% reduction
💡 Weather-Based Savings:
Farmers using weather-based irrigation save an additional 30-50% water compared to soil moisture alone. A 10-acre farm saves 500,000-1,000,000 liters/year by skipping irrigation before rain!
🔌 Hardware Setup for Weather Monitoring
- 🔹 Rain Sensor (LM393) — $6 · Detects rain/water
- 🔹 DHT22 Sensor — $10 · Temperature + humidity
- 🔹 Soil Moisture Sensor — $8 · Core soil data
- 🔹 Optional: Anemometer — $25 · Wind speed (high wind = more evaporation)
🔌 Wiring Diagram (Add Rain Sensor)
ESP32 → Rain Sensor (LM393)
GPIO35 → DO (Digital output - rain detected)
3.3V → VCC
GND → GND
ESP32 → DHT22 (Temperature + Humidity)
GPIO15 → DATA pin
3.3V → VCC
GND → GND
ESP32 → Soil Moisture Sensor
GPIO34 → AO (analog out)
3.3V → VCC
GND → GND
📖 Complete Weather-Based Irrigation Code
#include <DHT.h>
#include <WiFi.h>
#define RELAY_PIN 26
#define SOIL_PIN 34
#define RAIN_PIN 35
#define DHT_PIN 15
DHT dht(DHT_PIN, DHT22);
const unsigned long CHECK_INTERVAL = 1800000; // 30 minutes
// Weather thresholds
const int RAIN_DETECTED = LOW; // Rain sensor output LOW when wet
const float HOT_TEMP = 35.0; // >35°C = increase watering
const float LOW_HUMIDITY = 30.0; // <30% = plant stress
// Soil thresholds
const int DRY_THRESHOLD = 30;
const int OPTIMAL_THRESHOLD = 60;
unsigned long lastCheck = 0;
bool rainDetected = false;
float temperature = 0;
float humidity = 0;
void setup() {
Serial.begin(115200);
pinMode(RELAY_PIN, OUTPUT);
pinMode(RAIN_PIN, INPUT_PULLUP);
digitalWrite(RELAY_PIN, HIGH); // Pump OFF
dht.begin();
Serial.println("🌤️ Weather-Based Irrigation Started");
}
int getSoilMoisture() {
int raw = analogRead(SOIL_PIN);
return map(raw, 4095, 1500, 0, 100);
}
float getTemperature() {
return dht.readTemperature();
}
float getHumidity() {
return dht.readHumidity();
}
int calculateWateringDuration(int baseMinutes) {
int duration = baseMinutes;
if (rainDetected) {
Serial.println("🌧️ Rain detected - skipping irrigation");
return 0;
}
if (temperature > HOT_TEMP) {
int extra = baseMinutes * 0.5; // +50% for high heat
duration += extra;
Serial.printf("🌡️ Hot (%0.1f°C) - adding %d min\n", temperature, extra);
}
if (humidity < LOW_HUMIDITY) {
int extra = baseMinutes * 0.25; // +25% for dry air
duration += extra;
Serial.printf("💨 Low humidity (%0.1f%%) - adding %d min\n", humidity, extra);
}
return min(duration, 30); // Max 30 minutes (fail-safe)
}
void loop() {
unsigned long now = millis();
if (now - lastCheck >= CHECK_INTERVAL) {
lastCheck = now;
// Read all sensors
rainDetected = (digitalRead(RAIN_PIN) == RAIN_DETECTED);
temperature = getTemperature();
humidity = getHumidity();
int moisture = getSoilMoisture();
Serial.println("=================================");
Serial.printf("🌧️ Rain: %s\n", rainDetected ? "YES" : "NO");
Serial.printf("🌡️ Temp: %0.1f°C\n", temperature);
Serial.printf("💧 Humidity: %0.1f%%\n", humidity);
Serial.printf("🌱 Soil: %d%%\n", moisture);
// Decision logic
if (rainDetected) {
Serial.println("✅ Skipping irrigation (rain provides water)");
}
else if (moisture < DRY_THRESHOLD) {
int duration = calculateWateringDuration(10); // Base 10 min
if (duration > 0) {
Serial.printf("💧 Watering for %d minutes\n", duration);
digitalWrite(RELAY_PIN, LOW); // Pump ON
delay(duration * 60000);
digitalWrite(RELAY_PIN, HIGH); // Pump OFF
}
}
else if (moisture > OPTIMAL_THRESHOLD) {
Serial.println("✅ Soil moisture optimal - no watering");
}
else {
Serial.println("🌿 Soil moisture acceptable - no action");
}
}
}
💡 Weather API Alternative (No Extra Sensors):
You can get weather data from free APIs instead of adding rain/humidity sensors:
- OpenWeatherMap — Free tier: 1,000 calls/day
- WeatherAPI.com — Free tier: 1,000,000 calls/month
- Visual Crossing — Free tier: 1,000 calls/day
ESP32 connects to WiFi, fetches forecast, and decides to water. No extra hardware needed!
📖 Case Study — Weather Integration Saves $1,500/Year, South Africa:
A 25-acre maize farm added weather-based decision logic to their existing irrigation system:
- 🌧️ Rain detection: Skipped 12 irrigation cycles during rainy season
- 💰 Water savings: 850,000 liters saved per year ($1,200)
- ⚡ Energy savings: $300/year in pump electricity
- 📈 Yield impact: 0% loss (perfect moisture maintained)
- 🌱 Environmental: Reduced runoff and fertilizer leaching
"Before, I watered every Tuesday whether it rained or not. Now the system checks the weather first. I've cut my water bill in half and my crops are healthier!" — Farm manager, Free State Province
🌟 OceanRemote Automation Rules (Dashboard):
- RULE 1: IF rain_detected = true → SKIP irrigation cycle
- RULE 2: IF soil_moisture < 30% AND rain_detected = false → START pump
- RULE 3: IF temperature > 38°C → Double watering duration
- RULE 4: IF humidity < 25% → Add 25% to watering time
- RULE 5: IF forecast_rain_next_6h = true AND soil_moisture > 50% → SKIP
⚠️ Important Notes:
- Rain sensors can false-trigger from morning dew — add a 1-hour delay before skipping
- High wind (>20km/h) increases evaporation by 30-50% — consider wind sensor for accuracy
- Always keep a minimum watering schedule (e.g., at least once per 5 days) as fail-safe
- Weather API requires WiFi — ensure your ESP32 has reliable connection
🎯 Key Takeaways:
- ✅ Weather-based irrigation saves 30-50% MORE water than soil moisture alone
- ✅ Rain sensors or weather APIs prevent over-watering before storms
- ✅ High temperature (+50% water) and low humidity (+25% water) require adjustments
- ✅ A $6 rain sensor pays for itself in 1-2 months through water savings
- ✅ Always use fail-safe timers — never trust sensors 100%
- ✅ Combine soil + weather data for optimal irrigation decisions
💡 Key Takeaways:
- Apply these concepts directly to your farm or project.
- Take notes on important details for the quiz.
- Use the button below to track your progress.
×