OC
OceanRemote
Low-code IoT platform
โ† Back to Course

Weather-Based Irrigation Decisions

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.