ESP32 Alcohol Detection and Notification System

Overview

This project demonstrates an Alcohol Detection and Notification System using an ESP32 microcontroller, and MQ3 gas sensor, an OLED display, and a buzzer. The system detects alcohol level in the environment and performs the following actions:

Why We Built This Project: Addressing the Drink-and-Drive Menace

Every year, thousands of lives are lost due to accidents caused by drunk driving. The tragic stories of families shattered and lives cut short have inspired us to take a proactive step towards creating a safer world. Alcohol consumptions impairs judgement and reaction time, making it one of the leading causes of fatal road accidents globally. In our quest to address this issue, we developed the Alcohol Detector with Email Alerts. This project aims to detect alcohol presence in real time and immediately alert stakeholder, such as family members, fleet managers, or safety authorities. By leveraging cutting edge technology like ESP32 microcontroller, the MQ3 alcohol sensor, and an OLED display, we not only monitor but also communicate alerts effectively using automated emails. This solution is envisioned as a small yet significant step toward reducing accidents and promoting responsible behavior. With such devices integrated into vehicles, workplaces, or public safety setups, we can contribute to a safer and more responsible society 

Features

Things used in this project:

1. ESP32 (38 PIN) Wi-Fi + Bluetooth NodeMCU-32 Development Board: 

2. Buzzer:

3. 0.96 Inch Blue OLED Display Module I2C-4pin:

4. MQ-3 Alcohol Sensor:

5. Custom PCB

Why JLCPCB: The World's Best PCB Manufacturer

when it comes to PCB manufacturing, JLCPCB is an undisputed leader in the industry. With unmatched precision, exceptional quality, and cutting-edge technology, JLCPCB ensures that every PCB meets the most demanding requirements of professionals and hobbyists alike. Whether your project is for industrial applications, aerospace, medical equipment, or consumer electronics, JLCPCB delivers excellence in every order.

Here's what makes JLCPCB the best PCB manufacturer in the world:

Unmatched Material Versatility

JLCPCB provides a wide range of base materials to suit any project requirements:

Layer Options for Every Need

From simple 1 layer PCBs to advanced 32 layer PCBs, JLCPCB can handle projects of any complexity. Their high-precision multiplayer boards cater to industries where performance and reliability are paramount

Flexible PCB Quantities

Thickness & Colors: Tailored to Your Design

Surface Finish Options for Superior Quality

High-Spec Options for Demanding Applications

JLCPCB ensures high-quality standards with these advanced specification:

Perfect for Any Product Type

JLCPCB's Capabilities cater to a wider variety of applications, including:

Whether you're building prototypes or manufacturing mission critical systems, JLCPCB is your trusted partner

How This Project Works

Setting Up a Third-Party App Password for the project 

For this project to send email alerts, you need to configure your email account to allow ESP32 to sends emails on your behalf Due to enhanced security measures, most email providers (like Gmail) do not allow direct login from devices or applications. Instead, they require you to generate a third party app password for Gmail:

Why a Third-Party App Password is Required

Steps to Create a Third Party App Password

1. Enable 2-Factor Authentication (2FA):

2. Generate the App Password:

3. Use the App Password in the Your Code:

#define AUTHOR_PASSWORD "your_app_password"

 Code Explanation

The code initializes the ESP32, connects it to WI-FI, and sets up the OLED display. The MQ3 sensor monitors the environment for alcohol. When alcohol is detected an Email is sent using the ESP_Mail_Client library.

The OLED provides real-time feedback, and the buzzer signals the user

The following highlights the code's key features:

