#define BLYNK_TEMPLATE_ID "TMPL2w_NAePC9"
#define BLYNK_TEMPLATE_NAME "SmartHomeSys"
#define BLYNK_AUTH_TOKEN "NrvR751CFMNBCadIrdfXtZN5VthQ0-nO"
#include <WiFi.h>
#include <BlynkSimpleEsp32.h>
#include <DHT.h>
// Auth Token Blynk
char auth[] = "NrvR751CFMNBCadIrdfXtZN5VthQ0-nO"; // مكرر
// // WiFi credentials
char ssid[] = "Wokwi-GUEST";
char pass[] = "";
// Pins
#define PIR_PIN 23
#define MQ2_PIN 34 // analog pin
#define DHT_PIN 32
#define LDR_PIN 35
// #define LED_PIN 22
#define BUZZER_PIN 21
#define FAN_PIN 19
#define DHTTYPE DHT22
DHT dht(DHT_PIN, DHTTYPE);
float temp = 0;
float humidity = 0;
int lightLevel = 0;
// --------------------------
// Blynk Virtual Pins
// --------------------------
#define VP_PIR V0
#define VP_Gas V1
#define VP_Light V2
#define VP_Led V3
#define VP_Buzzer V4
#define VP_fan V5
#define VP_Temp V6
#define VP_Hum V7
int Rpin = 5;
int Gpin = 4;
int Bpin = 2;
#define BLYNK_Buzzer V4
// لما تضغطي الزر في التطبيق
BLYNK_WRITE(BLYNK_Buzzer)
{
int buttonState = param.asInt(); // قراءة قيمة الزر (0 أو 1)
if(buttonState == 1){
tone(BUZZER_PIN,5000);
digitalWrite(BUZZER_PIN, HIGH);
}
else{
// noTone(BUZZER_PIN);
digitalWrite(BUZZER_PIN, LOW);
}
}
void setup() {
Blynk.begin(BLYNK_AUTH_TOKEN, ssid, pass);
// DHT11
dht.begin();
// put your setup code here, to run once:
Serial.begin(115200);
Serial.println("Hello, ESP32!");
// Sensor Pins
pinMode(PIR_PIN, INPUT);
pinMode(MQ2_PIN, INPUT);
pinMode(LDR_PIN, INPUT);
// Output Pins
// pinMode(LED_PIN, OUTPUT);
pinMode(BUZZER_PIN, OUTPUT);
pinMode(FAN_PIN, OUTPUT);
pinMode(Rpin, OUTPUT);
pinMode(Gpin, OUTPUT);
pinMode(Bpin, OUTPUT);
}
void loop() {
Blynk.run();
int PIRVlue = digitalRead(PIR_PIN);
Serial.print("PIRVlue = ");
Serial.println(PIRVlue);
if(PIRVlue == 1)
{
// digitalWrite(LED_PIN, HIGH);
tone(BUZZER_PIN,5000);
// Blynk.notify("Motion detected!");
RGBColor(255,255,0);
Blynk.virtualWrite(V0, "Found dection");
}
else{
// digitalWrite(LED_PIN, LOW);
noTone(BUZZER_PIN);
RGBColor(0,0,0);
Blynk.virtualWrite(V0, "No dection");
}
int Gasvalue = analogRead(MQ2_PIN);
Serial.print("Gasvalue = ");
Serial.println(Gasvalue);
Blynk.virtualWrite(V1, Gasvalue);
if(Gasvalue>4000){
tone(BUZZER_PIN,5000);
RGBColor(255,0,0);
delay(1000); // this speeds up the simulation
}
else {
noTone(BUZZER_PIN);
}
// قراءة الحرارة والرطوبة
float temp = dht.readTemperature(); // °C
float humid = dht.readHumidity(); // %
Serial.print("Temperature = ");
Serial.println(temp);
Serial.print("Humidity = ");
Serial.println(humid);
if(temp > 30) {
RGBColor(0,255,0);
digitalWrite(FAN_PIN, HIGH); // تشغيل مروحة
Blynk.logEvent("HighTemp");
} else {
digitalWrite(FAN_PIN, LOW);
}
// // قراءة الإضاءة
// lightLevel = analogRead(LDR_PIN);
// if(lightLevel < 400) { // إذا الإضاءة منخفضة
// digitalWrite(LED_PIN, HIGH);
// } else {
// digitalWrite(LED_PIN, LOW);
// }
Blynk.virtualWrite(V6, temp);
Blynk.virtualWrite(VP_Gas, Gasvalue);
// إرسال القيم لـ Blynk
// Blynk.virtualWrite(VP_Temp, temp);
// Blynk.virtualWrite(VP_Hum, humid);
delay(1000);
}
void RGBColor(int R , int G , int B)
{
analogWrite(Rpin,R);
analogWrite(Gpin,G);
analogWrite(Bpin,B);
}