#define WOKWI_SIMULATION 1
#include <WiFi.h>
#include <PubSubClient.h>
#include <DHT.h>
#include <ESP32Servo.h>
// ================== CONFIG =================
const char* ssid = "Wokwi-GUEST";
const char* password = "";
const char* mqtt_server = "thingsboard.cloud";
const int mqtt_port = 1883;
const char* access_token = "UravcxW23y9PTJrkUxRS";
// ================== PINS ==================
#define DHTTYPE DHT22
#define RELAY_ON HIGH
#define RELAY_OFF LOW
#define PIN_KITCHEN_L 13
#define PIN_BATH_L 12
#define PIN_HALL_L 14
#define PIN_ROOM_L 27
#define PIN_FAN_H 26
#define PIN_FAN_R 25
#define PIN_PUMP 33
#define PIN_BUZZER 32
#define PIN_GAS_VALVE 22
#define WIN_KITCHEN_PIN 21
#define WIN_HALL_PIN 16
#define WIN_ROOM_PIN 17
#define WIN_BATH_PIN 15
#define PIR_HALL_PIN 36
#define PIR_ROOM_PIN 39
#define PIR_KITCHEN_PIN 18
#define PIR_BATH_PIN 19
#define DHT_HALL_PIN 4
#define DHT_ROOM_PIN 5
#define GAS_KITCHEN_PIN 34
#define GAS_BATH_PIN 35
#define FLAME_PIN 23
// ================== OBJECTS ==================
DHT dhtHall(DHT_HALL_PIN, DHTTYPE);
DHT dhtRoom(DHT_ROOM_PIN, DHTTYPE);
WiFiClient espClient;
PubSubClient client(espClient);
Servo servoGas, servoWinKitchen, servoWinHall, servoWinRoom, servoWinBath;
// ================== STATE ==================
float tHall=0,hHall=0,tRoom=0,hRoom=0;
unsigned long lastMotionHall=0,lastMotionRoom=0,lastMotionKitchen=0,lastMotionBath=0;
const long motionTimeout=5000;
bool autoModeHall=false,autoModeRoom=false,autoModeKitchen=false,autoModeBath=false;
bool isEmergency=false,isBuzzerMuted=false;
int kitchenLight=0,bathLight=0,hallLight=0,roomLight=0;
int hallFan=0,roomFan=0,pump=0,buzzer=0;
int gasValve=0,winKitchen=0,winBath=0,winHall=0,winRoom=0;
unsigned long lastSend=0;
// ================== SETUP ==================
void setup() {
Serial.begin(115200);
pinMode(PIN_KITCHEN_L, OUTPUT);
pinMode(PIN_BATH_L, OUTPUT);
pinMode(PIN_HALL_L, OUTPUT);
pinMode(PIN_ROOM_L, OUTPUT);
pinMode(PIN_FAN_H, OUTPUT);
pinMode(PIN_FAN_R, OUTPUT);
pinMode(PIN_PUMP, OUTPUT);
pinMode(PIN_BUZZER, OUTPUT);
pinMode(PIR_HALL_PIN, INPUT);
pinMode(PIR_ROOM_PIN, INPUT);
pinMode(PIR_KITCHEN_PIN, INPUT);
pinMode(PIR_BATH_PIN, INPUT);
pinMode(GAS_KITCHEN_PIN, INPUT);
pinMode(GAS_BATH_PIN, INPUT);
pinMode(FLAME_PIN, INPUT);
servoGas.attach(PIN_GAS_VALVE);
servoWinKitchen.attach(WIN_KITCHEN_PIN);
servoWinHall.attach(WIN_HALL_PIN);
servoWinRoom.attach(WIN_ROOM_PIN);
servoWinBath.attach(WIN_BATH_PIN);
dhtHall.begin();
dhtRoom.begin();
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
}
client.setServer(mqtt_server, mqtt_port);
}
// ================== LOOP ==================
void loop() {
if (!client.connected()) reconnect();
client.loop();
checkMotion();
checkSafety();
if (millis() - lastSend > 5000) {
readSensors();
sendAllData();
lastSend = millis();
}
}
// ================== MQTT ==================
void reconnect() {
while (!client.connected()) {
if (client.connect("ESP32", access_token, NULL)) {
Serial.println("Connected ✅");
} else {
delay(2000);
}
}
}
// ================== SENSORS ==================
void readSensors() {
tHall = dhtHall.readTemperature();
hHall = dhtHall.readHumidity();
tRoom = dhtRoom.readTemperature();
hRoom = dhtRoom.readHumidity();
}
// ================== MOTION ==================
void checkMotion() {
unsigned long now = millis();
if (autoModeHall && digitalRead(PIR_HALL_PIN)) {
lastMotionHall = now;
digitalWrite(PIN_HALL_L, RELAY_ON);
hallLight = 1;
} else if (now - lastMotionHall > motionTimeout) {
digitalWrite(PIN_HALL_L, RELAY_OFF);
hallLight = 0;
}
if (autoModeRoom && digitalRead(PIR_ROOM_PIN)) {
lastMotionRoom = now;
digitalWrite(PIN_ROOM_L, RELAY_ON);
roomLight = 1;
} else if (now - lastMotionRoom > motionTimeout) {
digitalWrite(PIN_ROOM_L, RELAY_OFF);
roomLight = 0;
}
if (autoModeKitchen && digitalRead(PIR_KITCHEN_PIN)) {
lastMotionKitchen = now;
digitalWrite(PIN_KITCHEN_L, RELAY_ON);
kitchenLight = 1;
} else if (now - lastMotionKitchen > motionTimeout) {
digitalWrite(PIN_KITCHEN_L, RELAY_OFF);
kitchenLight = 0;
}
if (autoModeBath && digitalRead(PIR_BATH_PIN)) {
lastMotionBath = now;
digitalWrite(PIN_BATH_L, RELAY_ON);
bathLight = 1;
} else if (now - lastMotionBath > motionTimeout) {
digitalWrite(PIN_BATH_L, RELAY_OFF);
bathLight = 0;
}
}
// ================== SAFETY ==================
void checkSafety() {
int gasKitchen = analogRead(GAS_KITCHEN_PIN);
int gasBath = analogRead(GAS_BATH_PIN);
bool fire = digitalRead(FLAME_PIN);
if (gasKitchen > 4000 || gasBath > 4000 || fire) {
isEmergency = true;
servoGas.write(90);
servoWinKitchen.write(175);
servoWinHall.write(175);
servoWinRoom.write(175);
servoWinBath.write(175);
if (!isBuzzerMuted) digitalWrite(PIN_BUZZER, HIGH);
} else {
isEmergency = false;
digitalWrite(PIN_BUZZER, LOW);
}
}
// ================== TELEMETRY ==================
void sendAllData() {
String msg = "{";
msg += "\"tHall\":" + String(tHall) + ",";
msg += "\"hHall\":" + String(hHall) + ",";
msg += "\"tRoom\":" + String(tRoom) + ",";
msg += "\"hRoom\":" + String(hRoom) + ",";
msg += "\"kitchenLight\":" + String(kitchenLight) + ",";
msg += "\"bathLight\":" + String(bathLight) + ",";
msg += "\"hallLight\":" + String(hallLight) + ",";
msg += "\"roomLight\":" + String(roomLight) + ",";
msg += "\"hallFan\":" + String(hallFan) + ",";
msg += "\"roomFan\":" + String(roomFan) + ",";
msg += "\"pump\":" + String(pump) + ",";
msg += "\"buzzer\":" + String(buzzer) + ",";
msg += "\"gasValve\":" + String(gasValve) + ",";
msg += "\"winKitchen\":" + String(winKitchen) + ",";
msg += "\"winBath\":" + String(winBath) + ",";
msg += "\"winHall\":" + String(winHall) + ",";
msg += "\"winRoom\":" + String(winRoom) + ",";
msg += "\"autoModeHall\":" + String(autoModeHall) + ",";
msg += "\"autoModeRoom\":" + String(autoModeRoom) + ",";
msg += "\"autoModeKitchen\":" + String(autoModeKitchen) + ",";
msg += "\"autoModeBath\":" + String(autoModeBath) + ",";
msg += "\"emergency\":" + String(isEmergency);
msg += "}";
client.publish("v1/devices/me/telemetry", msg.c_str());
Serial.println("📊 ALL 27 DATA SENT ✅");
}حمام
مطبخ
غرفه
صاله
gas