⚙ Main Components:
1. Arduino Uno – The main microcontroller that controls the whole system.
2. Ultrasonic Sensor (HC-SR04) – Used to measure distance and detect motion.
3. PIR Sensor – Detects human movement (motion detection).
4. Gas Sensor (MQ-2 or MQ-135) – Detects gas leaks or smoke.
5. LDR (Light Dependent Resistor) – Measures light intensity to automatically control lighting.
6. Buzzer – Sounds an alarm when motion or gas is detected.
7. LED or Bulb – Indicates system status or works as an automatic light.
8. Resistors and Jumper Wires – For proper circuit connection and voltage control.
9. Wi-Fi Module (ESP8266) – Connects the system to the internet to send alerts or control it remotely.
-----------------------------------------------------------------------------------------------------------------------------
// ==========================================================
// تعريف المتغيرات والإعدادات العامة (Configuration) code
// ==========================================================
String ssid = "Simulator Wifi"; // اسم شبكة الواي فاي للاتصال بها
String password = ""; // كلمة المرور (فارغة لشبكة مفتوحة)
String host = "api.thingspeak.com"; // عنوان خادم ThingSpeak
const int httpPort = 80; // منفذ HTTP القياسي
String url = "/update?api_key=OMPQYIH9IVI2HZBS&field";
// الرابط الأساسي لتحديث ThingSpeak. سيُضاف رقم الحقل وقيمة المتغير في دالة anydata()
int sensorState = 0; // حالة حساس الحركة
float thresholda = 0.25; // عتبة سفلية (مثلاً لمستوى الماء)
float thresholdb = 0.75; // عتبة علوية (مثلاً لمستوى الماء)
// ==========================================================
// دالة تهيئة الاتصال بـ ESP8266 (ESP8266 Setup Function)
// ==========================================================
int setupESP8266(void) {
// بدء الاتصال التسلسلي بـ ESP8266
Serial.begin(115200); // الاتصال التسلسلي مع الحاسوب (للتصحيح والمراقبة)
Serial.println("AT");
delay(10); // انتظار رد ESP
if (!Serial.find("OK")) return 1;
// الاتصال بشبكة الواي فاي
Serial.println("AT+CWJAP=\"" + ssid + "\",\"" + password + "\"");
delay(10);
if (!Serial.find("OK")) return 2;
// فتح اتصال TCP بخادم ThingSpeak
Serial.println("AT+CIPSTART=\"TCP\",\"" + host + "\"," + httpPort);
delay(50);
if (!Serial.find("OK")) return 3;
return 0; // نجاح الاتصال
}
// ==========================================================
// دالة الإعداد (SETUP)
// ==========================================================
void setup() {
// تحديد وظيفة الأرجل (Pins)
pinMode(2, INPUT); // حساس PIR (حركة)
pinMode(A0, INPUT); // حساس مستوى الماء
pinMode(A1, INPUT); // حساس الغاز
pinMode(3, OUTPUT); // LED لحساس PIR
pinMode(4, OUTPUT); // LED لحساس الماء
pinMode(5, OUTPUT); // LED لحساس الغاز
pinMode(7, OUTPUT); // LED لحساس المسافة
Serial.begin(9600); // بدء الاتصال التسلسلي للرصد على شاشة الكمبيوتر
setupESP8266(); // تهيئة الاتصال بالإنترنت
}
// ==========================================================
// دالة التكرار الرئيسية (LOOP)
// ==========================================================
void loop() {
// تشغيل LED عند بدء الدورة (يُستخدم كـ مؤشر)
digitalWrite(4, HIGH);
// استدعاء دوال قراءة الحساسات وإرسال البيانات
PIR();
WATER();
GAS();
DISTANCE();
//delay(1000); // تم إيقاف التأخير هنا لأن دالة anydata() تحتوي على تأخير 10 ثوانٍ
}
// ==========================================================
// 1. دالة حساس الحركة (PIR Sensor) - الحقل 1
// ==========================================================
void PIR(){
sensorState = digitalRead(2); // قراءة حالة الرجل D2
if (sensorState == HIGH) {
digitalWrite(3, HIGH); // تشغيل LED (حركة موجودة)
Serial.println("Motion Detected!");
anydata(1, 1); // إرسال القيمة 1 للحقل 1 (حركة)
} else {
digitalWrite(3, LOW); // إيقاف LED (لا توجد حركة)
anydata(0, 1); // إرسال القيمة 0 للحقل 1 (لا حركة)
}
delay(10);
}
// ==========================================================
// 2. دالة حساس مستوى الماء (Water Level Sensor) - الحقل 2
// ==========================================================
void WATER(){
// إعادة تعريف متغيرات العتبة داخل الدالة (يمكن تعريفها كـ global لتجنب تكرارها)
float thresholda = 0.25;
float thresholdb = 0.75;
float val = analogRead(A0); // قراءة القيمة التناظرية
float per = val / 1023.0; // تحويل القيمة إلى نسبة (من 0 إلى 1)
Serial.print("Water level is ");
Serial.println(per * 100);
if(per < thresholda){
// مستوى الماء منخفض جداً
digitalWrite(4, LOW); // إيقاف LED تحذيري
anydata(1, 2); // إرسال 1 للحقل 2 (تنبيه)
// حلقة انتظار حتى يرتفع مستوى الماء فوق العتبة العلوية
while(per < thresholdb){
val = analogRead(A0);
per = val / 1023.0;
digitalWrite(4, LOW);
anydata(1, 2);
Serial.print("Water level is ");
Serial.println(per * 100);
}
}
// مستوى الماء طبيعي
digitalWrite(4, HIGH); // تشغيل LED (حالة طبيعية)
anydata(0, 2); // إرسال 0 للحقل 2 (لا يوجد تنبيه)
}
// ==========================================================
// 3. دالة حساس الغاز (GAS Sensor) - الحقل 3
// ==========================================================
void GAS(){
int gas = analogRead(A1); // قراءة قيمة الغاز التناظرية
Serial.print("value of gas sensor is: ");
Serial.println(gas, DEC);
anydata(gas, 3); // إرسال قيمة الغاز للحقل 3
if (gas > 800){
digitalWrite(5, HIGH); // تشغيل LED (غاز مرتفع)
Serial.println("UNSAFE GAS DETECTED");
} else {
digitalWrite(5, LOW); // إيقاف LED
}
}
// ==========================================================
// 4. دالة حساس المسافة (Distance Sensor) - الحقل 4
// ==========================================================
void DISTANCE(){
// جزء قياس المسافة
pinMode(6, OUTPUT);
digitalWrite(6, LOW);
delayMicroseconds(2);
digitalWrite(6, HIGH);
delayMicroseconds(5);
digitalWrite(6, LOW);
pinMode(6, INPUT); // تبديل الرجل 6 لاستقبال صدى النبضة
long duration, cm;
duration = pulseIn(6, HIGH); // قياس زمن الرحلة
cm = microsecondsToCentimeters(duration); // تحويل الزمن إلى مسافة
Serial.print("Distance: ");
Serial.print(cm);
Serial.println("cm");
anydata(cm, 4); // إرسال المسافة للحقل 4
if(cm < 100){
digitalWrite(7, HIGH); // تشغيل LED (وجود شخص قريب)
Serial.println("Human Presence Detected");
// يوجد استدعاء مكرر للدالة anydata(cm, 2); هنا، قد يكون خطأ ويجب إزالته
} else {
digitalWrite(7, LOW);
}
}
// ==========================================================
// دالة تحويل الزمن إلى مسافة (Ultrasonic Utility)
// ==========================================================
long microsecondsToCentimeters(long microseconds) {
// السرعة = 343 متر/ثانية = 0.0343 سم/ميكروثانية
// المسافة = (الزمن * السرعة) / 2 (لأن الزمن هو ذهاب وعودة)
// 1.955 هو معامل تصحيح قد يكون خاصًا باللوحة أو المحاكي
return microseconds / 29 / 1.955;
}
// ==========================================================
// دالة إرسال البيانات إلى ThingSpeak (Data Sender)
// ==========================================================
void anydata(int value, int field ) {
// بناء حزمة HTTP GET
String httpPacket = "GET " + url + String(field) + "=" + String(value) + " HTTP/1.1\r\nHost: " + host + "\r\n\r\n";
int length = httpPacket.length();
// إرسال طول الحزمة عبر AT Command
Serial.print("AT+CIPSEND=");
Serial.println(length);
delay(10);
if (!Serial.find(">")) return;
// إرسال طلب HTTP الفعلي
Serial.print(httpPacket);
delay(10);
if (!Serial.find("SEND OK\r\n")) return;
// تأخير طويل للسماح لـ ThingSpeak بمعالجة الطلب (10 ثواني)
delay(10000);
}
______________________________________________________________
💡 Project Idea:
This project is designed to monitor and secure a home or area automatically using multiple sensors.
The Arduino reads data from all the sensors and performs actions such as:
Activating the buzzer alarm if motion or gas is detected.
Turning the light ON or OFF depending on the light level (using the LDR).
Sending notifications over Wi-Fi (to a smartphone or IoT platform) in case of danger or detection.
🏠 Applications:
Smart home security systems
Offices and workplaces for motion monitoring
Factories or warehouses to detect gas leaks or fire early
Automatic lighting systems that respond to environmental light
