#include <WiFi.h>
#include "ThingSpeak.h"
#include "DHTesp.h"
#include <ESP32Servo.h>
#include <Wire.h>
#include "RTClib.h"
// تعريف المتغيرات والثوابت
const int DHT_PIN = 15;
const int LDR_PIN = 34;
const int SERVO_PIN = 18;
const int RELAY_PIN_LIGHT = 16;
const int RELAY_PIN_FAN = 17;
const int BUTTON = 19;
const char* WIFI_NAME = "Wokwi-GUEST";
const char* WIFI_PASSWORD = "";
const int myChannelNumber = 2970046;
const char* myApiKey = "S9EZRJAWSFBE82J4";
const char* server = "api.thingspeak.com";
// بيانات الأسابيع
const float weeklyData[8][2] = {
{32, 35},
{29, 30},
{27, 29},
{25, 27},
{20, 22},
{18, 21},
{18, 21},
{18, 21}
};
WiFiClient client;
DHTesp dhtSensor;
Servo windowServo;
RTC_DS1307 rtc;
int ldr_value;
int currentweek = 1;
void setup() {
pinMode(DHT_PIN, INPUT);
pinMode(LDR_PIN, INPUT);
pinMode(SERVO_PIN, OUTPUT);
pinMode(RELAY_PIN_LIGHT, OUTPUT);
pinMode(RELAY_PIN_FAN, OUTPUT);
pinMode(BUTTON, INPUT_PULLUP);
Serial.begin(115200);
WiFi.begin(WIFI_NAME, WIFI_PASSWORD);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Wifi not connected");
}
Serial.println("Wifi connected!");
Serial.println("Local IP: " + String(WiFi.localIP()));
WiFi.mode(WIFI_STA);
ThingSpeak.begin(client);
dhtSensor.setup(DHT_PIN, DHTesp::DHT22);
windowServo.attach(SERVO_PIN);
if (!rtc.begin()) {
Serial.println("Couldn't find RTC");
while (1);
}
if (!rtc.isrunning()) {
rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
}
}
void loop() {
// تغيير الأسبوع عند الضغط
if (digitalRead(BUTTON) == HIGH) {
delay(100);
currentweek++;
if (currentweek > 8) {
currentweek = 1;
}
}
// قراءات المستشعرات
float T = dhtSensor.getTempAndHumidity().temperature;
float H = dhtSensor.getTempAndHumidity().humidity;
ldr_value = analogRead(LDR_PIN);
float T1 = weeklyData[currentweek - 1][0];
float T2 = weeklyData[currentweek - 1][1];
// وقت الآن
DateTime now = rtc.now();
int hour = now.hour();
// تحكم في النوافذ والمروحة حسب درجة الحرارة
if (!(T1 < T && T < T2)) {
windowServo.write(0); // فتح النافذة
delay(500);
digitalWrite(RELAY_PIN_FAN, HIGH); // تشغيل المروحة
} else {
digitalWrite(RELAY_PIN_FAN, LOW);
windowServo.write(90); // إغلاق النافذة
}
// تحكم في الإضاءة حسب الوقت
int lightStatus = 0;
if (hour >= 0 && hour < 23) {
digitalWrite(RELAY_PIN_LIGHT, HIGH); // تشغيل الضوء
lightStatus = 1;
Serial.println("LED: شغالة");
} else {
digitalWrite(RELAY_PIN_LIGHT, LOW); // إطفاء الضوء
lightStatus = 0;
Serial.println("LED: طافية");
}
// إرسال البيانات إلى ThingSpeak
ThingSpeak.setField(1, T);
ThingSpeak.setField(2, H);
ThingSpeak.setField(3, lightStatus); // حالة الضوء فقط
int x = ThingSpeak.writeFields(myChannelNumber, myApiKey);
// طباعة القيم
Serial.println("Temp: " + String(T) + "°C");
Serial.println("Humidity: " + String(H) + "%");
Serial.println("Current week: " + String(currentweek));
Serial.print("Current Time: ");
Serial.print(now.hour());
Serial.print(":");
Serial.print(now.minute());
Serial.print(":");
Serial.println(now.second());
if (x == 200) {
Serial.println("Data pushed successfully");
} else {
Serial.println("Push error: " + String(x));
}
delay(15000); // الانتظار 15 ثانية قبل إرسال البيانات مرة أخرى
}