#include <WiFi.h>
#include "DHT.h"
// ═══════ إعدادات WiFi (مش مستخدمة فعليًا في السيموليشن لكن سيبناها) ═══════
#define WIFI_SSID "MOHAMED9727"
#define WIFI_PASSWORD "MOHAMED22"
// ═══════ الأطراف (Pins) ═══════
#define DHTPIN 4
#define DHTTYPE DHT22
#define LDR_PIN 34
#define SOIL_PIN 35
#define PUMP_PIN 26
// ═══════ ثوابت التوقيت ═══════
#define LOOP_INTERVAL 2000
// ═══════ الأجسام البرمجية ═══════
DHT dht(DHTPIN, DHTTYPE);
unsigned long lastLoop = 0;
// ═══════ متغيرات وهمية بدل Firebase ═══════
bool autoIrrigation = true; // غيّريها للتجربة
bool pumpControl = false;
int moistureThreshold = 30;
bool lastAutoState = true;
// ════════════════════════════════════════
void setup() {
Serial.begin(115200);
delay(500);
dht.begin();
pinMode(PUMP_PIN, OUTPUT);
digitalWrite(PUMP_PIN, LOW);
Serial.println("\n--- Smart Irrigation System (Wokwi Mode) ---");
// WiFi فقط شكل تجميلي
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
Serial.print("Connecting WiFi");
int t = 0;
while (WiFi.status() != WL_CONNECTED && t < 10) {
delay(500);
Serial.print(".");
t++;
}
Serial.println("\nWiFi simulated connected!");
}
// ════════════════════════════════════════
void loop() {
if (millis() - lastLoop < LOOP_INTERVAL) return;
lastLoop = millis();
// ═══ 1. قراءة الحساسات ═══
float temp = dht.readTemperature();
float hum = dht.readHumidity();
if (isnan(temp) || isnan(hum)) {
Serial.println("[ERROR] DHT sensor failed");
return;
}
int ldrRaw = analogRead(LDR_PIN);
int lightPct = constrain(map(ldrRaw, 0, 4095, 0, 100), 0, 100);
int soilRaw = analogRead(SOIL_PIN);
int soilPct = constrain(map(soilRaw, 3931, 1575, 0, 100), 0, 100);
Serial.printf("Temp: %.1f | Hum: %.1f | Soil: %d%% | Light: %d%%\n",
temp, hum, soilPct, lightPct);
// ═══ 2. منطق التحكم بدل Firebase ═══
if (lastAutoState == true && autoIrrigation == false) {
Serial.println("[INFO] AUTO → MANUAL reset pump OFF");
pumpControl = false;
}
lastAutoState = autoIrrigation;
if (autoIrrigation) {
pumpControl = (soilPct < moistureThreshold);
Serial.printf("[AUTO] Threshold=%d | Pump=%s\n",
moistureThreshold,
pumpControl ? "ON" : "OFF");
} else {
Serial.printf("[MANUAL] Pump=%s\n",
pumpControl ? "ON" : "OFF");
}
// ═══ 3. تشغيل المضخة ═══
digitalWrite(PUMP_PIN, pumpControl ? HIGH : LOW);
Serial.println("────────────────────────");
}