#define BLYNK_TEMPLATE_ID "TMPL3a4HTVMPh"
#define BLYNK_TEMPLATE_NAME "wireless monitering system"
#define BLYNK_AUTH_TOKEN "7Ppe2R3me2BMEwg7tYS5qOeE1OJdfZmG"
#include <WiFi.h> // ESP32 WiFi library
#include <BlynkSimpleEsp32.h> // Blynk library for ESP32
#include <Wire.h> // Library for I2C communication
#include <RTClib.h> // Library for RTC (Real-Time Clock)
RTC_DS1307 rtc; // RTC object
// Blynk credentials
char auth[] = BLYNK_AUTH_TOKEN;
char ssid[] = "Wokwi-GUEST";
char pass[] = "";
// Pin Definitions for ESP32
const int pirPin = 15; // PIR sensor pin (ESP32 GPIO15)
const int magneticSensorPin = 27; // Magnetic sensor pin (ESP32 GPIO27)
const int ledPin = 2; // LED pin (ESP32 GPIO2)
const int routerPin = 4; // Router control pin (ESP32 GPIO4)
// State variables
int pirState = LOW; // PIR sensor state
int magneticSensorState = LOW; // Magnetic sensor state
bool routerState = false; // Router state
bool ledState = false; // LED state
// Time settings
bool morningMessageSent = false; // Flag for morning message
bool eveningMessageSent = false; // Flag for evening message
// Weekday array
char daysOfTheWeek[7][12] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
void setup() {
// Start serial communication
Serial.begin(115200);
// Set up I2C communication for RTC: GPIO21 (SCL), GPIO22 (SDA)
Wire.begin(21, 22);
// Initialize RTC
if (!rtc.begin()) {
Serial.println("Couldn't find RTC");
while (1); // Halt if RTC is not found
}
// Uncomment and set the date and time if RTC needs to be set for the first time
// rtc.adjust(DateTime(2024, 9, 7, 23, 56, 00));
// Initialize Blynk
Blynk.begin(auth, ssid, pass);
// Pin modes
pinMode(pirPin, INPUT); // Set PIR sensor pin as input
pinMode(magneticSensorPin, INPUT_PULLUP); // Set magnetic sensor pin as input with pull-up resistor
pinMode(ledPin, OUTPUT); // Set LED pin as output
pinMode(routerPin, OUTPUT); // Set router pin as output
// Initialize states
digitalWrite(ledPin, LOW); // Turn off LED initially
digitalWrite(routerPin, LOW); // Turn off router initially
}
void loop() {
Blynk.run(); // Run Blynk
// Get the current time and date from the RTC
DateTime now = rtc.now();
// Reset Blynk virtual pins
Blynk.virtualWrite(V1, false);
Blynk.virtualWrite(V2, false);
Blynk.virtualWrite(V3, false);
Blynk.virtualWrite(V4, false);
// Print current time and date
Serial.print("Current Date & Time: ");
Serial.print(daysOfTheWeek[now.dayOfTheWeek()]);
Serial.print(", ");
Serial.print(now.day(), DEC);
Serial.print('/');
Serial.print(now.month(), DEC);
Serial.print('/');
Serial.print(now.year(), DEC);
Serial.print(" - ");
Serial.print(now.hour(), DEC);
Serial.print(':');
Serial.print(now.minute(), DEC);
Serial.print(':');
Serial.println(now.second(), DEC);
// Control the router at the specified times
controlRouter(now);
// PIR Sensor: Motion Detection
pirState = digitalRead(pirPin); // Read PIR sensor state
if (pirState == HIGH) {
Serial.println("Motion detected!");
digitalWrite(ledPin, HIGH); // Turn on LED
ledState = true;
} else if (ledState) {
digitalWrite(ledPin, LOW); // Turn off LED after motion stops
ledState = false;
}
// Magnetic sensor: Check if the door is open or closed
magneticSensorState = digitalRead(magneticSensorPin); // Read magnetic sensor state
if (magneticSensorState == LOW) {
Serial.println("Door is closed.");
} else {
Serial.println("Door is open.");
}
// Send a message to the Blynk app at specific times
sendMessage(now);
delay(1000); // Wait for 1 second before next loop
}
// Control the router based on the time and day
void controlRouter(DateTime now) {
// Turn router on at 9:58
if (now.hour() == 9 && now.minute() == 58 && now.second() == 0 && !routerState) {
Serial.println("ROUTER ON.....");
routerState = true;
digitalWrite(routerPin, LOW); // Turn on router
}
// Turn router off at 9:59
if (now.hour() == 9 && now.minute() == 59 && now.second() == 0 && routerState) {
Serial.println("ROUTER OFF....");
routerState = false;
digitalWrite(routerPin, HIGH); // Turn off router
}
// Turn router on again at 10:02
if (now.hour() == 10 && now.minute() == 2 && now.second() == 0 && !routerState) {
Serial.println("ROUTER ON.....");
routerState = true;
digitalWrite(routerPin, LOW); // Turn on router
}
// Turn router off again at 10:03
if (now.hour() == 10 && now.minute() == 3 && now.second() == 0 && routerState) {
Serial.println("ROUTER OFF....");
routerState = false;
digitalWrite(routerPin, HIGH); // Turn off router
}
}
// Function to send messages to the Blynk app
void sendMessage(DateTime now) {
// Example: Send a message at 9:40 AM and 4:40 PM
if (now.hour() == 19 && now.minute() == 58 && magneticSensorState) {
Serial.println("Time out, door not open!"); // Morning message
Blynk.virtualWrite(V1, true);
morningMessageSent = true;
delay(5000);
Blynk.virtualWrite(V1, false);
}
if (now.hour() == 19 && now.minute() == 59 && !magneticSensorState) {
Serial.println("Time out, door open!"); // Morning message
Blynk.virtualWrite(V2, true);
morningMessageSent = true;
delay(5000);
Blynk.virtualWrite(V2, false);
}
if (now.hour() == 16 && now.minute() == 40 && magneticSensorState) {
Serial.println("Time out, door closed!"); // Evening message
Blynk.virtualWrite(V4, true);
eveningMessageSent = true;
delay(5000);
Blynk.virtualWrite(V4, false);
}
if (now.hour() == 16 && now.minute() == 40 && !magneticSensorState) {
Serial.println("Time out, door not closed!"); // Evening message
Blynk.virtualWrite(V3, true);
eveningMessageSent = true;
delay(5000);
Blynk.virtualWrite(V3, false);
}
}