OC
OceanRemote
Low-code IoT platform
← Back to Course

Safety Features and Fail-Safes

🛡️ Safety Features and Fail-Safes

📋 Critical Safety Features:

  • Maximum Run Time: Prevent pump from running indefinitely
  • Rain Sensor Override: Skip watering when raining
  • Manual Override Switch: Physical button to stop/start
  • Watchdog Timer: Reset ESP32 if code hangs
  • Dry Run Protection: Stop if pressure/flow is zero

📖 Safe Code with Fail-Safes:

#include 

#define WATCHDOG_TIMEOUT 30  // 30 seconds
#define MAX_PUMP_RUN 600     // 10 minutes maximum
#define RAIN_PIN 33
#define MANUAL_STOP_PIN 0

bool rainDetected() {
    return digitalRead(RAIN_PIN) == LOW;
}

bool manualStopPressed() {
    return digitalRead(MANUAL_STOP_PIN) == LOW;
}

void safeWaterZone(int zonePin, int duration) {
    if (duration > MAX_PUMP_RUN) {
        Serial.println("Duration exceeds safety limit!");
        duration = MAX_PUMP_RUN;
    }
    
    if (rainDetected()) {
        Serial.println("Rain detected - skipping irrigation");
        return;
    }
    
    digitalWrite(PUMP_RELAY, LOW);
    digitalWrite(zonePin, LOW);
    
    for (int i = 0; i < duration; i++) {
        if (manualStopPressed()) {
            Serial.println("Manual stop pressed!");
            break;
        }
        esp_task_wdt_reset();  // Reset watchdog
        delay(1000);
        
        if (rainDetected()) {
            Serial.println("Rain started - stopping irrigation");
            break;
        }
    }
    
    digitalWrite(zonePin, HIGH);
    digitalWrite(PUMP_RELAY, HIGH);
}

void setup() {
    esp_task_wdt_init(WATCHDOG_TIMEOUT, true);
    esp_task_wdt_add(NULL);
    
    pinMode(RAIN_PIN, INPUT_PULLUP);
    pinMode(MANUAL_STOP_PIN, INPUT_PULLUP);
}
    
💡 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.