#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <WiFi.h>
#include <ESP_Mail_Client.h>
// Wi-Fi credentials
#define WIFI_SSID "Your_SSID"
#define WIFI_PASSWORD "Your_PASSWORD"
// Email credentials
#define SMTP_HOST "smtp.gmail.com"
#define SMTP_PORT 465
#define AUTHOR_EMAIL "Your_Email@gmail.com"
#define AUTHOR_PASSWORD "Your_Email_App_Password"
#define RECIPIENT_EMAIL "Recipient_Email@gmail.com"
// GPIO pins
#define MQ3_SENSOR_PIN 35
#define BUZZER_PIN 25
// OLED display settings
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
#define OLED_RESET -1
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
// SMTPSession for email
SMTPSession smtp;
// Global variables
bool emailSent = false;
// Function to handle email sending status
void smtpCallback(SMTP_Status status) {
if (status.success()) {
Serial.println("Email sent successfully!");
emailSent = true;
} else {
Serial.printf("Failed to send email: %s\n", status.errorReason().c_str());
}
}
// Function to initialize OLED display with an animation
void initializeDisplay() {
if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
Serial.println("OLED initialization failed!");
while (true);
}
// Welcome animation
int rectWidth = 10, rectHeight = 10;
for (int i = 0; i < 20; i++) {
display.clearDisplay();
display.fillRect((SCREEN_WIDTH - rectWidth) / 2, (SCREEN_HEIGHT - rectHeight) / 2, rectWidth, rectHeight, SSD1306_WHITE);
display.display();
rectWidth += 5;
rectHeight += 3;
delay(50);
}
// Display project title
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(SSD1306_WHITE);
display.setCursor(15, 20);
display.println("Alcohol Detector");
display.display();
delay(2000);
}
// Function to send an email alert
void sendEmail() {
// Configure SMTP session
Session_Config config;
config.server.host_name = SMTP_HOST;
config.server.port = SMTP_PORT;
config.login.email = AUTHOR_EMAIL;
config.login.password = AUTHOR_PASSWORD;
config.secure.startTLS = true;
// Compose the email message
SMTP_Message message;
message.sender.name = "ESP32 Alcohol Detector";
message.sender.email = AUTHOR_EMAIL;
message.subject = "Alert: Alcohol Detected!";
message.addRecipient("User", RECIPIENT_EMAIL);
message.text.content = "Alcohol has been detected by the ESP32 system.";
message.text.charSet = "utf-8";
message.text.transfer_encoding = Content_Transfer_Encoding::enc_7bit;
// Connect to SMTP server and send email
if (!smtp.connect(&config)) {
Serial.printf("SMTP connection failed: %s\n", smtp.errorReason().c_str());
return;
}
if (!MailClient.sendMail(&smtp, &message)) {
Serial.printf("Email sending failed: %s\n", smtp.errorReason().c_str());
}
}
// Function to handle buzzer alert
void beepBuzzer() {
digitalWrite(BUZZER_PIN, HIGH);
delay(200);
digitalWrite(BUZZER_PIN, LOW);
}
// Setup function
void setup() {
Serial.begin(115200);
// Initialize OLED display
initializeDisplay();
// Setup GPIO pins
pinMode(MQ3_SENSOR_PIN, INPUT);
pinMode(BUZZER_PIN, OUTPUT);
digitalWrite(BUZZER_PIN, LOW);
// Connect to Wi-Fi
Serial.print("Connecting to Wi-Fi...");
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
while (WiFi.status() != WL_CONNECTED) {
Serial.print(".");
delay(300);
}
Serial.println("\nConnected to Wi-Fi!");
// Setup email callback
smtp.callback(smtpCallback);
}
// Main loop
void loop() {
int alcoholStatus = digitalRead(MQ3_SENSOR_PIN);
if (alcoholStatus == LOW && !emailSent) {
// Alcohol detected
display.clearDisplay();
display.setCursor(0, 0);
display.setTextSize(1);
display.println("Alcohol Detected!");
display.println("Sending Email...");
display.display();
// Send email alert
sendEmail();
// Wait for email to be sent
while (!emailSent) {
delay(100);
}
// Trigger buzzer and update display
beepBuzzer();
display.clearDisplay();
display.setCursor(0, 0);
display.println("Mail Sent!");
display.display();
// Prevent rapid re-triggering
delay(5000);
emailSent = false;
} else if (alcoholStatus != LOW) {
// No alcohol detected
display.clearDisplay();
display.setCursor(0, 0);
display.setTextSize(1);
display.println("Alcohol Not Detected");
display.display();
}
delay(1000);
}

Circuit Diagram 

The PCB was designed for this project, streamlining connections between components. The following connections were used ESP32 Pins:

Video Tutorial:

prefer this video for this project:

Conclusion

This Alcohol Detection System demonstrates how technology can play pivot role in addressing read world problems like drunk driving. By integrating the ESP32 microcontroller, MQ3 sensor, OLED display, and Buzzer, this project provides a robust and efficient solution to detect alcohol and alert stakeholders through automated email notifications. 

The System's design ensures accuracy, reliability and responsiveness, making it suitable for applications in vehicles , workplaces and public safety environments. The inclusion of custom PCBs from JLCPCB add a layer of professionalism and durability to the project, ensuring long term use and adaptability This project is a small step toward building a safer world