#include <WiFi.h>
// Define pins for door sensor and buzzer based on the provided motion sensor code
#define DOOR_SENSOR_PIN 19 // Using the motion sensor pin as the door sensor pin
#define BUZZER_PIN 2 // Using the same pin for the buzzer
// Replace with your phone's actual MAC address
//const char* phoneMAC = "42:13:37:55:AA:01";
const char* phoneMAC = "XX:XX:XX:XX:XX:XX"; //for checking for some random phone address that is not there
void setup() {
Serial.begin(9600);
Serial.print("Connecting to WiFi");
// Connect to Wi-Fi network
WiFi.begin("Wokwi-GUEST");
while (WiFi.status() != WL_CONNECTED) {
delay(100);
Serial.print(".");
}
Serial.println(" Connected!");
// Setup door sensor and buzzer pins
pinMode(DOOR_SENSOR_PIN, INPUT_PULLUP);
pinMode(BUZZER_PIN, OUTPUT);
digitalWrite(BUZZER_PIN, LOW);
}
bool isPhoneConnected() {
int n = WiFi.scanNetworks();
for (int i = 0; i < n; i++) {
//Serial.println(WiFi.BSSIDstr(i));
if (WiFi.BSSIDstr(i) == phoneMAC) {
Serial.println("phone is connected");
return true;
}
}
return false;
}
void loop() {
bool doorOpen=false;
if(digitalRead(DOOR_SENSOR_PIN)==LOW){
doorOpen = false;
}
else{
doorOpen=true;
Serial.println("Door is opened");
}
bool phoneConnected = isPhoneConnected();
if (doorOpen && !phoneConnected) {
Serial.println("Door opened and phone not connected! Alarm activated.");
digitalWrite(BUZZER_PIN, HIGH); // Activate buzzer
} else {
digitalWrite(BUZZER_PIN, LOW); // Deactivate buzzer
}
delay(1000); // Adjust delay as needed
}