#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <WiFi.h>
#include <ESP_Mail_Client.h>
// OLED Display Settings
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
#define OLED_SDA 21
#define OLED_SCL 22
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);
// Motion Sensor and Buzzer
#define PIR_PIN 33
#define BUZZER_PIN 25
// Wi-Fi Configuration
const char* ssid = "Wokwi-GUEST";
const char* password = "";
// Email Configuration
#define SMTP_HOST "smtp.gmail.com"
#define SMTP_PORT 465
const char* emailSender = "[email protected]";
const char* emailSenderPassword = "lqta lmwu pitl ognq";
const char* emailRecipient = "[email protected]";
// ESP Mail Client Instances
SMTP_Message message;
ESP_Mail_Session session;
SMTPSession smtp;
// Detection Counter
int detectionCount = 0;
void setup() {
Serial.begin(115200);
// Initialize OLED Display
Wire.begin(OLED_SDA, OLED_SCL);
if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
Serial.println(F("Failed to initialize the OLED display"));
for (;;);
}
display.clearDisplay();
displayMessage("Connecting to Wi-Fi...");
// Connect to Wi-Fi
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
displayMessage("Connecting...");
}
displayMessage("Wi-Fi Connected!");
// Initialize Motion Sensor and Buzzer
pinMode(PIR_PIN, INPUT);
pinMode(BUZZER_PIN, OUTPUT);
displayMessage("System Ready");
}
void loop() {
if (digitalRead(PIR_PIN) == HIGH) {
detectionCount++;
tone(BUZZER_PIN, 1000, 500); // Turn on buzzer
sendEmail();
displayDetection();
delay(1000); // Avoid multiple triggers
}
}
void sendEmail() {
Serial.println("Attempting to send email...");
// Configure email session
session.server.host_name = SMTP_HOST;
session.server.port = SMTP_PORT;
session.login.email = emailSender;
session.login.password = emailSenderPassword;
session.login.user_domain = "";
// Configure email message
message.sender.name = "ESP32 Alert";
message.sender.email = emailSender;
message.subject = "Motion Detected!";
message.addRecipient("User", emailRecipient);
message.text.content = "The ESP32 system has detected someone entering your room.";
// Send Email
if (!smtp.connect(&session)) {
Serial.println("Failed to connect to the SMTP server: " + smtp.errorReason());
return;
}
if (!MailClient.sendMail(&smtp, &message)) {
Serial.println("Error sending email: " + smtp.errorReason());
} else {
Serial.println("Email sent successfully!");
}
smtp.closeSession(); // Close the session after sending the email
}
void displayMessage(const String& message) {
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(SSD1306_WHITE);
display.setCursor(0, 0);
display.println(message);
display.display();
}
void displayDetection() {
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(SSD1306_WHITE);
display.setCursor(0, 0);
display.println("Email: ");
display.println(emailRecipient);
display.println();
display.println("Detections:");
display.setTextSize(2);
display.println(detectionCount);
display.display();
}